diff --git a/src/block/Bamboo.php b/src/block/Bamboo.php index f231d19b5..948755830 100644 --- a/src/block/Bamboo.php +++ b/src/block/Bamboo.php @@ -160,9 +160,10 @@ class Bamboo extends Transparent{ } public function onNearbyBlockChange() : void{ - $below = $this->position->getWorld()->getBlock($this->position->down()); + $world = $this->position->getWorld(); + $below = $world->getBlock($this->position->down()); if(!$this->canBeSupportedBy($below) && !$below->isSameType($this)){ - $this->position->getWorld()->useBreakOn($this->position); + $world->useBreakOn($this->position); } } @@ -208,7 +209,7 @@ class Bamboo extends Transparent{ } } - $tx = new BlockTransaction($this->position->getWorld()); + $tx = new BlockTransaction($world); foreach($newBlocks as $idx => $newBlock){ $tx->addBlock($this->position->subtract(0, $idx - $growAmount, 0), $newBlock); } diff --git a/src/block/BambooSapling.php b/src/block/BambooSapling.php index 216dc255a..f388d6e35 100644 --- a/src/block/BambooSapling.php +++ b/src/block/BambooSapling.php @@ -80,8 +80,9 @@ final class BambooSapling extends Flowable{ } public function onNearbyBlockChange() : void{ - if(!$this->canBeSupportedBy($this->position->getWorld()->getBlock($this->position->down()))){ - $this->position->getWorld()->useBreakOn($this->position); + $world = $this->position->getWorld(); + if(!$this->canBeSupportedBy($world->getBlock($this->position->down()))){ + $world->useBreakOn($this->position); } } diff --git a/src/block/BaseRail.php b/src/block/BaseRail.php index 933763122..971beffac 100644 --- a/src/block/BaseRail.php +++ b/src/block/BaseRail.php @@ -162,6 +162,7 @@ abstract class BaseRail extends Flowable{ $thisConnections = $this->getConnectedDirections(); $changed = false; + $world = $this->position->getWorld(); do{ $possible = $this->getPossibleConnectionDirections($thisConnections); $continue = false; @@ -189,7 +190,7 @@ abstract class BaseRail extends Flowable{ if(isset($otherPossible[$otherSide])){ $otherConnections[] = $otherSide; $other->setConnections($otherConnections); - $this->position->getWorld()->setBlock($other->position, $other); + $world->setBlock($other->position, $other); $changed = true; $thisConnections[] = $thisSide; @@ -202,7 +203,7 @@ abstract class BaseRail extends Flowable{ if($changed){ $this->setConnections($thisConnections); - $this->position->getWorld()->setBlock($this->position, $this); + $world->setBlock($this->position, $this); } } @@ -220,12 +221,13 @@ abstract class BaseRail extends Flowable{ } public function onNearbyBlockChange() : void{ + $world = $this->position->getWorld(); if(!$this->getSide(Facing::DOWN)->getSupportType(Facing::UP)->hasEdgeSupport()){ - $this->position->getWorld()->useBreakOn($this->position); + $world->useBreakOn($this->position); }else{ foreach($this->getCurrentShapeConnections() as $connection){ if(($connection & RailConnectionInfo::FLAG_ASCEND) !== 0 && !$this->getSide($connection & ~RailConnectionInfo::FLAG_ASCEND)->getSupportType(Facing::UP)->hasEdgeSupport()){ - $this->position->getWorld()->useBreakOn($this->position); + $world->useBreakOn($this->position); break; } } diff --git a/src/block/Bell.php b/src/block/Bell.php index 3cc131792..4c8d95146 100644 --- a/src/block/Bell.php +++ b/src/block/Bell.php @@ -153,10 +153,11 @@ final class Bell extends Transparent{ } public function ring(int $faceHit) : void{ - $this->position->getWorld()->addSound($this->position, new BellRingSound()); - $tile = $this->position->getWorld()->getTile($this->position); + $world = $this->position->getWorld(); + $world->addSound($this->position, new BellRingSound()); + $tile = $world->getTile($this->position); if($tile instanceof TileBell){ - $this->position->getWorld()->broadcastPacketToViewers($this->position, $tile->createFakeUpdatePacket($faceHit)); + $world->broadcastPacketToViewers($this->position, $tile->createFakeUpdatePacket($faceHit)); } } } diff --git a/src/block/Block.php b/src/block/Block.php index aab1a3bc8..ae2374c06 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -192,10 +192,11 @@ class Block{ } public function writeStateToWorld() : void{ - $this->position->getWorld()->getOrLoadChunkAtPosition($this->position)->setFullBlock($this->position->x & Chunk::COORD_MASK, $this->position->y, $this->position->z & Chunk::COORD_MASK, $this->getStateId()); + $world = $this->position->getWorld(); + $world->getOrLoadChunkAtPosition($this->position)->setFullBlock($this->position->x & Chunk::COORD_MASK, $this->position->y, $this->position->z & Chunk::COORD_MASK, $this->getStateId()); $tileType = $this->idInfo->getTileClass(); - $oldTile = $this->position->getWorld()->getTile($this->position); + $oldTile = $world->getTile($this->position); if($oldTile !== null){ if($tileType === null || !($oldTile instanceof $tileType)){ $oldTile->close(); @@ -209,8 +210,8 @@ class Block{ * @var Tile $tile * @see Tile::__construct() */ - $tile = new $tileType($this->position->getWorld(), $this->position->asVector3()); - $this->position->getWorld()->addTile($tile); + $tile = new $tileType($world, $this->position->asVector3()); + $world->addTile($tile); } } @@ -276,10 +277,11 @@ class Block{ * @param Item[] &$returnedItems Items to be added to the target's inventory (or dropped, if full) */ public function onBreak(Item $item, ?Player $player = null, array &$returnedItems = []) : bool{ - if(($t = $this->position->getWorld()->getTile($this->position)) !== null){ + $world = $this->position->getWorld(); + if(($t = $world->getTile($this->position)) !== null){ $t->onBlockDestroyed(); } - $this->position->getWorld()->setBlock($this->position, VanillaBlocks::AIR()); + $world->setBlock($this->position, VanillaBlocks::AIR()); return true; } diff --git a/src/block/BrewingStand.php b/src/block/BrewingStand.php index dabaa75e2..cdc34eef9 100644 --- a/src/block/BrewingStand.php +++ b/src/block/BrewingStand.php @@ -109,10 +109,11 @@ class BrewingStand extends Transparent{ } public function onScheduledUpdate() : void{ - $brewing = $this->position->getWorld()->getTile($this->position); + $world = $this->position->getWorld(); + $brewing = $world->getTile($this->position); if($brewing instanceof TileBrewingStand){ if($brewing->onUpdate()){ - $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, 1); + $world->scheduleDelayedBlockUpdate($this->position, 1); } $changed = false; @@ -125,7 +126,7 @@ class BrewingStand extends Transparent{ } if($changed){ - $this->position->getWorld()->setBlock($this->position, $this); + $world->setBlock($this->position, $this); } } } diff --git a/src/block/Button.php b/src/block/Button.php index 98fcf69a2..c9ec492d6 100644 --- a/src/block/Button.php +++ b/src/block/Button.php @@ -67,9 +67,10 @@ abstract class Button extends Flowable{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ if(!$this->pressed){ $this->pressed = true; - $this->position->getWorld()->setBlock($this->position, $this); - $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, $this->getActivationTime()); - $this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new RedstonePowerOnSound()); + $world = $this->position->getWorld(); + $world->setBlock($this->position, $this); + $world->scheduleDelayedBlockUpdate($this->position, $this->getActivationTime()); + $world->addSound($this->position->add(0.5, 0.5, 0.5), new RedstonePowerOnSound()); } return true; @@ -78,8 +79,9 @@ abstract class Button extends Flowable{ public function onScheduledUpdate() : void{ if($this->pressed){ $this->pressed = false; - $this->position->getWorld()->setBlock($this->position, $this); - $this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new RedstonePowerOffSound()); + $world = $this->position->getWorld(); + $world->setBlock($this->position, $this); + $world->addSound($this->position->add(0.5, 0.5, 0.5), new RedstonePowerOffSound()); } } diff --git a/src/block/Cactus.php b/src/block/Cactus.php index 40087f53e..8d1351174 100644 --- a/src/block/Cactus.php +++ b/src/block/Cactus.php @@ -83,13 +83,14 @@ class Cactus extends Transparent{ public function onNearbyBlockChange() : void{ $down = $this->getSide(Facing::DOWN); + $world = $this->position->getWorld(); if($down->getTypeId() !== BlockTypeIds::SAND && $down->getTypeId() !== BlockTypeIds::RED_SAND && !$down->isSameType($this)){ - $this->position->getWorld()->useBreakOn($this->position); + $world->useBreakOn($this->position); }else{ foreach(Facing::HORIZONTAL as $side){ $b = $this->getSide($side); if($b->isSolid()){ - $this->position->getWorld()->useBreakOn($this->position); + $world->useBreakOn($this->position); break; } } @@ -102,28 +103,29 @@ class Cactus extends Transparent{ public function onRandomTick() : void{ if(!$this->getSide(Facing::DOWN)->isSameType($this)){ + $world = $this->position->getWorld(); if($this->age === self::MAX_AGE){ for($y = 1; $y < 3; ++$y){ - if(!$this->position->getWorld()->isInWorld($this->position->x, $this->position->y + $y, $this->position->z)){ + if(!$world->isInWorld($this->position->x, $this->position->y + $y, $this->position->z)){ break; } - $b = $this->position->getWorld()->getBlockAt($this->position->x, $this->position->y + $y, $this->position->z); + $b = $world->getBlockAt($this->position->x, $this->position->y + $y, $this->position->z); if($b->getTypeId() === BlockTypeIds::AIR){ $ev = new BlockGrowEvent($b, VanillaBlocks::CACTUS()); $ev->call(); if($ev->isCancelled()){ break; } - $this->position->getWorld()->setBlock($b->position, $ev->getNewState()); + $world->setBlock($b->position, $ev->getNewState()); }else{ break; } } $this->age = 0; - $this->position->getWorld()->setBlock($this->position, $this); + $world->setBlock($this->position, $this); }else{ ++$this->age; - $this->position->getWorld()->setBlock($this->position, $this); + $world->setBlock($this->position, $this); } } } diff --git a/src/block/Chest.php b/src/block/Chest.php index 0019e16d5..bb10e91df 100644 --- a/src/block/Chest.php +++ b/src/block/Chest.php @@ -51,13 +51,13 @@ class Chest extends Transparent{ } public function onPostPlace() : void{ - $tile = $this->position->getWorld()->getTile($this->position); + $world = $this->position->getWorld(); + $tile = $world->getTile($this->position); if($tile instanceof TileChest){ foreach([false, true] as $clockwise){ $side = Facing::rotateY($this->facing, $clockwise); $c = $this->getSide($side); if($c instanceof Chest && $c->isSameType($this) && $c->facing === $this->facing){ - $world = $this->position->getWorld(); $pair = $world->getTile($c->position); if($pair instanceof TileChest && !$pair->isPaired()){ [$left, $right] = $clockwise ? [$c, $this] : [$this, $c]; diff --git a/src/block/DaylightSensor.php b/src/block/DaylightSensor.php index d266817d1..2fdcfdef4 100644 --- a/src/block/DaylightSensor.php +++ b/src/block/DaylightSensor.php @@ -84,21 +84,23 @@ class DaylightSensor extends Transparent{ } public function onScheduledUpdate() : void{ + $world = $this->position->getWorld(); $signalStrength = $this->recalculateSignalStrength(); if($this->signalStrength !== $signalStrength){ $this->signalStrength = $signalStrength; - $this->position->getWorld()->setBlock($this->position, $this); + $world->setBlock($this->position, $this); } - $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, 20); + $world->scheduleDelayedBlockUpdate($this->position, 20); } private function recalculateSignalStrength() : int{ - $lightLevel = $this->position->getWorld()->getRealBlockSkyLightAt($this->position->x, $this->position->y, $this->position->z); + $world = $this->position->getWorld(); + $lightLevel = $world->getRealBlockSkyLightAt($this->position->x, $this->position->y, $this->position->z); if($this->inverted){ return 15 - $lightLevel; } - $sunAngle = $this->position->getWorld()->getSunAnglePercentage(); + $sunAngle = $world->getSunAnglePercentage(); return max(0, (int) round($lightLevel * cos(($sunAngle + ((($sunAngle < 0.5 ? 0 : 1) - $sunAngle) / 5)) * 2 * M_PI))); } diff --git a/src/block/Dirt.php b/src/block/Dirt.php index c1e4e26c1..4a2d90d8a 100644 --- a/src/block/Dirt.php +++ b/src/block/Dirt.php @@ -54,8 +54,9 @@ class Dirt extends Opaque{ $item->applyDamage(1); $newBlock = $this->coarse ? VanillaBlocks::DIRT() : VanillaBlocks::FARMLAND(); - $this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new ItemUseOnBlockSound($newBlock)); - $this->position->getWorld()->setBlock($this->position, $newBlock); + $world = $this->position->getWorld(); + $world->addSound($this->position->add(0.5, 0.5, 0.5), new ItemUseOnBlockSound($newBlock)); + $world->setBlock($this->position, $newBlock); return true; } diff --git a/src/block/Door.php b/src/block/Door.php index 366670a41..263d854aa 100644 --- a/src/block/Door.php +++ b/src/block/Door.php @@ -147,13 +147,14 @@ class Door extends Transparent{ $this->open = !$this->open; $other = $this->getSide($this->top ? Facing::DOWN : Facing::UP); + $world = $this->position->getWorld(); if($other instanceof Door && $other->isSameType($this)){ $other->open = $this->open; - $this->position->getWorld()->setBlock($other->position, $other); + $world->setBlock($other->position, $other); } - $this->position->getWorld()->setBlock($this->position, $this); - $this->position->getWorld()->addSound($this->position, new DoorSound()); + $world->setBlock($this->position, $this); + $world->addSound($this->position, new DoorSound()); return true; } diff --git a/src/block/DragonEgg.php b/src/block/DragonEgg.php index 6b330246b..27a30a1c5 100644 --- a/src/block/DragonEgg.php +++ b/src/block/DragonEgg.php @@ -62,8 +62,9 @@ class DragonEgg extends Transparent implements Fallable{ } public function teleport() : void{ + $world = $this->position->getWorld(); for($tries = 0; $tries < 16; ++$tries){ - $block = $this->position->getWorld()->getBlockAt( + $block = $world->getBlockAt( $this->position->x + mt_rand(-16, 16), max(World::Y_MIN, min(World::Y_MAX - 1, $this->position->y + mt_rand(-8, 8))), $this->position->z + mt_rand(-16, 16) @@ -76,9 +77,9 @@ class DragonEgg extends Transparent implements Fallable{ } $blockPos = $ev->getTo(); - $this->position->getWorld()->addParticle($this->position, new DragonEggTeleportParticle($this->position->x - $blockPos->x, $this->position->y - $blockPos->y, $this->position->z - $blockPos->z)); - $this->position->getWorld()->setBlock($this->position, VanillaBlocks::AIR()); - $this->position->getWorld()->setBlock($blockPos, $this); + $world->addParticle($this->position, new DragonEggTeleportParticle($this->position->x - $blockPos->x, $this->position->y - $blockPos->y, $this->position->z - $blockPos->z)); + $world->setBlock($this->position, VanillaBlocks::AIR()); + $world->setBlock($blockPos, $this); break; } } diff --git a/src/block/Farmland.php b/src/block/Farmland.php index 037f31bbb..2e80c5d2a 100644 --- a/src/block/Farmland.php +++ b/src/block/Farmland.php @@ -73,16 +73,17 @@ class Farmland extends Transparent{ } public function onRandomTick() : void{ + $world = $this->position->getWorld(); if(!$this->canHydrate()){ if($this->wetness > 0){ $this->wetness--; - $this->position->getWorld()->setBlock($this->position, $this, false); + $world->setBlock($this->position, $this, false); }else{ - $this->position->getWorld()->setBlock($this->position, VanillaBlocks::DIRT()); + $world->setBlock($this->position, VanillaBlocks::DIRT()); } }elseif($this->wetness < self::MAX_WETNESS){ $this->wetness = self::MAX_WETNESS; - $this->position->getWorld()->setBlock($this->position, $this, false); + $world->setBlock($this->position, $this, false); } } diff --git a/src/block/FenceGate.php b/src/block/FenceGate.php index d18f1dcc0..f439819d3 100644 --- a/src/block/FenceGate.php +++ b/src/block/FenceGate.php @@ -112,8 +112,9 @@ class FenceGate extends Transparent{ } } - $this->position->getWorld()->setBlock($this->position, $this); - $this->position->getWorld()->addSound($this->position, new DoorSound()); + $world = $this->position->getWorld(); + $world->setBlock($this->position, $this); + $world->addSound($this->position, new DoorSound()); return true; } diff --git a/src/block/Fire.php b/src/block/Fire.php index 30ecb6138..df3f8c79f 100644 --- a/src/block/Fire.php +++ b/src/block/Fire.php @@ -62,13 +62,14 @@ class Fire extends BaseFire{ } public function onNearbyBlockChange() : void{ + $world = $this->position->getWorld(); $down = $this->getSide(Facing::DOWN); if(SoulFire::canBeSupportedBy($down)){ - $this->position->getWorld()->setBlock($this->position, VanillaBlocks::SOUL_FIRE()); + $world->setBlock($this->position, VanillaBlocks::SOUL_FIRE()); }elseif($down->isTransparent() && !$this->hasAdjacentFlammableBlocks()){ - $this->position->getWorld()->setBlock($this->position, VanillaBlocks::AIR()); + $world->setBlock($this->position, VanillaBlocks::AIR()); }else{ - $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, mt_rand(30, 40)); + $world->scheduleDelayedBlockUpdate($this->position, mt_rand(30, 40)); } } @@ -101,11 +102,12 @@ class Fire extends BaseFire{ } } + $world = $this->position->getWorld(); if($result !== null){ - $this->position->getWorld()->setBlock($this->position, $result); + $world->setBlock($this->position, $result); } - $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, mt_rand(30, 40)); + $world->scheduleDelayedBlockUpdate($this->position, mt_rand(30, 40)); if($canSpread){ $this->burnBlocksAround(); @@ -146,7 +148,8 @@ class Fire extends BaseFire{ if(!$ev->isCancelled()){ $block->onIncinerate(); - if($this->position->getWorld()->getBlock($block->getPosition())->isSameState($block)){ + $world = $this->position->getWorld(); + if($world->getBlock($block->getPosition())->isSameState($block)){ $spreadedFire = false; if(mt_rand(0, $this->age + 9) < 5){ //TODO: check rain $fire = clone $this; @@ -154,7 +157,7 @@ class Fire extends BaseFire{ $spreadedFire = $this->spreadBlock($block, $fire); } if(!$spreadedFire){ - $this->position->getWorld()->setBlock($block->position, VanillaBlocks::AIR()); + $world->setBlock($block->position, VanillaBlocks::AIR()); } } } diff --git a/src/block/FlowerPot.php b/src/block/FlowerPot.php index f2cafe15e..757c51850 100644 --- a/src/block/FlowerPot.php +++ b/src/block/FlowerPot.php @@ -115,6 +115,7 @@ class FlowerPot extends Flowable{ } public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ + $world = $this->position->getWorld(); $plant = $item->getBlock(); if($this->plant !== null){ if($this->isValidPlant($plant)){ @@ -130,16 +131,16 @@ class FlowerPot extends Flowable{ $removedItems = $player->getInventory()->addItem(...$removedItems); } foreach($removedItems as $drops){ - $this->position->getWorld()->dropItem($this->position->add(0.5, 0.5, 0.5), $drops); + $world->dropItem($this->position->add(0.5, 0.5, 0.5), $drops); } $this->setPlant(null); - $this->position->getWorld()->setBlock($this->position, $this); + $world->setBlock($this->position, $this); return true; }elseif($this->isValidPlant($plant)){ $this->setPlant($plant); $item->pop(); - $this->position->getWorld()->setBlock($this->position, $this); + $world->setBlock($this->position, $this); return true; } diff --git a/src/block/FrostedIce.php b/src/block/FrostedIce.php index 0cce7ca98..081e5ad71 100644 --- a/src/block/FrostedIce.php +++ b/src/block/FrostedIce.php @@ -51,16 +51,18 @@ class FrostedIce extends Ice{ } public function onNearbyBlockChange() : void{ + $world = $this->position->getWorld(); if(!$this->checkAdjacentBlocks(2)){ - $this->position->getWorld()->useBreakOn($this->position); + $world->useBreakOn($this->position); }else{ - $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, mt_rand(20, 40)); + $world->scheduleDelayedBlockUpdate($this->position, mt_rand(20, 40)); } } public function onRandomTick() : void{ + $world = $this->position->getWorld(); if((!$this->checkAdjacentBlocks(4) || mt_rand(0, 2) === 0) && - $this->position->getWorld()->getHighestAdjacentFullLightAt($this->position->x, $this->position->y, $this->position->z) >= 12 - $this->age){ + $world->getHighestAdjacentFullLightAt($this->position->x, $this->position->y, $this->position->z) >= 12 - $this->age){ if($this->tryMelt()){ foreach($this->getAllSides() as $block){ if($block instanceof FrostedIce){ @@ -69,7 +71,7 @@ class FrostedIce extends Ice{ } } }else{ - $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, mt_rand(20, 40)); + $world->scheduleDelayedBlockUpdate($this->position, mt_rand(20, 40)); } } @@ -101,18 +103,19 @@ class FrostedIce extends Ice{ * @return bool Whether the ice was destroyed. */ private function tryMelt() : bool{ + $world = $this->position->getWorld(); if($this->age >= self::MAX_AGE){ $ev = new BlockMeltEvent($this, VanillaBlocks::WATER()); $ev->call(); if(!$ev->isCancelled()){ - $this->position->getWorld()->setBlock($this->position, $ev->getNewState()); + $world->setBlock($this->position, $ev->getNewState()); } return true; } $this->age++; - $this->position->getWorld()->setBlock($this->position, $this); - $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, mt_rand(20, 40)); + $world->setBlock($this->position, $this); + $world->scheduleDelayedBlockUpdate($this->position, mt_rand(20, 40)); return false; } } diff --git a/src/block/Furnace.php b/src/block/Furnace.php index 3ec395883..8b86f3af6 100644 --- a/src/block/Furnace.php +++ b/src/block/Furnace.php @@ -74,12 +74,13 @@ class Furnace extends Opaque{ } public function onScheduledUpdate() : void{ - $furnace = $this->position->getWorld()->getTile($this->position); + $world = $this->position->getWorld(); + $furnace = $world->getTile($this->position); if($furnace instanceof TileFurnace && $furnace->onUpdate()){ if(mt_rand(1, 60) === 1){ //in vanilla this is between 1 and 5 seconds; try to average about 3 - $this->position->getWorld()->addSound($this->position, $furnace->getFurnaceType()->getCookSound()); + $world->addSound($this->position, $furnace->getFurnaceType()->getCookSound()); } - $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, 1); //TODO: check this + $world->scheduleDelayedBlockUpdate($this->position, 1); //TODO: check this } } } diff --git a/src/block/Grass.php b/src/block/Grass.php index 342565641..14353ac7c 100644 --- a/src/block/Grass.php +++ b/src/block/Grass.php @@ -53,13 +53,14 @@ class Grass extends Opaque{ } public function onRandomTick() : void{ - $lightAbove = $this->position->getWorld()->getFullLightAt($this->position->x, $this->position->y + 1, $this->position->z); - if($lightAbove < 4 && $this->position->getWorld()->getBlockAt($this->position->x, $this->position->y + 1, $this->position->z)->getLightFilter() >= 2){ + $world = $this->position->getWorld(); + $lightAbove = $world->getFullLightAt($this->position->x, $this->position->y + 1, $this->position->z); + if($lightAbove < 4 && $world->getBlockAt($this->position->x, $this->position->y + 1, $this->position->z)->getLightFilter() >= 2){ //grass dies $ev = new BlockSpreadEvent($this, $this, VanillaBlocks::DIRT()); $ev->call(); if(!$ev->isCancelled()){ - $this->position->getWorld()->setBlock($this->position, $ev->getNewState(), false); + $world->setBlock($this->position, $ev->getNewState(), false); } }elseif($lightAbove >= 9){ //try grass spread @@ -68,12 +69,12 @@ class Grass extends Opaque{ $y = mt_rand($this->position->y - 3, $this->position->y + 1); $z = mt_rand($this->position->z - 1, $this->position->z + 1); - $b = $this->position->getWorld()->getBlockAt($x, $y, $z); + $b = $world->getBlockAt($x, $y, $z); if( !($b instanceof Dirt) || $b->isCoarse() || - $this->position->getWorld()->getFullLightAt($x, $y + 1, $z) < 4 || - $this->position->getWorld()->getBlockAt($x, $y + 1, $z)->getLightFilter() >= 2 + $world->getFullLightAt($x, $y + 1, $z) < 4 || + $world->getBlockAt($x, $y + 1, $z)->getLightFilter() >= 2 ){ continue; } @@ -81,7 +82,7 @@ class Grass extends Opaque{ $ev = new BlockSpreadEvent($b, $this, VanillaBlocks::GRASS()); $ev->call(); if(!$ev->isCancelled()){ - $this->position->getWorld()->setBlock($b->position, $ev->getNewState(), false); + $world->setBlock($b->position, $ev->getNewState(), false); } } } @@ -91,23 +92,24 @@ class Grass extends Opaque{ if($face !== Facing::UP){ return false; } + $world = $this->position->getWorld(); if($item instanceof Fertilizer){ $item->pop(); - TallGrassObject::growGrass($this->position->getWorld(), $this->position, new Random(mt_rand()), 8, 2); + TallGrassObject::growGrass($world, $this->position, new Random(mt_rand()), 8, 2); return true; }elseif($item instanceof Hoe){ $item->applyDamage(1); $newBlock = VanillaBlocks::FARMLAND(); - $this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new ItemUseOnBlockSound($newBlock)); - $this->position->getWorld()->setBlock($this->position, $newBlock); + $world->addSound($this->position->add(0.5, 0.5, 0.5), new ItemUseOnBlockSound($newBlock)); + $world->setBlock($this->position, $newBlock); return true; }elseif($item instanceof Shovel && $this->getSide(Facing::UP)->getTypeId() === BlockTypeIds::AIR){ $item->applyDamage(1); $newBlock = VanillaBlocks::GRASS_PATH(); - $this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new ItemUseOnBlockSound($newBlock)); - $this->position->getWorld()->setBlock($this->position, $newBlock); + $world->addSound($this->position->add(0.5, 0.5, 0.5), new ItemUseOnBlockSound($newBlock)); + $world->setBlock($this->position, $newBlock); return true; } diff --git a/src/block/Ice.php b/src/block/Ice.php index ad83bb6ba..41f60e3f9 100644 --- a/src/block/Ice.php +++ b/src/block/Ice.php @@ -51,11 +51,12 @@ class Ice extends Transparent{ } public function onRandomTick() : void{ - if($this->position->getWorld()->getHighestAdjacentBlockLight($this->position->x, $this->position->y, $this->position->z) >= 12){ + $world = $this->position->getWorld(); + if($world->getHighestAdjacentBlockLight($this->position->x, $this->position->y, $this->position->z) >= 12){ $ev = new BlockMeltEvent($this, VanillaBlocks::WATER()); $ev->call(); if(!$ev->isCancelled()){ - $this->position->getWorld()->setBlock($this->position, $ev->getNewState()); + $world->setBlock($this->position, $ev->getNewState()); } } } diff --git a/src/block/ItemFrame.php b/src/block/ItemFrame.php index 8443972ec..c30cffd55 100644 --- a/src/block/ItemFrame.php +++ b/src/block/ItemFrame.php @@ -164,11 +164,12 @@ class ItemFrame extends Flowable{ if($this->framedItem === null){ return false; } + $world = $this->position->getWorld(); if(lcg_value() <= $this->itemDropChance){ - $this->position->getWorld()->dropItem($this->position->add(0.5, 0.5, 0.5), clone $this->framedItem); + $world->dropItem($this->position->add(0.5, 0.5, 0.5), clone $this->framedItem); } $this->setFramedItem(null); - $this->position->getWorld()->setBlock($this->position, $this); + $world->setBlock($this->position, $this); return true; } diff --git a/src/block/Leaves.php b/src/block/Leaves.php index c63939cd8..bf2dc18eb 100644 --- a/src/block/Leaves.php +++ b/src/block/Leaves.php @@ -117,11 +117,12 @@ class Leaves extends Transparent{ if(!$this->noDecay && $this->checkDecay){ $ev = new LeavesDecayEvent($this); $ev->call(); + $world = $this->position->getWorld(); if($ev->isCancelled() || $this->findLog($this->position)){ $this->checkDecay = false; - $this->position->getWorld()->setBlock($this->position, $this, false); + $world->setBlock($this->position, $this, false); }else{ - $this->position->getWorld()->useBreakOn($this->position); + $world->useBreakOn($this->position); } } } diff --git a/src/block/Lectern.php b/src/block/Lectern.php index 66c857110..c77afd8e1 100644 --- a/src/block/Lectern.php +++ b/src/block/Lectern.php @@ -126,8 +126,9 @@ class Lectern extends Transparent{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ if($this->book === null && $item instanceof WritableBookBase){ - $this->position->getWorld()->setBlock($this->position, $this->setBook($item)); - $this->position->getWorld()->addSound($this->position, new LecternPlaceBookSound()); + $world = $this->position->getWorld(); + $world->setBlock($this->position, $this->setBook($item)); + $world->addSound($this->position, new LecternPlaceBookSound()); $item->pop(); } return true; @@ -135,8 +136,9 @@ class Lectern extends Transparent{ public function onAttack(Item $item, int $face, ?Player $player = null) : bool{ if($this->book !== null){ - $this->position->getWorld()->dropItem($this->position->up(), $this->book); - $this->position->getWorld()->setBlock($this->position, $this->setBook(null)); + $world = $this->position->getWorld(); + $world->dropItem($this->position->up(), $this->book); + $world->setBlock($this->position, $this->setBook(null)); } return false; } @@ -150,12 +152,13 @@ class Lectern extends Transparent{ } $this->viewedPage = $newPage; + $world = $this->position->getWorld(); if(!$this->producingSignal){ $this->producingSignal = true; - $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, 1); + $world->scheduleDelayedBlockUpdate($this->position, 1); } - $this->position->getWorld()->setBlock($this->position, $this); + $world->setBlock($this->position, $this); return true; } diff --git a/src/block/Lever.php b/src/block/Lever.php index d8cb30658..45f5d2fb2 100644 --- a/src/block/Lever.php +++ b/src/block/Lever.php @@ -101,8 +101,9 @@ class Lever extends Flowable{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ $this->activated = !$this->activated; - $this->position->getWorld()->setBlock($this->position, $this); - $this->position->getWorld()->addSound( + $world = $this->position->getWorld(); + $world->setBlock($this->position, $this); + $world->addSound( $this->position->add(0.5, 0.5, 0.5), $this->activated ? new RedstonePowerOnSound() : new RedstonePowerOffSound() ); diff --git a/src/block/Liquid.php b/src/block/Liquid.php index 914b88404..b14457f25 100644 --- a/src/block/Liquid.php +++ b/src/block/Liquid.php @@ -315,7 +315,7 @@ abstract class Liquid extends Transparent{ } if($adjacentDecay <= self::MAX_DECAY){ - $calculator = new MinimumCostFlowCalculator($this->position->getWorld(), $this->getFlowDecayPerBlock(), \Closure::fromCallable([$this, 'canFlowInto'])); + $calculator = new MinimumCostFlowCalculator($world, $this->getFlowDecayPerBlock(), \Closure::fromCallable([$this, 'canFlowInto'])); foreach($calculator->getOptimalFlowDirections($this->position->getFloorX(), $this->position->getFloorY(), $this->position->getFloorZ()) as $facing){ $this->flowIntoBlock($world->getBlock($this->position->getSide($facing)), $adjacentDecay, false); } @@ -334,11 +334,12 @@ abstract class Liquid extends Transparent{ $ev = new BlockSpreadEvent($block, $this, $new); $ev->call(); if(!$ev->isCancelled()){ + $world = $this->position->getWorld(); if($block->getTypeId() !== BlockTypeIds::AIR){ - $this->position->getWorld()->useBreakOn($block->position); + $world->useBreakOn($block->position); } - $this->position->getWorld()->setBlock($block->position, $ev->getNewState()); + $world->setBlock($block->position, $ev->getNewState()); } } } @@ -368,8 +369,9 @@ abstract class Liquid extends Transparent{ $ev = new BlockFormEvent($this, $result); $ev->call(); if(!$ev->isCancelled()){ - $this->position->getWorld()->setBlock($this->position, $ev->getNewState()); - $this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new FizzSound(2.6 + (lcg_value() - lcg_value()) * 0.8)); + $world = $this->position->getWorld(); + $world->setBlock($this->position, $ev->getNewState()); + $world->addSound($this->position->add(0.5, 0.5, 0.5), new FizzSound(2.6 + (lcg_value() - lcg_value()) * 0.8)); } return true; } diff --git a/src/block/Mycelium.php b/src/block/Mycelium.php index 11d00c5d8..f7e989c45 100644 --- a/src/block/Mycelium.php +++ b/src/block/Mycelium.php @@ -49,13 +49,14 @@ class Mycelium extends Opaque{ $x = mt_rand($this->position->x - 1, $this->position->x + 1); $y = mt_rand($this->position->y - 2, $this->position->y + 2); $z = mt_rand($this->position->z - 1, $this->position->z + 1); - $block = $this->position->getWorld()->getBlockAt($x, $y, $z); + $world = $this->position->getWorld(); + $block = $world->getBlockAt($x, $y, $z); if($block instanceof Dirt && !$block->isCoarse()){ if($block->getSide(Facing::UP) instanceof Transparent){ $ev = new BlockSpreadEvent($block, $this, VanillaBlocks::MYCELIUM()); $ev->call(); if(!$ev->isCancelled()){ - $this->position->getWorld()->setBlock($block->position, $ev->getNewState()); + $world->setBlock($block->position, $ev->getNewState()); } } } diff --git a/src/block/Pumpkin.php b/src/block/Pumpkin.php index 19b3b2e56..1b7f6a9cd 100644 --- a/src/block/Pumpkin.php +++ b/src/block/Pumpkin.php @@ -36,8 +36,9 @@ class Pumpkin extends Opaque{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ if($item instanceof Shears && in_array($face, Facing::HORIZONTAL, true)){ $item->applyDamage(1); - $this->position->getWorld()->setBlock($this->position, VanillaBlocks::CARVED_PUMPKIN()->setFacing($face)); - $this->position->getWorld()->dropItem($this->position->add(0.5, 0.5, 0.5), VanillaItems::PUMPKIN_SEEDS()->setCount(1)); + $world = $this->position->getWorld(); + $world->setBlock($this->position, VanillaBlocks::CARVED_PUMPKIN()->setFacing($face)); + $world->dropItem($this->position->add(0.5, 0.5, 0.5), VanillaItems::PUMPKIN_SEEDS()->setCount(1)); return true; } return false; diff --git a/src/block/Sapling.php b/src/block/Sapling.php index c7117d4b2..629e1f81e 100644 --- a/src/block/Sapling.php +++ b/src/block/Sapling.php @@ -91,12 +91,13 @@ class Sapling extends Flowable{ } public function onRandomTick() : void{ - if($this->position->getWorld()->getFullLightAt($this->position->getFloorX(), $this->position->getFloorY(), $this->position->getFloorZ()) >= 8 && mt_rand(1, 7) === 1){ + $world = $this->position->getWorld(); + if($world->getFullLightAt($this->position->getFloorX(), $this->position->getFloorY(), $this->position->getFloorZ()) >= 8 && mt_rand(1, 7) === 1){ if($this->ready){ $this->grow(null); }else{ $this->ready = true; - $this->position->getWorld()->setBlock($this->position, $this); + $world->setBlock($this->position, $this); } } } diff --git a/src/block/SnowLayer.php b/src/block/SnowLayer.php index 65c08333e..84620ebca 100644 --- a/src/block/SnowLayer.php +++ b/src/block/SnowLayer.php @@ -106,11 +106,12 @@ class SnowLayer extends Flowable implements Fallable{ } public function onRandomTick() : void{ - if($this->position->getWorld()->getBlockLightAt($this->position->x, $this->position->y, $this->position->z) >= 12){ + $world = $this->position->getWorld(); + if($world->getBlockLightAt($this->position->x, $this->position->y, $this->position->z) >= 12){ $ev = new BlockMeltEvent($this, VanillaBlocks::AIR()); $ev->call(); if(!$ev->isCancelled()){ - $this->position->getWorld()->setBlock($this->position, $ev->getNewState()); + $world->setBlock($this->position, $ev->getNewState()); } } } diff --git a/src/block/Stem.php b/src/block/Stem.php index 0220659b6..175731c9e 100644 --- a/src/block/Stem.php +++ b/src/block/Stem.php @@ -35,13 +35,14 @@ abstract class Stem extends Crops{ public function onRandomTick() : void{ if(mt_rand(0, 2) === 1){ + $world = $this->position->getWorld(); if($this->age < self::MAX_AGE){ $block = clone $this; ++$block->age; $ev = new BlockGrowEvent($this, $block); $ev->call(); if(!$ev->isCancelled()){ - $this->position->getWorld()->setBlock($this->position, $ev->getNewState()); + $world->setBlock($this->position, $ev->getNewState()); } }else{ $grow = $this->getPlant(); @@ -57,7 +58,7 @@ abstract class Stem extends Crops{ $ev = new BlockGrowEvent($side, $grow); $ev->call(); if(!$ev->isCancelled()){ - $this->position->getWorld()->setBlock($side->position, $ev->getNewState()); + $world->setBlock($side->position, $ev->getNewState()); } } } diff --git a/src/block/Sugarcane.php b/src/block/Sugarcane.php index 2697a14f9..35c99bcab 100644 --- a/src/block/Sugarcane.php +++ b/src/block/Sugarcane.php @@ -46,25 +46,26 @@ class Sugarcane extends Flowable{ private function grow() : bool{ $grew = false; + $world = $this->position->getWorld(); for($y = 1; $y < 3; ++$y){ - if(!$this->position->getWorld()->isInWorld($this->position->x, $this->position->y + $y, $this->position->z)){ + if(!$world->isInWorld($this->position->x, $this->position->y + $y, $this->position->z)){ break; } - $b = $this->position->getWorld()->getBlockAt($this->position->x, $this->position->y + $y, $this->position->z); + $b = $world->getBlockAt($this->position->x, $this->position->y + $y, $this->position->z); if($b->getTypeId() === BlockTypeIds::AIR){ $ev = new BlockGrowEvent($b, VanillaBlocks::SUGARCANE()); $ev->call(); if($ev->isCancelled()){ break; } - $this->position->getWorld()->setBlock($b->position, $ev->getNewState()); + $world->setBlock($b->position, $ev->getNewState()); $grew = true; }else{ break; } } $this->age = 0; - $this->position->getWorld()->setBlock($this->position, $this); + $world->setBlock($this->position, $this); return $grew; } diff --git a/src/block/SweetBerryBush.php b/src/block/SweetBerryBush.php index ac040976a..2c75758e1 100644 --- a/src/block/SweetBerryBush.php +++ b/src/block/SweetBerryBush.php @@ -85,6 +85,7 @@ class SweetBerryBush extends Flowable{ } public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ + $world = $this->position->getWorld(); if($this->age < self::STAGE_MATURE && $item instanceof Fertilizer){ $block = clone $this; $block->age++; @@ -93,13 +94,13 @@ class SweetBerryBush extends Flowable{ $ev->call(); if(!$ev->isCancelled()){ - $this->position->getWorld()->setBlock($this->position, $ev->getNewState()); + $world->setBlock($this->position, $ev->getNewState()); $item->pop(); } }elseif(($dropAmount = $this->getBerryDropAmount()) > 0){ - $this->position->getWorld()->setBlock($this->position, $this->setAge(self::STAGE_BUSH_NO_BERRIES)); - $this->position->getWorld()->dropItem($this->position, $this->asItem()->setCount($dropAmount)); + $world->setBlock($this->position, $this->setAge(self::STAGE_BUSH_NO_BERRIES)); + $world->dropItem($this->position, $this->asItem()->setCount($dropAmount)); } return true; diff --git a/src/block/TNT.php b/src/block/TNT.php index 1b435b011..dca4920d3 100644 --- a/src/block/TNT.php +++ b/src/block/TNT.php @@ -94,11 +94,12 @@ class TNT extends Opaque{ } public function ignite(int $fuse = 80) : void{ - $this->position->getWorld()->setBlock($this->position, VanillaBlocks::AIR()); + $world = $this->position->getWorld(); + $world->setBlock($this->position, VanillaBlocks::AIR()); $mot = (new Random())->nextSignedFloat() * M_PI * 2; - $tnt = new PrimedTNT(Location::fromObject($this->position->add(0.5, 0, 0.5), $this->position->getWorld())); + $tnt = new PrimedTNT(Location::fromObject($this->position->add(0.5, 0, 0.5), $world)); $tnt->setFuse($fuse); $tnt->setWorksUnderwater($this->worksUnderwater); $tnt->setMotion(new Vector3(-sin($mot) * 0.02, 0.2, -cos($mot) * 0.02)); diff --git a/src/block/Trapdoor.php b/src/block/Trapdoor.php index 57b9f4f65..8f6d5aab1 100644 --- a/src/block/Trapdoor.php +++ b/src/block/Trapdoor.php @@ -89,8 +89,9 @@ class Trapdoor extends Transparent{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ $this->open = !$this->open; - $this->position->getWorld()->setBlock($this->position, $this); - $this->position->getWorld()->addSound($this->position, new DoorSound()); + $world = $this->position->getWorld(); + $world->setBlock($this->position, $this); + $world->addSound($this->position, new DoorSound()); return true; } } diff --git a/src/block/Vine.php b/src/block/Vine.php index 40e2cd5e8..6fa3ee8f1 100644 --- a/src/block/Vine.php +++ b/src/block/Vine.php @@ -130,10 +130,11 @@ class Vine extends Flowable{ } if($changed){ + $world = $this->position->getWorld(); if(count($this->faces) === 0){ - $this->position->getWorld()->useBreakOn($this->position); + $world->useBreakOn($this->position); }else{ - $this->position->getWorld()->setBlock($this->position, $this); + $world->setBlock($this->position, $this); } } } diff --git a/src/block/inventory/BarrelInventory.php b/src/block/inventory/BarrelInventory.php index 7de83bb03..0d17d2a3e 100644 --- a/src/block/inventory/BarrelInventory.php +++ b/src/block/inventory/BarrelInventory.php @@ -48,9 +48,10 @@ class BarrelInventory extends SimpleInventory implements BlockInventory{ protected function animateBlock(bool $isOpen) : void{ $holder = $this->getHolder(); - $block = $holder->getWorld()->getBlock($holder); + $world = $holder->getWorld(); + $block = $world->getBlock($holder); if($block instanceof Barrel){ - $holder->getWorld()->setBlock($holder, $block->setOpen($isOpen)); + $world->setBlock($holder, $block->setOpen($isOpen)); } } } diff --git a/src/block/tile/ContainerTrait.php b/src/block/tile/ContainerTrait.php index cbbc98843..199d4859a 100644 --- a/src/block/tile/ContainerTrait.php +++ b/src/block/tile/ContainerTrait.php @@ -100,8 +100,10 @@ trait ContainerTrait{ $inv = $this->getRealInventory(); $pos = $this->getPosition(); + $world = $pos->getWorld(); + $dropPos = $pos->add(0.5, 0.5, 0.5); foreach($inv->getContents() as $k => $item){ - $pos->getWorld()->dropItem($pos->add(0.5, 0.5, 0.5), $item); + $world->dropItem($dropPos, $item); } $inv->clearAll(); } diff --git a/src/block/utils/FallableTrait.php b/src/block/utils/FallableTrait.php index 95b59727c..1ed348dca 100644 --- a/src/block/utils/FallableTrait.php +++ b/src/block/utils/FallableTrait.php @@ -42,14 +42,15 @@ trait FallableTrait{ public function onNearbyBlockChange() : void{ $pos = $this->getPosition(); - $down = $pos->getWorld()->getBlock($pos->getSide(Facing::DOWN)); + $world = $pos->getWorld(); + $down = $world->getBlock($pos->getSide(Facing::DOWN)); if($down->canBeReplaced()){ - $pos->getWorld()->setBlock($pos, VanillaBlocks::AIR()); + $world->setBlock($pos, VanillaBlocks::AIR()); $block = $this; if(!($block instanceof Block)) throw new AssumptionFailedError(__TRAIT__ . " should only be used by Blocks"); - $fall = new FallingBlock(Location::fromObject($pos->add(0.5, 0, 0.5), $pos->getWorld()), $block); + $fall = new FallingBlock(Location::fromObject($pos->add(0.5, 0, 0.5), $world), $block); $fall->spawnToAll(); } } diff --git a/src/network/mcpe/handler/PreSpawnPacketHandler.php b/src/network/mcpe/handler/PreSpawnPacketHandler.php index 5237cdb4d..6b135c4c4 100644 --- a/src/network/mcpe/handler/PreSpawnPacketHandler.php +++ b/src/network/mcpe/handler/PreSpawnPacketHandler.php @@ -60,15 +60,16 @@ class PreSpawnPacketHandler extends PacketHandler{ public function setUp() : void{ $location = $this->player->getLocation(); + $world = $location->getWorld(); $levelSettings = new LevelSettings(); $levelSettings->seed = -1; $levelSettings->spawnSettings = new SpawnSettings(SpawnSettings::BIOME_TYPE_DEFAULT, "", DimensionIds::OVERWORLD); //TODO: implement this properly $levelSettings->worldGamemode = TypeConverter::getInstance()->coreGameModeToProtocol($this->server->getGamemode()); - $levelSettings->difficulty = $location->getWorld()->getDifficulty(); - $levelSettings->spawnPosition = BlockPosition::fromVector3($location->getWorld()->getSpawnLocation()); + $levelSettings->difficulty = $world->getDifficulty(); + $levelSettings->spawnPosition = BlockPosition::fromVector3($world->getSpawnLocation()); $levelSettings->hasAchievementsDisabled = true; - $levelSettings->time = $location->getWorld()->getTime(); + $levelSettings->time = $world->getTime(); $levelSettings->eduEditionOffer = 0; $levelSettings->rainLevel = 0; //TODO: implement these properly $levelSettings->lightningLevel = 0;