From a2543ff80d2906bccda1a4e2fdbd9d8e7d147fb3 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 14 Apr 2020 11:08:37 +0100 Subject: [PATCH] Position: add getLevelNonNull() this allows assuming that a position has a valid world in places where it's never expected to not be valid. Since this is the vast majority of usages, it eliminates a lot of possible null-pointer warnings given by static analysers. TODO: Consider whether we can make Position->getLevel/World use this behaviour out of the box in the next major version. --- src/pocketmine/Player.php | 10 +++--- src/pocketmine/block/Anvil.php | 2 +- src/pocketmine/block/BaseRail.php | 4 +-- src/pocketmine/block/Bed.php | 14 ++++---- src/pocketmine/block/Block.php | 6 ++-- src/pocketmine/block/BoneBlock.php | 2 +- src/pocketmine/block/BurningFurnace.php | 8 ++--- src/pocketmine/block/Cactus.php | 14 ++++---- src/pocketmine/block/Cake.php | 4 +-- src/pocketmine/block/Carpet.php | 4 +-- src/pocketmine/block/Chest.php | 10 +++--- src/pocketmine/block/Crops.php | 8 ++--- src/pocketmine/block/Dandelion.php | 4 +-- src/pocketmine/block/DeadBush.php | 2 +- src/pocketmine/block/Dirt.php | 4 +-- src/pocketmine/block/Door.php | 8 ++--- src/pocketmine/block/DoublePlant.php | 6 ++-- src/pocketmine/block/EnchantingTable.php | 4 +-- src/pocketmine/block/EnderChest.php | 8 ++--- src/pocketmine/block/Fallable.php | 2 +- src/pocketmine/block/FenceGate.php | 4 +-- src/pocketmine/block/Fire.php | 2 +- src/pocketmine/block/Flower.php | 4 +-- src/pocketmine/block/FlowerPot.php | 12 +++---- src/pocketmine/block/GlazedTerracotta.php | 2 +- src/pocketmine/block/GlowingRedstoneOre.php | 2 +- src/pocketmine/block/Grass.php | 6 ++-- src/pocketmine/block/HayBale.php | 2 +- src/pocketmine/block/Ice.php | 2 +- src/pocketmine/block/ItemFrame.php | 4 +-- src/pocketmine/block/Ladder.php | 2 +- src/pocketmine/block/Lava.php | 4 +-- src/pocketmine/block/Leaves.php | 8 ++--- src/pocketmine/block/MelonStem.php | 4 +-- src/pocketmine/block/Mycelium.php | 4 +-- src/pocketmine/block/NetherWartPlant.php | 6 ++-- src/pocketmine/block/Pumpkin.php | 2 +- src/pocketmine/block/PumpkinStem.php | 4 +-- src/pocketmine/block/Quartz.php | 2 +- src/pocketmine/block/RedMushroom.php | 4 +-- src/pocketmine/block/RedstoneOre.php | 6 ++-- src/pocketmine/block/Sapling.php | 10 +++--- src/pocketmine/block/SignPost.php | 8 ++--- src/pocketmine/block/Skull.php | 4 +-- src/pocketmine/block/Slab.php | 12 +++---- src/pocketmine/block/SnowLayer.php | 6 ++-- src/pocketmine/block/Stair.php | 2 +- src/pocketmine/block/StandingBanner.php | 8 ++--- src/pocketmine/block/Sugarcane.php | 20 +++++------ src/pocketmine/block/TNT.php | 4 +-- src/pocketmine/block/TallGrass.php | 4 +-- src/pocketmine/block/Torch.php | 6 ++-- src/pocketmine/block/Trapdoor.php | 4 +-- src/pocketmine/block/Vine.php | 2 +- src/pocketmine/block/WallBanner.php | 2 +- src/pocketmine/block/WallSign.php | 2 +- src/pocketmine/block/Water.php | 4 +-- src/pocketmine/block/WaterLily.php | 4 +-- src/pocketmine/block/Wood.php | 2 +- src/pocketmine/entity/Entity.php | 4 +-- src/pocketmine/entity/Living.php | 4 +-- src/pocketmine/entity/object/FallingBlock.php | 6 ++-- .../entity/EntityDamageByChildEntityEvent.php | 2 +- .../entity/EntityDamageByEntityEvent.php | 2 +- src/pocketmine/inventory/ChestInventory.php | 6 ++-- .../inventory/EnderChestInventory.php | 2 +- src/pocketmine/inventory/PlayerInventory.php | 2 +- src/pocketmine/item/ChorusFruit.php | 5 ++- src/pocketmine/item/PaintingItem.php | 2 +- src/pocketmine/level/Explosion.php | 2 +- src/pocketmine/level/Location.php | 2 +- src/pocketmine/level/Position.php | 16 ++++++++- src/pocketmine/tile/Chest.php | 4 +-- src/pocketmine/tile/Furnace.php | 4 +-- src/pocketmine/tile/Tile.php | 2 +- .../utils/AssumptionFailedError.php | 33 +++++++++++++++++++ 76 files changed, 229 insertions(+), 183 deletions(-) create mode 100644 src/pocketmine/utils/AssumptionFailedError.php diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index c48de117b..ff3c62bd0 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -1207,7 +1207,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ if(!($pos instanceof Position)){ $level = $this->level; }else{ - $level = $pos->getLevel(); + $level = $pos->getLevelNonNull(); } $this->spawnPosition = new Position($pos->x, $pos->y, $pos->z, $level); $pk = new SetSpawnPositionPacket(); @@ -2244,7 +2244,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ public function handleLevelSoundEvent(LevelSoundEventPacket $packet) : bool{ //TODO: add events so plugins can change this - $this->getLevel()->broadcastPacketToViewers($this, $packet); + $this->getLevelNonNull()->broadcastPacketToViewers($this, $packet); return true; } @@ -2706,7 +2706,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $item = $block->getPickedItem(); if($packet->addUserData){ - $tile = $this->getLevel()->getTile($block); + $tile = $this->getLevelNonNull()->getTile($block); if($tile instanceof Tile){ $nbt = $tile->getCleanedNBT(); if($nbt instanceof CompoundTag){ @@ -3583,7 +3583,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ } if($this->hasValidSpawnPosition()){ - $this->namedtag->setString("SpawnLevel", $this->spawnPosition->getLevel()->getFolderName()); + $this->namedtag->setString("SpawnLevel", $this->spawnPosition->getLevelNonNull()->getFolderName()); $this->namedtag->setInt("SpawnX", $this->spawnPosition->getFloorX()); $this->namedtag->setInt("SpawnY", $this->spawnPosition->getFloorY()); $this->namedtag->setInt("SpawnZ", $this->spawnPosition->getFloorZ()); @@ -3666,7 +3666,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $ev = new PlayerRespawnEvent($this, $this->getSpawn()); $ev->call(); - $realSpawn = Position::fromObject($ev->getRespawnPosition()->add(0.5, 0, 0.5), $ev->getRespawnPosition()->getLevel()); + $realSpawn = Position::fromObject($ev->getRespawnPosition()->add(0.5, 0, 0.5), $ev->getRespawnPosition()->getLevelNonNull()); $this->teleport($realSpawn); $this->setSprinting(false); diff --git a/src/pocketmine/block/Anvil.php b/src/pocketmine/block/Anvil.php index 2eb8880af..7fa7cfc1a 100644 --- a/src/pocketmine/block/Anvil.php +++ b/src/pocketmine/block/Anvil.php @@ -110,6 +110,6 @@ class Anvil extends Fallable{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $direction = ($player !== null ? $player->getDirection() : 0) & 0x03; $this->meta = $this->getVariant() | $direction; - return $this->getLevel()->setBlock($blockReplace, $this, true, true); + return $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); } } diff --git a/src/pocketmine/block/BaseRail.php b/src/pocketmine/block/BaseRail.php index c5fb8b2e3..1cd8cf167 100644 --- a/src/pocketmine/block/BaseRail.php +++ b/src/pocketmine/block/BaseRail.php @@ -91,7 +91,7 @@ abstract class BaseRail extends Flowable{ } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ - if(!$blockReplace->getSide(Vector3::SIDE_DOWN)->isTransparent() and $this->getLevel()->setBlock($blockReplace, $this, true, true)){ + if(!$blockReplace->getSide(Vector3::SIDE_DOWN)->isTransparent() and $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true)){ $this->tryReconnect(); return true; } @@ -279,7 +279,7 @@ abstract class BaseRail extends Flowable{ isset(self::ASCENDING_SIDES[$this->meta & 0x07]) and $this->getSide(self::ASCENDING_SIDES[$this->meta & 0x07])->isTransparent() )){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } diff --git a/src/pocketmine/block/Bed.php b/src/pocketmine/block/Bed.php index 803f07ea3..166b1c35b 100644 --- a/src/pocketmine/block/Bed.php +++ b/src/pocketmine/block/Bed.php @@ -83,7 +83,7 @@ class Bed extends Transparent{ $this->meta &= ~self::BITFLAG_OCCUPIED; } - $this->getLevel()->setBlock($this, $this, false, false); + $this->getLevelNonNull()->setBlock($this, $this, false, false); if(($other = $this->getOtherHalf()) !== null and $other->isOccupied() !== $occupied){ $other->setOccupied($occupied); @@ -137,7 +137,7 @@ class Bed extends Transparent{ return true; } - $time = $this->getLevel()->getTimeOfDay(); + $time = $this->getLevelNonNull()->getTimeOfDay(); $isNight = ($time >= Level::TIME_NIGHT and $time < Level::TIME_SUNRISE); @@ -168,11 +168,11 @@ class Bed extends Transparent{ $meta = (($player instanceof Player ? $player->getDirection() : 0) - 1) & 0x03; $next = $this->getSide(self::getOtherHalfSide($meta)); if($next->canBeReplaced() and !$next->getSide(Vector3::SIDE_DOWN)->isTransparent()){ - $this->getLevel()->setBlock($blockReplace, BlockFactory::get($this->id, $meta), true, true); - $this->getLevel()->setBlock($next, BlockFactory::get($this->id, $meta | self::BITFLAG_HEAD), true, true); + $this->getLevelNonNull()->setBlock($blockReplace, BlockFactory::get($this->id, $meta), true, true); + $this->getLevelNonNull()->setBlock($next, BlockFactory::get($this->id, $meta | self::BITFLAG_HEAD), true, true); - Tile::createTile(Tile::BED, $this->getLevel(), TileBed::createNBT($this, $face, $item, $player)); - Tile::createTile(Tile::BED, $this->getLevel(), TileBed::createNBT($next, $face, $item, $player)); + Tile::createTile(Tile::BED, $this->getLevelNonNull(), TileBed::createNBT($this, $face, $item, $player)); + Tile::createTile(Tile::BED, $this->getLevelNonNull(), TileBed::createNBT($next, $face, $item, $player)); return true; } @@ -194,7 +194,7 @@ class Bed extends Transparent{ } private function getItem() : Item{ - $tile = $this->getLevel()->getTile($this); + $tile = $this->getLevelNonNull()->getTile($this); if($tile instanceof TileBed){ return ItemFactory::get($this->getItemId(), $tile->getColor()); } diff --git a/src/pocketmine/block/Block.php b/src/pocketmine/block/Block.php index df18785e1..f5583753e 100644 --- a/src/pocketmine/block/Block.php +++ b/src/pocketmine/block/Block.php @@ -154,7 +154,7 @@ class Block extends Position implements BlockIds, Metadatable{ * Places the Block, using block space and block target, and side. Returns if the block has been placed. */ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ - return $this->getLevel()->setBlock($this, $this, true, true); + return $this->getLevelNonNull()->setBlock($this, $this, true, true); } /** @@ -204,7 +204,7 @@ class Block extends Position implements BlockIds, Metadatable{ * Do the actions needed so the block is broken with the Item */ public function onBreak(Item $item, Player $player = null) : bool{ - return $this->getLevel()->setBlock($this, BlockFactory::get(Block::AIR), true, true); + return $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::AIR), true, true); } /** @@ -479,7 +479,7 @@ class Block extends Position implements BlockIds, Metadatable{ */ public function getSide(int $side, int $step = 1){ if($this->isValid()){ - return $this->getLevel()->getBlock(Vector3::getSide($side, $step)); + return $this->getLevelNonNull()->getBlock(Vector3::getSide($side, $step)); } return BlockFactory::get(Block::AIR, 0, Position::fromObject(Vector3::getSide($side, $step))); diff --git a/src/pocketmine/block/BoneBlock.php b/src/pocketmine/block/BoneBlock.php index 696565fae..364e8332b 100644 --- a/src/pocketmine/block/BoneBlock.php +++ b/src/pocketmine/block/BoneBlock.php @@ -55,7 +55,7 @@ class BoneBlock extends Solid{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $this->meta = PillarRotationHelper::getMetaFromFace($this->meta, $face); - return $this->getLevel()->setBlock($blockReplace, $this, true, true); + return $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); } public function getVariantBitmask() : int{ diff --git a/src/pocketmine/block/BurningFurnace.php b/src/pocketmine/block/BurningFurnace.php index c189f5b65..fdb61536c 100644 --- a/src/pocketmine/block/BurningFurnace.php +++ b/src/pocketmine/block/BurningFurnace.php @@ -68,18 +68,18 @@ class BurningFurnace extends Solid{ 3 => 3 ]; $this->meta = $faces[$player instanceof Player ? $player->getDirection() : 0]; - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); - Tile::createTile(Tile::FURNACE, $this->getLevel(), TileFurnace::createNBT($this, $face, $item, $player)); + Tile::createTile(Tile::FURNACE, $this->getLevelNonNull(), TileFurnace::createNBT($this, $face, $item, $player)); return true; } public function onActivate(Item $item, Player $player = null) : bool{ if($player instanceof Player){ - $furnace = $this->getLevel()->getTile($this); + $furnace = $this->getLevelNonNull()->getTile($this); if(!($furnace instanceof TileFurnace)){ - $furnace = Tile::createTile(Tile::FURNACE, $this->getLevel(), TileFurnace::createNBT($this)); + $furnace = Tile::createTile(Tile::FURNACE, $this->getLevelNonNull(), TileFurnace::createNBT($this)); if(!($furnace instanceof TileFurnace)){ return true; } diff --git a/src/pocketmine/block/Cactus.php b/src/pocketmine/block/Cactus.php index 30effe652..2fd5fbff7 100644 --- a/src/pocketmine/block/Cactus.php +++ b/src/pocketmine/block/Cactus.php @@ -72,12 +72,12 @@ class Cactus extends Transparent{ public function onNearbyBlockChange() : void{ $down = $this->getSide(Vector3::SIDE_DOWN); if($down->getId() !== self::SAND and $down->getId() !== self::CACTUS){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); }else{ for($side = 2; $side <= 5; ++$side){ $b = $this->getSide($side); if($b->isSolid()){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); break; } } @@ -92,23 +92,23 @@ class Cactus extends Transparent{ if($this->getSide(Vector3::SIDE_DOWN)->getId() !== self::CACTUS){ if($this->meta === 0x0f){ for($y = 1; $y < 3; ++$y){ - $b = $this->getLevel()->getBlockAt($this->x, $this->y + $y, $this->z); + $b = $this->getLevelNonNull()->getBlockAt($this->x, $this->y + $y, $this->z); if($b->getId() === self::AIR){ $ev = new BlockGrowEvent($b, BlockFactory::get(Block::CACTUS)); $ev->call(); if($ev->isCancelled()){ break; } - $this->getLevel()->setBlock($b, $ev->getNewState(), true); + $this->getLevelNonNull()->setBlock($b, $ev->getNewState(), true); }else{ break; } } $this->meta = 0; - $this->getLevel()->setBlock($this, $this); + $this->getLevelNonNull()->setBlock($this, $this); }else{ ++$this->meta; - $this->getLevel()->setBlock($this, $this); + $this->getLevelNonNull()->setBlock($this, $this); } } } @@ -121,7 +121,7 @@ class Cactus extends Transparent{ $block2 = $this->getSide(Vector3::SIDE_WEST); $block3 = $this->getSide(Vector3::SIDE_EAST); if(!$block0->isSolid() and !$block1->isSolid() and !$block2->isSolid() and !$block3->isSolid()){ - $this->getLevel()->setBlock($this, $this, true); + $this->getLevelNonNull()->setBlock($this, $this, true); return true; } diff --git a/src/pocketmine/block/Cake.php b/src/pocketmine/block/Cake.php index 9ac80f6a7..8cb3165ec 100644 --- a/src/pocketmine/block/Cake.php +++ b/src/pocketmine/block/Cake.php @@ -66,7 +66,7 @@ class Cake extends Transparent implements FoodSource{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $down = $this->getSide(Vector3::SIDE_DOWN); if($down->getId() !== self::AIR){ - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; } @@ -76,7 +76,7 @@ class Cake extends Transparent implements FoodSource{ public function onNearbyBlockChange() : void{ if($this->getSide(Vector3::SIDE_DOWN)->getId() === self::AIR){ //Replace with common break method - $this->getLevel()->setBlock($this, BlockFactory::get(Block::AIR), true); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::AIR), true); } } diff --git a/src/pocketmine/block/Carpet.php b/src/pocketmine/block/Carpet.php index 225f9db09..bd700f8d8 100644 --- a/src/pocketmine/block/Carpet.php +++ b/src/pocketmine/block/Carpet.php @@ -64,7 +64,7 @@ class Carpet extends Flowable{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $down = $this->getSide(Vector3::SIDE_DOWN); if($down->getId() !== self::AIR){ - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; } @@ -74,7 +74,7 @@ class Carpet extends Flowable{ public function onNearbyBlockChange() : void{ if($this->getSide(Vector3::SIDE_DOWN)->getId() === self::AIR){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } diff --git a/src/pocketmine/block/Chest.php b/src/pocketmine/block/Chest.php index 47d96e33f..af2b8e498 100644 --- a/src/pocketmine/block/Chest.php +++ b/src/pocketmine/block/Chest.php @@ -81,7 +81,7 @@ class Chest extends Transparent{ } $c = $this->getSide($side); if($c->getId() === $this->id and $c->getDamage() === $this->meta){ - $tile = $this->getLevel()->getTile($c); + $tile = $this->getLevelNonNull()->getTile($c); if($tile instanceof TileChest and !$tile->isPaired()){ $chest = $tile; break; @@ -89,8 +89,8 @@ class Chest extends Transparent{ } } - $this->getLevel()->setBlock($blockReplace, $this, true, true); - $tile = Tile::createTile(Tile::CHEST, $this->getLevel(), TileChest::createNBT($this, $face, $item, $player)); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); + $tile = Tile::createTile(Tile::CHEST, $this->getLevelNonNull(), TileChest::createNBT($this, $face, $item, $player)); if($chest instanceof TileChest and $tile instanceof TileChest){ $chest->pairWith($tile); @@ -103,12 +103,12 @@ class Chest extends Transparent{ public function onActivate(Item $item, Player $player = null) : bool{ if($player instanceof Player){ - $t = $this->getLevel()->getTile($this); + $t = $this->getLevelNonNull()->getTile($this); $chest = null; if($t instanceof TileChest){ $chest = $t; }else{ - $chest = Tile::createTile(Tile::CHEST, $this->getLevel(), TileChest::createNBT($this)); + $chest = Tile::createTile(Tile::CHEST, $this->getLevelNonNull(), TileChest::createNBT($this)); if(!($chest instanceof TileChest)){ return true; } diff --git a/src/pocketmine/block/Crops.php b/src/pocketmine/block/Crops.php index eaebfe0a2..81afd0c17 100644 --- a/src/pocketmine/block/Crops.php +++ b/src/pocketmine/block/Crops.php @@ -33,7 +33,7 @@ abstract class Crops extends Flowable{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ if($blockReplace->getSide(Vector3::SIDE_DOWN)->getId() === Block::FARMLAND){ - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; } @@ -52,7 +52,7 @@ abstract class Crops extends Flowable{ $ev = new BlockGrowEvent($this, $block); $ev->call(); if(!$ev->isCancelled()){ - $this->getLevel()->setBlock($this, $ev->getNewState(), true, true); + $this->getLevelNonNull()->setBlock($this, $ev->getNewState(), true, true); } $item->pop(); @@ -65,7 +65,7 @@ abstract class Crops extends Flowable{ public function onNearbyBlockChange() : void{ if($this->getSide(Vector3::SIDE_DOWN)->getId() !== Block::FARMLAND){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } @@ -81,7 +81,7 @@ abstract class Crops extends Flowable{ $ev = new BlockGrowEvent($this, $block); $ev->call(); if(!$ev->isCancelled()){ - $this->getLevel()->setBlock($this, $ev->getNewState(), true, true); + $this->getLevelNonNull()->setBlock($this, $ev->getNewState(), true, true); } } } diff --git a/src/pocketmine/block/Dandelion.php b/src/pocketmine/block/Dandelion.php index 3071df931..6ffb28a7f 100644 --- a/src/pocketmine/block/Dandelion.php +++ b/src/pocketmine/block/Dandelion.php @@ -42,7 +42,7 @@ class Dandelion extends Flowable{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $down = $this->getSide(Vector3::SIDE_DOWN); if($down->getId() === Block::GRASS or $down->getId() === Block::DIRT or $down->getId() === Block::FARMLAND){ - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; } @@ -52,7 +52,7 @@ class Dandelion extends Flowable{ public function onNearbyBlockChange() : void{ if($this->getSide(Vector3::SIDE_DOWN)->isTransparent()){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } diff --git a/src/pocketmine/block/DeadBush.php b/src/pocketmine/block/DeadBush.php index c42abcd9f..8ee1b0977 100644 --- a/src/pocketmine/block/DeadBush.php +++ b/src/pocketmine/block/DeadBush.php @@ -51,7 +51,7 @@ class DeadBush extends Flowable{ public function onNearbyBlockChange() : void{ if($this->getSide(Vector3::SIDE_DOWN)->isTransparent()){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } diff --git a/src/pocketmine/block/Dirt.php b/src/pocketmine/block/Dirt.php index 96f8da27c..9b00df1e5 100644 --- a/src/pocketmine/block/Dirt.php +++ b/src/pocketmine/block/Dirt.php @@ -54,9 +54,9 @@ class Dirt extends Solid{ if($item instanceof Hoe){ $item->applyDamage(1); if($this->meta === 1){ - $this->getLevel()->setBlock($this, BlockFactory::get(Block::DIRT), true); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::DIRT), true); }else{ - $this->getLevel()->setBlock($this, BlockFactory::get(Block::FARMLAND), true); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::FARMLAND), true); } return true; diff --git a/src/pocketmine/block/Door.php b/src/pocketmine/block/Door.php index d100a4c70..d4de03dc4 100644 --- a/src/pocketmine/block/Door.php +++ b/src/pocketmine/block/Door.php @@ -201,9 +201,9 @@ abstract class Door extends Transparent{ public function onNearbyBlockChange() : void{ if($this->getSide(Vector3::SIDE_DOWN)->getId() === self::AIR){ //Replace with common break method - $this->getLevel()->setBlock($this, BlockFactory::get(Block::AIR), false); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::AIR), false); if($this->getSide(Vector3::SIDE_UP) instanceof Door){ - $this->getLevel()->setBlock($this->getSide(Vector3::SIDE_UP), BlockFactory::get(Block::AIR), false); + $this->getLevelNonNull()->setBlock($this->getSide(Vector3::SIDE_UP), BlockFactory::get(Block::AIR), false); } } } @@ -230,8 +230,8 @@ abstract class Door extends Transparent{ } $this->setDamage($player->getDirection() & 0x03); - $this->getLevel()->setBlock($blockReplace, $this, true, true); //Bottom - $this->getLevel()->setBlock($blockUp, BlockFactory::get($this->getId(), $metaUp), true); //Top + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); //Bottom + $this->getLevelNonNull()->setBlock($blockUp, BlockFactory::get($this->getId(), $metaUp), true); //Top return true; } diff --git a/src/pocketmine/block/DoublePlant.php b/src/pocketmine/block/DoublePlant.php index ccb8c1cbd..5894560d1 100644 --- a/src/pocketmine/block/DoublePlant.php +++ b/src/pocketmine/block/DoublePlant.php @@ -57,8 +57,8 @@ class DoublePlant extends Flowable{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $id = $blockReplace->getSide(Vector3::SIDE_DOWN)->getId(); if(($id === Block::GRASS or $id === Block::DIRT) and $blockReplace->getSide(Vector3::SIDE_UP)->canBeReplaced()){ - $this->getLevel()->setBlock($blockReplace, $this, false, false); - $this->getLevel()->setBlock($blockReplace->getSide(Vector3::SIDE_UP), BlockFactory::get($this->id, $this->meta | self::BITFLAG_TOP), false, false); + $this->getLevelNonNull()->setBlock($blockReplace, $this, false, false); + $this->getLevelNonNull()->setBlock($blockReplace->getSide(Vector3::SIDE_UP), BlockFactory::get($this->id, $this->meta | self::BITFLAG_TOP), false, false); return true; } @@ -85,7 +85,7 @@ class DoublePlant extends Flowable{ public function onNearbyBlockChange() : void{ if(!$this->isValidHalfPlant() or (($this->meta & self::BITFLAG_TOP) === 0 and $this->getSide(Vector3::SIDE_DOWN)->isTransparent())){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } diff --git a/src/pocketmine/block/EnchantingTable.php b/src/pocketmine/block/EnchantingTable.php index 5574677c4..2724ff51e 100644 --- a/src/pocketmine/block/EnchantingTable.php +++ b/src/pocketmine/block/EnchantingTable.php @@ -40,9 +40,9 @@ class EnchantingTable extends Transparent{ } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); - Tile::createTile(Tile::ENCHANT_TABLE, $this->getLevel(), TileEnchantTable::createNBT($this, $face, $item, $player)); + Tile::createTile(Tile::ENCHANT_TABLE, $this->getLevelNonNull(), TileEnchantTable::createNBT($this, $face, $item, $player)); return true; } diff --git a/src/pocketmine/block/EnderChest.php b/src/pocketmine/block/EnderChest.php index 1f1f5e6dd..00438b472 100644 --- a/src/pocketmine/block/EnderChest.php +++ b/src/pocketmine/block/EnderChest.php @@ -69,8 +69,8 @@ class EnderChest extends Chest{ $this->meta = $faces[$player instanceof Player ? $player->getDirection() : 0]; - $this->getLevel()->setBlock($blockReplace, $this, true, true); - Tile::createTile(Tile::ENDER_CHEST, $this->getLevel(), TileEnderChest::createNBT($this, $face, $item, $player)); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); + Tile::createTile(Tile::ENDER_CHEST, $this->getLevelNonNull(), TileEnderChest::createNBT($this, $face, $item, $player)); return true; } @@ -78,12 +78,12 @@ class EnderChest extends Chest{ public function onActivate(Item $item, Player $player = null) : bool{ if($player instanceof Player){ - $t = $this->getLevel()->getTile($this); + $t = $this->getLevelNonNull()->getTile($this); $enderChest = null; if($t instanceof TileEnderChest){ $enderChest = $t; }else{ - $enderChest = Tile::createTile(Tile::ENDER_CHEST, $this->getLevel(), TileEnderChest::createNBT($this)); + $enderChest = Tile::createTile(Tile::ENDER_CHEST, $this->getLevelNonNull(), TileEnderChest::createNBT($this)); if(!($enderChest instanceof TileEnderChest)){ return true; } diff --git a/src/pocketmine/block/Fallable.php b/src/pocketmine/block/Fallable.php index 10cba9554..15d87b97d 100644 --- a/src/pocketmine/block/Fallable.php +++ b/src/pocketmine/block/Fallable.php @@ -37,7 +37,7 @@ abstract class Fallable extends Solid{ $nbt->setInt("TileID", $this->getId()); $nbt->setByte("Data", $this->getDamage()); - $fall = Entity::createEntity("FallingSand", $this->getLevel(), $nbt); + $fall = Entity::createEntity("FallingSand", $this->getLevelNonNull(), $nbt); if($fall !== null){ $fall->spawnToAll(); diff --git a/src/pocketmine/block/FenceGate.php b/src/pocketmine/block/FenceGate.php index 5f5a4d391..66b135f70 100644 --- a/src/pocketmine/block/FenceGate.php +++ b/src/pocketmine/block/FenceGate.php @@ -69,7 +69,7 @@ class FenceGate extends Transparent{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $this->meta = ($player instanceof Player ? ($player->getDirection() - 1) & 0x03 : 0); - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; } @@ -85,7 +85,7 @@ class FenceGate extends Transparent{ $this->meta |= (($player->getDirection() - 1) & 0x02); } - $this->getLevel()->setBlock($this, $this, true); + $this->getLevelNonNull()->setBlock($this, $this, true); $this->level->addSound(new DoorSound($this)); return true; } diff --git a/src/pocketmine/block/Fire.php b/src/pocketmine/block/Fire.php index f89b2f887..832ab6194 100644 --- a/src/pocketmine/block/Fire.php +++ b/src/pocketmine/block/Fire.php @@ -82,7 +82,7 @@ class Fire extends Flowable{ public function onNearbyBlockChange() : void{ if(!$this->getSide(Vector3::SIDE_DOWN)->isSolid() and !$this->hasAdjacentFlammableBlocks()){ - $this->getLevel()->setBlock($this, BlockFactory::get(Block::AIR), true); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::AIR), true); }else{ $this->level->scheduleDelayedBlockUpdate($this, mt_rand(30, 40)); } diff --git a/src/pocketmine/block/Flower.php b/src/pocketmine/block/Flower.php index 4bb3ee7ce..7a1b5baf9 100644 --- a/src/pocketmine/block/Flower.php +++ b/src/pocketmine/block/Flower.php @@ -62,7 +62,7 @@ class Flower extends Flowable{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $down = $this->getSide(Vector3::SIDE_DOWN); if($down->getId() === Block::GRASS or $down->getId() === Block::DIRT or $down->getId() === Block::FARMLAND){ - $this->getLevel()->setBlock($blockReplace, $this, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true); return true; } @@ -72,7 +72,7 @@ class Flower extends Flowable{ public function onNearbyBlockChange() : void{ if($this->getSide(Vector3::SIDE_DOWN)->isTransparent()){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } diff --git a/src/pocketmine/block/FlowerPot.php b/src/pocketmine/block/FlowerPot.php index cc5d3b9ed..5e1a41d48 100644 --- a/src/pocketmine/block/FlowerPot.php +++ b/src/pocketmine/block/FlowerPot.php @@ -62,19 +62,19 @@ class FlowerPot extends Flowable{ return false; } - $this->getLevel()->setBlock($blockReplace, $this, true, true); - Tile::createTile(Tile::FLOWER_POT, $this->getLevel(), TileFlowerPot::createNBT($this, $face, $item, $player)); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); + Tile::createTile(Tile::FLOWER_POT, $this->getLevelNonNull(), TileFlowerPot::createNBT($this, $face, $item, $player)); return true; } public function onNearbyBlockChange() : void{ if($this->getSide(Vector3::SIDE_DOWN)->isTransparent()){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } public function onActivate(Item $item, Player $player = null) : bool{ - $pot = $this->getLevel()->getTile($this); + $pot = $this->getLevelNonNull()->getTile($this); if(!($pot instanceof TileFlowerPot)){ return false; } @@ -83,7 +83,7 @@ class FlowerPot extends Flowable{ } $this->setDamage(self::STATE_FULL); //specific damage value is unnecessary, it just needs to be non-zero to show an item. - $this->getLevel()->setBlock($this, $this, true, false); + $this->getLevelNonNull()->setBlock($this, $this, true, false); $pot->setItem($item->pop()); return true; @@ -96,7 +96,7 @@ class FlowerPot extends Flowable{ public function getDropsForCompatibleTool(Item $item) : array{ $items = parent::getDropsForCompatibleTool($item); - $tile = $this->getLevel()->getTile($this); + $tile = $this->getLevelNonNull()->getTile($this); if($tile instanceof TileFlowerPot){ $item = $tile->getItem(); if($item->getId() !== Item::AIR){ diff --git a/src/pocketmine/block/GlazedTerracotta.php b/src/pocketmine/block/GlazedTerracotta.php index 9e3862666..e2ae343bf 100644 --- a/src/pocketmine/block/GlazedTerracotta.php +++ b/src/pocketmine/block/GlazedTerracotta.php @@ -53,7 +53,7 @@ class GlazedTerracotta extends Solid{ $this->meta = $faces[(~($player->getDirection() - 1)) & 0x03]; } - return $this->getLevel()->setBlock($blockReplace, $this, true, true); + return $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); } public function getVariantBitmask() : int{ diff --git a/src/pocketmine/block/GlowingRedstoneOre.php b/src/pocketmine/block/GlowingRedstoneOre.php index 0825bf546..94a3ce8ad 100644 --- a/src/pocketmine/block/GlowingRedstoneOre.php +++ b/src/pocketmine/block/GlowingRedstoneOre.php @@ -53,6 +53,6 @@ class GlowingRedstoneOre extends RedstoneOre{ } public function onRandomTick() : void{ - $this->getLevel()->setBlock($this, BlockFactory::get(Block::REDSTONE_ORE, $this->meta), false, false); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::REDSTONE_ORE, $this->meta), false, false); } } diff --git a/src/pocketmine/block/Grass.php b/src/pocketmine/block/Grass.php index afd4b54ad..8313cc78c 100644 --- a/src/pocketmine/block/Grass.php +++ b/src/pocketmine/block/Grass.php @@ -100,17 +100,17 @@ class Grass extends Solid{ public function onActivate(Item $item, Player $player = null) : bool{ if($item->getId() === Item::DYE and $item->getDamage() === 0x0F){ $item->pop(); - TallGrassObject::growGrass($this->getLevel(), $this, new Random(mt_rand()), 8, 2); + TallGrassObject::growGrass($this->getLevelNonNull(), $this, new Random(mt_rand()), 8, 2); return true; }elseif($item instanceof Hoe){ $item->applyDamage(1); - $this->getLevel()->setBlock($this, BlockFactory::get(Block::FARMLAND)); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::FARMLAND)); return true; }elseif($item instanceof Shovel and $this->getSide(Vector3::SIDE_UP)->getId() === Block::AIR){ $item->applyDamage(1); - $this->getLevel()->setBlock($this, BlockFactory::get(Block::GRASS_PATH)); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::GRASS_PATH)); return true; } diff --git a/src/pocketmine/block/HayBale.php b/src/pocketmine/block/HayBale.php index 7dd2989da..a97c769ae 100644 --- a/src/pocketmine/block/HayBale.php +++ b/src/pocketmine/block/HayBale.php @@ -46,7 +46,7 @@ class HayBale extends Solid{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $this->meta = PillarRotationHelper::getMetaFromFace($this->meta, $face); - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; } diff --git a/src/pocketmine/block/Ice.php b/src/pocketmine/block/Ice.php index 3c214cf3b..3e9953b50 100644 --- a/src/pocketmine/block/Ice.php +++ b/src/pocketmine/block/Ice.php @@ -57,7 +57,7 @@ class Ice extends Transparent{ public function onBreak(Item $item, Player $player = null) : bool{ if(($player === null or $player->isSurvival()) and !$item->hasEnchantment(Enchantment::SILK_TOUCH)){ - return $this->getLevel()->setBlock($this, BlockFactory::get(Block::WATER), true); + return $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::WATER), true); } return parent::onBreak($item, $player); } diff --git a/src/pocketmine/block/ItemFrame.php b/src/pocketmine/block/ItemFrame.php index 6644c682b..cdb3ca89a 100644 --- a/src/pocketmine/block/ItemFrame.php +++ b/src/pocketmine/block/ItemFrame.php @@ -46,7 +46,7 @@ class ItemFrame extends Flowable{ public function onActivate(Item $item, Player $player = null) : bool{ $tile = $this->level->getTile($this); if(!($tile instanceof TileItemFrame)){ - $tile = Tile::createTile(Tile::ITEM_FRAME, $this->getLevel(), TileItemFrame::createNBT($this)); + $tile = Tile::createTile(Tile::ITEM_FRAME, $this->getLevelNonNull(), TileItemFrame::createNBT($this)); if(!($tile instanceof TileItemFrame)){ return true; } @@ -88,7 +88,7 @@ class ItemFrame extends Flowable{ $this->meta = $faces[$face]; $this->level->setBlock($blockReplace, $this, true, true); - Tile::createTile(Tile::ITEM_FRAME, $this->getLevel(), TileItemFrame::createNBT($this, $face, $item, $player)); + Tile::createTile(Tile::ITEM_FRAME, $this->getLevelNonNull(), TileItemFrame::createNBT($this, $face, $item, $player)); return true; diff --git a/src/pocketmine/block/Ladder.php b/src/pocketmine/block/Ladder.php index d2aab6a85..2de51c5ad 100644 --- a/src/pocketmine/block/Ladder.php +++ b/src/pocketmine/block/Ladder.php @@ -100,7 +100,7 @@ class Ladder extends Transparent{ ]; if(isset($faces[$face])){ $this->meta = $faces[$face]; - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; } diff --git a/src/pocketmine/block/Lava.php b/src/pocketmine/block/Lava.php index aa111d28a..086fd6666 100644 --- a/src/pocketmine/block/Lava.php +++ b/src/pocketmine/block/Lava.php @@ -115,8 +115,8 @@ class Lava extends Liquid{ } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ - $ret = $this->getLevel()->setBlock($this, $this, true, false); - $this->getLevel()->scheduleDelayedBlockUpdate($this, $this->tickRate()); + $ret = $this->getLevelNonNull()->setBlock($this, $this, true, false); + $this->getLevelNonNull()->scheduleDelayedBlockUpdate($this, $this->tickRate()); return $ret; } diff --git a/src/pocketmine/block/Leaves.php b/src/pocketmine/block/Leaves.php index 788e4a781..6eeb83e49 100644 --- a/src/pocketmine/block/Leaves.php +++ b/src/pocketmine/block/Leaves.php @@ -139,7 +139,7 @@ class Leaves extends Transparent{ public function onNearbyBlockChange() : void{ if(($this->meta & 0b00001100) === 0){ $this->meta |= 0x08; - $this->getLevel()->setBlock($this, $this, true, false); + $this->getLevelNonNull()->setBlock($this, $this, true, false); } } @@ -155,16 +155,16 @@ class Leaves extends Transparent{ $ev = new LeavesDecayEvent($this); $ev->call(); if($ev->isCancelled() or $this->findLog($this, $visited, 0)){ - $this->getLevel()->setBlock($this, $this, false, false); + $this->getLevelNonNull()->setBlock($this, $this, false, false); }else{ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $this->meta |= 0x04; - return $this->getLevel()->setBlock($this, $this, true); + return $this->getLevelNonNull()->setBlock($this, $this, true); } public function getVariantBitmask() : int{ diff --git a/src/pocketmine/block/MelonStem.php b/src/pocketmine/block/MelonStem.php index d8bf91caa..420059529 100644 --- a/src/pocketmine/block/MelonStem.php +++ b/src/pocketmine/block/MelonStem.php @@ -49,7 +49,7 @@ class MelonStem extends Crops{ $ev = new BlockGrowEvent($this, $block); $ev->call(); if(!$ev->isCancelled()){ - $this->getLevel()->setBlock($this, $ev->getNewState(), true); + $this->getLevelNonNull()->setBlock($this, $ev->getNewState(), true); } }else{ for($side = 2; $side <= 5; ++$side){ @@ -64,7 +64,7 @@ class MelonStem extends Crops{ $ev = new BlockGrowEvent($side, BlockFactory::get(Block::MELON_BLOCK)); $ev->call(); if(!$ev->isCancelled()){ - $this->getLevel()->setBlock($side, $ev->getNewState(), true); + $this->getLevelNonNull()->setBlock($side, $ev->getNewState(), true); } } } diff --git a/src/pocketmine/block/Mycelium.php b/src/pocketmine/block/Mycelium.php index edb42d0d1..26b1ea366 100644 --- a/src/pocketmine/block/Mycelium.php +++ b/src/pocketmine/block/Mycelium.php @@ -64,13 +64,13 @@ class Mycelium extends Solid{ $x = mt_rand($this->x - 1, $this->x + 1); $y = mt_rand($this->y - 2, $this->y + 2); $z = mt_rand($this->z - 1, $this->z + 1); - $block = $this->getLevel()->getBlockAt($x, $y, $z); + $block = $this->getLevelNonNull()->getBlockAt($x, $y, $z); if($block->getId() === Block::DIRT){ if($block->getSide(Vector3::SIDE_UP) instanceof Transparent){ $ev = new BlockSpreadEvent($block, $this, BlockFactory::get(Block::MYCELIUM)); $ev->call(); if(!$ev->isCancelled()){ - $this->getLevel()->setBlock($block, $ev->getNewState()); + $this->getLevelNonNull()->setBlock($block, $ev->getNewState()); } } } diff --git a/src/pocketmine/block/NetherWartPlant.php b/src/pocketmine/block/NetherWartPlant.php index 7428730ac..3ab129b57 100644 --- a/src/pocketmine/block/NetherWartPlant.php +++ b/src/pocketmine/block/NetherWartPlant.php @@ -46,7 +46,7 @@ class NetherWartPlant extends Flowable{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $down = $this->getSide(Vector3::SIDE_DOWN); if($down->getId() === Block::SOUL_SAND){ - $this->getLevel()->setBlock($blockReplace, $this, false, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, false, true); return true; } @@ -56,7 +56,7 @@ class NetherWartPlant extends Flowable{ public function onNearbyBlockChange() : void{ if($this->getSide(Vector3::SIDE_DOWN)->getId() !== Block::SOUL_SAND){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } @@ -71,7 +71,7 @@ class NetherWartPlant extends Flowable{ $ev = new BlockGrowEvent($this, $block); $ev->call(); if(!$ev->isCancelled()){ - $this->getLevel()->setBlock($this, $ev->getNewState(), false, true); + $this->getLevelNonNull()->setBlock($this, $ev->getNewState(), false, true); } } } diff --git a/src/pocketmine/block/Pumpkin.php b/src/pocketmine/block/Pumpkin.php index 811dd06e5..e5ce7e4b2 100644 --- a/src/pocketmine/block/Pumpkin.php +++ b/src/pocketmine/block/Pumpkin.php @@ -51,7 +51,7 @@ class Pumpkin extends Solid{ if($player instanceof Player){ $this->meta = ((int) $player->getDirection() + 1) % 4; } - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; } diff --git a/src/pocketmine/block/PumpkinStem.php b/src/pocketmine/block/PumpkinStem.php index 8a6ca57b5..aa06ac367 100644 --- a/src/pocketmine/block/PumpkinStem.php +++ b/src/pocketmine/block/PumpkinStem.php @@ -49,7 +49,7 @@ class PumpkinStem extends Crops{ $ev = new BlockGrowEvent($this, $block); $ev->call(); if(!$ev->isCancelled()){ - $this->getLevel()->setBlock($this, $ev->getNewState(), true); + $this->getLevelNonNull()->setBlock($this, $ev->getNewState(), true); } }else{ for($side = 2; $side <= 5; ++$side){ @@ -64,7 +64,7 @@ class PumpkinStem extends Crops{ $ev = new BlockGrowEvent($side, BlockFactory::get(Block::PUMPKIN)); $ev->call(); if(!$ev->isCancelled()){ - $this->getLevel()->setBlock($side, $ev->getNewState(), true); + $this->getLevelNonNull()->setBlock($side, $ev->getNewState(), true); } } } diff --git a/src/pocketmine/block/Quartz.php b/src/pocketmine/block/Quartz.php index d21c62ecf..bfaaea46f 100644 --- a/src/pocketmine/block/Quartz.php +++ b/src/pocketmine/block/Quartz.php @@ -58,7 +58,7 @@ class Quartz extends Solid{ if($this->getVariant() !== self::NORMAL){ $this->meta = PillarRotationHelper::getMetaFromFace($this->meta, $face); } - return $this->getLevel()->setBlock($blockReplace, $this, true, true); + return $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); } public function getToolType() : int{ diff --git a/src/pocketmine/block/RedMushroom.php b/src/pocketmine/block/RedMushroom.php index 5c4d5296b..d9d48b62a 100644 --- a/src/pocketmine/block/RedMushroom.php +++ b/src/pocketmine/block/RedMushroom.php @@ -45,14 +45,14 @@ class RedMushroom extends Flowable{ public function onNearbyBlockChange() : void{ if($this->getSide(Vector3::SIDE_DOWN)->isTransparent()){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $down = $this->getSide(Vector3::SIDE_DOWN); if(!$down->isTransparent()){ - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; } diff --git a/src/pocketmine/block/RedstoneOre.php b/src/pocketmine/block/RedstoneOre.php index 0557d0be9..a9fab687c 100644 --- a/src/pocketmine/block/RedstoneOre.php +++ b/src/pocketmine/block/RedstoneOre.php @@ -47,16 +47,16 @@ class RedstoneOre extends Solid{ } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ - return $this->getLevel()->setBlock($this, $this, true, false); + return $this->getLevelNonNull()->setBlock($this, $this, true, false); } public function onActivate(Item $item, Player $player = null) : bool{ - $this->getLevel()->setBlock($this, BlockFactory::get(Block::GLOWING_REDSTONE_ORE, $this->meta)); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::GLOWING_REDSTONE_ORE, $this->meta)); return false; //this shouldn't prevent block placement } public function onNearbyBlockChange() : void{ - $this->getLevel()->setBlock($this, BlockFactory::get(Block::GLOWING_REDSTONE_ORE, $this->meta)); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::GLOWING_REDSTONE_ORE, $this->meta)); } public function getToolType() : int{ diff --git a/src/pocketmine/block/Sapling.php b/src/pocketmine/block/Sapling.php index 682f2251a..2fdeb3ebf 100644 --- a/src/pocketmine/block/Sapling.php +++ b/src/pocketmine/block/Sapling.php @@ -59,7 +59,7 @@ class Sapling extends Flowable{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $down = $this->getSide(Vector3::SIDE_DOWN); if($down->getId() === self::GRASS or $down->getId() === self::DIRT or $down->getId() === self::FARMLAND){ - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; } @@ -70,7 +70,7 @@ class Sapling extends Flowable{ public function onActivate(Item $item, Player $player = null) : bool{ if($item->getId() === Item::DYE and $item->getDamage() === 0x0F){ //Bonemeal //TODO: change log type - Tree::growTree($this->getLevel(), $this->x, $this->y, $this->z, new Random(mt_rand()), $this->getVariant()); + Tree::growTree($this->getLevelNonNull(), $this->x, $this->y, $this->z, new Random(mt_rand()), $this->getVariant()); $item->pop(); @@ -82,7 +82,7 @@ class Sapling extends Flowable{ public function onNearbyBlockChange() : void{ if($this->getSide(Vector3::SIDE_DOWN)->isTransparent()){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } @@ -93,10 +93,10 @@ class Sapling extends Flowable{ public function onRandomTick() : void{ if($this->level->getFullLightAt($this->x, $this->y, $this->z) >= 8 and mt_rand(1, 7) === 1){ if(($this->meta & 0x08) === 0x08){ - Tree::growTree($this->getLevel(), $this->x, $this->y, $this->z, new Random(mt_rand()), $this->getVariant()); + Tree::growTree($this->getLevelNonNull(), $this->x, $this->y, $this->z, new Random(mt_rand()), $this->getVariant()); }else{ $this->meta |= 0x08; - $this->getLevel()->setBlock($this, $this, true); + $this->getLevelNonNull()->setBlock($this, $this, true); } } } diff --git a/src/pocketmine/block/SignPost.php b/src/pocketmine/block/SignPost.php index bba195a22..c63307446 100644 --- a/src/pocketmine/block/SignPost.php +++ b/src/pocketmine/block/SignPost.php @@ -62,13 +62,13 @@ class SignPost extends Transparent{ if($face === Vector3::SIDE_UP){ $this->meta = $player !== null ? (floor((($player->yaw + 180) * 16 / 360) + 0.5) & 0x0f) : 0; - $this->getLevel()->setBlock($blockReplace, $this, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true); }else{ $this->meta = $face; - $this->getLevel()->setBlock($blockReplace, BlockFactory::get(Block::WALL_SIGN, $this->meta), true); + $this->getLevelNonNull()->setBlock($blockReplace, BlockFactory::get(Block::WALL_SIGN, $this->meta), true); } - Tile::createTile(Tile::SIGN, $this->getLevel(), TileSign::createNBT($this, $face, $item, $player)); + Tile::createTile(Tile::SIGN, $this->getLevelNonNull(), TileSign::createNBT($this, $face, $item, $player)); return true; } @@ -78,7 +78,7 @@ class SignPost extends Transparent{ public function onNearbyBlockChange() : void{ if($this->getSide(Vector3::SIDE_DOWN)->getId() === self::AIR){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } diff --git a/src/pocketmine/block/Skull.php b/src/pocketmine/block/Skull.php index 3dfd1b04b..dfeb2f711 100644 --- a/src/pocketmine/block/Skull.php +++ b/src/pocketmine/block/Skull.php @@ -65,8 +65,8 @@ class Skull extends Flowable{ } $this->meta = $face; - $this->getLevel()->setBlock($blockReplace, $this, true); - Tile::createTile(Tile::SKULL, $this->getLevel(), TileSkull::createNBT($this, $face, $item, $player)); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true); + Tile::createTile(Tile::SKULL, $this->getLevelNonNull(), TileSkull::createNBT($this, $face, $item, $player)); return true; } diff --git a/src/pocketmine/block/Slab.php b/src/pocketmine/block/Slab.php index dcfa75923..3657cb1d3 100644 --- a/src/pocketmine/block/Slab.php +++ b/src/pocketmine/block/Slab.php @@ -56,11 +56,11 @@ abstract class Slab extends Transparent{ $this->meta &= 0x07; if($face === Vector3::SIDE_DOWN){ if($blockClicked->getId() === $this->id and ($blockClicked->getDamage() & 0x08) === 0x08 and $blockClicked->getVariant() === $this->getVariant()){ - $this->getLevel()->setBlock($blockClicked, BlockFactory::get($this->getDoubleSlabId(), $this->getVariant()), true); + $this->getLevelNonNull()->setBlock($blockClicked, BlockFactory::get($this->getDoubleSlabId(), $this->getVariant()), true); return true; }elseif($blockReplace->getId() === $this->id and $blockReplace->getVariant() === $this->getVariant()){ - $this->getLevel()->setBlock($blockReplace, BlockFactory::get($this->getDoubleSlabId(), $this->getVariant()), true); + $this->getLevelNonNull()->setBlock($blockReplace, BlockFactory::get($this->getDoubleSlabId(), $this->getVariant()), true); return true; }else{ @@ -68,18 +68,18 @@ abstract class Slab extends Transparent{ } }elseif($face === Vector3::SIDE_UP){ if($blockClicked->getId() === $this->id and ($blockClicked->getDamage() & 0x08) === 0 and $blockClicked->getVariant() === $this->getVariant()){ - $this->getLevel()->setBlock($blockClicked, BlockFactory::get($this->getDoubleSlabId(), $this->getVariant()), true); + $this->getLevelNonNull()->setBlock($blockClicked, BlockFactory::get($this->getDoubleSlabId(), $this->getVariant()), true); return true; }elseif($blockReplace->getId() === $this->id and $blockReplace->getVariant() === $this->getVariant()){ - $this->getLevel()->setBlock($blockReplace, BlockFactory::get($this->getDoubleSlabId(), $this->getVariant()), true); + $this->getLevelNonNull()->setBlock($blockReplace, BlockFactory::get($this->getDoubleSlabId(), $this->getVariant()), true); return true; } }else{ //TODO: collision if($blockReplace->getId() === $this->id){ if($blockReplace->getVariant() === $this->getVariant()){ - $this->getLevel()->setBlock($blockReplace, BlockFactory::get($this->getDoubleSlabId(), $this->getVariant()), true); + $this->getLevelNonNull()->setBlock($blockReplace, BlockFactory::get($this->getDoubleSlabId(), $this->getVariant()), true); return true; } @@ -95,7 +95,7 @@ abstract class Slab extends Transparent{ if($blockReplace->getId() === $this->id and $blockClicked->getVariant() !== $this->getVariant()){ return false; } - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; } diff --git a/src/pocketmine/block/SnowLayer.php b/src/pocketmine/block/SnowLayer.php index ab8168e82..21453ab9f 100644 --- a/src/pocketmine/block/SnowLayer.php +++ b/src/pocketmine/block/SnowLayer.php @@ -60,7 +60,7 @@ class SnowLayer extends Flowable{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ if($blockReplace->getSide(Vector3::SIDE_DOWN)->isSolid()){ //TODO: fix placement - $this->getLevel()->setBlock($blockReplace, $this, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true); return true; } @@ -70,7 +70,7 @@ class SnowLayer extends Flowable{ public function onNearbyBlockChange() : void{ if(!$this->getSide(Vector3::SIDE_DOWN)->isSolid()){ - $this->getLevel()->setBlock($this, BlockFactory::get(Block::AIR), false, false); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::AIR), false, false); } } @@ -80,7 +80,7 @@ class SnowLayer extends Flowable{ public function onRandomTick() : void{ if($this->level->getBlockLightAt($this->x, $this->y, $this->z) >= 12){ - $this->getLevel()->setBlock($this, BlockFactory::get(Block::AIR), false, false); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::AIR), false, false); } } diff --git a/src/pocketmine/block/Stair.php b/src/pocketmine/block/Stair.php index cf9328aac..352bacd82 100644 --- a/src/pocketmine/block/Stair.php +++ b/src/pocketmine/block/Stair.php @@ -93,7 +93,7 @@ abstract class Stair extends Transparent{ if(($clickVector->y > 0.5 and $face !== Vector3::SIDE_UP) or $face === Vector3::SIDE_DOWN){ $this->meta |= 0x04; //Upside-down stairs } - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; } diff --git a/src/pocketmine/block/StandingBanner.php b/src/pocketmine/block/StandingBanner.php index bc8c9012c..090f5c83a 100644 --- a/src/pocketmine/block/StandingBanner.php +++ b/src/pocketmine/block/StandingBanner.php @@ -62,13 +62,13 @@ class StandingBanner extends Transparent{ if($face !== Vector3::SIDE_DOWN){ if($face === Vector3::SIDE_UP and $player !== null){ $this->meta = floor((($player->yaw + 180) * 16 / 360) + 0.5) & 0x0f; - $this->getLevel()->setBlock($blockReplace, $this, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true); }else{ $this->meta = $face; - $this->getLevel()->setBlock($blockReplace, BlockFactory::get(Block::WALL_BANNER, $this->meta), true); + $this->getLevelNonNull()->setBlock($blockReplace, BlockFactory::get(Block::WALL_BANNER, $this->meta), true); } - Tile::createTile(Tile::BANNER, $this->getLevel(), TileBanner::createNBT($this, $face, $item, $player)); + Tile::createTile(Tile::BANNER, $this->getLevelNonNull(), TileBanner::createNBT($this, $face, $item, $player)); return true; } @@ -77,7 +77,7 @@ class StandingBanner extends Transparent{ public function onNearbyBlockChange() : void{ if($this->getSide(Vector3::SIDE_DOWN)->getId() === self::AIR){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } diff --git a/src/pocketmine/block/Sugarcane.php b/src/pocketmine/block/Sugarcane.php index ad4e95aff..cdda01f8e 100644 --- a/src/pocketmine/block/Sugarcane.php +++ b/src/pocketmine/block/Sugarcane.php @@ -46,20 +46,20 @@ class Sugarcane extends Flowable{ if($item->getId() === Item::DYE and $item->getDamage() === 0x0F){ //Bonemeal if($this->getSide(Vector3::SIDE_DOWN)->getId() !== self::SUGARCANE_BLOCK){ for($y = 1; $y < 3; ++$y){ - $b = $this->getLevel()->getBlockAt($this->x, $this->y + $y, $this->z); + $b = $this->getLevelNonNull()->getBlockAt($this->x, $this->y + $y, $this->z); if($b->getId() === self::AIR){ $ev = new BlockGrowEvent($b, BlockFactory::get(Block::SUGARCANE_BLOCK)); $ev->call(); if($ev->isCancelled()){ break; } - $this->getLevel()->setBlock($b, $ev->getNewState(), true); + $this->getLevelNonNull()->setBlock($b, $ev->getNewState(), true); }else{ break; } } $this->meta = 0; - $this->getLevel()->setBlock($this, $this, true); + $this->getLevelNonNull()->setBlock($this, $this, true); } $item->pop(); @@ -73,7 +73,7 @@ class Sugarcane extends Flowable{ public function onNearbyBlockChange() : void{ $down = $this->getSide(Vector3::SIDE_DOWN); if($down->isTransparent() and $down->getId() !== self::SUGARCANE_BLOCK){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } @@ -85,17 +85,17 @@ class Sugarcane extends Flowable{ if($this->getSide(Vector3::SIDE_DOWN)->getId() !== self::SUGARCANE_BLOCK){ if($this->meta === 0x0F){ for($y = 1; $y < 3; ++$y){ - $b = $this->getLevel()->getBlockAt($this->x, $this->y + $y, $this->z); + $b = $this->getLevelNonNull()->getBlockAt($this->x, $this->y + $y, $this->z); if($b->getId() === self::AIR){ - $this->getLevel()->setBlock($b, BlockFactory::get(Block::SUGARCANE_BLOCK), true); + $this->getLevelNonNull()->setBlock($b, BlockFactory::get(Block::SUGARCANE_BLOCK), true); break; } } $this->meta = 0; - $this->getLevel()->setBlock($this, $this, true); + $this->getLevelNonNull()->setBlock($this, $this, true); }else{ ++$this->meta; - $this->getLevel()->setBlock($this, $this, true); + $this->getLevelNonNull()->setBlock($this, $this, true); } } } @@ -103,7 +103,7 @@ class Sugarcane extends Flowable{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $down = $this->getSide(Vector3::SIDE_DOWN); if($down->getId() === self::SUGARCANE_BLOCK){ - $this->getLevel()->setBlock($blockReplace, BlockFactory::get(Block::SUGARCANE_BLOCK), true); + $this->getLevelNonNull()->setBlock($blockReplace, BlockFactory::get(Block::SUGARCANE_BLOCK), true); return true; }elseif($down->getId() === self::GRASS or $down->getId() === self::DIRT or $down->getId() === self::SAND){ @@ -112,7 +112,7 @@ class Sugarcane extends Flowable{ $block2 = $down->getSide(Vector3::SIDE_WEST); $block3 = $down->getSide(Vector3::SIDE_EAST); if(($block0 instanceof Water) or ($block1 instanceof Water) or ($block2 instanceof Water) or ($block3 instanceof Water)){ - $this->getLevel()->setBlock($blockReplace, BlockFactory::get(Block::SUGARCANE_BLOCK), true); + $this->getLevelNonNull()->setBlock($blockReplace, BlockFactory::get(Block::SUGARCANE_BLOCK), true); return true; } diff --git a/src/pocketmine/block/TNT.php b/src/pocketmine/block/TNT.php index 44cfb6fbc..6c5f8252f 100644 --- a/src/pocketmine/block/TNT.php +++ b/src/pocketmine/block/TNT.php @@ -78,13 +78,13 @@ class TNT extends Solid{ * @return void */ public function ignite(int $fuse = 80){ - $this->getLevel()->setBlock($this, BlockFactory::get(Block::AIR), true); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::AIR), true); $mot = (new Random())->nextSignedFloat() * M_PI * 2; $nbt = Entity::createBaseNBT($this->add(0.5, 0, 0.5), new Vector3(-sin($mot) * 0.02, 0.2, -cos($mot) * 0.02)); $nbt->setShort("Fuse", $fuse); - $tnt = Entity::createEntity("PrimedTNT", $this->getLevel(), $nbt); + $tnt = Entity::createEntity("PrimedTNT", $this->getLevelNonNull(), $nbt); if($tnt !== null){ $tnt->spawnToAll(); diff --git a/src/pocketmine/block/TallGrass.php b/src/pocketmine/block/TallGrass.php index 7bcd619fa..a072cfcba 100644 --- a/src/pocketmine/block/TallGrass.php +++ b/src/pocketmine/block/TallGrass.php @@ -53,7 +53,7 @@ class TallGrass extends Flowable{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $down = $this->getSide(Vector3::SIDE_DOWN)->getId(); if($down === self::GRASS or $down === self::DIRT){ - $this->getLevel()->setBlock($blockReplace, $this, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true); return true; } @@ -63,7 +63,7 @@ class TallGrass extends Flowable{ public function onNearbyBlockChange() : void{ if($this->getSide(Vector3::SIDE_DOWN)->isTransparent()){ //Replace with common break method - $this->getLevel()->setBlock($this, BlockFactory::get(Block::AIR), true, true); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::AIR), true, true); } } diff --git a/src/pocketmine/block/Torch.php b/src/pocketmine/block/Torch.php index e88767cfd..d95f3b119 100644 --- a/src/pocketmine/block/Torch.php +++ b/src/pocketmine/block/Torch.php @@ -57,7 +57,7 @@ class Torch extends Flowable{ $face = $faces[$meta] ?? Vector3::SIDE_DOWN; if($this->getSide($face)->isTransparent() and !($face === Vector3::SIDE_DOWN and ($below->getId() === self::FENCE or $below->getId() === self::COBBLESTONE_WALL))){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } @@ -73,12 +73,12 @@ class Torch extends Flowable{ Vector3::SIDE_EAST => 1 ]; $this->meta = $faces[$face]; - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; }elseif(!$below->isTransparent() or $below->getId() === self::FENCE or $below->getId() === self::COBBLESTONE_WALL){ $this->meta = 0; - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; } diff --git a/src/pocketmine/block/Trapdoor.php b/src/pocketmine/block/Trapdoor.php index 25c747cbd..0cbbd1564 100644 --- a/src/pocketmine/block/Trapdoor.php +++ b/src/pocketmine/block/Trapdoor.php @@ -136,7 +136,7 @@ class Trapdoor extends Transparent{ if(($clickVector->y > 0.5 and $face !== self::SIDE_UP) or $face === self::SIDE_DOWN){ $this->meta |= self::MASK_UPPER; //top half of block } - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; } @@ -146,7 +146,7 @@ class Trapdoor extends Transparent{ public function onActivate(Item $item, Player $player = null) : bool{ $this->meta ^= self::MASK_OPENED; - $this->getLevel()->setBlock($this, $this, true); + $this->getLevelNonNull()->setBlock($this, $this, true); $this->level->addSound(new DoorSound($this)); return true; } diff --git a/src/pocketmine/block/Vine.php b/src/pocketmine/block/Vine.php index dadc7a329..5473fd247 100644 --- a/src/pocketmine/block/Vine.php +++ b/src/pocketmine/block/Vine.php @@ -150,7 +150,7 @@ class Vine extends Flowable{ $this->meta |= $blockReplace->meta; } - $this->getLevel()->setBlock($blockReplace, $this, true, true); + $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); return true; } diff --git a/src/pocketmine/block/WallBanner.php b/src/pocketmine/block/WallBanner.php index a0a16ea44..90a9b0271 100644 --- a/src/pocketmine/block/WallBanner.php +++ b/src/pocketmine/block/WallBanner.php @@ -33,7 +33,7 @@ class WallBanner extends StandingBanner{ public function onNearbyBlockChange() : void{ if($this->getSide($this->meta ^ 0x01)->getId() === self::AIR){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } } diff --git a/src/pocketmine/block/WallSign.php b/src/pocketmine/block/WallSign.php index 58b0d8a82..9714ad5eb 100644 --- a/src/pocketmine/block/WallSign.php +++ b/src/pocketmine/block/WallSign.php @@ -33,7 +33,7 @@ class WallSign extends SignPost{ public function onNearbyBlockChange() : void{ if($this->getSide($this->meta ^ 0x01)->getId() === self::AIR){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } } diff --git a/src/pocketmine/block/Water.php b/src/pocketmine/block/Water.php index 8f490c4aa..b893ba9b2 100644 --- a/src/pocketmine/block/Water.php +++ b/src/pocketmine/block/Water.php @@ -75,8 +75,8 @@ class Water extends Liquid{ } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ - $ret = $this->getLevel()->setBlock($this, $this, true, false); - $this->getLevel()->scheduleDelayedBlockUpdate($this, $this->tickRate()); + $ret = $this->getLevelNonNull()->setBlock($this, $this, true, false); + $this->getLevelNonNull()->scheduleDelayedBlockUpdate($this, $this->tickRate()); return $ret; } diff --git a/src/pocketmine/block/WaterLily.php b/src/pocketmine/block/WaterLily.php index 8d4ebc7cf..ea9010fc2 100644 --- a/src/pocketmine/block/WaterLily.php +++ b/src/pocketmine/block/WaterLily.php @@ -59,7 +59,7 @@ class WaterLily extends Flowable{ if($blockClicked instanceof Water){ $up = $blockClicked->getSide(Vector3::SIDE_UP); if($up->getId() === Block::AIR){ - $this->getLevel()->setBlock($up, $this, true, true); + $this->getLevelNonNull()->setBlock($up, $this, true, true); return true; } } @@ -69,7 +69,7 @@ class WaterLily extends Flowable{ public function onNearbyBlockChange() : void{ if(!($this->getSide(Vector3::SIDE_DOWN) instanceof Water)){ - $this->getLevel()->useBreakOn($this); + $this->getLevelNonNull()->useBreakOn($this); } } diff --git a/src/pocketmine/block/Wood.php b/src/pocketmine/block/Wood.php index 2c309aed6..707256c6e 100644 --- a/src/pocketmine/block/Wood.php +++ b/src/pocketmine/block/Wood.php @@ -56,7 +56,7 @@ class Wood extends Solid{ public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ $this->meta = PillarRotationHelper::getMetaFromFace($this->meta, $face); - return $this->getLevel()->setBlock($blockReplace, $this, true, true); + return $this->getLevelNonNull()->setBlock($blockReplace, $this, true, true); } public function getVariantBitmask() : int{ diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 8838433ba..d1bdb9e98 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -1743,7 +1743,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{ } if($pos instanceof Position and $pos->level !== null and $pos->level !== $this->level){ - if(!$this->switchLevel($pos->getLevel())){ + if(!$this->switchLevel($pos->getLevelNonNull())){ return false; } } @@ -1849,7 +1849,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{ $pitch = $pitch ?? $pos->pitch; } $from = Position::fromObject($this, $this->level); - $to = Position::fromObject($pos, $pos instanceof Position ? $pos->getLevel() : $this->level); + $to = Position::fromObject($pos, $pos instanceof Position ? $pos->getLevelNonNull() : $this->level); $ev = new EntityTeleportEvent($this, $from, $to); $ev->call(); if($ev->isCancelled()){ diff --git a/src/pocketmine/entity/Living.php b/src/pocketmine/entity/Living.php index 9d9f0e1b8..79e4cfabc 100644 --- a/src/pocketmine/entity/Living.php +++ b/src/pocketmine/entity/Living.php @@ -191,7 +191,7 @@ abstract class Living extends Entity implements Damageable{ public function hasLineOfSight(Entity $entity) : bool{ //TODO: head height return true; - //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; + //return $this->getLevelNonNull()->rayTraceBlocks(Vector3::createVector($this->x, $this->y + $this->height, $this->z), Vector3::createVector($entity->x, $entity->y + $entity->height, $entity->z)) === null; } /** @@ -615,7 +615,7 @@ abstract class Living extends Entity implements Damageable{ $ev = new EntityDeathEvent($this, $this->getDrops(), $this->getXpDropAmount()); $ev->call(); foreach($ev->getDrops() as $item){ - $this->getLevel()->dropItem($this, $item); + $this->getLevelNonNull()->dropItem($this, $item); } //TODO: check death conditions (must have been damaged by player < 5 seconds from death) diff --git a/src/pocketmine/entity/object/FallingBlock.php b/src/pocketmine/entity/object/FallingBlock.php index c96723a61..c44d25d98 100644 --- a/src/pocketmine/entity/object/FallingBlock.php +++ b/src/pocketmine/entity/object/FallingBlock.php @@ -98,7 +98,7 @@ class FallingBlock extends Entity{ $hasUpdate = parent::entityBaseTick($tickDiff); if(!$this->isFlaggedForDespawn()){ - $pos = Position::fromObject($this->add(-$this->width / 2, $this->height, -$this->width / 2)->floor(), $this->getLevel()); + $pos = Position::fromObject($this->add(-$this->width / 2, $this->height, -$this->width / 2)->floor(), $this->getLevelNonNull()); $this->block->position($pos); @@ -113,12 +113,12 @@ class FallingBlock extends Entity{ $block = $this->level->getBlock($pos); if(($block->isTransparent() and !$block->canBeReplaced()) or ($this->onGround and abs($this->y - $this->getFloorY()) > 0.001)){ //FIXME: anvils are supposed to destroy torches - $this->getLevel()->dropItem($this, ItemFactory::get($this->getBlock(), $this->getDamage())); + $this->getLevelNonNull()->dropItem($this, ItemFactory::get($this->getBlock(), $this->getDamage())); }else{ $ev = new EntityBlockChangeEvent($this, $block, $blockTarget ?? $this->block); $ev->call(); if(!$ev->isCancelled()){ - $this->getLevel()->setBlock($pos, $ev->getTo(), true); + $this->getLevelNonNull()->setBlock($pos, $ev->getTo(), true); } } $hasUpdate = true; diff --git a/src/pocketmine/event/entity/EntityDamageByChildEntityEvent.php b/src/pocketmine/event/entity/EntityDamageByChildEntityEvent.php index 764d844da..1ad1e5b0e 100644 --- a/src/pocketmine/event/entity/EntityDamageByChildEntityEvent.php +++ b/src/pocketmine/event/entity/EntityDamageByChildEntityEvent.php @@ -44,6 +44,6 @@ class EntityDamageByChildEntityEvent extends EntityDamageByEntityEvent{ * Returns the entity which caused the damage, or null if the entity has been killed or closed. */ public function getChild() : ?Entity{ - return $this->getEntity()->getLevel()->getServer()->findEntity($this->childEntityEid); + return $this->getEntity()->getLevelNonNull()->getServer()->findEntity($this->childEntityEid); } } diff --git a/src/pocketmine/event/entity/EntityDamageByEntityEvent.php b/src/pocketmine/event/entity/EntityDamageByEntityEvent.php index 3bef428ad..28241e6d2 100644 --- a/src/pocketmine/event/entity/EntityDamageByEntityEvent.php +++ b/src/pocketmine/event/entity/EntityDamageByEntityEvent.php @@ -62,7 +62,7 @@ class EntityDamageByEntityEvent extends EntityDamageEvent{ * Returns the attacking entity, or null if the attacker has been killed or closed. */ public function getDamager() : ?Entity{ - return $this->getEntity()->getLevel()->getServer()->findEntity($this->damagerEntityId); + return $this->getEntity()->getLevelNonNull()->getServer()->findEntity($this->damagerEntityId); } public function getKnockBack() : float{ diff --git a/src/pocketmine/inventory/ChestInventory.php b/src/pocketmine/inventory/ChestInventory.php index b7cd3765e..0f45ef431 100644 --- a/src/pocketmine/inventory/ChestInventory.php +++ b/src/pocketmine/inventory/ChestInventory.php @@ -74,7 +74,7 @@ class ChestInventory extends ContainerInventory{ if(count($this->getViewers()) === 1 and $this->getHolder()->isValid()){ //TODO: this crap really shouldn't be managed by the inventory $this->broadcastBlockEventPacket(true); - $this->getHolder()->getLevel()->broadcastLevelSoundEvent($this->getHolder()->add(0.5, 0.5, 0.5), $this->getOpenSound()); + $this->getHolder()->getLevelNonNull()->broadcastLevelSoundEvent($this->getHolder()->add(0.5, 0.5, 0.5), $this->getOpenSound()); } } @@ -82,7 +82,7 @@ class ChestInventory extends ContainerInventory{ if(count($this->getViewers()) === 1 and $this->getHolder()->isValid()){ //TODO: this crap really shouldn't be managed by the inventory $this->broadcastBlockEventPacket(false); - $this->getHolder()->getLevel()->broadcastLevelSoundEvent($this->getHolder()->add(0.5, 0.5, 0.5), $this->getCloseSound()); + $this->getHolder()->getLevelNonNull()->broadcastLevelSoundEvent($this->getHolder()->add(0.5, 0.5, 0.5), $this->getCloseSound()); } parent::onClose($who); } @@ -96,6 +96,6 @@ class ChestInventory extends ContainerInventory{ $pk->z = (int) $holder->z; $pk->eventType = 1; //it's always 1 for a chest $pk->eventData = $isOpen ? 1 : 0; - $holder->getLevel()->broadcastPacketToViewers($holder, $pk); + $holder->getLevelNonNull()->broadcastPacketToViewers($holder, $pk); } } diff --git a/src/pocketmine/inventory/EnderChestInventory.php b/src/pocketmine/inventory/EnderChestInventory.php index 5203a589e..6c8a7c6ce 100644 --- a/src/pocketmine/inventory/EnderChestInventory.php +++ b/src/pocketmine/inventory/EnderChestInventory.php @@ -56,7 +56,7 @@ class EnderChestInventory extends ChestInventory{ */ public function setHolderPosition(EnderChest $enderChest){ $this->holder->setComponents($enderChest->getFloorX(), $enderChest->getFloorY(), $enderChest->getFloorZ()); - $this->holder->setLevel($enderChest->getLevel()); + $this->holder->setLevel($enderChest->getLevelNonNull()); } protected function getOpenSound() : int{ diff --git a/src/pocketmine/inventory/PlayerInventory.php b/src/pocketmine/inventory/PlayerInventory.php index 4fa1ebed7..7d7d159c6 100644 --- a/src/pocketmine/inventory/PlayerInventory.php +++ b/src/pocketmine/inventory/PlayerInventory.php @@ -173,7 +173,7 @@ class PlayerInventory extends BaseInventory{ $this->sendSlot($this->getHeldItemIndex(), $target); } }else{ - $this->getHolder()->getLevel()->getServer()->broadcastPacket($target, $pk); + $this->getHolder()->getLevelNonNull()->getServer()->broadcastPacket($target, $pk); if(in_array($this->getHolder(), $target, true)){ $this->sendSlot($this->getHeldItemIndex(), $this->getHolder()); } diff --git a/src/pocketmine/item/ChorusFruit.php b/src/pocketmine/item/ChorusFruit.php index f6744a167..d66192aa8 100644 --- a/src/pocketmine/item/ChorusFruit.php +++ b/src/pocketmine/item/ChorusFruit.php @@ -50,11 +50,10 @@ class ChorusFruit extends Food{ } public function onConsume(Living $consumer){ - $level = $consumer->getLevel(); - assert($level !== null); + $level = $consumer->getLevelNonNull(); $minX = $consumer->getFloorX() - 8; - $minY = min($consumer->getFloorY(), $consumer->getLevel()->getWorldHeight()) - 8; + $minY = min($consumer->getFloorY(), $consumer->getLevelNonNull()->getWorldHeight()) - 8; $minZ = $consumer->getFloorZ() - 8; $maxX = $minX + 16; diff --git a/src/pocketmine/item/PaintingItem.php b/src/pocketmine/item/PaintingItem.php index 115dba494..208e0d7d2 100644 --- a/src/pocketmine/item/PaintingItem.php +++ b/src/pocketmine/item/PaintingItem.php @@ -93,7 +93,7 @@ class PaintingItem extends Item{ $nbt->setInt("TileY", $blockClicked->getFloorY()); $nbt->setInt("TileZ", $blockClicked->getFloorZ()); - $entity = Entity::createEntity("Painting", $blockReplace->getLevel(), $nbt); + $entity = Entity::createEntity("Painting", $blockReplace->getLevelNonNull(), $nbt); if($entity instanceof Entity){ $this->pop(); diff --git a/src/pocketmine/level/Explosion.php b/src/pocketmine/level/Explosion.php index 4af974365..72101a075 100644 --- a/src/pocketmine/level/Explosion.php +++ b/src/pocketmine/level/Explosion.php @@ -74,7 +74,7 @@ class Explosion{ throw new \InvalidArgumentException("Position does not have a valid world"); } $this->source = $center; - $this->level = $center->getLevel(); + $this->level = $center->getLevelNonNull(); if($size <= 0){ throw new \InvalidArgumentException("Explosion radius must be greater than 0, got $size"); diff --git a/src/pocketmine/level/Location.php b/src/pocketmine/level/Location.php index a3394e31a..15362ac3e 100644 --- a/src/pocketmine/level/Location.php +++ b/src/pocketmine/level/Location.php @@ -75,7 +75,7 @@ class Location extends Position{ } public function __toString(){ - return "Location (level=" . ($this->isValid() ? $this->getLevel()->getName() : "null") . ", x=$this->x, y=$this->y, z=$this->z, yaw=$this->yaw, pitch=$this->pitch)"; + return "Location (level=" . ($this->isValid() ? $this->getLevelNonNull()->getName() : "null") . ", x=$this->x, y=$this->y, z=$this->z, yaw=$this->yaw, pitch=$this->pitch)"; } public function equals(Vector3 $v) : bool{ diff --git a/src/pocketmine/level/Position.php b/src/pocketmine/level/Position.php index 19e4fcdb5..29832f54d 100644 --- a/src/pocketmine/level/Position.php +++ b/src/pocketmine/level/Position.php @@ -25,6 +25,7 @@ namespace pocketmine\level; use pocketmine\math\Vector3; use pocketmine\utils\MainLogger; +use pocketmine\utils\AssumptionFailedError; use function assert; class Position extends Vector3{ @@ -71,6 +72,19 @@ class Position extends Vector3{ return $this->level; } + /** + * Returns the position's world if valid. Throws an error if the world is unexpectedly null. + * + * @throws AssumptionFailedError + */ + public function getLevelNonNull() : Level{ + $world = $this->getLevel(); + if($world === null){ + throw new AssumptionFailedError("Position world is null"); + } + return $world; + } + /** * Sets the target Level of the position. * @@ -112,7 +126,7 @@ class Position extends Vector3{ } public function __toString(){ - return "Position(level=" . ($this->isValid() ? $this->getLevel()->getName() : "null") . ",x=" . $this->x . ",y=" . $this->y . ",z=" . $this->z . ")"; + return "Position(level=" . ($this->isValid() ? $this->getLevelNonNull()->getName() : "null") . ",x=" . $this->x . ",y=" . $this->y . ",z=" . $this->z . ")"; } public function equals(Vector3 $v) : bool{ diff --git a/src/pocketmine/tile/Chest.php b/src/pocketmine/tile/Chest.php index 8066d69d9..41468fb1c 100644 --- a/src/pocketmine/tile/Chest.php +++ b/src/pocketmine/tile/Chest.php @@ -121,7 +121,7 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{ * @return void */ protected function checkPairing(){ - if($this->isPaired() and !$this->getLevel()->isInLoadedTerrain(new Vector3($this->pairX, $this->y, $this->pairZ))){ + if($this->isPaired() and !$this->getLevelNonNull()->isInLoadedTerrain(new Vector3($this->pairX, $this->y, $this->pairZ))){ //paired to a tile in an unloaded chunk $this->doubleInventory = null; @@ -160,7 +160,7 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{ public function getPair() : ?Chest{ if($this->isPaired()){ - $tile = $this->getLevel()->getTileAt($this->pairX, $this->y, $this->pairZ); + $tile = $this->getLevelNonNull()->getTileAt($this->pairX, $this->y, $this->pairZ); if($tile instanceof Chest){ return $tile; } diff --git a/src/pocketmine/tile/Furnace.php b/src/pocketmine/tile/Furnace.php index 984141c7a..920b93249 100644 --- a/src/pocketmine/tile/Furnace.php +++ b/src/pocketmine/tile/Furnace.php @@ -148,7 +148,7 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ $this->maxTime = $this->burnTime = $ev->getBurnTime(); if($this->getBlock()->getId() === Block::FURNACE){ - $this->getLevel()->setBlock($this, BlockFactory::get(Block::BURNING_FURNACE, $this->getBlock()->getDamage()), true); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::BURNING_FURNACE, $this->getBlock()->getDamage()), true); } if($this->burnTime > 0 and $ev->isBurning()){ @@ -211,7 +211,7 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ $ret = true; }else{ if($this->getBlock()->getId() === Block::BURNING_FURNACE){ - $this->getLevel()->setBlock($this, BlockFactory::get(Block::FURNACE, $this->getBlock()->getDamage()), true); + $this->getLevelNonNull()->setBlock($this, BlockFactory::get(Block::FURNACE, $this->getBlock()->getDamage()), true); } $this->burnTime = $this->cookTime = $this->maxTime = 0; } diff --git a/src/pocketmine/tile/Tile.php b/src/pocketmine/tile/Tile.php index ac044a840..6ade66bb9 100644 --- a/src/pocketmine/tile/Tile.php +++ b/src/pocketmine/tile/Tile.php @@ -168,7 +168,7 @@ abstract class Tile extends Position{ parent::__construct($nbt->getInt(self::TAG_X), $nbt->getInt(self::TAG_Y), $nbt->getInt(self::TAG_Z), $level); $this->readSaveData($nbt); - $this->getLevel()->addTile($this); + $this->getLevelNonNull()->addTile($this); } public function getId() : int{ diff --git a/src/pocketmine/utils/AssumptionFailedError.php b/src/pocketmine/utils/AssumptionFailedError.php new file mode 100644 index 000000000..df7d2d464 --- /dev/null +++ b/src/pocketmine/utils/AssumptionFailedError.php @@ -0,0 +1,33 @@ +