From b2201c8c59600cf1f8db76c0d0b6be211b0dbf04 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 24 Nov 2018 19:17:03 +0000 Subject: [PATCH] Further cleanup to general AABB handling --- src/pocketmine/block/Anvil.php | 5 +---- src/pocketmine/block/Bed.php | 2 +- src/pocketmine/block/Block.php | 2 +- src/pocketmine/block/Cactus.php | 4 ++-- src/pocketmine/block/Cake.php | 14 ++++---------- src/pocketmine/block/Carpet.php | 2 +- src/pocketmine/block/Chest.php | 2 +- src/pocketmine/block/CocoaBlock.php | 6 +++--- src/pocketmine/block/DaylightSensor.php | 3 ++- src/pocketmine/block/Door.php | 5 +++-- src/pocketmine/block/EnchantingTable.php | 3 ++- src/pocketmine/block/EndPortalFrame.php | 9 +-------- src/pocketmine/block/EndRod.php | 2 +- src/pocketmine/block/Farmland.php | 2 +- src/pocketmine/block/FenceGate.php | 3 +-- src/pocketmine/block/FlowerPot.php | 3 +-- src/pocketmine/block/GrassPath.php | 2 +- src/pocketmine/block/Ladder.php | 3 +-- src/pocketmine/block/Skull.php | 3 +-- src/pocketmine/block/Slab.php | 6 +----- src/pocketmine/block/SoulSand.php | 3 ++- src/pocketmine/block/Trapdoor.php | 3 +-- src/pocketmine/block/WaterLily.php | 3 +-- src/pocketmine/level/Explosion.php | 2 +- src/pocketmine/level/Level.php | 2 +- 25 files changed, 36 insertions(+), 58 deletions(-) diff --git a/src/pocketmine/block/Anvil.php b/src/pocketmine/block/Anvil.php index 065c8b8ac..78909e666 100644 --- a/src/pocketmine/block/Anvil.php +++ b/src/pocketmine/block/Anvil.php @@ -74,10 +74,7 @@ class Anvil extends Fallable{ } public function recalculateBoundingBox() : ?AxisAlignedBB{ - $inset = 0.125; - - $bb = new AxisAlignedBB(0, 0, 0, 1, 1, 1); - return $bb->squash(Facing::axis(Facing::rotate($this->facing, Facing::AXIS_Y, false)), $inset); + return AxisAlignedBB::one()->squash(Facing::axis(Facing::rotate($this->facing, Facing::AXIS_Y, false)), 1 / 8); } public function onActivate(Item $item, Player $player = null) : bool{ diff --git a/src/pocketmine/block/Bed.php b/src/pocketmine/block/Bed.php index 9586015a0..0ddceb3c7 100644 --- a/src/pocketmine/block/Bed.php +++ b/src/pocketmine/block/Bed.php @@ -80,7 +80,7 @@ class Bed extends Transparent{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - return new AxisAlignedBB(0, 0, 0, 1, 0.5625, 1); + return AxisAlignedBB::one()->trim(Facing::UP, 7 / 16); } public function isHeadPart() : bool{ diff --git a/src/pocketmine/block/Block.php b/src/pocketmine/block/Block.php index 75167523e..5fd0d27ae 100644 --- a/src/pocketmine/block/Block.php +++ b/src/pocketmine/block/Block.php @@ -714,7 +714,7 @@ class Block extends Position implements BlockIds, Metadatable{ * @return AxisAlignedBB|null */ protected function recalculateBoundingBox() : ?AxisAlignedBB{ - return new AxisAlignedBB(0, 0, 0, 1, 1, 1); + return AxisAlignedBB::one(); } /** diff --git a/src/pocketmine/block/Cactus.php b/src/pocketmine/block/Cactus.php index b0a0d6592..19faaa0da 100644 --- a/src/pocketmine/block/Cactus.php +++ b/src/pocketmine/block/Cactus.php @@ -69,8 +69,8 @@ class Cactus extends Transparent{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - static $shrinkSize = 0.0625; - return new AxisAlignedBB($shrinkSize, $shrinkSize, $shrinkSize, 1 - $shrinkSize, 1 - $shrinkSize, 1 - $shrinkSize); + static $shrinkSize = 1 / 16; + return AxisAlignedBB::one()->contract($shrinkSize, 0, $shrinkSize)->trim(Facing::UP, $shrinkSize); } public function onEntityCollide(Entity $entity) : void{ diff --git a/src/pocketmine/block/Cake.php b/src/pocketmine/block/Cake.php index 35862baca..ffa436c57 100644 --- a/src/pocketmine/block/Cake.php +++ b/src/pocketmine/block/Cake.php @@ -66,16 +66,10 @@ class Cake extends Transparent implements FoodSource{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - $f = $this->bites * 0.125; //1 slice width - - return new AxisAlignedBB( - 0.0625 + $f, - 0, - 0.0625, - 1 - 0.0625, - 0.5, - 1 - 0.0625 - ); + return AxisAlignedBB::one() + ->contract(1 / 16, 0, 1 / 16) + ->trim(Facing::UP, 0.5) + ->trim(Facing::WEST, $this->bites / 8); } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ diff --git a/src/pocketmine/block/Carpet.php b/src/pocketmine/block/Carpet.php index b9ca2d13d..bc8efe52c 100644 --- a/src/pocketmine/block/Carpet.php +++ b/src/pocketmine/block/Carpet.php @@ -40,7 +40,7 @@ class Carpet extends Flowable{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - return new AxisAlignedBB(0, 0, 0, 1, 0.0625, 1); + return AxisAlignedBB::one()->trim(Facing::UP, 15 / 16); } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ diff --git a/src/pocketmine/block/Chest.php b/src/pocketmine/block/Chest.php index c5f22ebed..ce41b7a7b 100644 --- a/src/pocketmine/block/Chest.php +++ b/src/pocketmine/block/Chest.php @@ -69,7 +69,7 @@ class Chest extends Transparent{ protected function recalculateBoundingBox() : ?AxisAlignedBB{ //these are slightly bigger than in PC - return new AxisAlignedBB(0.025, 0, 0.025, 0.975, 0.95, 0.975); + return AxisAlignedBB::one()->contract(0.025, 0, 0.025)->trim(Facing::UP, 0.05); } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ diff --git a/src/pocketmine/block/CocoaBlock.php b/src/pocketmine/block/CocoaBlock.php index 15523b53c..b2225bf73 100644 --- a/src/pocketmine/block/CocoaBlock.php +++ b/src/pocketmine/block/CocoaBlock.php @@ -74,10 +74,10 @@ class CocoaBlock extends Transparent{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - $bb = new AxisAlignedBB(0, (7 - $this->age * 2) / 16, 0, 1, 0.75, 1); - - return $bb + return AxisAlignedBB::one() ->squash(Facing::axis(Facing::rotate($this->facing, Facing::AXIS_Y, true)), (6 - $this->age) / 16) //sides + ->trim(Facing::DOWN, (7 - $this->age * 2) / 16) + ->trim(Facing::UP, 0.25) ->trim(Facing::opposite($this->facing), 1 / 16) //gap between log and pod ->trim($this->facing, (11 - $this->age * 2) / 16); //outward face } diff --git a/src/pocketmine/block/DaylightSensor.php b/src/pocketmine/block/DaylightSensor.php index faa2acc23..ee64b39d8 100644 --- a/src/pocketmine/block/DaylightSensor.php +++ b/src/pocketmine/block/DaylightSensor.php @@ -25,6 +25,7 @@ namespace pocketmine\block; use pocketmine\item\Item; use pocketmine\math\AxisAlignedBB; +use pocketmine\math\Facing; use pocketmine\Player; class DaylightSensor extends Transparent{ @@ -82,7 +83,7 @@ class DaylightSensor extends Transparent{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - return new AxisAlignedBB(0, 0, 0, 1, 0.5, 1); + return AxisAlignedBB::one()->trim(Facing::UP, 0.5); } public function onActivate(Item $item, Player $player = null) : bool{ diff --git a/src/pocketmine/block/Door.php b/src/pocketmine/block/Door.php index 5aea3c1ec..dcb6c65d9 100644 --- a/src/pocketmine/block/Door.php +++ b/src/pocketmine/block/Door.php @@ -93,8 +93,9 @@ abstract class Door extends Transparent{ protected function recalculateBoundingBox() : ?AxisAlignedBB{ $this->updateStateFromOtherHalf(); - $bb = new AxisAlignedBB(0, 0, 0, 1, 2, 1); - return $bb->trim($this->open ? Facing::rotate($this->facing, Facing::AXIS_Y, !$this->hingeRight) : $this->facing, 13 / 16); + return AxisAlignedBB::one() + ->extend(Facing::UP, 1) + ->trim($this->open ? Facing::rotate($this->facing, Facing::AXIS_Y, !$this->hingeRight) : $this->facing, 13 / 16); } public function onNearbyBlockChange() : void{ diff --git a/src/pocketmine/block/EnchantingTable.php b/src/pocketmine/block/EnchantingTable.php index c608d394e..6a8b68d90 100644 --- a/src/pocketmine/block/EnchantingTable.php +++ b/src/pocketmine/block/EnchantingTable.php @@ -27,6 +27,7 @@ use pocketmine\inventory\EnchantInventory; use pocketmine\item\Item; use pocketmine\item\TieredTool; use pocketmine\math\AxisAlignedBB; +use pocketmine\math\Facing; use pocketmine\math\Vector3; use pocketmine\Player; use pocketmine\tile\EnchantTable as TileEnchantTable; @@ -70,7 +71,7 @@ class EnchantingTable extends Transparent{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - return new AxisAlignedBB(0, 0, 0, 1, 0.75, 1); + return AxisAlignedBB::one()->trim(Facing::UP, 0.25); } public function onActivate(Item $item, Player $player = null) : bool{ diff --git a/src/pocketmine/block/EndPortalFrame.php b/src/pocketmine/block/EndPortalFrame.php index 5c002b71f..438d0a2c2 100644 --- a/src/pocketmine/block/EndPortalFrame.php +++ b/src/pocketmine/block/EndPortalFrame.php @@ -77,14 +77,7 @@ class EndPortalFrame extends Solid{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - return new AxisAlignedBB( - 0, - 0, - 0, - 1, - $this->eye ? 1 : 0.8125, - 1 - ); + return AxisAlignedBB::one()->trim(Facing::UP, 3 / 16); } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ diff --git a/src/pocketmine/block/EndRod.php b/src/pocketmine/block/EndRod.php index d4caf3bc4..eeebbd200 100644 --- a/src/pocketmine/block/EndRod.php +++ b/src/pocketmine/block/EndRod.php @@ -83,7 +83,7 @@ class EndRod extends Flowable{ protected function recalculateBoundingBox() : ?AxisAlignedBB{ $myAxis = Facing::axis($this->facing); - $bb = new AxisAlignedBB(0, 0, 0, 1, 1, 1); + $bb = AxisAlignedBB::one(); foreach([Facing::AXIS_Y, Facing::AXIS_Z, Facing::AXIS_X] as $axis){ if($axis === $myAxis){ continue; diff --git a/src/pocketmine/block/Farmland.php b/src/pocketmine/block/Farmland.php index 89c50a41c..3303bc4d6 100644 --- a/src/pocketmine/block/Farmland.php +++ b/src/pocketmine/block/Farmland.php @@ -64,7 +64,7 @@ class Farmland extends Transparent{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - return new AxisAlignedBB(0, 0, 0, 1, 1, 1); //TODO: y max should be 0.9375, but MCPE currently treats them as a full block (https://bugs.mojang.com/browse/MCPE-12109) + return AxisAlignedBB::one(); //TODO: this should be trimmed at the top by 1/16, but MCPE currently treats them as a full block (https://bugs.mojang.com/browse/MCPE-12109) } public function onNearbyBlockChange() : void{ diff --git a/src/pocketmine/block/FenceGate.php b/src/pocketmine/block/FenceGate.php index 6da249915..5dcb60ded 100644 --- a/src/pocketmine/block/FenceGate.php +++ b/src/pocketmine/block/FenceGate.php @@ -64,8 +64,7 @@ class FenceGate extends Transparent{ return null; } - $bb = new AxisAlignedBB(0, 0, 0, 1, 1.5, 1); - return $bb->squash(Facing::axis($this->facing), 6 / 16); + return AxisAlignedBB::one()->extend(Facing::UP, 0.5)->squash(Facing::axis($this->facing), 6 / 16); } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ diff --git a/src/pocketmine/block/FlowerPot.php b/src/pocketmine/block/FlowerPot.php index e8b2eb8b3..0399fd26e 100644 --- a/src/pocketmine/block/FlowerPot.php +++ b/src/pocketmine/block/FlowerPot.php @@ -60,8 +60,7 @@ class FlowerPot extends Flowable{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - static $f = 0.3125; - return new AxisAlignedBB($f, 0, $f, 1 - $f, 0.375, 1 - $f); + return AxisAlignedBB::one()->contract(3 / 16, 0, 3 / 16)->trim(Facing::UP, 5 / 8); } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ diff --git a/src/pocketmine/block/GrassPath.php b/src/pocketmine/block/GrassPath.php index 389d5ee58..459ff721d 100644 --- a/src/pocketmine/block/GrassPath.php +++ b/src/pocketmine/block/GrassPath.php @@ -45,7 +45,7 @@ class GrassPath extends Transparent{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - return new AxisAlignedBB(0, 0, 0, 1, 1, 1); //TODO: y max should be 0.9375, but MCPE currently treats them as a full block (https://bugs.mojang.com/browse/MCPE-12109) + return AxisAlignedBB::one(); //TODO: this should be trimmed at the top by 1/16, but MCPE currently treats them as a full block (https://bugs.mojang.com/browse/MCPE-12109) } public function getHardness() : float{ diff --git a/src/pocketmine/block/Ladder.php b/src/pocketmine/block/Ladder.php index f6f2218b0..1b834204e 100644 --- a/src/pocketmine/block/Ladder.php +++ b/src/pocketmine/block/Ladder.php @@ -79,8 +79,7 @@ class Ladder extends Transparent{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - $bb = new AxisAlignedBB(0, 0, 0, 1, 1, 1); - return $bb->trim($this->facing, 13 / 16); + return AxisAlignedBB::one()->trim($this->facing, 13 / 16); } diff --git a/src/pocketmine/block/Skull.php b/src/pocketmine/block/Skull.php index e676b57ab..d19815f4d 100644 --- a/src/pocketmine/block/Skull.php +++ b/src/pocketmine/block/Skull.php @@ -65,8 +65,7 @@ class Skull extends Flowable{ protected function recalculateBoundingBox() : ?AxisAlignedBB{ //TODO: different bounds depending on attached face - static $f = 0.25; - return new AxisAlignedBB($f, 0, $f, 1 - $f, 0.5, 1 - $f); + return AxisAlignedBB::one()->contract(0.25, 0, 0.25)->trim(Facing::UP, 0.5); } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ diff --git a/src/pocketmine/block/Slab.php b/src/pocketmine/block/Slab.php index d6b8fb63f..ef7950e2f 100644 --- a/src/pocketmine/block/Slab.php +++ b/src/pocketmine/block/Slab.php @@ -101,10 +101,6 @@ abstract class Slab extends Transparent{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - if($this->top){ - return new AxisAlignedBB(0, 0.5, 0, 1, 1, 1); - }else{ - return new AxisAlignedBB(0, 0, 0, 1, 0.5, 1); - } + return AxisAlignedBB::one()->trim($this->top ? Facing::DOWN : Facing::UP, 0.5); } } diff --git a/src/pocketmine/block/SoulSand.php b/src/pocketmine/block/SoulSand.php index 9df0ac22b..8dc21f87c 100644 --- a/src/pocketmine/block/SoulSand.php +++ b/src/pocketmine/block/SoulSand.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\block; use pocketmine\math\AxisAlignedBB; +use pocketmine\math\Facing; class SoulSand extends Solid{ @@ -46,6 +47,6 @@ class SoulSand extends Solid{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - return new AxisAlignedBB(0, 0, 0, 1, 1 - 0.125, 1); + return AxisAlignedBB::one()->trim(Facing::UP, 1 / 8); } } diff --git a/src/pocketmine/block/Trapdoor.php b/src/pocketmine/block/Trapdoor.php index 42cb728b9..625824409 100644 --- a/src/pocketmine/block/Trapdoor.php +++ b/src/pocketmine/block/Trapdoor.php @@ -73,8 +73,7 @@ class Trapdoor extends Transparent{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - $bb = new AxisAlignedBB(0, 0, 0, 1, 1, 1); - return $bb->trim($this->open ? $this->facing : ($this->top ? Facing::DOWN : Facing::UP), 13 / 16); + return AxisAlignedBB::one()->trim($this->open ? $this->facing : ($this->top ? Facing::DOWN : Facing::UP), 13 / 16); } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ diff --git a/src/pocketmine/block/WaterLily.php b/src/pocketmine/block/WaterLily.php index faf1316fa..0c4c05ff1 100644 --- a/src/pocketmine/block/WaterLily.php +++ b/src/pocketmine/block/WaterLily.php @@ -46,8 +46,7 @@ class WaterLily extends Flowable{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - static $f = 0.0625; - return new AxisAlignedBB($f, 0, $f, 1 - $f, 0.015625, 1 - $f); + return AxisAlignedBB::one()->contract(1 / 16, 0, 1 / 16)->trim(Facing::UP, 63 / 64); } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ diff --git a/src/pocketmine/level/Explosion.php b/src/pocketmine/level/Explosion.php index 3d1a96391..3f2b0e734 100644 --- a/src/pocketmine/level/Explosion.php +++ b/src/pocketmine/level/Explosion.php @@ -238,7 +238,7 @@ class Explosion{ $ev = new BlockUpdateEvent($this->level->getBlockAt($sideBlock->x, $sideBlock->y, $sideBlock->z)); $ev->call(); if(!$ev->isCancelled()){ - foreach($this->level->getNearbyEntities(new AxisAlignedBB($sideBlock->x - 1, $sideBlock->y - 1, $sideBlock->z - 1, $sideBlock->x + 2, $sideBlock->y + 2, $sideBlock->z + 2)) as $entity){ + foreach($this->level->getNearbyEntities(AxisAlignedBB::one()->offset($sideBlock->x, $sideBlock->y, $sideBlock->z)->expand(1, 1, 1)) as $entity){ $entity->onNearbyBlockChange(); } $ev->getBlock()->onNearbyBlockChange(); diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 65610a025..3914e9133 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -1546,7 +1546,7 @@ class Level implements ChunkManager, Metadatable{ $ev = new BlockUpdateEvent($block); $ev->call(); if(!$ev->isCancelled()){ - foreach($this->getNearbyEntities(new AxisAlignedBB($block->x - 1, $block->y - 1, $block->z - 1, $block->x + 2, $block->y + 2, $block->z + 2)) as $entity){ + foreach($this->getNearbyEntities(AxisAlignedBB::one()->offset($block->x, $block->y, $block->z)->expand(1, 1, 1)) as $entity){ $entity->onNearbyBlockChange(); } $ev->getBlock()->onNearbyBlockChange();