diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index b6e91472c..bb2ad190d 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -1057,8 +1057,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ return; } - $oldPos = new Vector3($this->x, $this->y, $this->z); - $distanceSquared = $oldPos->distanceSquared($this->newPosition); + $distanceSquared = $this->newPosition->distanceSquared($this); $revert = false; @@ -1558,7 +1557,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } break; case ProtocolInfo::USE_ITEM_PACKET: - $blockVector = new Vector3($packet->x, $packet->y, $packet->z); + $blockVector = Vector3::createVector($packet->x, $packet->y, $packet->z); $this->craftingType = 0; @@ -1718,7 +1717,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } $this->craftingType = 0; - $vector = new Vector3($packet->x, $packet->y, $packet->z); + $vector = Vector3::createVector($packet->x, $packet->y, $packet->z); if($this->isCreative()){ @@ -2185,7 +2184,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ } $this->craftingType = 0; - $t = $this->level->getTile($v = new Vector3($packet->x, $packet->y, $packet->z)); + $t = $this->level->getTile(Vector3::createVector($packet->x, $packet->y, $packet->z)); if($t instanceof Sign){ $nbt = new NBT(NBT::LITTLE_ENDIAN); $nbt->read($packet->namedtag); @@ -2193,7 +2192,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ if($nbt["id"] !== Tile::SIGN){ $t->spawnTo($this); }else{ - $ev = new SignChangeEvent($this->level->getBlock($v), $this, [ + $ev = new SignChangeEvent($t->getBlock(), $this, [ $nbt["Text1"], $nbt["Text2"], $nbt["Text3"], $nbt["Text4"] ]); diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index b4700540b..18b76853b 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -50,6 +50,8 @@ use pocketmine\level\generator\GenerationRequestManager; use pocketmine\level\generator\Generator; use pocketmine\level\generator\Normal; use pocketmine\level\Level; +use pocketmine\math\AxisAlignedBB; +use pocketmine\math\Vector3; use pocketmine\metadata\EntityMetadataStore; use pocketmine\metadata\LevelMetadataStore; use pocketmine\metadata\PlayerMetadataStore; @@ -2111,6 +2113,15 @@ class Server{ $this->generationManager->process(); + + if(($this->tickCounter % 600) === 0){ + Vector3::clearVectors(); + AxisAlignedBB::clearBoundingBoxes(); + }else{ + Vector3::clearVectorList(); + AxisAlignedBB::clearBoundingBoxPool(); + } + Timings::$serverTickTimer->stopTiming(); TimingsHandler::tick(); diff --git a/src/pocketmine/block/Bed.php b/src/pocketmine/block/Bed.php index fccd98fcc..e012b867f 100644 --- a/src/pocketmine/block/Bed.php +++ b/src/pocketmine/block/Bed.php @@ -36,11 +36,7 @@ class Bed extends Transparent{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } - - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, diff --git a/src/pocketmine/block/Block.php b/src/pocketmine/block/Block.php index ea28c66e3..6374fa73a 100644 --- a/src/pocketmine/block/Block.php +++ b/src/pocketmine/block/Block.php @@ -512,7 +512,6 @@ class Block extends Position implements Metadatable{ protected $name = "Unknown"; protected $breakTime = 0.20; protected $hardness = 10; - protected $boundingBox = null; public $hasEntityCollision = false; public $isActivable = false; public $breakable = true; @@ -820,7 +819,6 @@ class Block extends Position implements Metadatable{ $this->y = (int) $v->y; $this->z = (int) $v->z; $this->level = $v->level; - $this->boundingBox = null; } /** @@ -899,11 +897,7 @@ class Block extends Position implements Metadatable{ * @return AxisAlignedBB */ public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } - - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, diff --git a/src/pocketmine/block/Cactus.php b/src/pocketmine/block/Cactus.php index e2eaa2d06..29110b7da 100644 --- a/src/pocketmine/block/Cactus.php +++ b/src/pocketmine/block/Cactus.php @@ -43,11 +43,8 @@ class Cactus extends Transparent{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x + 0.0625, $this->y, $this->z + 0.0625, @@ -82,7 +79,7 @@ class Cactus extends Transparent{ if($this->getSide(0)->getID() !== self::CACTUS){ if($this->meta == 0x0F){ for($y = 1; $y < 3; ++$y){ - $b = $this->getLevel()->getBlock(new Vector3($this->x, $this->y + $y, $this->z)); + $b = $this->getLevel()->getBlock(Vector3::createVector($this->x, $this->y + $y, $this->z)); if($b->getID() === self::AIR){ Server::getInstance()->getPluginManager()->callEvent($ev = new BlockGrowEvent($b, new Cactus())); if(!$ev->isCancelled()){ diff --git a/src/pocketmine/block/Cake.php b/src/pocketmine/block/Cake.php index 0f98dc373..7cff10727 100644 --- a/src/pocketmine/block/Cake.php +++ b/src/pocketmine/block/Cake.php @@ -38,13 +38,10 @@ class Cake extends Transparent{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } $f = (1 + $this->getDamage() * 2) / 16; - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x + $f, $this->y, $this->z + 0.0625, diff --git a/src/pocketmine/block/Carpet.php b/src/pocketmine/block/Carpet.php index 91269b9b0..d7b315072 100644 --- a/src/pocketmine/block/Carpet.php +++ b/src/pocketmine/block/Carpet.php @@ -54,11 +54,8 @@ class Carpet extends Flowable{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, diff --git a/src/pocketmine/block/Chest.php b/src/pocketmine/block/Chest.php index ad24a0316..9242b4a99 100644 --- a/src/pocketmine/block/Chest.php +++ b/src/pocketmine/block/Chest.php @@ -43,11 +43,8 @@ class Chest extends Transparent{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x + 0.0625, $this->y, $this->z + 0.0625, diff --git a/src/pocketmine/block/Door.php b/src/pocketmine/block/Door.php index a28598740..04506d99b 100644 --- a/src/pocketmine/block/Door.php +++ b/src/pocketmine/block/Door.php @@ -53,14 +53,11 @@ abstract class Door extends Transparent{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } $f = 0.1875; $damage = $this->getFullDamage(); - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, @@ -76,7 +73,7 @@ abstract class Door extends Transparent{ if($j === 0){ if($flag){ if(!$flag1){ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, @@ -85,7 +82,7 @@ abstract class Door extends Transparent{ $this->z + $f ); }else{ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z + 1 - $f, @@ -95,7 +92,7 @@ abstract class Door extends Transparent{ ); } }else{ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, @@ -107,7 +104,7 @@ abstract class Door extends Transparent{ }elseif($j === 1){ if($flag){ if(!$flag1){ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x + 1 - $f, $this->y, $this->z, @@ -116,7 +113,7 @@ abstract class Door extends Transparent{ $this->z + 1 ); }else{ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, @@ -126,7 +123,7 @@ abstract class Door extends Transparent{ ); } }else{ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, @@ -138,7 +135,7 @@ abstract class Door extends Transparent{ }elseif($j === 2){ if($flag){ if(!$flag1){ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z + 1 - $f, @@ -147,7 +144,7 @@ abstract class Door extends Transparent{ $this->z + 1 ); }else{ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, @@ -157,7 +154,7 @@ abstract class Door extends Transparent{ ); } }else{ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x + 1 - $f, $this->y, $this->z, @@ -169,7 +166,7 @@ abstract class Door extends Transparent{ }elseif($j === 3){ if($flag){ if(!$flag1){ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, @@ -178,7 +175,7 @@ abstract class Door extends Transparent{ $this->z + 1 ); }else{ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x + 1 - $f, $this->y, $this->z, @@ -188,7 +185,7 @@ abstract class Door extends Transparent{ ); } }else{ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z + 1 - $f, @@ -199,7 +196,7 @@ abstract class Door extends Transparent{ } } - return $this->boundingBox = $bb; + return $bb; } public function onUpdate($type){ diff --git a/src/pocketmine/block/EndPortal.php b/src/pocketmine/block/EndPortal.php index 46eaefeb6..af4ef3132 100644 --- a/src/pocketmine/block/EndPortal.php +++ b/src/pocketmine/block/EndPortal.php @@ -30,11 +30,8 @@ class EndPortal extends Solid{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, diff --git a/src/pocketmine/block/Farmland.php b/src/pocketmine/block/Farmland.php index ec170171a..004f7f8f8 100644 --- a/src/pocketmine/block/Farmland.php +++ b/src/pocketmine/block/Farmland.php @@ -31,11 +31,8 @@ class Farmland extends Solid{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, diff --git a/src/pocketmine/block/Fence.php b/src/pocketmine/block/Fence.php index 5ff6694b0..35bd7c246 100644 --- a/src/pocketmine/block/Fence.php +++ b/src/pocketmine/block/Fence.php @@ -32,9 +32,6 @@ class Fence extends Transparent{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } $flag = $this->canConnect($this->getSide(2)); $flag1 = $this->canConnect($this->getSide(3)); @@ -46,7 +43,7 @@ class Fence extends Transparent{ $f2 = $flag ? 0 : 0.375; $f3 = $flag1 ? 1 : 0.625; - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x + $f, $this->y, $this->z + $f2, diff --git a/src/pocketmine/block/FenceGate.php b/src/pocketmine/block/FenceGate.php index 2d8f6a235..9195347aa 100644 --- a/src/pocketmine/block/FenceGate.php +++ b/src/pocketmine/block/FenceGate.php @@ -39,9 +39,6 @@ class FenceGate extends Transparent{ public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } if(($this->getDamage() & 0x04) > 0){ return null; @@ -49,7 +46,7 @@ class FenceGate extends Transparent{ $i = ($this->getDamage() & 0x03); if($i === 2 and $i === 0){ - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z + 0.375, @@ -58,7 +55,7 @@ class FenceGate extends Transparent{ $this->z + 0.625 ); }else{ - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x + 0.375, $this->y, $this->z, diff --git a/src/pocketmine/block/Ladder.php b/src/pocketmine/block/Ladder.php index 3ce78534e..10b7c4fb8 100644 --- a/src/pocketmine/block/Ladder.php +++ b/src/pocketmine/block/Ladder.php @@ -44,14 +44,11 @@ class Ladder extends Transparent{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } $f = 0.125; if($this->meta === 2){ - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z + 1 - $f, @@ -60,7 +57,7 @@ class Ladder extends Transparent{ $this->z + 1 ); }elseif($this->meta === 3){ - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, @@ -69,7 +66,7 @@ class Ladder extends Transparent{ $this->z + $f ); }elseif($this->meta === 4){ - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x + 1 - $f, $this->y, $this->z, @@ -78,7 +75,7 @@ class Ladder extends Transparent{ $this->z + 1 ); }elseif($this->meta === 5){ - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, diff --git a/src/pocketmine/block/Liquid.php b/src/pocketmine/block/Liquid.php index 96c173939..20862628f 100644 --- a/src/pocketmine/block/Liquid.php +++ b/src/pocketmine/block/Liquid.php @@ -80,7 +80,7 @@ abstract class Liquid extends Transparent{ } public function getFlowVector(){ - $vector = new Vector3(0, 0, 0); + $vector = Vector3::createVector(0, 0, 0); $decay = $this->getEffectiveFlowDecay($this); @@ -99,7 +99,7 @@ abstract class Liquid extends Transparent{ }elseif($j === 3){ ++$z; } - $sideBlock = $this->getLevel()->getBlock(new Vector3($x, $y, $z)); + $sideBlock = $this->getLevel()->getBlock(Vector3::createVector($x, $y, $z)); $blockDecay = $this->getEffectiveFlowDecay($sideBlock); if($blockDecay < 0){ @@ -314,7 +314,7 @@ abstract class Liquid extends Transparent{ }elseif($j === 3){ ++$z; } - $blockSide = $this->getLevel()->getBlock(new Vector3($x, $y, $z)); + $blockSide = $this->getLevel()->getBlock(Vector3::createVector($x, $y, $z)); if(!$blockSide->isFlowable and !($blockSide instanceof Liquid)){ continue; @@ -356,7 +356,7 @@ abstract class Liquid extends Transparent{ }elseif($j === 3){ ++$z; } - $block = $this->getLevel()->getBlock(new Vector3($x, $y, $z)); + $block = $this->getLevel()->getBlock(Vector3::createVector($x, $y, $z)); if(!$block->isFlowable and !($block instanceof Liquid)){ continue; diff --git a/src/pocketmine/block/Slab.php b/src/pocketmine/block/Slab.php index 241f62d80..35bdcc09a 100644 --- a/src/pocketmine/block/Slab.php +++ b/src/pocketmine/block/Slab.php @@ -48,12 +48,9 @@ class Slab extends Transparent{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } if(($this->meta & 0x08) > 0){ - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y + 0.5, $this->z, @@ -62,7 +59,7 @@ class Slab extends Transparent{ $this->z + 1 ); }else{ - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, diff --git a/src/pocketmine/block/SoulSand.php b/src/pocketmine/block/SoulSand.php index cde302304..3276c7613 100644 --- a/src/pocketmine/block/SoulSand.php +++ b/src/pocketmine/block/SoulSand.php @@ -31,11 +31,8 @@ class SoulSand extends Solid{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, diff --git a/src/pocketmine/block/Stair.php b/src/pocketmine/block/Stair.php index 74afea529..c88f5bafd 100644 --- a/src/pocketmine/block/Stair.php +++ b/src/pocketmine/block/Stair.php @@ -54,7 +54,7 @@ abstract class Stair extends Transparent{ $f3 = 0.5; } - if($bb->intersectsWith($bb2 = new AxisAlignedBB( + if($bb->intersectsWith($bb2 = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y + $f, $this->z, @@ -66,7 +66,7 @@ abstract class Stair extends Transparent{ } if($j === 0){ - if($bb->intersectsWith($bb2 = new AxisAlignedBB( + if($bb->intersectsWith($bb2 = AxisAlignedBB::getBoundingBoxFromPool( $this->x + 0.5, $this->y + $f2, $this->z, @@ -77,7 +77,7 @@ abstract class Stair extends Transparent{ $list[] = $bb2; } }elseif($j === 1){ - if($bb->intersectsWith($bb2 = new AxisAlignedBB( + if($bb->intersectsWith($bb2 = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y + $f2, $this->z, @@ -88,7 +88,7 @@ abstract class Stair extends Transparent{ $list[] = $bb2; } }elseif($j === 2){ - if($bb->intersectsWith($bb2 = new AxisAlignedBB( + if($bb->intersectsWith($bb2 = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y + $f2, $this->z + 0.5, @@ -99,7 +99,7 @@ abstract class Stair extends Transparent{ $list[] = $bb2; } }elseif($j === 3){ - if($bb->intersectsWith($bb2 = new AxisAlignedBB( + if($bb->intersectsWith($bb2 = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y + $f2, $this->z, @@ -114,12 +114,9 @@ abstract class Stair extends Transparent{ */ public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } if(($this->getDamage() & 0x04) > 0){ - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y + 0.5, $this->z, @@ -128,7 +125,7 @@ abstract class Stair extends Transparent{ $this->z + 1 ); }else{ - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, diff --git a/src/pocketmine/block/StoneWall.php b/src/pocketmine/block/StoneWall.php index 8abf9688a..a665e2bb8 100644 --- a/src/pocketmine/block/StoneWall.php +++ b/src/pocketmine/block/StoneWall.php @@ -37,9 +37,6 @@ class StoneWall extends Transparent{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } $flag = $this->canConnect($this->getSide(2)); $flag1 = $this->canConnect($this->getSide(3)); @@ -62,7 +59,7 @@ class StoneWall extends Transparent{ $f3 = 0.6875; } - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x + $f, $this->y, $this->z + $f2, diff --git a/src/pocketmine/block/Sugarcane.php b/src/pocketmine/block/Sugarcane.php index 262604a48..bf6cba252 100644 --- a/src/pocketmine/block/Sugarcane.php +++ b/src/pocketmine/block/Sugarcane.php @@ -49,7 +49,7 @@ class Sugarcane extends Flowable{ if($item->getID() === Item::DYE and $item->getDamage() === 0x0F){ //Bonemeal if($this->getSide(0)->getID() !== self::SUGARCANE_BLOCK){ for($y = 1; $y < 3; ++$y){ - $b = $this->getLevel()->getBlock(new Vector3($this->x, $this->y + $y, $this->z)); + $b = $this->getLevel()->getBlock(Vector3::createVector($this->x, $this->y + $y, $this->z)); if($b->getID() === self::AIR){ Server::getInstance()->getPluginManager()->callEvent($ev = new BlockGrowEvent($b, new Sugarcane())); if(!$ev->isCancelled()){ @@ -83,7 +83,7 @@ class Sugarcane extends Flowable{ if($this->getSide(0)->getID() !== self::SUGARCANE_BLOCK){ if($this->meta === 0x0F){ for($y = 1; $y < 3; ++$y){ - $b = $this->getLevel()->getBlock(new Vector3($this->x, $this->y + $y, $this->z)); + $b = $this->getLevel()->getBlock(Vector3::createVector($this->x, $this->y + $y, $this->z)); if($b->getID() === self::AIR){ $this->getLevel()->setBlock($b, new Sugarcane(), true); break; diff --git a/src/pocketmine/block/Thin.php b/src/pocketmine/block/Thin.php index dd621ec7a..59a0f6b50 100644 --- a/src/pocketmine/block/Thin.php +++ b/src/pocketmine/block/Thin.php @@ -30,9 +30,6 @@ abstract class Thin extends Transparent{ public $isSolid = false; public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } $f = 0.4375; $f1 = 0.5625; @@ -66,7 +63,7 @@ abstract class Thin extends Transparent{ $f3 = 1; } - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x + $f, $this->y, $this->z + $f2, diff --git a/src/pocketmine/block/Trapdoor.php b/src/pocketmine/block/Trapdoor.php index d6093df8c..cceb46d11 100644 --- a/src/pocketmine/block/Trapdoor.php +++ b/src/pocketmine/block/Trapdoor.php @@ -38,9 +38,6 @@ class Trapdoor extends Transparent{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } $damage = $this->getDamage(); @@ -49,7 +46,7 @@ class Trapdoor extends Transparent{ $bb = null; if(($damage & 0x08) > 0){ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y + 1 - $f, $this->z, @@ -58,7 +55,7 @@ class Trapdoor extends Transparent{ $this->z + 1 ); }else{ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, @@ -70,7 +67,7 @@ class Trapdoor extends Transparent{ if(($damage & 0x04) > 0){ if(($damage & 0x03) === 0){ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z + 1 - $f, @@ -79,7 +76,7 @@ class Trapdoor extends Transparent{ $this->z + 1 ); }elseif(($damage & 0x03) === 1){ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, @@ -88,7 +85,7 @@ class Trapdoor extends Transparent{ $this->z + $f ); }if(($damage & 0x03) === 2){ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x + 1 - $f, $this->y, $this->z, @@ -97,7 +94,7 @@ class Trapdoor extends Transparent{ $this->z + 1 ); }if(($damage & 0x03) === 3){ - $bb = new AxisAlignedBB( + $bb = AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, @@ -108,7 +105,7 @@ class Trapdoor extends Transparent{ } } - return $this->boundingBox = $bb; + return $bb; } public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){ diff --git a/src/pocketmine/block/Vine.php b/src/pocketmine/block/Vine.php index f58196204..a1e63f3b0 100644 --- a/src/pocketmine/block/Vine.php +++ b/src/pocketmine/block/Vine.php @@ -44,9 +44,6 @@ class Vine extends Transparent{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } $f1 = 1; $f2 = 1; @@ -96,7 +93,7 @@ class Vine extends Transparent{ $f6 = 1; } - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x + $f1, $this->y + $f2, $this->z + $f3, diff --git a/src/pocketmine/block/WoodSlab.php b/src/pocketmine/block/WoodSlab.php index e3d38d964..804c3d68f 100644 --- a/src/pocketmine/block/WoodSlab.php +++ b/src/pocketmine/block/WoodSlab.php @@ -46,12 +46,9 @@ class WoodSlab extends Transparent{ } public function getBoundingBox(){ - if($this->boundingBox !== null){ - return $this->boundingBox; - } if(($this->meta & 0x08) > 0){ - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y + 0.5, $this->z, @@ -60,7 +57,7 @@ class WoodSlab extends Transparent{ $this->z + 1 ); }else{ - return $this->boundingBox = new AxisAlignedBB( + return AxisAlignedBB::getBoundingBoxFromPool( $this->x, $this->y, $this->z, diff --git a/src/pocketmine/command/defaults/TeleportCommand.php b/src/pocketmine/command/defaults/TeleportCommand.php index 797a2e177..fa6cf24e6 100644 --- a/src/pocketmine/command/defaults/TeleportCommand.php +++ b/src/pocketmine/command/defaults/TeleportCommand.php @@ -98,7 +98,7 @@ class TeleportCommand extends VanillaCommand{ $x = $this->getRelativeDouble($target->x, $sender, $args[$pos++]); $y = $this->getRelativeDouble($target->y, $sender, $args[$pos++], 0, 128); $z = $this->getRelativeDouble($target->z, $sender, $args[$pos]); - $target->teleport(new Vector3($x, $y, $z)); + $target->teleport(Vector3::createVector($x, $y, $z)); Command::broadcastCommandMessage($sender, "Teleported " . $target->getDisplayName() . " to " . round($x, 2) . ", " . round($y, 2) . ", " . round($z, 2)); return true; diff --git a/src/pocketmine/entity/Arrow.php b/src/pocketmine/entity/Arrow.php index 5a010f211..b24ad1116 100644 --- a/src/pocketmine/entity/Arrow.php +++ b/src/pocketmine/entity/Arrow.php @@ -87,7 +87,7 @@ class Arrow extends Projectile{ $this->keepMovement = $this->checkObstruction($this->x, ($this->boundingBox->minY + $this->boundingBox->maxY) / 2, $this->z); - $moveVector = new Vector3($this->x + $this->motionX, $this->y + $this->motionY, $this->z + $this->motionZ); + $moveVector = Vector3::createVector($this->x + $this->motionX, $this->y + $this->motionY, $this->z + $this->motionZ); $list = $this->getLevel()->getCollidingEntities($this->boundingBox->addCoord($this->motionX, $this->motionY, $this->motionZ)->expand(1, 1, 1), $this); diff --git a/src/pocketmine/entity/DroppedItem.php b/src/pocketmine/entity/DroppedItem.php index 79d5b5ae8..6d685134b 100644 --- a/src/pocketmine/entity/DroppedItem.php +++ b/src/pocketmine/entity/DroppedItem.php @@ -99,7 +99,7 @@ class DroppedItem extends Entity{ $friction = 1 - $this->drag; if($this->onGround and ($this->motionX != 0 or $this->motionZ != 0)){ - $friction = $this->getLevel()->getBlock(new Vector3($this->getFloorX(), $this->getFloorY() - 1, $this->getFloorZ()))->frictionFactor * $friction; + $friction = $this->getLevel()->getBlock(Vector3::createVector($this->getFloorX(), $this->getFloorY() - 1, $this->getFloorZ()))->frictionFactor * $friction; } $this->motionX *= $friction; diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 2ebce59fc..990142918 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -173,7 +173,7 @@ abstract class Entity extends Location implements Metadatable{ $this->boundingBox = new AxisAlignedBB(0, 0, 0, 0, 0, 0); $this->setPositionAndRotation( - new Vector3( + Vector3::createVector( $this->namedtag["Pos"][0], $this->namedtag["Pos"][1], $this->namedtag["Pos"][2] @@ -182,7 +182,7 @@ abstract class Entity extends Location implements Metadatable{ $this->namedtag->Rotation[1], true ); - $this->setMotion(new Vector3($this->namedtag["Motion"][0], $this->namedtag["Motion"][1], $this->namedtag["Motion"][2])); + $this->setMotion(Vector3::createVector($this->namedtag["Motion"][0], $this->namedtag["Motion"][1], $this->namedtag["Motion"][2])); if(!isset($this->namedtag->FallDistance)){ $this->namedtag->FallDistance = new Float("FallDistance", 0); @@ -387,15 +387,15 @@ abstract class Entity extends Location implements Metadatable{ $list = $this->level->getCollisionBlocks($this->boundingBox); - if(count($list) === 0 and !$this->level->isFullBlock(new Vector3($i, $j, $k))){ + if(count($list) === 0 and !$this->level->isFullBlock(Vector3::createVector($i, $j, $k))){ return false; }else{ - $flag = !$this->level->isFullBlock(new Vector3($i - 1, $j, $k)); - $flag1 = !$this->level->isFullBlock(new Vector3($i + 1, $j, $k)); - //$flag2 = !$this->level->isFullBlock(new Vector3($i, $j - 1, $k)); - $flag3 = !$this->level->isFullBlock(new Vector3($i, $j + 1, $k)); - $flag4 = !$this->level->isFullBlock(new Vector3($i, $j, $k - 1)); - $flag5 = !$this->level->isFullBlock(new Vector3($i, $j, $k + 1)); + $flag = !$this->level->isFullBlock(Vector3::createVector($i - 1, $j, $k)); + $flag1 = !$this->level->isFullBlock(Vector3::createVector($i + 1, $j, $k)); + //$flag2 = !$this->level->isFullBlock(Vector3::createVector($i, $j - 1, $k)); + $flag3 = !$this->level->isFullBlock(Vector3::createVector($i, $j + 1, $k)); + $flag4 = !$this->level->isFullBlock(Vector3::createVector($i, $j, $k - 1)); + $flag5 = !$this->level->isFullBlock(Vector3::createVector($i, $j, $k + 1)); $direction = 3; //UP! $limit = 9999; @@ -583,7 +583,7 @@ abstract class Entity extends Location implements Metadatable{ $x = -$xz * sin(deg2rad($this->yaw)); $z = $xz * cos(deg2rad($this->yaw)); - return new Vector3($x, $y, $z); + return Vector3::createVector($x, $y, $z); } public function onUpdate($currentTick){ @@ -745,7 +745,7 @@ abstract class Entity extends Location implements Metadatable{ } public function isInsideOfWater(){ - $block = $this->level->getBlock($pos = (new Vector3($this->x, $y = ($this->y + $this->getEyeHeight()), $this->z))->floor()); + $block = $this->level->getBlock($pos = Vector3::createVector($this->x, $y = ($this->y + $this->getEyeHeight()), $this->z)->floor()); if($block instanceof Water){ $f = ($pos->y + 1) - ($block->getFluidHeightPercent() - 0.1111111); @@ -756,7 +756,7 @@ abstract class Entity extends Location implements Metadatable{ } public function isInsideOfSolid(){ - $block = $this->level->getBlock($pos = (new Vector3($this->x, $y = ($this->y + $this->getEyeHeight()), $this->z))->floor()); + $block = $this->level->getBlock($pos = Vector3::createVector($this->x, $y = ($this->y + $this->getEyeHeight()), $this->z)->floor()); $bb = $block->getBoundingBox(); @@ -779,7 +779,7 @@ abstract class Entity extends Location implements Metadatable{ if($this->keepMovement){ $this->boundingBox->offset($dx, $dy, $dz); - $pos = new Vector3(($this->boundingBox->minX + $this->boundingBox->maxX) / 2, $this->boundingBox->minY - $this->ySize, ($this->boundingBox->minZ + $this->boundingBox->maxZ) / 2); + $pos = Vector3::createVector(($this->boundingBox->minX + $this->boundingBox->maxX) / 2, $this->boundingBox->minY - $this->ySize, ($this->boundingBox->minZ + $this->boundingBox->maxZ) / 2); $this->setPosition($pos); }else{ @@ -803,7 +803,7 @@ abstract class Entity extends Location implements Metadatable{ $movY = $dy; $movZ = $dz; - $axisalignedbb = clone $this->boundingBox; + $axisalignedbb = AxisAlignedBB::cloneBoundingBoxFromPool($this->boundingBox); /*$sneakFlag = $this->onGround and $this instanceof Player; @@ -881,7 +881,7 @@ abstract class Entity extends Location implements Metadatable{ $dy = $this->stepHeight; $dz = $movZ; - $axisalignedbb1 = clone $this->boundingBox; + $axisalignedbb1 = AxisAlignedBB::cloneBoundingBoxFromPool($this->boundingBox); $this->boundingBox->setBB($axisalignedbb); @@ -941,7 +941,7 @@ abstract class Entity extends Location implements Metadatable{ } - $pos = new Vector3( + $pos = Vector3::createVector( ($this->boundingBox->minX + $this->boundingBox->maxX) / 2, $this->boundingBox->minY - $this->ySize, ($this->boundingBox->minZ + $this->boundingBox->maxZ) / 2 @@ -956,7 +956,7 @@ abstract class Entity extends Location implements Metadatable{ if($this instanceof Player){ if(!$this->onGround or $movY != 0){ - $bb = clone $this->boundingBox; + $bb = AxisAlignedBB::cloneBoundingBoxFromPool($this->boundingBox); $bb->maxY = $bb->minY + 1; if(count($this->level->getCollisionBlocks($bb->expand(0.01, 0.01, 0.01))) > 0){ $this->onGround = true; @@ -998,12 +998,13 @@ abstract class Entity extends Location implements Metadatable{ $maxY = Math::floorFloat($this->boundingBox->maxY - 0.001); $maxZ = Math::floorFloat($this->boundingBox->maxZ - 0.001); - $vector = new Vector3(0, 0, 0); + $vector = Vector3::createVector(0, 0, 0); + $v = Vector3::createVector(0, 0, 0); - for($z = $minZ; $z <= $maxZ; ++$z){ - for($x = $minX; $x <= $maxX; ++$x){ - for($y = $minY; $y <= $maxY; ++$y){ - $block = $this->level->getBlock(new Vector3($x, $y, $z)); + for($v->z = $minZ; $v->z < $maxZ; ++$v->z){ + for($v->x = $minX; $v->x < $maxX; ++$v->x){ + for($v->y = $minY; $v->y < $maxY; ++$v->y){ + $block = $this->level->getBlock($v); if($block !== null and $block->hasEntityCollision){ $block->onEntityCollide($this); if(!($this instanceof Player)){ @@ -1082,7 +1083,7 @@ abstract class Entity extends Location implements Metadatable{ } public function getMotion(){ - return new Vector3($this->motionX, $this->motionY, $this->motionZ); + return Vector3::createVector($this->motionX, $this->motionY, $this->motionZ); } public function setMotion(Vector3 $motion){ @@ -1145,7 +1146,7 @@ abstract class Entity extends Location implements Metadatable{ $this->ySize = 0; $pos = $ev->getTo(); - $this->setMotion(new Vector3(0, 0, 0)); + $this->setMotion(Vector3::createVector(0, 0, 0)); if($this->setPositionAndRotation($pos, $yaw === null ? $this->yaw : $yaw, $pitch === null ? $this->pitch : $pitch, true) !== false){ $this->fallDistance = 0; $this->onGround = true; diff --git a/src/pocketmine/entity/FallingBlock.php b/src/pocketmine/entity/FallingBlock.php index ac83fe800..d8577ae7d 100644 --- a/src/pocketmine/entity/FallingBlock.php +++ b/src/pocketmine/entity/FallingBlock.php @@ -27,6 +27,7 @@ use pocketmine\event\entity\EntityBlockChangeEvent; use pocketmine\event\entity\EntityDamageEvent; use pocketmine\event\entity\EntityRegainHealthEvent; use pocketmine\item\Item; +use pocketmine\math\Vector3; use pocketmine\nbt\tag\Byte; use pocketmine\nbt\tag\String; use pocketmine\network\protocol\AddEntityPacket; @@ -81,12 +82,12 @@ class FallingBlock extends Entity{ if(!$this->dead){ if($this->ticksLived === 1){ - $block = $this->level->getBlock($this->floor()); + $block = $this->level->getBlock($pos = Vector3::createVector($this->x, $this->y, $this->z)->floor()); if($block->getID() != $this->blockId){ $this->kill(); return true; } - $this->level->setBlock($this->floor(), Block::get(0), true); + $this->level->setBlock($pos, Block::get(0), true); } @@ -100,7 +101,7 @@ class FallingBlock extends Entity{ $this->motionY *= 1 - $this->drag; $this->motionZ *= $friction; - $pos = $this->floor(); + $pos = Vector3::createVector($this->x, $this->y, $this->z)->floor(); if($this->onGround){ $this->kill(); diff --git a/src/pocketmine/entity/Living.php b/src/pocketmine/entity/Living.php index 837a925a6..f201d736e 100644 --- a/src/pocketmine/entity/Living.php +++ b/src/pocketmine/entity/Living.php @@ -65,7 +65,7 @@ abstract class Living extends Entity implements Damageable{ public function hasLineOfSight(Entity $entity){ //TODO: head height return true; - //return $this->getLevel()->rayTraceBlocks(new Vector3($this->x, $this->y + $this->height, $this->z), new Vector3($entity->x, $entity->y + $entity->height, $entity->z)) === null; + //return $this->getLevel()->rayTraceBlocks(Vector3::createVector($this->x, $this->y + $this->height, $this->z), Vector3::createVector($entity->x, $entity->y + $entity->height, $entity->z)) === null; } public function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC){ @@ -99,7 +99,7 @@ abstract class Living extends Entity implements Damageable{ $f = sqrt($x ** 2 + $z ** 2); $base = 0.4; - $motion = new Vector3($this->motionX, $this->motionY, $this->motionZ); + $motion = Vector3::createVector($this->motionX, $this->motionY, $this->motionZ); $motion->x /= 2; $motion->y /= 2; diff --git a/src/pocketmine/event/inventory/InventoryTransactionEvent.php b/src/pocketmine/event/inventory/InventoryTransactionEvent.php index 15a68549d..53a4c4e5f 100644 --- a/src/pocketmine/event/inventory/InventoryTransactionEvent.php +++ b/src/pocketmine/event/inventory/InventoryTransactionEvent.php @@ -42,6 +42,9 @@ class InventoryTransactionEvent extends Event implements Cancellable{ $this->ts = $ts; } + /** + * @return TransactionGroup + */ public function getTransaction(){ return $this->ts; } diff --git a/src/pocketmine/inventory/SimpleTransactionGroup.php b/src/pocketmine/inventory/SimpleTransactionGroup.php index 06795daa0..9f37310a1 100644 --- a/src/pocketmine/inventory/SimpleTransactionGroup.php +++ b/src/pocketmine/inventory/SimpleTransactionGroup.php @@ -129,9 +129,8 @@ class SimpleTransactionGroup implements TransactionGroup{ public function canExecute(){ $haveItems = []; $needItems = []; - $this->matchItems($haveItems, $needItems); - return count($haveItems) === 0 and count($needItems) === 0 and count($this->transactions) > 0; + return $this->matchItems($haveItems, $needItems) and count($haveItems) === 0 and count($needItems) === 0 and count($this->transactions) > 0; } public function execute(){ diff --git a/src/pocketmine/level/Explosion.php b/src/pocketmine/level/Explosion.php index c22441dab..74a2f36cb 100644 --- a/src/pocketmine/level/Explosion.php +++ b/src/pocketmine/level/Explosion.php @@ -84,27 +84,31 @@ class Explosion{ return false; } + $pointer = Vector3::createVector(0, 0, 0); + $vector = Vector3::createVector(0, 0, 0); + $vBlock = Vector3::createVector(0, 0, 0); + $mRays = $this->rays - 1; for($i = 0; $i < $this->rays; ++$i){ for($j = 0; $j < $this->rays; ++$j){ //break 2 gets here for($k = 0; $k < $this->rays; ++$k){ if($i == 0 or $i == $mRays or $j == 0 or $j == $mRays or $k == 0 or $k == $mRays){ - $vector = new Vector3($i / $mRays * 2 - 1, $j / $mRays * 2 - 1, $k / $mRays * 2 - 1); //($i / $mRays) * 2 - 1 - $vector = $vector->normalize()->multiply($this->stepLen); - $pointer = clone $this->source; + $vector->setComponents($i / $mRays * 2 - 1, $j / $mRays * 2 - 1, $k / $mRays * 2 - 1); + $vector->setComponents(($vector->x / ($len = $vector->length())) * $this->stepLen, ($vector->y / $len) * $this->stepLen, ($vector->z / $len) * $this->stepLen); + $pointer->setComponents($this->source->x, $this->source->y, $this->source->z); for($blastForce = $this->size * (mt_rand(700, 1300) / 1000); $blastForce > 0; $blastForce -= $this->stepLen * 0.75){ - $vBlock = $pointer->floor(); + $x = (int) $pointer->x; + $y = (int) $pointer->y; + $z = (int) $pointer->z; + $vBlock->setComponents($pointer->x >= $x ? $x : $x - 1, $pointer->y >= $y ? $y : $y - 1, $pointer->z >= $z ? $z : $z - 1); if($vBlock->y < 0 or $vBlock->y > 127){ break; } $block = $this->level->getBlock($vBlock); if(!($block instanceof Air)){ - $block->x = $vBlock->x; - $block->y = $vBlock->y; - $block->z = $vBlock->z; $blastForce -= ($block->getHardness() / 5 + 0.3) * $this->stepLen; if($blastForce > 0){ $index = ($block->x << 15) + ($block->z << 7) + $block->y; @@ -113,7 +117,9 @@ class Explosion{ } } } - $pointer = $pointer->add($vector); + $pointer->x += $vector->x; + $pointer->y += $vector->y; + $pointer->z += $vector->z; } } } @@ -125,7 +131,7 @@ class Explosion{ public function explodeB(){ $send = []; - $source = $this->source->floor(); + $source = Vector3::cloneVector($this->source)->floor(); $yield = (1 / $this->size) * 100; if($this->what instanceof Entity){ @@ -146,7 +152,7 @@ class Explosion{ $minZ = Math::floorFloat($this->source->z - $explosionSize - 1); $maxZ = Math::floorFloat($this->source->z + $explosionSize + 1); - $explosionBB = new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ); + $explosionBB = AxisAlignedBB::getBoundingBoxFromPool($minX, $minY, $minZ, $maxX, $maxY, $maxZ); $list = $this->level->getNearbyEntities($explosionBB, $this->what instanceof Entity ? $this->what : null); foreach($list as $entity){ diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 309c3e40e..63bf1ed7d 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -742,10 +742,12 @@ class Level implements ChunkManager, Metadatable{ $collides = []; - for($z = $minZ; $z < $maxZ; ++$z){ - for($x = $minX; $x < $maxX; ++$x){ - for($y = $minY - 1; $y < $maxY; ++$y){ - $block = $this->getBlock(new Vector3($x, $y, $z)); + $v = Vector3::createVector(0, 0, 0); + + for($v->z = $minZ; $v->z < $maxZ; ++$v->z){ + for($v->x = $minX; $v->x < $maxX; ++$v->x){ + for($v->y = $minY - 1; $v->y < $maxY; ++$v->y){ + $block = $this->getBlock($v); if(!($block instanceof Air)){ $block->collidesWithBB($bb, $collides); } @@ -787,11 +789,12 @@ class Level implements ChunkManager, Metadatable{ $maxZ = Math::floorFloat($bb->maxZ + 1); $collides = []; + $v = Vector3::createVector(0, 0, 0); - for($z = $minZ; $z < $maxZ; ++$z){ - for($x = $minX; $x < $maxX; ++$x){ - for($y = $minY - 1; $y < $maxY; ++$y){ - $block = $this->getBlock(new Vector3($x, $y, $z)); + for($v->z = $minZ; $v->z < $maxZ; ++$v->z){ + for($v->x = $minX; $v->x < $maxX; ++$v->x){ + for($v->y = $minY - 1; $v->y < $maxY; ++$v->y){ + $block = $this->getBlock($v); if(!($block instanceof Air)){ $block->collidesWithBB($bb, $collides); } @@ -801,7 +804,7 @@ class Level implements ChunkManager, Metadatable{ if($entities){ foreach($this->getCollidingEntities($bb->grow(0.25, 0.25, 0.25), $entity) as $ent){ - $collides[] = clone $ent->boundingBox; + $collides[] = AxisAlignedBB::cloneBoundingBoxFromPool($ent->boundingBox); } } @@ -819,7 +822,7 @@ class Level implements ChunkManager, Metadatable{ $y2 = (int) $pos2->y; $z2 = (int) $pos2->z; - $block = $this->getBlock(new Vector3($x1, $y1, $z1)); + $block = $this->getBlock(Vector3::createVector($x1, $y1, $z1)); if(!$flag1 or $block->getBoundingBox() !== null){ $ob = $block->calculateIntercept($pos1, $pos2); @@ -976,7 +979,7 @@ class Level implements ChunkManager, Metadatable{ $this->server->getPluginManager()->callEvent($ev = new BlockUpdateEvent($block)); if(!$ev->isCancelled()){ $ev->getBlock()->onUpdate(self::BLOCK_UPDATE_NORMAL); - 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::getBoundingBoxFromPool($block->x - 1, $block->y - 1, $block->z - 1, $block->x + 2, $block->y + 2, $block->z + 2)) as $entity){ $entity->scheduleUpdate(); } } @@ -991,7 +994,7 @@ class Level implements ChunkManager, Metadatable{ * @param int $delay */ public function dropItem(Vector3 $source, Item $item, Vector3 $motion = null, $delay = 10){ - $motion = $motion === null ? new Vector3(lcg_value() * 0.2 - 0.1, 0.2, lcg_value() * 0.2 - 0.1) : $motion; + $motion = $motion === null ? Vector3::createVector(lcg_value() * 0.2 - 0.1, 0.2, lcg_value() * 0.2 - 0.1) : $motion; if($item->getID() > 0 and $item->getCount() > 0){ $itemEntity = new DroppedItem($this->getChunk($source->getX() >> 4, $source->getZ() >> 4), new Compound("", [ "Pos" => new Enum("Pos", [ @@ -1074,7 +1077,7 @@ class Level implements ChunkManager, Metadatable{ $level = $target->getLevel(); if($level instanceof Level){ - $above = $level->getBlock(new Vector3($target->x, $target->y + 1, $target->z)); + $above = $level->getBlock(Vector3::createVector($target->x, $target->y + 1, $target->z)); if($above instanceof Block){ if($above->getID() === Item::FIRE){ $level->setBlock($above, new Air(), true); @@ -1258,7 +1261,7 @@ class Level implements ChunkManager, Metadatable{ } /** - * Gets the list of all the entitites in this level + * Gets the list of all the entities in this level * * @return Entity[] */ @@ -1923,27 +1926,26 @@ class Level implements ChunkManager, Metadatable{ $x = Math::floorFloat($spawn->x); $y = Math::floorFloat($spawn->y); $z = Math::floorFloat($spawn->z); - for(; $y > 0; --$y){ - $v = new Vector3($x, $y, $z); + $v = Vector3::createVector($x, $y, $z); + for(; $v->y > 0; --$v->y){ $b = $this->getBlock($v->getSide(0)); - if($b === false){ + if($b === null){ return $spawn; }elseif(!($b instanceof Air)){ break; } } - for(; $y < 128; ++$y){ - $v = new Vector3($x, $y, $z); + for(; $v->y < 128; ++$v->y){ if($this->getBlock($v->getSide(1)) instanceof Air){ if($this->getBlock($v) instanceof Air){ - return new Position($spawn->x, $y === Math::floorFloat($spawn->y) ? $spawn->y : $y, $spawn->z, $this); + return new Position($spawn->x, $v->y === Math::floorFloat($spawn->y) ? $spawn->y : $v->y, $spawn->z, $this); } }else{ - ++$y; + ++$v->y; } } - return new Position($spawn->x, $y, $spawn->z, $this); + return new Position($spawn->x, $v->y, $spawn->z, $this); } return false; diff --git a/src/pocketmine/level/MovingObjectPosition.php b/src/pocketmine/level/MovingObjectPosition.php index 82757d791..4c73d64bd 100644 --- a/src/pocketmine/level/MovingObjectPosition.php +++ b/src/pocketmine/level/MovingObjectPosition.php @@ -64,7 +64,7 @@ class MovingObjectPosition{ $ob->blockX = $x; $ob->blockY = $y; $ob->blockZ = $z; - $ob->hitVector = new Vector3($hitVector->x, $hitVector->y, $hitVector->z); + $ob->hitVector = Vector3::createVector($hitVector->x, $hitVector->y, $hitVector->z); return $ob; } @@ -77,7 +77,7 @@ class MovingObjectPosition{ $ob = new MovingObjectPosition; $ob->typeOfHit = 1; $ob->entityHit = $entity; - $ob->hitVector = new Vector3($entity->x, $entity->y, $entity->z); + $ob->hitVector = Vector3::createVector($entity->x, $entity->y, $entity->z); return $ob; } } diff --git a/src/pocketmine/math/AxisAlignedBB.php b/src/pocketmine/math/AxisAlignedBB.php index 914896c21..e9bc2773e 100644 --- a/src/pocketmine/math/AxisAlignedBB.php +++ b/src/pocketmine/math/AxisAlignedBB.php @@ -22,11 +22,13 @@ namespace pocketmine\math; use pocketmine\level\MovingObjectPosition; -/** - * WARNING: This class is available on the PocketMine-MP Zephir project. - * If this class is modified, remember to modify the PHP C extension. - */ class AxisAlignedBB{ + + + /** @var AxisAlignedBB[] */ + private static $boundingBoxes = []; + private static $nextBoundingBox = 0; + public $minX; public $minY; public $minZ; @@ -43,6 +45,46 @@ class AxisAlignedBB{ $this->maxZ = $maxZ; } + public static function clearBoundingBoxes(){ + self::$nextBoundingBox = 0; + self::$boundingBoxes = []; + } + + public static function clearBoundingBoxPool(){ + self::$nextBoundingBox = 0; + } + + /** + * @param $minX + * @param $minY + * @param $minZ + * @param $maxX + * @param $maxY + * @param $maxZ + * + * @return AxisAlignedBB + */ + public static function getBoundingBoxFromPool($minX, $minY, $minZ, $maxX, $maxY, $maxZ){ + if(self::$nextBoundingBox >= count(self::$boundingBoxes)){ + self::$boundingBoxes[] = new AxisAlignedBB(0, 0, 0, 0, 0, 0); + } + + return self::$boundingBoxes[self::$nextBoundingBox++]->setBounds($minX, $minY, $minZ, $maxX, $maxY, $maxZ); + } + + /** + * @param AxisAlignedBB $bb + * + * @return AxisAlignedBB + */ + public static function cloneBoundingBoxFromPool(AxisAlignedBB $bb){ + if(self::$nextBoundingBox >= count(self::$boundingBoxes)){ + self::$boundingBoxes[] = new AxisAlignedBB(0, 0, 0, 0, 0, 0); + } + + return self::$boundingBoxes[self::$nextBoundingBox++]->setBounds($bb->minX, $bb->minY, $bb->minZ, $bb->maxX, $bb->maxY, $bb->maxZ); + } + public function setBounds($minX, $minY, $minZ, $maxX, $maxY, $maxZ){ $this->minX = $minX; $this->minY = $minY; @@ -55,7 +97,7 @@ class AxisAlignedBB{ } public function addCoord($x, $y, $z){ - $vec = clone $this; + $vec = self::cloneBoundingBoxFromPool($this); if($x < 0){ $vec->minX += $x; @@ -79,7 +121,7 @@ class AxisAlignedBB{ } public function grow($x, $y, $z){ - $vec = clone $this; + $vec = self::cloneBoundingBoxFromPool($this); $vec->minX -= $x; $vec->minY -= $y; $vec->minZ -= $z; @@ -113,7 +155,7 @@ class AxisAlignedBB{ } public function shrink($x, $y, $z){ - $vec = clone $this; + $vec = self::cloneBoundingBoxFromPool($this); $vec->minX += $x; $vec->minY += $y; $vec->minZ += $z; @@ -146,7 +188,7 @@ class AxisAlignedBB{ } public function getOffsetBoundingBox($x, $y, $z){ - $vec = clone $this; + $vec = self::cloneBoundingBoxFromPool($this); $vec->minX += $x; $vec->minY += $y; $vec->minZ += $z; diff --git a/src/pocketmine/math/Vector3.php b/src/pocketmine/math/Vector3.php index 8a8864d93..19bcba46b 100644 --- a/src/pocketmine/math/Vector3.php +++ b/src/pocketmine/math/Vector3.php @@ -27,6 +27,10 @@ namespace pocketmine\math; */ class Vector3{ + /** @var Vector3[] */ + private static $vectorList = []; + private static $nextVector = 0; + const SIDE_DOWN = 0; const SIDE_UP = 1; const SIDE_NORTH = 2; @@ -44,6 +48,38 @@ class Vector3{ $this->z = $z; } + public static function clearVectors(){ + self::$nextVector = 0; + self::$vectorList = []; + } + + public static function clearVectorList(){ + self::$nextVector = 0; + } + + /** + * @param $x + * @param $y + * @param $z + * + * @return Vector3 + */ + public static function createVector($x, $y, $z){ + if(self::$nextVector >= count(self::$vectorList)){ + self::$vectorList[] = new Vector3(0, 0, 0); + } + + return self::$vectorList[self::$nextVector++]->setComponents($x, $y, $z); + } + + public static function cloneVector(Vector3 $vector){ + if(self::$nextVector >= count(self::$vectorList)){ + self::$vectorList[] = new Vector3(0, 0, 0); + } + + return self::$vectorList[self::$nextVector++]->setComponents($vector->x, $vector->y, $vector->z); + } + public function getX(){ return $this->x; } @@ -97,9 +133,9 @@ class Vector3{ */ public function add($x, $y = 0, $z = 0){ if($x instanceof Vector3){ - return new Vector3($this->x + $x->x, $this->y + $x->y, $this->z + $x->z); + return self::createVector($this->x + $x->x, $this->y + $x->y, $this->z + $x->z); }else{ - return new Vector3($this->x + $x, $this->y + $y, $this->z + $z); + return self::createVector($this->x + $x, $this->y + $y, $this->z + $z); } } @@ -119,15 +155,15 @@ class Vector3{ } public function multiply($number){ - return new Vector3($this->x * $number, $this->y * $number, $this->z * $number); + return self::createVector($this->x * $number, $this->y * $number, $this->z * $number); } public function divide($number){ - return new Vector3($this->x / $number, $this->y / $number, $this->z / $number); + return self::createVector($this->x / $number, $this->y / $number, $this->z / $number); } public function ceil(){ - return new Vector3((int) ($this->x + 1), (int) ($this->y + 1), (int) ($this->z + 1)); + return self::createVector((int) ($this->x + 1), (int) ($this->y + 1), (int) ($this->z + 1)); } public function floor(){ @@ -148,17 +184,17 @@ class Vector3{ public function getSide($side, $step = 1){ switch((int) $side){ case self::SIDE_DOWN: - return new Vector3($this->x, $this->y - $step, $this->z); + return self::createVector($this->x, $this->y - $step, $this->z); case self::SIDE_UP: - return new Vector3($this->x, $this->y + $step, $this->z); + return self::createVector($this->x, $this->y + $step, $this->z); case self::SIDE_NORTH: - return new Vector3($this->x, $this->y, $this->z - $step); + return self::createVector($this->x, $this->y, $this->z - $step); case self::SIDE_SOUTH: - return new Vector3($this->x, $this->y, $this->z + $step); + return self::createVector($this->x, $this->y, $this->z + $step); case self::SIDE_WEST: - return new Vector3($this->x - $step, $this->y, $this->z); + return self::createVector($this->x - $step, $this->y, $this->z); case self::SIDE_EAST: - return new Vector3($this->x + $step, $this->y, $this->z); + return self::createVector($this->x + $step, $this->y, $this->z); default: return $this; } @@ -218,7 +254,7 @@ class Vector3{ return $this->divide($len); } - return new Vector3(0, 0, 0); + return self::createVector(0, 0, 0); } public function dot(Vector3 $v){ @@ -226,7 +262,7 @@ class Vector3{ } public function cross(Vector3 $v){ - return new Vector3( + return self::createVector( $this->y * $v->z - $this->z * $v->y, $this->z * $v->x - $this->x * $v->z, $this->x * $v->y - $this->y * $v->x @@ -256,7 +292,7 @@ class Vector3{ if($f < 0 or $f > 1){ return null; }else{ - return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f); + return self::createVector($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f); } } @@ -283,7 +319,7 @@ class Vector3{ if($f < 0 or $f > 1){ return null; }else{ - return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f); + return self::createVector($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f); } } @@ -310,10 +346,24 @@ class Vector3{ if($f < 0 or $f > 1){ return null; }else{ - return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f); + return self::createVector($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f); } } + /** + * @param $x + * @param $y + * @param $z + * + * @return Vector3 + */ + public function setComponents($x, $y, $z){ + $this->x = $x; + $this->y = $y; + $this->z = $z; + return $this; + } + public function __toString(){ return "Vector3(x=" . $this->x . ",y=" . $this->y . ",z=" . $this->z . ")"; } diff --git a/src/pocketmine/tile/Chest.php b/src/pocketmine/tile/Chest.php index 628228980..b0ff4f7d8 100644 --- a/src/pocketmine/tile/Chest.php +++ b/src/pocketmine/tile/Chest.php @@ -196,7 +196,7 @@ class Chest extends Spawnable implements InventoryHolder, Container{ */ public function getPair(){ if($this->isPaired()){ - $tile = $this->getLevel()->getTile(new Vector3((int) $this->namedtag["pairx"], $this->y, (int) $this->namedtag["pairz"])); + $tile = $this->getLevel()->getTile(Vector3::createVector((int) $this->namedtag["pairx"], $this->y, (int) $this->namedtag["pairz"])); if($tile instanceof Chest){ return $tile; } diff --git a/src/pocketmine/utils/BlockIterator.php b/src/pocketmine/utils/BlockIterator.php index 2e2197829..31eee943b 100644 --- a/src/pocketmine/utils/BlockIterator.php +++ b/src/pocketmine/utils/BlockIterator.php @@ -58,7 +58,7 @@ class BlockIterator implements \Iterator{ $this->level = $level; $this->maxDistance = (int) $maxDistance; - $startClone = clone $start; + $startClone = Vector3::createVector($start->x, $start->y, $start->z); $startClone->y += $yOffset; $this->currentDistance = 0; @@ -71,7 +71,7 @@ class BlockIterator implements \Iterator{ $secondPosition = 0; $thirdPosition = 0; - $startBlock = $this->level->getBlock($startClone->floor()); + $startBlock = $this->level->getBlock(Vector3::createVector($startClone->x, $startClone->y, $startClone->z)->floor()); if($this->getXLength($direction) > $mainDirection){ $this->mainFace = $this->getXFace($direction);