From 5443b102579a295a4f074904f4470af56eabfae5 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 21 Dec 2016 14:45:34 +0000 Subject: [PATCH] Use -1 for anydamage and empty string for null NBT, closes #146 --- src/pocketmine/Player.php | 8 +++---- src/pocketmine/inventory/BaseInventory.php | 22 +++++++++---------- src/pocketmine/inventory/CraftingManager.php | 16 +++++++------- src/pocketmine/inventory/ShapelessRecipe.php | 2 +- src/pocketmine/item/Item.php | 20 ++++++++++------- src/pocketmine/item/ItemBlock.php | 6 ++--- .../network/protocol/CraftingDataPacket.php | 2 +- 7 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 2367b3d04..79dee8833 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -2658,7 +2658,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade */ foreach($packet->input as $i => $item){ if($item->getDamage() === -1 or $item->getDamage() === 0xffff){ - $item->setDamage(null); + $item->setDamage(-1); } if($i < 9 and $item->getId() > 0){ @@ -2674,7 +2674,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade $item = $packet->input[$y * 3 + $x]; $ingredient = $recipe->getIngredient($x, $y); if($item->getCount() > 0){ - if($ingredient === null or !$ingredient->deepEquals($item, $ingredient->getDamage() !== null, $ingredient->getCompoundTag() !== null)){ + if($ingredient === null or !$ingredient->deepEquals($item, !$ingredient->hasAnyDamageValue(), $ingredient->hasCompoundTag())){ $canCraft = false; break; } @@ -2689,7 +2689,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade $item = clone $packet->input[$y * 3 + $x]; foreach($needed as $k => $n){ - if($n->deepEquals($item, $n->getDamage() !== null, $n->getCompoundTag() !== null)){ + if($n->deepEquals($item, !$n->hasAnyDamageValue(), $n->hasCompoundTag())){ $remove = min($n->getCount(), $item->getCount()); $n->setCount($n->getCount() - $remove); $item->setCount($item->getCount() - $remove); @@ -2729,7 +2729,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade foreach($ingredients as $ingredient){ $slot = -1; foreach($this->inventory->getContents() as $index => $item){ - if($ingredient->getId() !== 0 and $ingredient->deepEquals($item, $ingredient->getDamage() !== null) and ($item->getCount() - $used[$index]) >= 1){ + if($ingredient->getId() !== 0 and $ingredient->deepEquals($item, !$ingredient->hasAnyDamageValue()) and ($item->getCount() - $used[$index]) >= 1){ $slot = $index; $used[$index]++; break; diff --git a/src/pocketmine/inventory/BaseInventory.php b/src/pocketmine/inventory/BaseInventory.php index 8ed844aaa..2275cb6ae 100644 --- a/src/pocketmine/inventory/BaseInventory.php +++ b/src/pocketmine/inventory/BaseInventory.php @@ -158,8 +158,8 @@ abstract class BaseInventory implements Inventory{ public function contains(Item $item){ $count = max(1, $item->getCount()); - $checkDamage = $item->getDamage() === null ? false : true; - $checkTags = $item->getCompoundTag() === null ? false : true; + $checkDamage = !$item->hasAnyDamageValue(); + $checkTags = $item->hasCompoundTag(); foreach($this->getContents() as $i){ if($item->equals($i, $checkDamage, $checkTags)){ $count -= $i->getCount(); @@ -174,8 +174,8 @@ abstract class BaseInventory implements Inventory{ public function all(Item $item){ $slots = []; - $checkDamage = $item->getDamage() === null ? false : true; - $checkTags = $item->getCompoundTag() === null ? false : true; + $checkDamage = !$item->hasAnyDamageValue(); + $checkTags = $item->hasCompoundTag(); foreach($this->getContents() as $index => $i){ if($item->equals($i, $checkDamage, $checkTags)){ $slots[$index] = $i; @@ -186,8 +186,8 @@ abstract class BaseInventory implements Inventory{ } public function remove(Item $item){ - $checkDamage = $item->getDamage() === null ? false : true; - $checkTags = $item->getCompoundTag() === null ? false : true; + $checkDamage = !$item->hasAnyDamageValue(); + $checkTags = $item->hasCompoundTag(); foreach($this->getContents() as $index => $i){ if($item->equals($i, $checkDamage, $checkTags)){ @@ -198,8 +198,8 @@ abstract class BaseInventory implements Inventory{ public function first(Item $item){ $count = max(1, $item->getCount()); - $checkDamage = $item->getDamage() === null ? false : true; - $checkTags = $item->getCompoundTag() === null ? false : true; + $checkDamage = !$item->hasAnyDamageValue(); + $checkTags = $item->hasCompoundTag(); foreach($this->getContents() as $index => $i){ if($item->equals($i, $checkDamage, $checkTags) and $i->getCount() >= $count){ @@ -222,8 +222,8 @@ abstract class BaseInventory implements Inventory{ public function canAddItem(Item $item){ $item = clone $item; - $checkDamage = $item->getDamage() === null ? false : true; - $checkTags = $item->getCompoundTag() === null ? false : true; + $checkDamage = !$item->hasAnyDamageValue(); + $checkTags = $item->hasCompoundTag(); for($i = 0; $i < $this->getSize(); ++$i){ $slot = $this->getItem($i); if($item->equals($slot, $checkDamage, $checkTags)){ @@ -322,7 +322,7 @@ abstract class BaseInventory implements Inventory{ } foreach($itemSlots as $index => $slot){ - if($slot->equals($item, $slot->getDamage() === null ? false : true, $slot->getCompoundTag() === null ? false : true)){ + if($slot->equals($item, !$slot->hasAnyDamageValue(), $slot->hasCompoundTag())){ $amount = min($item->getCount(), $slot->getCount()); $slot->setCount($slot->getCount() - $amount); $item->setCount($item->getCount() - $amount); diff --git a/src/pocketmine/inventory/CraftingManager.php b/src/pocketmine/inventory/CraftingManager.php index 11fa5fffe..888091775 100644 --- a/src/pocketmine/inventory/CraftingManager.php +++ b/src/pocketmine/inventory/CraftingManager.php @@ -68,7 +68,7 @@ class CraftingManager{ $shape = array_chunk($recipe["input"], $recipe["width"]); foreach($shape as $y => $row){ foreach($row as $x => $ingredient){ - $result->addIngredient($x, $y, Item::get($ingredient["id"], ($ingredient["damage"] < 0 ? null : $ingredient["damage"]), $ingredient["count"], $ingredient["nbt"])); + $result->addIngredient($x, $y, Item::get($ingredient["id"], ($ingredient["damage"] < 0 ? -1 : $ingredient["damage"]), $ingredient["count"], $ingredient["nbt"])); } } $this->registerRecipe($result); @@ -78,7 +78,7 @@ class CraftingManager{ case 3: $result = $recipe["output"]; $resultItem = Item::get($result["id"], $result["damage"], $result["count"], $result["nbt"]); - $this->registerRecipe(new FurnaceRecipe($resultItem, Item::get($recipe["inputId"], $recipe["inputDamage"] ?? null, 1))); + $this->registerRecipe(new FurnaceRecipe($resultItem, Item::get($recipe["inputId"], $recipe["inputDamage"] ?? -1, 1))); break; default: break; @@ -110,7 +110,7 @@ class CraftingManager{ */ public function getRecipe(UUID $id){ $index = $id->toBinary(); - return isset($this->recipes[$index]) ? $this->recipes[$index] : null; + return $this->recipes[$index] ?? null; } /** @@ -154,7 +154,7 @@ class CraftingManager{ foreach($v as $item){ if($item !== null){ /** @var Item $item */ - $hash .= $item->getId() . ":" . ($item->getDamage() === null ? "?" : $item->getDamage()) . "x" . $item->getCount() . ","; + $hash .= $item->getId() . ":" . ($item->hasAnyDamageValue() ? "?" : $item->getDamage()) . "x" . $item->getCount() . ","; } } @@ -174,7 +174,7 @@ class CraftingManager{ $ingredients = $recipe->getIngredientList(); usort($ingredients, [$this, "sort"]); foreach($ingredients as $item){ - $hash .= $item->getId() . ":" . ($item->getDamage() === null ? "?" : $item->getDamage()) . "x" . $item->getCount() . ","; + $hash .= $item->getId() . ":" . ($item->hasAnyDamageValue() ? "?" : $item->getDamage()) . "x" . $item->getCount() . ","; } $this->recipeLookup[$result->getId() . ":" . $result->getDamage()][$hash] = $recipe; } @@ -184,7 +184,7 @@ class CraftingManager{ */ public function registerFurnaceRecipe(FurnaceRecipe $recipe){ $input = $recipe->getInput(); - $this->furnaceRecipes[$input->getId() . ":" . ($input->getDamage() === null ? "?" : $input->getDamage())] = $recipe; + $this->furnaceRecipes[$input->getId() . ":" . ($input->hasAnyDamageValue() ? "?" : $input->getDamage())] = $recipe; } /** @@ -200,7 +200,7 @@ class CraftingManager{ $ingredients = $recipe->getIngredientList(); usort($ingredients, [$this, "sort"]); foreach($ingredients as $item){ - $hash .= $item->getId() . ":" . ($item->getDamage() === null ? "?" : $item->getDamage()) . "x" . $item->getCount() . ","; + $hash .= $item->getId() . ":" . ($item->hasAnyDamageValue() ? "?" : $item->getDamage()) . "x" . $item->getCount() . ","; } if(isset($this->recipeLookup[$idx][$hash])){ @@ -217,7 +217,7 @@ class CraftingManager{ foreach($ingredients as $item){ $amount = $item->getCount(); foreach($checkInput as $k => $checkItem){ - if($checkItem->equals($item, $checkItem->getDamage() === null ? false : true, $checkItem->getCompoundTag() === null ? false : true)){ + if($checkItem->equals($item, !$checkItem->hasAnyDamageValue(), $checkItem->hasCompoundTag())){ $remove = min($checkItem->getCount(), $amount); $checkItem->setCount($checkItem->getCount() - $remove); if($checkItem->getCount() === 0){ diff --git a/src/pocketmine/inventory/ShapelessRecipe.php b/src/pocketmine/inventory/ShapelessRecipe.php index 99b0778c5..8e5cf8375 100644 --- a/src/pocketmine/inventory/ShapelessRecipe.php +++ b/src/pocketmine/inventory/ShapelessRecipe.php @@ -87,7 +87,7 @@ class ShapelessRecipe implements Recipe{ if($item->getCount() <= 0){ break; } - if($ingredient->equals($item, $item->getDamage() === null ? false : true, $item->getCompoundTag() === null ? false : true)){ + if($ingredient->equals($item, !$item->hasAnyDamageValue(), $item->hasCompoundTag())){ unset($this->ingredients[$index]); $item->setCount($item->getCount() - 1); } diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index fe9fdfed4..8a4c8cf62 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -339,7 +339,7 @@ class Item implements ItemIds, \JsonSerializable{ public function __construct(int $id, $meta = 0, int $count = 1, string $name = "Unknown"){ $this->id = $id & 0xffff; - $this->meta = $meta !== null ? $meta & 0xffff : null; + $this->meta = $meta !== -1 ? $meta & 0xffff : -1; $this->count = $count; $this->name = $name; if(!isset($this->block) and $this->id <= 0xff and isset(Block::$list[$this->id])){ @@ -352,7 +352,7 @@ class Item implements ItemIds, \JsonSerializable{ if($tags instanceof CompoundTag){ $this->setNamedTag($tags); }else{ - $this->tags = $tags; + $this->tags = (string) $tags; $this->cachedNBT = null; } @@ -362,12 +362,12 @@ class Item implements ItemIds, \JsonSerializable{ /** * @return string */ - public function getCompoundTag(){ + public function getCompoundTag() : string{ return $this->tags; } public function hasCompoundTag() : bool{ - return $this->tags !== "" and $this->tags !== null; + return $this->tags !== ""; } public function hasCustomBlockData() : bool{ @@ -672,12 +672,16 @@ class Item implements ItemIds, \JsonSerializable{ return $this->id; } - final public function getDamage(){ + final public function getDamage() : int{ return $this->meta; } - public function setDamage($meta){ - $this->meta = $meta !== null ? $meta & 0xFFFF : null; + public function setDamage(int $meta){ + $this->meta = $meta !== -1 ? $meta & 0xFFFF : -1; + } + + public function hasAnyDamageValue() : bool{ + return $this->meta === -1; } public function getMaxStackSize(){ @@ -757,7 +761,7 @@ class Item implements ItemIds, \JsonSerializable{ public final function deepEquals(Item $item, bool $checkDamage = true, bool $checkCompound = true) : bool{ if($this->equals($item, $checkDamage, $checkCompound)){ return true; - }elseif($item->hasCompoundTag() or $this->hasCompoundTag()){ + }elseif($item->hasCompoundTag() and $this->hasCompoundTag()){ return NBT::matchTree($this->getNamedTag(), $item->getNamedTag()); } diff --git a/src/pocketmine/item/ItemBlock.php b/src/pocketmine/item/ItemBlock.php index e7e827512..a169f8ccf 100644 --- a/src/pocketmine/item/ItemBlock.php +++ b/src/pocketmine/item/ItemBlock.php @@ -32,9 +32,9 @@ class ItemBlock extends Item{ parent::__construct($block->getId(), $block->getDamage(), $count, $block->getName()); } - public function setDamage($meta){ - $this->meta = $meta !== null ? $meta & 0xf : null; - $this->block->setDamage($this->meta); + public function setDamage(int $meta){ + $this->meta = $meta !== -1 ? $meta & 0xf : -1; + $this->block->setDamage($this->meta !== -1 ? $this->meta : 0); } public function __clone(){ diff --git a/src/pocketmine/network/protocol/CraftingDataPacket.php b/src/pocketmine/network/protocol/CraftingDataPacket.php index c89096763..11e94caf9 100644 --- a/src/pocketmine/network/protocol/CraftingDataPacket.php +++ b/src/pocketmine/network/protocol/CraftingDataPacket.php @@ -152,7 +152,7 @@ class CraftingDataPacket extends DataPacket{ } private static function writeFurnaceRecipe(FurnaceRecipe $recipe, BinaryStream $stream){ - if($recipe->getInput()->getDamage() !== null){ //Data recipe + if(!$recipe->getInput()->hasAnyDamageValue()){ //Data recipe $stream->putVarInt($recipe->getInput()->getId()); $stream->putVarInt($recipe->getInput()->getDamage()); $stream->putSlot($recipe->getResult());