From 3ea8da2dd35490226e61ec9791e8a0809cb85e87 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 24 May 2019 17:21:44 +0100 Subject: [PATCH] Use EnumTrait->equals() instead of direct comparison It's not guaranteed that objects provided are the same as those in the enum registry, so they can't be directly compared. Implementing comparison with === would require some kind of __equals() implementation or an extension to hook into such functionality. --- src/pocketmine/Player.php | 14 +++++------ src/pocketmine/Server.php | 2 +- src/pocketmine/block/CocoaBlock.php | 4 ++-- src/pocketmine/block/Leaves.php | 2 +- src/pocketmine/block/Slab.php | 24 +++++++++---------- .../command/defaults/GamemodeCommand.php | 2 +- .../network/mcpe/NetworkSession.php | 2 +- src/pocketmine/world/World.php | 4 ++-- src/pocketmine/world/biome/ForestBiome.php | 2 +- .../world/generator/object/Tree.php | 8 +++---- 10 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index e586786b4..ba6559837 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -1320,7 +1320,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, * @return bool */ public function isSurvival(bool $literal = false) : bool{ - return $this->gamemode === GameMode::SURVIVAL() or (!$literal and $this->gamemode === GameMode::ADVENTURE()); + return $this->gamemode->equals(GameMode::SURVIVAL()) or (!$literal and $this->gamemode->equals(GameMode::ADVENTURE())); } /** @@ -1332,7 +1332,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, * @return bool */ public function isCreative(bool $literal = false) : bool{ - return $this->gamemode === GameMode::CREATIVE() or (!$literal and $this->gamemode === GameMode::SPECTATOR()); + return $this->gamemode->equals(GameMode::CREATIVE()) or (!$literal and $this->gamemode->equals(GameMode::SPECTATOR())); } /** @@ -1344,14 +1344,14 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, * @return bool */ public function isAdventure(bool $literal = false) : bool{ - return $this->gamemode === GameMode::ADVENTURE() or (!$literal and $this->gamemode === GameMode::SPECTATOR()); + return $this->gamemode->equals(GameMode::ADVENTURE()) or (!$literal and $this->gamemode->equals(GameMode::SPECTATOR())); } /** * @return bool */ public function isSpectator() : bool{ - return $this->gamemode === GameMode::SPECTATOR(); + return $this->gamemode->equals(GameMode::SPECTATOR()); } /** @@ -1360,7 +1360,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, * @return bool */ public function hasFiniteResources() : bool{ - return $this->gamemode === GameMode::SURVIVAL() or $this->gamemode === GameMode::ADVENTURE(); + return $this->gamemode->equals(GameMode::SURVIVAL()) or $this->gamemode->equals(GameMode::ADVENTURE()); } public function isFireProof() : bool{ @@ -1759,7 +1759,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, } $result = $item->onClickAir($this, $directionVector); - if($result === ItemUseResult::FAIL()){ + if($result->equals(ItemUseResult::FAIL())){ return false; } @@ -1819,7 +1819,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, } $result = $item->onReleaseUsing($this); - if($result === ItemUseResult::SUCCESS()){ + if($result->equals(ItemUseResult::SUCCESS())){ $this->resetItemCooldown($item); $this->inventory->setItemInHand($item); return true; diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 3669ef713..2a5a534f3 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -1554,7 +1554,7 @@ class Server{ } } - if($type === PluginLoadOrder::POSTWORLD()){ + if($type->equals(PluginLoadOrder::POSTWORLD())){ $this->commandMap->registerServerAliases(); DefaultPermissions::registerCorePermissions(); } diff --git a/src/pocketmine/block/CocoaBlock.php b/src/pocketmine/block/CocoaBlock.php index b2ccd8816..a7d5a876e 100644 --- a/src/pocketmine/block/CocoaBlock.php +++ b/src/pocketmine/block/CocoaBlock.php @@ -73,7 +73,7 @@ class CocoaBlock extends Transparent{ } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if(Facing::axis($face) !== Facing::AXIS_Y and $blockClicked instanceof Wood and $blockClicked->getTreeType() === TreeType::JUNGLE()){ + if(Facing::axis($face) !== Facing::AXIS_Y and $blockClicked instanceof Wood and $blockClicked->getTreeType()->equals(TreeType::JUNGLE())){ $this->facing = $face; return parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player); } @@ -96,7 +96,7 @@ class CocoaBlock extends Transparent{ public function onNearbyBlockChange() : void{ $side = $this->getSide(Facing::opposite($this->facing)); - if(!($side instanceof Wood) or $side->getTreeType() !== TreeType::JUNGLE()){ + if(!($side instanceof Wood) or !$side->getTreeType()->equals(TreeType::JUNGLE())){ $this->world->useBreakOn($this); } } diff --git a/src/pocketmine/block/Leaves.php b/src/pocketmine/block/Leaves.php index 67e6ad439..55d48de5f 100644 --- a/src/pocketmine/block/Leaves.php +++ b/src/pocketmine/block/Leaves.php @@ -125,7 +125,7 @@ class Leaves extends Transparent{ if(mt_rand(1, 20) === 1){ //Saplings $drops[] = ItemFactory::get(Item::SAPLING, $this->treeType->getMagicNumber()); } - if(($this->treeType === TreeType::OAK() or $this->treeType === TreeType::DARK_OAK()) and mt_rand(1, 200) === 1){ //Apples + if(($this->treeType->equals(TreeType::OAK()) or $this->treeType->equals(TreeType::DARK_OAK())) and mt_rand(1, 200) === 1){ //Apples $drops[] = ItemFactory::get(Item::APPLE); } diff --git a/src/pocketmine/block/Slab.php b/src/pocketmine/block/Slab.php index 57eab2184..598241f4b 100644 --- a/src/pocketmine/block/Slab.php +++ b/src/pocketmine/block/Slab.php @@ -43,12 +43,12 @@ abstract class Slab extends Transparent{ } public function getId() : int{ - return $this->slabType === SlabType::DOUBLE() ? $this->idInfo->getSecondId() : parent::getId(); + return $this->slabType->equals(SlabType::DOUBLE()) ? $this->idInfo->getSecondId() : parent::getId(); } protected function writeStateToMeta() : int{ - if($this->slabType !== SlabType::DOUBLE()){ - return ($this->slabType === SlabType::TOP() ? BlockLegacyMetadata::SLAB_FLAG_UPPER : 0); + if(!$this->slabType->equals(SlabType::DOUBLE())){ + return ($this->slabType->equals(SlabType::TOP()) ? BlockLegacyMetadata::SLAB_FLAG_UPPER : 0); } return 0; } @@ -66,7 +66,7 @@ abstract class Slab extends Transparent{ } public function isTransparent() : bool{ - return $this->slabType !== SlabType::DOUBLE(); + return !$this->slabType->equals(SlabType::DOUBLE()); } /** @@ -93,8 +93,8 @@ abstract class Slab extends Transparent{ return true; } - if($blockReplace instanceof Slab and $blockReplace->slabType !== SlabType::DOUBLE() and $blockReplace->isSameType($this)){ - if($blockReplace->slabType === SlabType::TOP()){ //Trying to combine with top slab + if($blockReplace instanceof Slab and !$blockReplace->slabType->equals(SlabType::DOUBLE()) and $blockReplace->isSameType($this)){ + if($blockReplace->slabType->equals(SlabType::TOP())){ //Trying to combine with top slab return $clickVector->y <= 0.5 or (!$isClickedBlock and $face === Facing::UP); }else{ return $clickVector->y >= 0.5 or (!$isClickedBlock and $face === Facing::DOWN); @@ -105,9 +105,9 @@ abstract class Slab extends Transparent{ } public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($blockReplace instanceof Slab and $blockReplace->slabType !== SlabType::DOUBLE() and $blockReplace->isSameType($this) and ( - ($blockReplace->slabType === SlabType::TOP() and ($clickVector->y <= 0.5 or $face === Facing::UP)) or - ($blockReplace->slabType === SlabType::BOTTOM() and ($clickVector->y >= 0.5 or $face === Facing::DOWN)) + if($blockReplace instanceof Slab and !$blockReplace->slabType->equals(SlabType::DOUBLE()) and $blockReplace->isSameType($this) and ( + ($blockReplace->slabType->equals(SlabType::TOP()) and ($clickVector->y <= 0.5 or $face === Facing::UP)) or + ($blockReplace->slabType->equals(SlabType::BOTTOM()) and ($clickVector->y >= 0.5 or $face === Facing::DOWN)) )){ //Clicked in empty half of existing slab $this->slabType = SlabType::DOUBLE(); @@ -119,13 +119,13 @@ abstract class Slab extends Transparent{ } protected function recalculateBoundingBox() : ?AxisAlignedBB{ - if($this->slabType === SlabType::DOUBLE()){ + if($this->slabType->equals(SlabType::DOUBLE())){ return parent::recalculateBoundingBox(); } - return AxisAlignedBB::one()->trim($this->slabType === SlabType::TOP() ? Facing::DOWN : Facing::UP, 0.5); + return AxisAlignedBB::one()->trim($this->slabType->equals(SlabType::TOP()) ? Facing::DOWN : Facing::UP, 0.5); } public function getDropsForCompatibleTool(Item $item) : array{ - return [$this->asItem()->setCount($this->slabType === SlabType::DOUBLE() ? 2 : 1)]; + return [$this->asItem()->setCount($this->slabType->equals(SlabType::DOUBLE()) ? 2 : 1)]; } } diff --git a/src/pocketmine/command/defaults/GamemodeCommand.php b/src/pocketmine/command/defaults/GamemodeCommand.php index 6f13bab5b..5a0aa01a8 100644 --- a/src/pocketmine/command/defaults/GamemodeCommand.php +++ b/src/pocketmine/command/defaults/GamemodeCommand.php @@ -72,7 +72,7 @@ class GamemodeCommand extends VanillaCommand{ } $target->setGamemode($gameMode); - if($gameMode !== $target->getGamemode()){ + if(!$gameMode->equals($target->getGamemode())){ $sender->sendMessage("Game mode change for " . $target->getName() . " failed!"); }else{ if($target === $sender){ diff --git a/src/pocketmine/network/mcpe/NetworkSession.php b/src/pocketmine/network/mcpe/NetworkSession.php index 969c0ef52..8eeafc2bc 100644 --- a/src/pocketmine/network/mcpe/NetworkSession.php +++ b/src/pocketmine/network/mcpe/NetworkSession.php @@ -852,7 +852,7 @@ class NetworkSession{ * @return int */ public static function getClientFriendlyGamemode(GameMode $gamemode) : int{ - if($gamemode === GameMode::SPECTATOR()){ + if($gamemode->equals(GameMode::SPECTATOR())){ return GameMode::CREATIVE()->getMagicNumber(); } diff --git a/src/pocketmine/world/World.php b/src/pocketmine/world/World.php index 92ff2b764..136e266a7 100644 --- a/src/pocketmine/world/World.php +++ b/src/pocketmine/world/World.php @@ -1788,8 +1788,8 @@ class World implements ChunkManager, Metadatable{ if(!$player->isSneaking()){ $result = $item->onActivate($player, $blockReplace, $blockClicked, $face, $clickVector); - if($result !== ItemUseResult::NONE()){ - return $result === ItemUseResult::SUCCESS(); + if(!$result->equals(ItemUseResult::NONE())){ + return $result->equals(ItemUseResult::SUCCESS()); } } }else{ diff --git a/src/pocketmine/world/biome/ForestBiome.php b/src/pocketmine/world/biome/ForestBiome.php index b3e45cdcc..b791416a5 100644 --- a/src/pocketmine/world/biome/ForestBiome.php +++ b/src/pocketmine/world/biome/ForestBiome.php @@ -48,7 +48,7 @@ class ForestBiome extends GrassyBiome{ $this->setElevation(63, 81); - if($type === TreeType::BIRCH()){ + if($this->type->equals(TreeType::BIRCH())){ $this->temperature = 0.6; $this->rainfall = 0.5; }else{ diff --git a/src/pocketmine/world/generator/object/Tree.php b/src/pocketmine/world/generator/object/Tree.php index eaa914236..c3f2b72aa 100644 --- a/src/pocketmine/world/generator/object/Tree.php +++ b/src/pocketmine/world/generator/object/Tree.php @@ -64,17 +64,17 @@ abstract class Tree{ /** @var null|Tree $tree */ $tree = null; $type = $type ?? TreeType::OAK(); - if($type === TreeType::SPRUCE()){ + if($type->equals(TreeType::SPRUCE())){ $tree = new SpruceTree(); - }elseif($type === TreeType::BIRCH()){ + }elseif($type->equals(TreeType::BIRCH())){ if($random->nextBoundedInt(39) === 0){ $tree = new BirchTree(true); }else{ $tree = new BirchTree(); } - }elseif($type === TreeType::JUNGLE()){ + }elseif($type->equals(TreeType::JUNGLE())){ $tree = new JungleTree(); - }elseif($type === TreeType::OAK()){ //default + }elseif($type->equals(TreeType::OAK())){ //default $tree = new OakTree(); /*if($random->nextRange(0, 9) === 0){ $tree = new BigTree();