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.
This commit is contained in:
Dylan K. Taylor 2019-05-24 17:21:44 +01:00
parent 9ce1e29a17
commit 3ea8da2dd3
10 changed files with 32 additions and 32 deletions

View File

@ -1320,7 +1320,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
* @return bool * @return bool
*/ */
public function isSurvival(bool $literal = false) : 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 * @return bool
*/ */
public function isCreative(bool $literal = false) : 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 * @return bool
*/ */
public function isAdventure(bool $literal = false) : 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 * @return bool
*/ */
public function isSpectator() : 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 * @return bool
*/ */
public function hasFiniteResources() : 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{ public function isFireProof() : bool{
@ -1759,7 +1759,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
} }
$result = $item->onClickAir($this, $directionVector); $result = $item->onClickAir($this, $directionVector);
if($result === ItemUseResult::FAIL()){ if($result->equals(ItemUseResult::FAIL())){
return false; return false;
} }
@ -1819,7 +1819,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
} }
$result = $item->onReleaseUsing($this); $result = $item->onReleaseUsing($this);
if($result === ItemUseResult::SUCCESS()){ if($result->equals(ItemUseResult::SUCCESS())){
$this->resetItemCooldown($item); $this->resetItemCooldown($item);
$this->inventory->setItemInHand($item); $this->inventory->setItemInHand($item);
return true; return true;

View File

@ -1554,7 +1554,7 @@ class Server{
} }
} }
if($type === PluginLoadOrder::POSTWORLD()){ if($type->equals(PluginLoadOrder::POSTWORLD())){
$this->commandMap->registerServerAliases(); $this->commandMap->registerServerAliases();
DefaultPermissions::registerCorePermissions(); DefaultPermissions::registerCorePermissions();
} }

View File

@ -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{ 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; $this->facing = $face;
return parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player); return parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player);
} }
@ -96,7 +96,7 @@ class CocoaBlock extends Transparent{
public function onNearbyBlockChange() : void{ public function onNearbyBlockChange() : void{
$side = $this->getSide(Facing::opposite($this->facing)); $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); $this->world->useBreakOn($this);
} }
} }

View File

@ -125,7 +125,7 @@ class Leaves extends Transparent{
if(mt_rand(1, 20) === 1){ //Saplings if(mt_rand(1, 20) === 1){ //Saplings
$drops[] = ItemFactory::get(Item::SAPLING, $this->treeType->getMagicNumber()); $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); $drops[] = ItemFactory::get(Item::APPLE);
} }

View File

