Use -1 for anydamage and empty string for null NBT, closes #146

This commit is contained in:
Dylan K. Taylor 2016-12-21 14:45:34 +00:00
parent 300a3d5ccd
commit 5443b10257
7 changed files with 40 additions and 36 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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){

View File

@ -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);
}

View File

@ -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());
}

View File

@ -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(){

View File

@ -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());