From 69e379165142483e1d30dbe5c8acfac62a806b8d Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Sun, 19 Nov 2023 15:36:29 +0100 Subject: [PATCH 01/32] Facing and Axis to enum --- src/Axis.php | 17 +++----- src/AxisAlignedBB.php | 35 +++++---------- src/Facing.php | 85 ++++++++++++++++++------------------ src/RayTraceResult.php | 7 +-- src/Vector3.php | 12 +++-- tests/phpunit/FacingTest.php | 24 +++++----- 6 files changed, 80 insertions(+), 100 deletions(-) diff --git a/src/Axis.php b/src/Axis.php index 68f3198..34b3027 100644 --- a/src/Axis.php +++ b/src/Axis.php @@ -23,24 +23,21 @@ namespace pocketmine\math; -final class Axis{ - private function __construct(){ - //NOOP - } - - public const Y = 0; - public const Z = 1; - public const X = 2; +enum Axis: int{ + case Y = 0; + case Z = 1; + case X = 2; /** + * @deprecated * Returns a human-readable string representation of the given axis. */ - public static function toString(int $axis) : string{ + public static function toString(Axis $axis) : string{ return match($axis){ Axis::Y => "y", Axis::Z => "z", Axis::X => "x", - default => throw new \InvalidArgumentException("Invalid axis $axis") + default => throw new \InvalidArgumentException("Invalid axis " . $axis->name) }; } } diff --git a/src/AxisAlignedBB.php b/src/AxisAlignedBB.php index cf4a3e2..ae4b4cf 100644 --- a/src/AxisAlignedBB.php +++ b/src/AxisAlignedBB.php @@ -136,12 +136,10 @@ public function offsetCopy(float $x, float $y, float $z) : AxisAlignedBB{ /** * Offsets this AxisAlignedBB in the given direction by the specified distance. * - * @param int $face one of the Facing::* constants - * * @return $this */ - public function offsetTowards(int $face, float $distance) : AxisAlignedBB{ - [$offsetX, $offsetY, $offsetZ] = Facing::OFFSET[$face] ?? throw new \InvalidArgumentException("Invalid Facing $face"); + public function offsetTowards(Facing $face, float $distance) : AxisAlignedBB{ + [$offsetX, $offsetY, $offsetZ] = $face->offset(); return $this->offset($offsetX * $distance, $offsetY * $distance, $offsetZ * $distance); } @@ -149,7 +147,7 @@ public function offsetTowards(int $face, float $distance) : AxisAlignedBB{ /** * Returns an offset clone of this AxisAlignedBB. */ - public function offsetTowardsCopy(int $face, float $distance) : AxisAlignedBB{ + public function offsetTowardsCopy(Facing $face, float $distance) : AxisAlignedBB{ return (clone $this)->offsetTowards($face, $distance); } @@ -184,7 +182,7 @@ public function contractedCopy(float $x, float $y, float $z) : AxisAlignedBB{ * @return $this * @throws \InvalidArgumentException */ - public function extend(int $face, float $distance) : AxisAlignedBB{ + public function extend(Facing $face, float $distance) : AxisAlignedBB{ match($face){ Facing::DOWN => $this->minY -= $distance, Facing::UP => $this->maxY += $distance, @@ -192,7 +190,6 @@ public function extend(int $face, float $distance) : AxisAlignedBB{ Facing::SOUTH => $this->maxZ += $distance, Facing::WEST => $this->minX -= $distance, Facing::EAST => $this->maxX += $distance, - default => throw new \InvalidArgumentException("Invalid face $face"), }; return $this; @@ -204,7 +201,7 @@ public function extend(int $face, float $distance) : AxisAlignedBB{ * * @throws \InvalidArgumentException */ - public function extendedCopy(int $face, float $distance) : AxisAlignedBB{ + public function extendedCopy(Facing $face, float $distance) : AxisAlignedBB{ return (clone $this)->extend($face, $distance); } @@ -217,7 +214,7 @@ public function extendedCopy(int $face, float $distance) : AxisAlignedBB{ * @return $this * @throws \InvalidArgumentException */ - public function trim(int $face, float $distance) : AxisAlignedBB{ + public function trim(Facing $face, float $distance) : AxisAlignedBB{ return $this->extend($face, -$distance); } @@ -227,20 +224,18 @@ public function trim(int $face, float $distance) : AxisAlignedBB{ * * @throws \InvalidArgumentException */ - public function trimmedCopy(int $face, float $distance) : AxisAlignedBB{ + public function trimmedCopy(Facing $face, float $distance) : AxisAlignedBB{ return $this->extendedCopy($face, -$distance); } /** * Increases the dimension of the AABB along the given axis. * - * @param int $axis one of the Axis::* constants * @param float $distance Negative values reduce width, positive values increase width. * * @return $this - * @throws \InvalidArgumentException */ - public function stretch(int $axis, float $distance) : AxisAlignedBB{ + public function stretch(Axis $axis, float $distance) : AxisAlignedBB{ if($axis === Axis::Y){ $this->minY -= $distance; $this->maxY += $distance; @@ -250,19 +245,16 @@ public function stretch(int $axis, float $distance) : AxisAlignedBB{ }elseif($axis === Axis::X){ $this->minX -= $distance; $this->maxX += $distance; - }else{ - throw new \InvalidArgumentException("Invalid axis $axis"); } + return $this; } /** * Returns a stretched copy of this bounding box. * @see AxisAlignedBB::stretch() - * - * @throws \InvalidArgumentException */ - public function stretchedCopy(int $axis, float $distance) : AxisAlignedBB{ + public function stretchedCopy(Axis $axis, float $distance) : AxisAlignedBB{ return (clone $this)->stretch($axis, $distance); } @@ -271,19 +263,16 @@ public function stretchedCopy(int $axis, float $distance) : AxisAlignedBB{ * @see AxisAlignedBB::stretch() * * @return $this - * @throws \InvalidArgumentException */ - public function squash(int $axis, float $distance) : AxisAlignedBB{ + public function squash(Axis $axis, float $distance) : AxisAlignedBB{ return $this->stretch($axis, -$distance); } /** * Returns a squashed copy of this bounding box. * @see AxisAlignedBB::squash() - * - * @throws \InvalidArgumentException */ - public function squashedCopy(int $axis, float $distance) : AxisAlignedBB{ + public function squashedCopy(Axis $axis, float $distance) : AxisAlignedBB{ return $this->stretchedCopy($axis, -$distance); } diff --git a/src/Facing.php b/src/Facing.php index 134a29b..cf3f4ad 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -25,21 +25,21 @@ use function in_array; -final class Facing{ - private function __construct(){ - //NOOP - } +enum Facing: int{ - public const FLAG_AXIS_POSITIVE = 1; + private const FLAG_AXIS_POSITIVE = 1; /* most significant 2 bits = axis, least significant bit = is positive direction */ - public const DOWN = Axis::Y << 1; - public const UP = (Axis::Y << 1) | self::FLAG_AXIS_POSITIVE; - public const NORTH = Axis::Z << 1; - public const SOUTH = (Axis::Z << 1) | self::FLAG_AXIS_POSITIVE; - public const WEST = Axis::X << 1; - public const EAST = (Axis::X << 1) | self::FLAG_AXIS_POSITIVE; + case DOWN = Axis::Y << 1; + case UP = (Axis::Y << 1) | self::FLAG_AXIS_POSITIVE; + case NORTH = Axis::Z << 1; + case SOUTH = (Axis::Z << 1) | self::FLAG_AXIS_POSITIVE; + case WEST = Axis::X << 1; + case EAST = (Axis::X << 1) | self::FLAG_AXIS_POSITIVE; + /** + * @deprecated use Facing::cases() + */ public const ALL = [ self::DOWN, self::UP, @@ -56,6 +56,9 @@ private function __construct(){ self::EAST ]; + /** + * @deprecated + */ public const OFFSET = [ self::DOWN => [ 0, -1, 0], self::UP => [ 0, +1, 0], @@ -65,6 +68,9 @@ private function __construct(){ self::EAST => [+1, 0, 0] ]; + /** + * @var Facing[][] + */ private const CLOCKWISE = [ Axis::Y => [ self::NORTH => self::EAST, @@ -89,67 +95,60 @@ private function __construct(){ /** * Returns the axis of the given direction. */ - public static function axis(int $direction) : int{ - return $direction >> 1; //shift off positive/negative bit + public static function axis(Facing $direction) : Axis{ + return Axis::from($direction >> 1); //shift off positive/negative bit } /** * Returns whether the direction is facing the positive of its axis. */ - public static function isPositive(int $direction) : bool{ - return ($direction & self::FLAG_AXIS_POSITIVE) === self::FLAG_AXIS_POSITIVE; + public static function isPositive(Facing $direction) : bool{ + return ($direction->value & self::FLAG_AXIS_POSITIVE) === self::FLAG_AXIS_POSITIVE; } /** * Returns the opposite Facing of the specified one. * - * @param int $direction 0-5 one of the Facing::* constants + * @throws \ValueError if opposite facing don't exist */ - public static function opposite(int $direction) : int{ - return $direction ^ self::FLAG_AXIS_POSITIVE; + public static function opposite(Facing $direction) : Facing{ + return self::from($direction->value ^ self::FLAG_AXIS_POSITIVE); } /** * Rotates the given direction around the axis. - * - * @throws \InvalidArgumentException if not possible to rotate $direction around $axis */ - public static function rotate(int $direction, int $axis, bool $clockwise) : int{ - if(!isset(self::CLOCKWISE[$axis])){ - throw new \InvalidArgumentException("Invalid axis $axis"); - } - if(!isset(self::CLOCKWISE[$axis][$direction])){ - throw new \InvalidArgumentException("Cannot rotate facing \"" . self::toString($direction) . "\" around axis \"" . Axis::toString($axis) . "\""); - } - - $rotated = self::CLOCKWISE[$axis][$direction]; + public static function rotate(Facing $direction, Axis $axis, bool $clockwise) : Facing{ + $rotated = self::CLOCKWISE[$axis->value][$direction->value]; return $clockwise ? $rotated : self::opposite($rotated); } - /** - * @throws \InvalidArgumentException - */ - public static function rotateY(int $direction, bool $clockwise) : int{ + public static function rotateY(Facing $direction, bool $clockwise) : Facing{ return self::rotate($direction, Axis::Y, $clockwise); } - /** - * @throws \InvalidArgumentException - */ - public static function rotateZ(int $direction, bool $clockwise) : int{ + public static function rotateZ(Facing $direction, bool $clockwise) : Facing{ return self::rotate($direction, Axis::Z, $clockwise); } - /** - * @throws \InvalidArgumentException - */ - public static function rotateX(int $direction, bool $clockwise) : int{ + public static function rotateX(Facing $direction, bool $clockwise) : Facing{ return self::rotate($direction, Axis::X, $clockwise); } + public function offset(): array { + return match($this){ + self::DOWN => [ 0, -1, 0], + self::UP => [ 0, +1, 0], + self::NORTH => [ 0, 0, -1], + self::SOUTH => [ 0, 0, +1], + self::WEST => [-1, 0, 0], + self::EAST => [+1, 0, 0] + }; + } + /** * Validates the given integer as a Facing direction. - * + * @deprecated * @throws \InvalidArgumentException if the argument is not a valid Facing constant */ public static function validate(int $facing) : void{ @@ -172,4 +171,4 @@ public static function toString(int $facing) : string{ default => throw new \InvalidArgumentException("Invalid facing $facing") }; } -} +} \ No newline at end of file diff --git a/src/RayTraceResult.php b/src/RayTraceResult.php index 4d0928a..156c7b1 100644 --- a/src/RayTraceResult.php +++ b/src/RayTraceResult.php @@ -28,12 +28,9 @@ */ class RayTraceResult{ - /** - * @param int $hitFace one of the Facing::* constants - */ public function __construct( public AxisAlignedBB $bb, - public int $hitFace, + public Facing $hitFace, public Vector3 $hitVector ){} @@ -41,7 +38,7 @@ public function getBoundingBox() : AxisAlignedBB{ return $this->bb; } - public function getHitFace() : int{ + public function getHitFace() : Facing{ return $this->hitFace; } diff --git a/src/Vector3.php b/src/Vector3.php index ca8701d..00e15e7 100644 --- a/src/Vector3.php +++ b/src/Vector3.php @@ -117,8 +117,8 @@ public function abs() : Vector3{ /** * @return Vector3 */ - public function getSide(int $side, int $step = 1){ - [$offsetX, $offsetY, $offsetZ] = Facing::OFFSET[$side] ?? [0, 0, 0]; + public function getSide(Facing $side, int $step = 1){ + [$offsetX, $offsetY, $offsetZ] = $side->offset(); return $this->add($offsetX * $step, $offsetY * $step, $offsetZ * $step); } @@ -174,7 +174,7 @@ public function east(int $step = 1){ * @phpstan-return \Generator */ public function sides(int $step = 1) : \Generator{ - foreach(Facing::ALL as $facing){ + foreach(Facing::cases() as $facing){ yield $facing => $this->getSide($facing, $step); } } @@ -191,13 +191,11 @@ public function sidesArray(bool $keys = false, int $step = 1) : array{ /** * Yields vectors stepped out from this one in directions except those on the given axis. * - * @param int $axis Facing directions on this axis will be excluded - * * @return \Generator|Vector3[] * @phpstan-return \Generator */ - public function sidesAroundAxis(int $axis, int $step = 1) : \Generator{ - foreach(Facing::ALL as $facing){ + public function sidesAroundAxis(Axis $axis, int $step = 1) : \Generator{ + foreach(Facing::cases() as $facing){ if(Facing::axis($facing) !== $axis){ yield $facing => $this->getSide($facing, $step); } diff --git a/tests/phpunit/FacingTest.php b/tests/phpunit/FacingTest.php index 9e42070..fd1b8cd 100644 --- a/tests/phpunit/FacingTest.php +++ b/tests/phpunit/FacingTest.php @@ -37,10 +37,10 @@ public function axisProvider() : \Generator{ /** * @dataProvider axisProvider * - * @param int $direction - * @param int $axis + * @param Facing $facing + * @param Axis $axis */ - public function testAxis(int $direction, int $axis) : void{ + public function testAxis(Facing $direction, Axis $axis) : void{ self::assertEquals($axis, Facing::axis($direction)); } @@ -53,10 +53,10 @@ public function oppositeProvider() : \Generator{ /** * @dataProvider oppositeProvider * - * @param int $dir1 - * @param int $dir2 + * @param Facing $dir1 + * @param Facing $dir2 */ - public function testOpposite(int $dir1, int $dir2) : void{ + public function testOpposite(Facing $dir1, Facing $dir2) : void{ self::assertEquals($dir2, Facing::opposite($dir1)); self::assertEquals($dir1, Facing::opposite($dir2)); } @@ -73,10 +73,10 @@ public function positiveProvider() : \Generator{ /** * @dataProvider positiveProvider * - * @param int $facing + * @param Facing $facing * @param bool $positive */ - public function testIsPositive(int $facing, bool $positive) : void{ + public function testIsPositive(Facing $facing, bool $positive) : void{ self::assertEquals($positive, Facing::isPositive($facing)); } @@ -117,12 +117,12 @@ public function rotateProvider() : \Generator{ /** * @dataProvider rotateProvider * - * @param int $direction - * @param int $axis + * @param Facing $direction + * @param Axis $axis * @param bool $clockwise - * @param int $expected + * @param Facing $expected */ - public function testRotate(int $direction, int $axis, bool $clockwise, int $expected) : void{ + public function testRotate(Facing $direction, Axis $axis, bool $clockwise, Facing $expected) : void{ self::assertEquals($expected, Facing::rotate($direction, $axis, $clockwise)); } } From 7886af051f098a9fde4368ecba2c623cc7d11a91 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Sun, 19 Nov 2023 15:44:18 +0100 Subject: [PATCH 02/32] Modif toString function --- src/Axis.php | 3 +-- src/Facing.php | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Axis.php b/src/Axis.php index 34b3027..c487c57 100644 --- a/src/Axis.php +++ b/src/Axis.php @@ -29,7 +29,7 @@ enum Axis: int{ case X = 2; /** - * @deprecated + * @deprecated use Axis->name * Returns a human-readable string representation of the given axis. */ public static function toString(Axis $axis) : string{ @@ -37,7 +37,6 @@ public static function toString(Axis $axis) : string{ Axis::Y => "y", Axis::Z => "z", Axis::X => "x", - default => throw new \InvalidArgumentException("Invalid axis " . $axis->name) }; } } diff --git a/src/Facing.php b/src/Facing.php index cf3f4ad..4fe5d7c 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -158,9 +158,10 @@ public static function validate(int $facing) : void{ } /** + * @deprecated use Facing->name * Returns a human-readable string representation of the given Facing direction. */ - public static function toString(int $facing) : string{ + public static function toString(Facing $facing) : string{ return match($facing){ self::DOWN => "down", self::UP => "up", @@ -168,7 +169,6 @@ public static function toString(int $facing) : string{ self::SOUTH => "south", self::WEST => "west", self::EAST => "east", - default => throw new \InvalidArgumentException("Invalid facing $facing") }; } } \ No newline at end of file From 7f6b638f5c24bd0362004a00cbe0cdce64527403 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Mon, 20 Nov 2023 21:06:23 +0100 Subject: [PATCH 03/32] Remove useless doc --- src/Facing.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Facing.php b/src/Facing.php index 4fe5d7c..6b42eff 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -108,8 +108,6 @@ public static function isPositive(Facing $direction) : bool{ /** * Returns the opposite Facing of the specified one. - * - * @throws \ValueError if opposite facing don't exist */ public static function opposite(Facing $direction) : Facing{ return self::from($direction->value ^ self::FLAG_AXIS_POSITIVE); From c7e15adc937eded4447c7f360630e416db9f695f Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Mon, 20 Nov 2023 21:08:37 +0100 Subject: [PATCH 04/32] remove outside functionality of this pr --- src/AxisAlignedBB.php | 2 +- src/Facing.php | 14 -------------- src/Vector3.php | 2 +- 3 files changed, 2 insertions(+), 16 deletions(-) diff --git a/src/AxisAlignedBB.php b/src/AxisAlignedBB.php index ae4b4cf..5e628a0 100644 --- a/src/AxisAlignedBB.php +++ b/src/AxisAlignedBB.php @@ -139,7 +139,7 @@ public function offsetCopy(float $x, float $y, float $z) : AxisAlignedBB{ * @return $this */ public function offsetTowards(Facing $face, float $distance) : AxisAlignedBB{ - [$offsetX, $offsetY, $offsetZ] = $face->offset(); + [$offsetX, $offsetY, $offsetZ] = Facing::OFFSET[$face->value]; return $this->offset($offsetX * $distance, $offsetY * $distance, $offsetZ * $distance); } diff --git a/src/Facing.php b/src/Facing.php index 6b42eff..58d232a 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -56,9 +56,6 @@ enum Facing: int{ self::EAST ]; - /** - * @deprecated - */ public const OFFSET = [ self::DOWN => [ 0, -1, 0], self::UP => [ 0, +1, 0], @@ -133,17 +130,6 @@ public static function rotateX(Facing $direction, bool $clockwise) : Facing{ return self::rotate($direction, Axis::X, $clockwise); } - public function offset(): array { - return match($this){ - self::DOWN => [ 0, -1, 0], - self::UP => [ 0, +1, 0], - self::NORTH => [ 0, 0, -1], - self::SOUTH => [ 0, 0, +1], - self::WEST => [-1, 0, 0], - self::EAST => [+1, 0, 0] - }; - } - /** * Validates the given integer as a Facing direction. * @deprecated diff --git a/src/Vector3.php b/src/Vector3.php index 00e15e7..cc92f2c 100644 --- a/src/Vector3.php +++ b/src/Vector3.php @@ -118,7 +118,7 @@ public function abs() : Vector3{ * @return Vector3 */ public function getSide(Facing $side, int $step = 1){ - [$offsetX, $offsetY, $offsetZ] = $side->offset(); + [$offsetX, $offsetY, $offsetZ] = Facing::OFFSET[$side->value]; return $this->add($offsetX * $step, $offsetY * $step, $offsetZ * $step); } From a45f1f0c91e414a19442d5a30e38ca7383280068 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Mon, 20 Nov 2023 21:23:25 +0100 Subject: [PATCH 05/32] Ditch numeric values --- src/Facing.php | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/Facing.php b/src/Facing.php index 58d232a..cd56c89 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -25,17 +25,13 @@ use function in_array; -enum Facing: int{ - - private const FLAG_AXIS_POSITIVE = 1; - - /* most significant 2 bits = axis, least significant bit = is positive direction */ - case DOWN = Axis::Y << 1; - case UP = (Axis::Y << 1) | self::FLAG_AXIS_POSITIVE; - case NORTH = Axis::Z << 1; - case SOUTH = (Axis::Z << 1) | self::FLAG_AXIS_POSITIVE; - case WEST = Axis::X << 1; - case EAST = (Axis::X << 1) | self::FLAG_AXIS_POSITIVE; +enum Facing{ + case DOWN; + case UP; + case NORTH; + case SOUTH; + case WEST; + case EAST; /** * @deprecated use Facing::cases() @@ -93,21 +89,32 @@ enum Facing: int{ * Returns the axis of the given direction. */ public static function axis(Facing $direction) : Axis{ - return Axis::from($direction >> 1); //shift off positive/negative bit + return match ($direction) { + self::DOWN, self::UP => Axis::Y, + self::NORTH, self::SOUTH => Axis::Z, + self::WEST, self::EAST => Axis::X, + }; } /** * Returns whether the direction is facing the positive of its axis. */ public static function isPositive(Facing $direction) : bool{ - return ($direction->value & self::FLAG_AXIS_POSITIVE) === self::FLAG_AXIS_POSITIVE; + return in_array($direction, [self::UP, self::SOUTH, self::EAST]); } /** * Returns the opposite Facing of the specified one. */ public static function opposite(Facing $direction) : Facing{ - return self::from($direction->value ^ self::FLAG_AXIS_POSITIVE); + return match ($direction) { + self::DOWN => self::UP, + self::EAST => self::SOUTH, + self::WEST => self::EAST, + self::UP => self::DOWN, + self::SOUTH => self::EAST, + self::EAST => self::WEST, + }; } /** From ac698137ca78c431e23bd1c0ef2195adfa0e0f7c Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Tue, 21 Nov 2023 14:06:22 +0100 Subject: [PATCH 06/32] Remove ditch values --- src/Axis.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Axis.php b/src/Axis.php index c487c57..99c6d69 100644 --- a/src/Axis.php +++ b/src/Axis.php @@ -23,10 +23,10 @@ namespace pocketmine\math; -enum Axis: int{ - case Y = 0; - case Z = 1; - case X = 2; +enum Axis{ + case Y; + case Z; + case X; /** * @deprecated use Axis->name From d5ad9acbb1ba84d7c30b5b701a30824d4e1a3d5e Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Tue, 21 Nov 2023 16:17:06 +0100 Subject: [PATCH 07/32] Fix incorrect opposite side --- src/Facing.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Facing.php b/src/Facing.php index cd56c89..4627de1 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -109,10 +109,10 @@ public static function isPositive(Facing $direction) : bool{ public static function opposite(Facing $direction) : Facing{ return match ($direction) { self::DOWN => self::UP, - self::EAST => self::SOUTH, + self::NORTH => self::SOUTH, self::WEST => self::EAST, self::UP => self::DOWN, - self::SOUTH => self::EAST, + self::SOUTH => self::NORTH, self::EAST => self::WEST, }; } From d5e44f7ba9116450e8559a472a38cf58045989a9 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Tue, 21 Nov 2023 17:04:38 +0100 Subject: [PATCH 08/32] Modifications --- src/Facing.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Facing.php b/src/Facing.php index 4627de1..450b41c 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -100,7 +100,7 @@ public static function axis(Facing $direction) : Axis{ * Returns whether the direction is facing the positive of its axis. */ public static function isPositive(Facing $direction) : bool{ - return in_array($direction, [self::UP, self::SOUTH, self::EAST]); + return $direction === self::UP || $direction === self::SOUTH || $direction === self::EAST; } /** @@ -153,13 +153,6 @@ public static function validate(int $facing) : void{ * Returns a human-readable string representation of the given Facing direction. */ public static function toString(Facing $facing) : string{ - return match($facing){ - self::DOWN => "down", - self::UP => "up", - self::NORTH => "north", - self::SOUTH => "south", - self::WEST => "west", - self::EAST => "east", - }; + return strtolower($facing->name); } } \ No newline at end of file From 19d771c1e1a020a4503db7d676e8e898ca7216f0 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Tue, 21 Nov 2023 17:06:45 +0100 Subject: [PATCH 09/32] Follow order of cases --- src/Facing.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Facing.php b/src/Facing.php index 450b41c..25ebbfd 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -109,10 +109,10 @@ public static function isPositive(Facing $direction) : bool{ public static function opposite(Facing $direction) : Facing{ return match ($direction) { self::DOWN => self::UP, - self::NORTH => self::SOUTH, - self::WEST => self::EAST, self::UP => self::DOWN, + self::NORTH => self::SOUTH, self::SOUTH => self::NORTH, + self::WEST => self::EAST, self::EAST => self::WEST, }; } From bca40e8a81003f6cf326c750c91c455a284bce22 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Mon, 27 Nov 2023 17:35:49 +0100 Subject: [PATCH 10/32] Fix bug in rotate function --- src/Facing.php | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/src/Facing.php b/src/Facing.php index 25ebbfd..1a24678 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -61,30 +61,6 @@ enum Facing{ self::EAST => [+1, 0, 0] ]; - /** - * @var Facing[][] - */ - private const CLOCKWISE = [ - Axis::Y => [ - self::NORTH => self::EAST, - self::EAST => self::SOUTH, - self::SOUTH => self::WEST, - self::WEST => self::NORTH - ], - Axis::Z => [ - self::UP => self::EAST, - self::EAST => self::DOWN, - self::DOWN => self::WEST, - self::WEST => self::UP - ], - Axis::X => [ - self::UP => self::NORTH, - self::NORTH => self::DOWN, - self::DOWN => self::SOUTH, - self::SOUTH => self::UP - ] - ]; - /** * Returns the axis of the given direction. */ @@ -121,7 +97,27 @@ public static function opposite(Facing $direction) : Facing{ * Rotates the given direction around the axis. */ public static function rotate(Facing $direction, Axis $axis, bool $clockwise) : Facing{ - $rotated = self::CLOCKWISE[$axis->value][$direction->value]; + $rotated = match ($axis) { + Axis::Y => match ($direction) { + self::NORTH => self::EAST, + self::EAST => self::SOUTH, + self::SOUTH => self::WEST, + self::WEST => self::NORTH + }, + Axis::Z => match ($direction) { + self::UP => self::EAST, + self::EAST => self::DOWN, + self::DOWN => self::WEST, + self::WEST => self::UP + }, + Axis::X => match ($direction) { + self::UP => self::NORTH, + self::NORTH => self::DOWN, + self::DOWN => self::SOUTH, + self::SOUTH => self::UP + } + }; + return $clockwise ? $rotated : self::opposite($rotated); } @@ -139,7 +135,7 @@ public static function rotateX(Facing $direction, bool $clockwise) : Facing{ /** * Validates the given integer as a Facing direction. - * @deprecated + * @deprecated * @throws \InvalidArgumentException if the argument is not a valid Facing constant */ public static function validate(int $facing) : void{ From 5d66326bc6de32990e6caee456e8c844967addbe Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Mon, 27 Nov 2023 17:41:56 +0100 Subject: [PATCH 11/32] Modification --- src/Axis.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Axis.php b/src/Axis.php index 99c6d69..b696bc4 100644 --- a/src/Axis.php +++ b/src/Axis.php @@ -33,10 +33,6 @@ enum Axis{ * Returns a human-readable string representation of the given axis. */ public static function toString(Axis $axis) : string{ - return match($axis){ - Axis::Y => "y", - Axis::Z => "z", - Axis::X => "x", - }; + return strtolower($axis->name); } } From 1c1a45e7769e9a418379132a30c498c21fe2e2f2 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Sun, 12 May 2024 18:04:55 +0200 Subject: [PATCH 12/32] Updates --- src/Axis.php | 8 ------- src/AxisAlignedBB.php | 2 +- src/Facing.php | 53 +++++++++---------------------------------- src/Vector3.php | 2 +- 4 files changed, 13 insertions(+), 52 deletions(-) diff --git a/src/Axis.php b/src/Axis.php index b696bc4..f656929 100644 --- a/src/Axis.php +++ b/src/Axis.php @@ -27,12 +27,4 @@ enum Axis{ case Y; case Z; case X; - - /** - * @deprecated use Axis->name - * Returns a human-readable string representation of the given axis. - */ - public static function toString(Axis $axis) : string{ - return strtolower($axis->name); - } } diff --git a/src/AxisAlignedBB.php b/src/AxisAlignedBB.php index 5e628a0..ae4b4cf 100644 --- a/src/AxisAlignedBB.php +++ b/src/AxisAlignedBB.php @@ -139,7 +139,7 @@ public function offsetCopy(float $x, float $y, float $z) : AxisAlignedBB{ * @return $this */ public function offsetTowards(Facing $face, float $distance) : AxisAlignedBB{ - [$offsetX, $offsetY, $offsetZ] = Facing::OFFSET[$face->value]; + [$offsetX, $offsetY, $offsetZ] = $face->offset(); return $this->offset($offsetX * $distance, $offsetY * $distance, $offsetZ * $distance); } diff --git a/src/Facing.php b/src/Facing.php index 1a24678..b7c0bbb 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -23,8 +23,6 @@ namespace pocketmine\math; -use function in_array; - enum Facing{ case DOWN; case UP; @@ -33,18 +31,6 @@ enum Facing{ case WEST; case EAST; - /** - * @deprecated use Facing::cases() - */ - public const ALL = [ - self::DOWN, - self::UP, - self::NORTH, - self::SOUTH, - self::WEST, - self::EAST - ]; - public const HORIZONTAL = [ self::NORTH, self::SOUTH, @@ -52,15 +38,6 @@ enum Facing{ self::EAST ]; - public const OFFSET = [ - self::DOWN => [ 0, -1, 0], - self::UP => [ 0, +1, 0], - self::NORTH => [ 0, 0, -1], - self::SOUTH => [ 0, 0, +1], - self::WEST => [-1, 0, 0], - self::EAST => [+1, 0, 0] - ]; - /** * Returns the axis of the given direction. */ @@ -72,6 +49,17 @@ public static function axis(Facing $direction) : Axis{ }; } + public function offset(): array{ + return match ($this) { + self::DOWN => [ 0, -1, 0], + self::UP => [ 0, +1, 0], + self::NORTH => [ 0, 0, -1], + self::SOUTH => [ 0, 0, +1], + self::WEST => [-1, 0, 0], + self::EAST => [+1, 0, 0] + }; + } + /** * Returns whether the direction is facing the positive of its axis. */ @@ -132,23 +120,4 @@ public static function rotateZ(Facing $direction, bool $clockwise) : Facing{ public static function rotateX(Facing $direction, bool $clockwise) : Facing{ return self::rotate($direction, Axis::X, $clockwise); } - - /** - * Validates the given integer as a Facing direction. - * @deprecated - * @throws \InvalidArgumentException if the argument is not a valid Facing constant - */ - public static function validate(int $facing) : void{ - if(!in_array($facing, self::ALL, true)){ - throw new \InvalidArgumentException("Invalid direction $facing"); - } - } - - /** - * @deprecated use Facing->name - * Returns a human-readable string representation of the given Facing direction. - */ - public static function toString(Facing $facing) : string{ - return strtolower($facing->name); - } } \ No newline at end of file diff --git a/src/Vector3.php b/src/Vector3.php index cc92f2c..00e15e7 100644 --- a/src/Vector3.php +++ b/src/Vector3.php @@ -118,7 +118,7 @@ public function abs() : Vector3{ * @return Vector3 */ public function getSide(Facing $side, int $step = 1){ - [$offsetX, $offsetY, $offsetZ] = Facing::OFFSET[$side->value]; + [$offsetX, $offsetY, $offsetZ] = $side->offset(); return $this->add($offsetX * $step, $offsetY * $step, $offsetZ * $step); } From 54d40bb57089aa2b8bf2a5d4a6b48d4a58f42470 Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Sun, 12 May 2024 18:21:58 +0200 Subject: [PATCH 13/32] Fix bugs --- src/AxisAlignedBB.php | 17 +++++++++-------- src/Facing.php | 14 ++++++++++---- src/Vector3.php | 12 ++++++------ 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/AxisAlignedBB.php b/src/AxisAlignedBB.php index ae4b4cf..e4798c9 100644 --- a/src/AxisAlignedBB.php +++ b/src/AxisAlignedBB.php @@ -457,17 +457,18 @@ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResu $face = -1; foreach([ - Facing::WEST => $v1, - Facing::EAST => $v2, - Facing::DOWN => $v3, - Facing::UP => $v4, - Facing::NORTH => $v5, - Facing::SOUTH => $v6 - ] as $f => $v){ + [Facing::WEST, $v1], + [Facing::EAST, $v2], + [Facing::DOWN, $v3], + [Facing::UP, $v4], + [Facing::NORTH, $v5], + [Facing::SOUTH, $v6] + ] as $value){ + $v = $value[1]; if($v !== null and ($d = $pos1->distanceSquared($v)) < $distance){ $vector = $v; $distance = $d; - $face = $f; + $face = $value[0]; } } diff --git a/src/Facing.php b/src/Facing.php index b7c0bbb..3b08952 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -49,7 +49,10 @@ public static function axis(Facing $direction) : Axis{ }; } - public function offset(): array{ + /** + * @return non-empty-array + */ + public function offset() : array{ return match ($this) { self::DOWN => [ 0, -1, 0], self::UP => [ 0, +1, 0], @@ -90,19 +93,22 @@ public static function rotate(Facing $direction, Axis $axis, bool $clockwise) : self::NORTH => self::EAST, self::EAST => self::SOUTH, self::SOUTH => self::WEST, - self::WEST => self::NORTH + self::WEST => self::NORTH, + default => throw new \InvalidArgumentException("Face " . strtolower($direction->name) . " not match with Axis " . strtolower($axis->name)) }, Axis::Z => match ($direction) { self::UP => self::EAST, self::EAST => self::DOWN, self::DOWN => self::WEST, - self::WEST => self::UP + self::WEST => self::UP, + default => throw new \InvalidArgumentException("Face " . strtolower($direction->name) . " not match with Axis " . strtolower($axis->name)) }, Axis::X => match ($direction) { self::UP => self::NORTH, self::NORTH => self::DOWN, self::DOWN => self::SOUTH, - self::SOUTH => self::UP + self::SOUTH => self::UP, + default => throw new \InvalidArgumentException("Face " . strtolower($direction->name) . " not match with Axis " . strtolower($axis->name)) } }; diff --git a/src/Vector3.php b/src/Vector3.php index 00e15e7..891ae9e 100644 --- a/src/Vector3.php +++ b/src/Vector3.php @@ -170,12 +170,12 @@ public function east(int $step = 1){ * * @param int $step Distance in each direction to shift the vector * - * @return \Generator|Vector3[] - * @phpstan-return \Generator + * @return \Generator|array + * @phpstan-return \Generator */ public function sides(int $step = 1) : \Generator{ foreach(Facing::cases() as $facing){ - yield $facing => $this->getSide($facing, $step); + yield [$facing, $this->getSide($facing, $step)]; } } @@ -191,13 +191,13 @@ public function sidesArray(bool $keys = false, int $step = 1) : array{ /** * Yields vectors stepped out from this one in directions except those on the given axis. * - * @return \Generator|Vector3[] - * @phpstan-return \Generator + * @return \Generator|array + * @phpstan-return \Generator */ public function sidesAroundAxis(Axis $axis, int $step = 1) : \Generator{ foreach(Facing::cases() as $facing){ if(Facing::axis($facing) !== $axis){ - yield $facing => $this->getSide($facing, $step); + yield [$facing, $this->getSide($facing, $step)]; } } } From 7857d7480b9710d122c33c85acf5b1608184a06a Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Sun, 12 May 2024 18:30:58 +0200 Subject: [PATCH 14/32] Fix cs --- src/AxisAlignedBB.php | 3 ++- src/Vector3.php | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/AxisAlignedBB.php b/src/AxisAlignedBB.php index e4798c9..8fbc675 100644 --- a/src/AxisAlignedBB.php +++ b/src/AxisAlignedBB.php @@ -454,7 +454,7 @@ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResu $vector = null; $distance = PHP_INT_MAX; - $face = -1; + $face = null; foreach([ [Facing::WEST, $v1], @@ -468,6 +468,7 @@ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResu if($v !== null and ($d = $pos1->distanceSquared($v)) < $distance){ $vector = $v; $distance = $d; + /** @var Facing $face */ $face = $value[0]; } } diff --git a/src/Vector3.php b/src/Vector3.php index 891ae9e..82f912e 100644 --- a/src/Vector3.php +++ b/src/Vector3.php @@ -170,8 +170,8 @@ public function east(int $step = 1){ * * @param int $step Distance in each direction to shift the vector * - * @return \Generator|array - * @phpstan-return \Generator + * @return \Generator|mixed[] [Facing $facing, Vector3 $vector] + * @phpstan-return \Generator */ public function sides(int $step = 1) : \Generator{ foreach(Facing::cases() as $facing){ @@ -182,7 +182,7 @@ public function sides(int $step = 1) : \Generator{ /** * Same as sides() but returns a pre-populated array instead of Generator. * - * @return Vector3[] + * @return array{Facing, Vector3} */ public function sidesArray(bool $keys = false, int $step = 1) : array{ return iterator_to_array($this->sides($step), $keys); @@ -191,8 +191,8 @@ public function sidesArray(bool $keys = false, int $step = 1) : array{ /** * Yields vectors stepped out from this one in directions except those on the given axis. * - * @return \Generator|array - * @phpstan-return \Generator + * @return \Generator|mixed[] [Facing $facing, Vector3 $vector] + * @phpstan-return \Generator */ public function sidesAroundAxis(Axis $axis, int $step = 1) : \Generator{ foreach(Facing::cases() as $facing){ From 2395e80a56876b7390a8dc10fb62c12198e487fb Mon Sep 17 00:00:00 2001 From: Dhaiven Date: Sun, 12 May 2024 18:33:51 +0200 Subject: [PATCH 15/32] Fix cs --- src/AxisAlignedBB.php | 1 + src/Vector3.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/AxisAlignedBB.php b/src/AxisAlignedBB.php index 8fbc675..c9c69e6 100644 --- a/src/AxisAlignedBB.php +++ b/src/AxisAlignedBB.php @@ -454,6 +454,7 @@ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResu $vector = null; $distance = PHP_INT_MAX; + /** @var Facing $face */ $face = null; foreach([ diff --git a/src/Vector3.php b/src/Vector3.php index 82f912e..cc1760f 100644 --- a/src/Vector3.php +++ b/src/Vector3.php @@ -182,7 +182,7 @@ public function sides(int $step = 1) : \Generator{ /** * Same as sides() but returns a pre-populated array instead of Generator. * - * @return array{Facing, Vector3} + * @return array */ public function sidesArray(bool $keys = false, int $step = 1) : array{ return iterator_to_array($this->sides($step), $keys); From 64d011214add8223de8ea1ef21007c3ea5404174 Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:40:46 +0100 Subject: [PATCH 16/32] Replace static functions by object functions --- src/Facing.php | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Facing.php b/src/Facing.php index 3b08952..af1eef7 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -41,8 +41,8 @@ enum Facing{ /** * Returns the axis of the given direction. */ - public static function axis(Facing $direction) : Axis{ - return match ($direction) { + public function axis() : Axis{ + return match ($this) { self::DOWN, self::UP => Axis::Y, self::NORTH, self::SOUTH => Axis::Z, self::WEST, self::EAST => Axis::X, @@ -66,15 +66,15 @@ public function offset() : array{ /** * Returns whether the direction is facing the positive of its axis. */ - public static function isPositive(Facing $direction) : bool{ - return $direction === self::UP || $direction === self::SOUTH || $direction === self::EAST; + public function isPositive() : bool{ + return $this === self::UP || $this === self::SOUTH || $this === self::EAST; } /** * Returns the opposite Facing of the specified one. */ - public static function opposite(Facing $direction) : Facing{ - return match ($direction) { + public function opposite() : Facing{ + return match ($this) { self::DOWN => self::UP, self::UP => self::DOWN, self::NORTH => self::SOUTH, @@ -87,43 +87,43 @@ public static function opposite(Facing $direction) : Facing{ /** * Rotates the given direction around the axis. */ - public static function rotate(Facing $direction, Axis $axis, bool $clockwise) : Facing{ + public function rotate(Axis $axis, bool $clockwise) : Facing{ $rotated = match ($axis) { - Axis::Y => match ($direction) { + Axis::Y => match ($this) { self::NORTH => self::EAST, self::EAST => self::SOUTH, self::SOUTH => self::WEST, self::WEST => self::NORTH, - default => throw new \InvalidArgumentException("Face " . strtolower($direction->name) . " not match with Axis " . strtolower($axis->name)) + default => throw new \InvalidArgumentException("Face " . strtolower($this->name) . " not match with Axis " . strtolower($axis->name)) }, - Axis::Z => match ($direction) { + Axis::Z => match ($this) { self::UP => self::EAST, self::EAST => self::DOWN, self::DOWN => self::WEST, self::WEST => self::UP, - default => throw new \InvalidArgumentException("Face " . strtolower($direction->name) . " not match with Axis " . strtolower($axis->name)) + default => throw new \InvalidArgumentException("Face " . strtolower($this->name) . " not match with Axis " . strtolower($axis->name)) }, - Axis::X => match ($direction) { + Axis::X => match ($this) { self::UP => self::NORTH, self::NORTH => self::DOWN, self::DOWN => self::SOUTH, self::SOUTH => self::UP, - default => throw new \InvalidArgumentException("Face " . strtolower($direction->name) . " not match with Axis " . strtolower($axis->name)) + default => throw new \InvalidArgumentException("Face " . strtolower($this->name) . " not match with Axis " . strtolower($axis->name)) } }; - return $clockwise ? $rotated : self::opposite($rotated); + return $clockwise ? $rotated : $rotated->opposite(); } - public static function rotateY(Facing $direction, bool $clockwise) : Facing{ - return self::rotate($direction, Axis::Y, $clockwise); + public function rotateY(bool $clockwise) : Facing{ + return $this->rotate(Axis::Y, $clockwise); } - public static function rotateZ(Facing $direction, bool $clockwise) : Facing{ - return self::rotate($direction, Axis::Z, $clockwise); + public function rotateZ(bool $clockwise) : Facing{ + return $this->rotate(Axis::Z, $clockwise); } - public static function rotateX(Facing $direction, bool $clockwise) : Facing{ - return self::rotate($direction, Axis::X, $clockwise); + public function rotateX(bool $clockwise) : Facing{ + return $this->rotate(Axis::X, $clockwise); } } \ No newline at end of file From 4fe606a680cb794c7aa5d8833bdf25ee1f07e4b2 Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:47:34 +0100 Subject: [PATCH 17/32] Add facing to key for Generator --- src/Vector3.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Vector3.php b/src/Vector3.php index cc1760f..3e449a9 100644 --- a/src/Vector3.php +++ b/src/Vector3.php @@ -170,19 +170,19 @@ public function east(int $step = 1){ * * @param int $step Distance in each direction to shift the vector * - * @return \Generator|mixed[] [Facing $facing, Vector3 $vector] - * @phpstan-return \Generator + * @return \Generator|Vector3[] + * @phpstan-return \Generator */ public function sides(int $step = 1) : \Generator{ foreach(Facing::cases() as $facing){ - yield [$facing, $this->getSide($facing, $step)]; + yield $facing => $this->getSide($facing, $step); } } /** * Same as sides() but returns a pre-populated array instead of Generator. * - * @return array + * @return Vector3[] */ public function sidesArray(bool $keys = false, int $step = 1) : array{ return iterator_to_array($this->sides($step), $keys); @@ -191,13 +191,13 @@ public function sidesArray(bool $keys = false, int $step = 1) : array{ /** * Yields vectors stepped out from this one in directions except those on the given axis. * - * @return \Generator|mixed[] [Facing $facing, Vector3 $vector] - * @phpstan-return \Generator + * @return \Generator|Vector3[] + * @phpstan-return \Generator */ public function sidesAroundAxis(Axis $axis, int $step = 1) : \Generator{ foreach(Facing::cases() as $facing){ - if(Facing::axis($facing) !== $axis){ - yield [$facing, $this->getSide($facing, $step)]; + if($facing->axis() !== $axis){ + yield $facing => $this->getSide($facing, $step); } } } From 9016d06d7a0c7be74ca0b4acd13b087e6348e8c5 Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:49:20 +0100 Subject: [PATCH 18/32] Fix tests --- tests/phpunit/FacingTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/FacingTest.php b/tests/phpunit/FacingTest.php index 81b3755..22a630c 100644 --- a/tests/phpunit/FacingTest.php +++ b/tests/phpunit/FacingTest.php @@ -37,7 +37,7 @@ public static function axisProvider() : \Generator{ #[DataProvider("axisProvider")] public function testAxis(Facing $direction, Axis $axis) : void{ - self::assertEquals($axis, Facing::axis($direction)); + self::assertEquals($axis, $direction->axis()); } public static function oppositeProvider() : \Generator{ @@ -48,8 +48,8 @@ public static function oppositeProvider() : \Generator{ #[DataProvider("oppositeProvider")] public function testOpposite(Facing $dir1, Facing $dir2) : void{ - self::assertEquals($dir2, Facing::opposite($dir1)); - self::assertEquals($dir1, Facing::opposite($dir2)); + self::assertEquals($dir2, $dir1->opposite()); + self::assertEquals($dir1, $dir2->opposite()); } public static function positiveProvider() : \Generator{ @@ -63,7 +63,7 @@ public static function positiveProvider() : \Generator{ #[DataProvider("positiveProvider")] public function testIsPositive(Facing $facing, bool $positive) : void{ - self::assertEquals($positive, Facing::isPositive($facing)); + self::assertEquals($positive, $facing->isPositive()); } public static function rotateProvider() : \Generator{ @@ -102,6 +102,6 @@ public static function rotateProvider() : \Generator{ #[DataProvider("rotateProvider")] public function testRotate(Facing $direction, Axis $axis, bool $clockwise, Facing $expected) : void{ - self::assertEquals($expected, Facing::rotate($direction, $axis, $clockwise)); + self::assertEquals($expected, $direction->rotate($axis, $clockwise)); } } From 4d1f0e4e1aca622fd46644a2b1f5fc6af76fae5f Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Mon, 2 Dec 2024 14:02:43 +0100 Subject: [PATCH 19/32] Prefix phpstan return --- src/Facing.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Facing.php b/src/Facing.php index af1eef7..e07fb58 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -50,7 +50,7 @@ public function axis() : Axis{ } /** - * @return non-empty-array + * @phpstan-return non-empty-array */ public function offset() : array{ return match ($this) { From 0fdc693df05006c8ee9ee130e21548a2d550340b Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Mon, 2 Dec 2024 14:40:37 +0100 Subject: [PATCH 20/32] Update documentation --- src/AxisAlignedBB.php | 6 ------ src/Facing.php | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/AxisAlignedBB.php b/src/AxisAlignedBB.php index c9c69e6..c437c6b 100644 --- a/src/AxisAlignedBB.php +++ b/src/AxisAlignedBB.php @@ -180,7 +180,6 @@ public function contractedCopy(float $x, float $y, float $z) : AxisAlignedBB{ * @param float $distance Negative values pull the face in, positive values push out. * * @return $this - * @throws \InvalidArgumentException */ public function extend(Facing $face, float $distance) : AxisAlignedBB{ match($face){ @@ -198,8 +197,6 @@ public function extend(Facing $face, float $distance) : AxisAlignedBB{ /** * Returns an extended clone of this bounding box. * @see AxisAlignedBB::extend() - * - * @throws \InvalidArgumentException */ public function extendedCopy(Facing $face, float $distance) : AxisAlignedBB{ return (clone $this)->extend($face, $distance); @@ -212,7 +209,6 @@ public function extendedCopy(Facing $face, float $distance) : AxisAlignedBB{ * @param float $distance Positive values pull the face in, negative values push out. * * @return $this - * @throws \InvalidArgumentException */ public function trim(Facing $face, float $distance) : AxisAlignedBB{ return $this->extend($face, -$distance); @@ -221,8 +217,6 @@ public function trim(Facing $face, float $distance) : AxisAlignedBB{ /** * Returns a trimmed clone of this bounding box. * @see AxisAlignedBB::trim() - * - * @throws \InvalidArgumentException */ public function trimmedCopy(Facing $face, float $distance) : AxisAlignedBB{ return $this->extendedCopy($face, -$distance); diff --git a/src/Facing.php b/src/Facing.php index e07fb58..8552283 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -86,6 +86,8 @@ public function opposite() : Facing{ /** * Rotates the given direction around the axis. + * + * @throws \InvalidArgumentException */ public function rotate(Axis $axis, bool $clockwise) : Facing{ $rotated = match ($axis) { @@ -115,14 +117,32 @@ public function rotate(Axis $axis, bool $clockwise) : Facing{ return $clockwise ? $rotated : $rotated->opposite(); } + /** + * Rotates the given direction around the Y axis. + * + * @see Facing::rotate() + * @throws \InvalidArgumentException + */ public function rotateY(bool $clockwise) : Facing{ return $this->rotate(Axis::Y, $clockwise); } + /** + * Rotates the given direction around the Z axis. + * + * @see Facing::rotate() + * @throws \InvalidArgumentException + */ public function rotateZ(bool $clockwise) : Facing{ return $this->rotate(Axis::Z, $clockwise); } + /** + * Rotates the given direction around the X axis. + * + * @see Facing::rotate() + * @throws \InvalidArgumentException + */ public function rotateX(bool $clockwise) : Facing{ return $this->rotate(Axis::X, $clockwise); } From c3a71f9580145299b9774e16ce0a0959a02239d6 Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Mon, 2 Dec 2024 16:42:37 +0100 Subject: [PATCH 21/32] Remove @var to specific content of variable --- src/AxisAlignedBB.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/AxisAlignedBB.php b/src/AxisAlignedBB.php index c437c6b..3a8614f 100644 --- a/src/AxisAlignedBB.php +++ b/src/AxisAlignedBB.php @@ -448,7 +448,6 @@ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResu $vector = null; $distance = PHP_INT_MAX; - /** @var Facing $face */ $face = null; foreach([ @@ -463,7 +462,6 @@ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResu if($v !== null and ($d = $pos1->distanceSquared($v)) < $distance){ $vector = $v; $distance = $d; - /** @var Facing $face */ $face = $value[0]; } } From 8ccea24b89a094bcff4cb36518c8ad2bf7225a89 Mon Sep 17 00:00:00 2001 From: "Dylan T." Date: Mon, 2 Dec 2024 16:15:18 +0000 Subject: [PATCH 22/32] Update AxisAlignedBB.php --- src/AxisAlignedBB.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/AxisAlignedBB.php b/src/AxisAlignedBB.php index 3a8614f..2104c8f 100644 --- a/src/AxisAlignedBB.php +++ b/src/AxisAlignedBB.php @@ -446,9 +446,8 @@ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResu $v6 = null; } - $vector = null; $distance = PHP_INT_MAX; - $face = null; + $hitInfo = null; foreach([ [Facing::WEST, $v1], @@ -460,16 +459,16 @@ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResu ] as $value){ $v = $value[1]; if($v !== null and ($d = $pos1->distanceSquared($v)) < $distance){ - $vector = $v; $distance = $d; - $face = $value[0]; + $hitInfo = $value; } } - if($vector === null){ + if($hitInfo === null){ return null; } + [$face, $vector] = $hitInfo; return new RayTraceResult($this, $face, $vector); } From 9f734d4031bd48f458aaaa88d664df34f20f5dd0 Mon Sep 17 00:00:00 2001 From: "Dylan T." Date: Mon, 2 Dec 2024 16:17:42 +0000 Subject: [PATCH 23/32] smh --- src/AxisAlignedBB.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/AxisAlignedBB.php b/src/AxisAlignedBB.php index 2104c8f..7f6b485 100644 --- a/src/AxisAlignedBB.php +++ b/src/AxisAlignedBB.php @@ -456,11 +456,10 @@ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResu [Facing::UP, $v4], [Facing::NORTH, $v5], [Facing::SOUTH, $v6] - ] as $value){ - $v = $value[1]; + ] as [$facing, $v]){ if($v !== null and ($d = $pos1->distanceSquared($v)) < $distance){ $distance = $d; - $hitInfo = $value; + $hitInfo = [$facing, $v]; } } From c1ccf1e7e93e7e69407774c30fae3fee8ca55b40 Mon Sep 17 00:00:00 2001 From: "Dylan T." Date: Mon, 2 Dec 2024 16:31:03 +0000 Subject: [PATCH 24/32] formatting tidy I was going to submit these as review comments, but it's less effort if i just fix them directly --- src/Facing.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Facing.php b/src/Facing.php index 8552283..90fd1b2 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -42,7 +42,7 @@ enum Facing{ * Returns the axis of the given direction. */ public function axis() : Axis{ - return match ($this) { + return match($this){ self::DOWN, self::UP => Axis::Y, self::NORTH, self::SOUTH => Axis::Z, self::WEST, self::EAST => Axis::X, @@ -53,7 +53,7 @@ public function axis() : Axis{ * @phpstan-return non-empty-array */ public function offset() : array{ - return match ($this) { + return match($this){ self::DOWN => [ 0, -1, 0], self::UP => [ 0, +1, 0], self::NORTH => [ 0, 0, -1], @@ -74,7 +74,7 @@ public function isPositive() : bool{ * Returns the opposite Facing of the specified one. */ public function opposite() : Facing{ - return match ($this) { + return match($this){ self::DOWN => self::UP, self::UP => self::DOWN, self::NORTH => self::SOUTH, @@ -90,22 +90,22 @@ public function opposite() : Facing{ * @throws \InvalidArgumentException */ public function rotate(Axis $axis, bool $clockwise) : Facing{ - $rotated = match ($axis) { - Axis::Y => match ($this) { + $rotated = match($axis){ + Axis::Y => match($this){ self::NORTH => self::EAST, self::EAST => self::SOUTH, self::SOUTH => self::WEST, self::WEST => self::NORTH, default => throw new \InvalidArgumentException("Face " . strtolower($this->name) . " not match with Axis " . strtolower($axis->name)) }, - Axis::Z => match ($this) { + Axis::Z => match($this){ self::UP => self::EAST, self::EAST => self::DOWN, self::DOWN => self::WEST, self::WEST => self::UP, default => throw new \InvalidArgumentException("Face " . strtolower($this->name) . " not match with Axis " . strtolower($axis->name)) }, - Axis::X => match ($this) { + Axis::X => match($this){ self::UP => self::NORTH, self::NORTH => self::DOWN, self::DOWN => self::SOUTH, @@ -146,4 +146,4 @@ public function rotateZ(bool $clockwise) : Facing{ public function rotateX(bool $clockwise) : Facing{ return $this->rotate(Axis::X, $clockwise); } -} \ No newline at end of file +} From c9ebd9f053accf45baefcef9b6a6903bc2c366b6 Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Mon, 2 Dec 2024 18:40:58 +0100 Subject: [PATCH 25/32] Remove unrealated changes --- src/Facing.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Facing.php b/src/Facing.php index 90fd1b2..7c583fd 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -50,7 +50,7 @@ public function axis() : Axis{ } /** - * @phpstan-return non-empty-array + * @phpstan-return array{-1|0|1, -1|0|1, -1|0|1} */ public function offset() : array{ return match($this){ @@ -87,7 +87,7 @@ public function opposite() : Facing{ /** * Rotates the given direction around the axis. * - * @throws \InvalidArgumentException + * @throws \InvalidArgumentException if not possible to rotate this direction around $axis */ public function rotate(Axis $axis, bool $clockwise) : Facing{ $rotated = match($axis){ @@ -96,21 +96,21 @@ public function rotate(Axis $axis, bool $clockwise) : Facing{ self::EAST => self::SOUTH, self::SOUTH => self::WEST, self::WEST => self::NORTH, - default => throw new \InvalidArgumentException("Face " . strtolower($this->name) . " not match with Axis " . strtolower($axis->name)) + default => throw new \InvalidArgumentException("Cannot rotate facing \"" . strtolower($this->name) . "\" around axis \"" . strtolower($axis->name) . "\"") }, Axis::Z => match($this){ self::UP => self::EAST, self::EAST => self::DOWN, self::DOWN => self::WEST, self::WEST => self::UP, - default => throw new \InvalidArgumentException("Face " . strtolower($this->name) . " not match with Axis " . strtolower($axis->name)) + default => throw new \InvalidArgumentException("Cannot rotate facing \"" . strtolower($this->name) . "\" around axis \"" . strtolower($axis->name) . "\"") }, Axis::X => match($this){ self::UP => self::NORTH, self::NORTH => self::DOWN, self::DOWN => self::SOUTH, self::SOUTH => self::UP, - default => throw new \InvalidArgumentException("Face " . strtolower($this->name) . " not match with Axis " . strtolower($axis->name)) + default => throw new \InvalidArgumentException("Cannot rotate facing \"" . strtolower($this->name) . "\" around axis \"" . strtolower($axis->name) . "\"") } }; From adf4a5a0f06a6ecd873964d18a832ca9e09f898b Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Mon, 2 Dec 2024 18:44:36 +0100 Subject: [PATCH 26/32] Cs --- tests/phpunit/FacingTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/FacingTest.php b/tests/phpunit/FacingTest.php index 22a630c..41c988d 100644 --- a/tests/phpunit/FacingTest.php +++ b/tests/phpunit/FacingTest.php @@ -35,7 +35,7 @@ public static function axisProvider() : \Generator{ yield [Facing::EAST, Axis::X]; } - #[DataProvider("axisProvider")] + #[DataProvider("axisProvider")] public function testAxis(Facing $direction, Axis $axis) : void{ self::assertEquals($axis, $direction->axis()); } From f76eca361e2b5f9d5146e176c93f817f99ddab6f Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Tue, 3 Dec 2024 12:35:04 +0100 Subject: [PATCH 27/32] Remove unrelated documentation --- src/Facing.php | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/Facing.php b/src/Facing.php index 7c583fd..ebdc537 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -117,32 +117,14 @@ public function rotate(Axis $axis, bool $clockwise) : Facing{ return $clockwise ? $rotated : $rotated->opposite(); } - /** - * Rotates the given direction around the Y axis. - * - * @see Facing::rotate() - * @throws \InvalidArgumentException - */ public function rotateY(bool $clockwise) : Facing{ return $this->rotate(Axis::Y, $clockwise); } - /** - * Rotates the given direction around the Z axis. - * - * @see Facing::rotate() - * @throws \InvalidArgumentException - */ public function rotateZ(bool $clockwise) : Facing{ return $this->rotate(Axis::Z, $clockwise); } - /** - * Rotates the given direction around the X axis. - * - * @see Facing::rotate() - * @throws \InvalidArgumentException - */ public function rotateX(bool $clockwise) : Facing{ return $this->rotate(Axis::X, $clockwise); } From e212ce7f1e9770fa36f15f6fd3425eedc817b63c Mon Sep 17 00:00:00 2001 From: Hugo_ <55756021+Dhaiven@users.noreply.github.com> Date: Tue, 3 Dec 2024 12:38:05 +0100 Subject: [PATCH 28/32] Don't repeat error or condition --- src/Facing.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Facing.php b/src/Facing.php index ebdc537..4cbf3ff 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -67,7 +67,10 @@ public function offset() : array{ * Returns whether the direction is facing the positive of its axis. */ public function isPositive() : bool{ - return $this === self::UP || $this === self::SOUTH || $this === self::EAST; + return match($this){ + self::UP, self::SOUTH, self::EAST => true, + self::DOWN, self::NORTH, self::WEST => false, + }; } /** @@ -96,24 +99,28 @@ public function rotate(Axis $axis, bool $clockwise) : Facing{ self::EAST => self::SOUTH, self::SOUTH => self::WEST, self::WEST => self::NORTH, - default => throw new \InvalidArgumentException("Cannot rotate facing \"" . strtolower($this->name) . "\" around axis \"" . strtolower($axis->name) . "\"") + default => null }, Axis::Z => match($this){ self::UP => self::EAST, self::EAST => self::DOWN, self::DOWN => self::WEST, self::WEST => self::UP, - default => throw new \InvalidArgumentException("Cannot rotate facing \"" . strtolower($this->name) . "\" around axis \"" . strtolower($axis->name) . "\"") + default => null }, Axis::X => match($this){ self::UP => self::NORTH, self::NORTH => self::DOWN, self::DOWN => self::SOUTH, self::SOUTH => self::UP, - default => throw new \InvalidArgumentException("Cannot rotate facing \"" . strtolower($this->name) . "\" around axis \"" . strtolower($axis->name) . "\"") + default => null } }; + if($rotated === null) { + throw new \InvalidArgumentException("Cannot rotate facing \"" . strtolower($this->name) . "\" around axis \"" . strtolower($axis->name) . "\""); + } + return $clockwise ? $rotated : $rotated->opposite(); } From e3fe09a3a12509a06b3aa567901aac019a726b20 Mon Sep 17 00:00:00 2001 From: "Dylan T." Date: Tue, 3 Dec 2024 12:27:45 +0000 Subject: [PATCH 29/32] Update Facing.php --- src/Facing.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Facing.php b/src/Facing.php index 4cbf3ff..19875a4 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -124,14 +124,23 @@ public function rotate(Axis $axis, bool $clockwise) : Facing{ return $clockwise ? $rotated : $rotated->opposite(); } + /** + * @throws \InvalidArgumentException + */ public function rotateY(bool $clockwise) : Facing{ return $this->rotate(Axis::Y, $clockwise); } + /** + * @throws \InvalidArgumentException + */ public function rotateZ(bool $clockwise) : Facing{ return $this->rotate(Axis::Z, $clockwise); } + /** + * @throws \InvalidArgumentException + */ public function rotateX(bool $clockwise) : Facing{ return $this->rotate(Axis::X, $clockwise); } From 686592943105dc43f43a9045fdabf9bb22d63906 Mon Sep 17 00:00:00 2001 From: "Dylan T." Date: Tue, 3 Dec 2024 12:33:18 +0000 Subject: [PATCH 30/32] Update Vector3.php --- src/Vector3.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Vector3.php b/src/Vector3.php index 3e449a9..c94bde3 100644 --- a/src/Vector3.php +++ b/src/Vector3.php @@ -190,6 +190,8 @@ public function sidesArray(bool $keys = false, int $step = 1) : array{ /** * Yields vectors stepped out from this one in directions except those on the given axis. + * + * @param Axis $axis Facing directions on this axis will be excluded * * @return \Generator|Vector3[] * @phpstan-return \Generator From f25c60812376f6f3eaab42314899d5b55166d89f Mon Sep 17 00:00:00 2001 From: "Dylan T." Date: Tue, 3 Dec 2024 12:35:09 +0000 Subject: [PATCH 31/32] github web editor try not to suck challenge IMPOSSIBLE --- src/Vector3.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Vector3.php b/src/Vector3.php index c94bde3..72ba37a 100644 --- a/src/Vector3.php +++ b/src/Vector3.php @@ -190,7 +190,7 @@ public function sidesArray(bool $keys = false, int $step = 1) : array{ /** * Yields vectors stepped out from this one in directions except those on the given axis. - * + * * @param Axis $axis Facing directions on this axis will be excluded * * @return \Generator|Vector3[] From 49e2fef3384ef1d64d688d7eb2f2e9c429b84177 Mon Sep 17 00:00:00 2001 From: "Dylan T." Date: Tue, 3 Dec 2024 12:36:31 +0000 Subject: [PATCH 32/32] Update Facing.php --- src/Facing.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Facing.php b/src/Facing.php index 19875a4..f6b0b51 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -117,7 +117,7 @@ public function rotate(Axis $axis, bool $clockwise) : Facing{ } }; - if($rotated === null) { + if($rotated === null){ throw new \InvalidArgumentException("Cannot rotate facing \"" . strtolower($this->name) . "\" around axis \"" . strtolower($axis->name) . "\""); }