@ -43,12 +43,12 @@ abstract class Slab extends Transparent{
} }
public function getId() : int{ 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{ protected function writeStateToMeta() : int{
if($this->slabType !== SlabType::DOUBLE()){ if(!$this->slabType->equals(SlabType::DOUBLE())){
return ($this->slabType === SlabType::TOP() ? BlockLegacyMetadata::SLAB_FLAG_UPPER : 0); return ($this->slabType->equals(SlabType::TOP()) ? BlockLegacyMetadata::SLAB_FLAG_UPPER : 0);
} }
return 0; return 0;
} }
@ -66,7 +66,7 @@ abstract class Slab extends Transparent{
} }
public function isTransparent() : bool{ 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; return true;
} }
if($blockReplace instanceof Slab and $blockReplace->slabType !== SlabType::DOUBLE() and $blockReplace->isSameType($this)){ if($blockReplace instanceof Slab and !$blockReplace->slabType->equals(SlabType::DOUBLE()) and $blockReplace->isSameType($this)){
if($blockReplace->slabType === SlabType::TOP()){ //Trying to combine with top slab if($blockReplace->slabType->equals(SlabType::TOP())){ //Trying to combine with top slab
return $clickVector->y <= 0.5 or (!$isClickedBlock and $face === Facing::UP); return $clickVector->y <= 0.5 or (!$isClickedBlock and $face === Facing::UP);
}else{ }else{
return $clickVector->y >= 0.5 or (!$isClickedBlock and $face === Facing::DOWN); 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{ 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 ( if($blockReplace instanceof Slab and !$blockReplace->slabType->equals(SlabType::DOUBLE()) and $blockReplace->isSameType($this) and (
($blockReplace->slabType === SlabType::TOP() and ($clickVector->y <= 0.5 or $face === Facing::UP)) or ($blockReplace->slabType->equals(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)) ($blockReplace->slabType->equals(SlabType::BOTTOM()) and ($clickVector->y >= 0.5 or $face === Facing::DOWN))
)){ )){
//Clicked in empty half of existing slab //Clicked in empty half of existing slab
$this->slabType = SlabType::DOUBLE(); $this->slabType = SlabType::DOUBLE();
@ -119,13 +119,13 @@ abstract class Slab extends Transparent{
} }
protected function recalculateBoundingBox() : ?AxisAlignedBB{ protected function recalculateBoundingBox() : ?AxisAlignedBB{
if($this->slabType === SlabType::DOUBLE()){ if($this->slabType->equals(SlabType::DOUBLE())){
return parent::recalculateBoundingBox(); 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{ 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)];
} }
} }

View File

@ -72,7 +72,7 @@ class GamemodeCommand extends VanillaCommand{
} }
$target->setGamemode($gameMode); $target->setGamemode($gameMode);
if($gameMode !== $target->getGamemode()){ if(!$gameMode->equals($target->getGamemode())){
$sender->sendMessage("Game mode change for " . $target->getName() . " failed!"); $sender->sendMessage("Game mode change for " . $target->getName() . " failed!");
}else{ }else{
if($target === $sender){ if($target === $sender){

View File

@ -852,7 +852,7 @@ class NetworkSession{
* @return int * @return int
*/ */
public static function getClientFriendlyGamemode(GameMode $gamemode) : int{ public static function getClientFriendlyGamemode(GameMode $gamemode) : int{
if($gamemode === GameMode::SPECTATOR()){ if($gamemode->equals(GameMode::SPECTATOR())){
return GameMode::CREATIVE()->getMagicNumber(); return GameMode::CREATIVE()->getMagicNumber();
} }

View File

@ -1788,8 +1788,8 @@ class World implements ChunkManager, Metadatable{
if(!$player->isSneaking()){ if(!$player->isSneaking()){
$result = $item->onActivate($player, $blockReplace, $blockClicked, $face, $clickVector); $result = $item->onActivate($player, $blockReplace, $blockClicked, $face, $clickVector);
if($result !== ItemUseResult::NONE()){ if(!$result->equals(ItemUseResult::NONE())){
return $result === ItemUseResult::SUCCESS(); return $result->equals(ItemUseResult::SUCCESS());
} }
} }
}else{ }else{

View File

@ -48,7 +48,7 @@ class ForestBiome extends GrassyBiome{
$this->setElevation(63, 81); $this->setElevation(63, 81);
if($type === TreeType::BIRCH()){ if($this->type->equals(TreeType::BIRCH())){
$this->temperature = 0.6; $this->temperature = 0.6;
$this->rainfall = 0.5; $this->rainfall = 0.5;
}else{ }else{

View File

@ -64,17 +64,17 @@ abstract class Tree{
/** @var null|Tree $tree */ /** @var null|Tree $tree */
$tree = null; $tree = null;
$type = $type ?? TreeType::OAK(); $type = $type ?? TreeType::OAK();
if($type === TreeType::SPRUCE()){ if($type->equals(TreeType::SPRUCE())){
$tree = new SpruceTree(); $tree = new SpruceTree();
}elseif($type === TreeType::BIRCH()){ }elseif($type->equals(TreeType::BIRCH())){
if($random->nextBoundedInt(39) === 0){ if($random->nextBoundedInt(39) === 0){
$tree = new BirchTree(true); $tree = new BirchTree(true);
}else{ }else{
$tree = new BirchTree(); $tree = new BirchTree();
} }
}elseif($type === TreeType::JUNGLE()){ }elseif($type->equals(TreeType::JUNGLE())){
$tree = new JungleTree(); $tree = new JungleTree();
}elseif($type === TreeType::OAK()){ //default }elseif($type->equals(TreeType::OAK())){ //default
$tree = new OakTree(); $tree = new OakTree();
/*if($random->nextRange(0, 9) === 0){ /*if($random->nextRange(0, 9) === 0){
$tree = new BigTree(); $tree = new BigTree();