Added compound tag checking for Item->equals()

This commit is contained in:
Shoghi Cervantes 2015-08-06 20:25:22 +02:00
parent a65109ff34
commit 091d0b3ff9
7 changed files with 29 additions and 22 deletions

View File

@ -1945,7 +1945,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
break; break;
} }
} }
}elseif($item === null or $slot === -1 or !$item->equals($packet->item, true)){ // packet error or not implemented }elseif($item === null or $slot === -1 or !$item->equals($packet->item)){ // packet error or not implemented
$this->inventory->sendContents($this); $this->inventory->sendContents($this);
break; break;
}elseif($this->isCreative()){ }elseif($this->isCreative()){
@ -1985,7 +1985,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
if($this->level->useItemOn($blockVector, $item, $packet->face, $packet->fx, $packet->fy, $packet->fz, $this) === true){ if($this->level->useItemOn($blockVector, $item, $packet->face, $packet->fx, $packet->fy, $packet->fz, $this) === true){
break; break;
} }
}elseif(!$this->inventory->getItemInHand()->equals($packet->item, true)){ }elseif(!$this->inventory->getItemInHand()->equals($packet->item)){
$this->inventory->sendHeldItem($this); $this->inventory->sendHeldItem($this);
}else{ }else{
$item = $this->inventory->getItemInHand(); $item = $this->inventory->getItemInHand();
@ -2015,7 +2015,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
if($this->isCreative()){ if($this->isCreative()){
$item = $this->inventory->getItemInHand(); $item = $this->inventory->getItemInHand();
}elseif(!$this->inventory->getItemInHand()->equals($packet->item, true)){ }elseif(!$this->inventory->getItemInHand()->equals($packet->item)){
$this->inventory->sendHeldItem($this); $this->inventory->sendHeldItem($this);
break; break;
}else{ }else{
@ -2723,7 +2723,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
break; break;
} }
if($transaction->getSourceItem()->equals($transaction->getTargetItem(), true) and $transaction->getTargetItem()->getCount() === $transaction->getSourceItem()->getCount()){ //No changes! if($transaction->getSourceItem()->equals($transaction->getTargetItem()) and $transaction->getTargetItem()->getCount() === $transaction->getSourceItem()->getCount()){ //No changes!
//No changes, just a local inventory update sent by the server //No changes, just a local inventory update sent by the server
break; break;
} }

View File

@ -160,8 +160,9 @@ abstract class BaseInventory implements Inventory{
public function contains(Item $item){ public function contains(Item $item){
$count = max(1, $item->getCount()); $count = max(1, $item->getCount());
$checkDamage = $item->getDamage() === null ? false : true; $checkDamage = $item->getDamage() === null ? false : true;
$checkTags = $item->getCompoundTag() === null ? false : true;
foreach($this->getContents() as $i){ foreach($this->getContents() as $i){
if($item->equals($i, $checkDamage)){ if($item->equals($i, $checkDamage, $checkTags)){
$count -= $i->getCount(); $count -= $i->getCount();
if($count <= 0){ if($count <= 0){
return true; return true;
@ -175,8 +176,9 @@ abstract class BaseInventory implements Inventory{
public function all(Item $item){ public function all(Item $item){
$slots = []; $slots = [];
$checkDamage = $item->getDamage() === null ? false : true; $checkDamage = $item->getDamage() === null ? false : true;
$checkTags = $item->getCompoundTag() === null ? false : true;
foreach($this->getContents() as $index => $i){ foreach($this->getContents() as $index => $i){
if($item->equals($i, $checkDamage)){ if($item->equals($i, $checkDamage, $checkTags)){
$slots[$index] = $i; $slots[$index] = $i;
} }
} }
@ -186,8 +188,10 @@ abstract class BaseInventory implements Inventory{
public function remove(Item $item){ public function remove(Item $item){
$checkDamage = $item->getDamage() === null ? false : true; $checkDamage = $item->getDamage() === null ? false : true;
$checkTags = $item->getCompoundTag() === null ? false : true;
foreach($this->getContents() as $index => $i){ foreach($this->getContents() as $index => $i){
if($item->equals($i, $checkDamage)){ if($item->equals($i, $checkDamage, $checkTags)){
$this->clear($index); $this->clear($index);
} }
} }
@ -196,8 +200,10 @@ abstract class BaseInventory implements Inventory{
public function first(Item $item){ public function first(Item $item){
$count = max(1, $item->getCount()); $count = max(1, $item->getCount());
$checkDamage = $item->getDamage() === null ? false : true; $checkDamage = $item->getDamage() === null ? false : true;
$checkTags = $item->getCompoundTag() === null ? false : true;
foreach($this->getContents() as $index => $i){ foreach($this->getContents() as $index => $i){
if($item->equals($i, $checkDamage) and $i->getCount() >= $count){ if($item->equals($i, $checkDamage, $checkTags) and $i->getCount() >= $count){
return $index; return $index;
} }
} }
@ -218,9 +224,10 @@ abstract class BaseInventory implements Inventory{
public function canAddItem(Item $item){ public function canAddItem(Item $item){
$item = clone $item; $item = clone $item;
$checkDamage = $item->getDamage() === null ? false : true; $checkDamage = $item->getDamage() === null ? false : true;
$checkTags = $item->getCompoundTag() === null ? false : true;
for($i = 0; $i < $this->getSize(); ++$i){ for($i = 0; $i < $this->getSize(); ++$i){
$slot = $this->getItem($i); $slot = $this->getItem($i);
if($item->equals($slot, $checkDamage)){ if($item->equals($slot, $checkDamage, $checkTags)){
if(($diff = $slot->getMaxStackSize() - $slot->getCount()) > 0){ if(($diff = $slot->getMaxStackSize() - $slot->getCount()) > 0){
$item->setCount($item->getCount() - $diff); $item->setCount($item->getCount() - $diff);
} }
@ -258,7 +265,7 @@ abstract class BaseInventory implements Inventory{
} }
foreach($itemSlots as $index => $slot){ foreach($itemSlots as $index => $slot){
if($slot->equals($item, true) and $item->getCount() < $item->getMaxStackSize()){ if($slot->equals($item) and $item->getCount() < $item->getMaxStackSize()){
$amount = min($item->getMaxStackSize() - $item->getCount(), $slot->getCount(), $this->getMaxStackSize()); $amount = min($item->getMaxStackSize() - $item->getCount(), $slot->getCount(), $this->getMaxStackSize());
if($amount > 0){ if($amount > 0){
$slot->setCount($slot->getCount() - $amount); $slot->setCount($slot->getCount() - $amount);
@ -316,7 +323,7 @@ abstract class BaseInventory implements Inventory{
} }
foreach($itemSlots as $index => $slot){ foreach($itemSlots as $index => $slot){
if($slot->equals($item, $slot->getDamage() === null ? false : true)){ if($slot->equals($item, $slot->getDamage() === null ? false : true, $slot->getCompoundTag() === null ? false : true)){
$amount = min($item->getCount(), $slot->getCount()); $amount = min($item->getCount(), $slot->getCount());
$slot->setCount($slot->getCount() - $amount); $slot->setCount($slot->getCount() - $amount);
$item->setCount($item->getCount() - $amount); $item->setCount($item->getCount() - $amount);

View File

@ -443,7 +443,7 @@ class CraftingManager{
foreach($ingredients as $item){ foreach($ingredients as $item){
$amount = $item->getCount(); $amount = $item->getCount();
foreach($checkInput as $k => $checkItem){ foreach($checkInput as $k => $checkItem){
if($checkItem->equals($item, $checkItem->getDamage() === null ? false : true)){ if($checkItem->equals($item, $checkItem->getDamage() === null ? false : true, $checkItem->getCompoundTag() === null ? false : true)){
$remove = min($checkItem->getCount(), $amount); $remove = min($checkItem->getCount(), $amount);
$checkItem->setCount($checkItem->getCount() - $remove); $checkItem->setCount($checkItem->getCount() - $remove);
if($checkItem->getCount() === 0){ if($checkItem->getCount() === 0){
@ -506,7 +506,7 @@ class CraftingManager{
foreach($input as $item){ foreach($input as $item){
$amount = $item->getCount(); $amount = $item->getCount();
foreach($checkInput as $k => $checkItem){ foreach($checkInput as $k => $checkItem){
if($checkItem->equals($item, $checkItem->getDamage() === null ? false : true)){ if($checkItem->equals($item, $checkItem->getDamage() === null ? false : true, $checkItem->getCompoundTag() === null ? false : true)){
$remove = min($checkItem->getCount(), $amount); $remove = min($checkItem->getCount(), $amount);
$checkItem->setCount($checkItem->getCount() - $remove); $checkItem->setCount($checkItem->getCount() - $remove);
if($checkItem->getCount() === 0){ if($checkItem->getCount() === 0){
@ -540,7 +540,7 @@ class CraftingManager{
} }
$checkResult = $recipe->getResult(); $checkResult = $recipe->getResult();
if($checkResult->equals($result, true) and $checkResult->getCount() === $result->getCount()){ if($checkResult->equals($result) and $checkResult->getCount() === $result->getCount()){
return $recipe; return $recipe;
} }

View File

@ -87,7 +87,7 @@ class ShapelessRecipe implements Recipe{
if($item->getCount() <= 0){ if($item->getCount() <= 0){
break; break;
} }
if($ingredient->equals($item, $item->getDamage() === null ? false : true)){ if($ingredient->equals($item, $item->getDamage() === null ? false : true, $item->getCompoundTag() === null ? false : true)){
unset($this->ingredients[$index]); unset($this->ingredients[$index]);
$item->setCount($item->getCount() - 1); $item->setCount($item->getCount() - 1);
} }

View File

@ -98,7 +98,7 @@ class SimpleTransactionGroup implements TransactionGroup{
} }
$checkSourceItem = $ts->getInventory()->getItem($ts->getSlot()); $checkSourceItem = $ts->getInventory()->getItem($ts->getSlot());
$sourceItem = $ts->getSourceItem(); $sourceItem = $ts->getSourceItem();
if(!$checkSourceItem->equals($sourceItem, true) or $sourceItem->getCount() !== $checkSourceItem->getCount()){ if(!$checkSourceItem->equals($sourceItem) or $sourceItem->getCount() !== $checkSourceItem->getCount()){
return false; return false;
} }
if($sourceItem->getId() !== Item::AIR){ if($sourceItem->getId() !== Item::AIR){
@ -108,7 +108,7 @@ class SimpleTransactionGroup implements TransactionGroup{
foreach($needItems as $i => $needItem){ foreach($needItems as $i => $needItem){
foreach($haveItems as $j => $haveItem){ foreach($haveItems as $j => $haveItem){
if($needItem->equals($haveItem, true)){ if($needItem->equals($haveItem)){
$amount = min($needItem->getCount(), $haveItem->getCount()); $amount = min($needItem->getCount(), $haveItem->getCount());
$needItem->setCount($needItem->getCount() - $amount); $needItem->setCount($needItem->getCount() - $amount);
$haveItem->setCount($haveItem->getCount() - $amount); $haveItem->setCount($haveItem->getCount() - $amount);

View File

@ -406,7 +406,7 @@ class Item{
protected $block; protected $block;
protected $id; protected $id;
protected $meta; protected $meta;
protected $nbt; protected $nbt = "";
public $count; public $count;
protected $durability = 0; protected $durability = 0;
protected $name; protected $name;
@ -1048,7 +1048,7 @@ class Item{
} }
final public function __toString(){ final public function __toString(){
return "Item " . $this->name . " (" . $this->id . ":" . ($this->meta === null ? "?" : $this->meta) . ")x" . $this->count . ($this->hasCompoundTag() ? " userData:0x".bin2hex($this->getCompoundTag()) : ""); return "Item " . $this->name . " (" . $this->id . ":" . ($this->meta === null ? "?" : $this->meta) . ")x" . $this->count . ($this->hasCompoundTag() ? " tags:0x".bin2hex($this->getCompoundTag()) : "");
} }
public function getDestroySpeed(Block $block, Player $player){ public function getDestroySpeed(Block $block, Player $player){
@ -1059,8 +1059,8 @@ class Item{
return false; return false;
} }
public final function equals(Item $item, $checkDamage = false){ public final function equals(Item $item, $checkDamage = true, $checkCompound = true){
return $this->id === $item->getId() and ($checkDamage === false or $this->getDamage() === $item->getDamage()); return $this->id === $item->getId() and ($checkDamage === false or $this->getDamage() === $item->getDamage()) and ($checkCompound === false or $this->getCompoundTag() === $item->getCompoundTag());
} }
} }

View File

@ -212,7 +212,7 @@ class Furnace extends Tile implements InventoryHolder, Container{
$raw = $this->inventory->getSmelting(); $raw = $this->inventory->getSmelting();
$product = $this->inventory->getResult(); $product = $this->inventory->getResult();
$smelt = $this->server->getCraftingManager()->matchFurnaceRecipe($raw); $smelt = $this->server->getCraftingManager()->matchFurnaceRecipe($raw);
$canSmelt = ($smelt instanceof FurnaceRecipe and $raw->getCount() > 0 and (($smelt->getResult()->equals($product, true) and $product->getCount() < $product->getMaxStackSize()) or $product->getId() === Item::AIR)); $canSmelt = ($smelt instanceof FurnaceRecipe and $raw->getCount() > 0 and (($smelt->getResult()->equals($product) and $product->getCount() < $product->getMaxStackSize()) or $product->getId() === Item::AIR));
if($this->namedtag["BurnTime"] <= 0 and $canSmelt and $fuel->getFuelTime() !== null and $fuel->getCount() > 0){ if($this->namedtag["BurnTime"] <= 0 and $canSmelt and $fuel->getFuelTime() !== null and $fuel->getCount() > 0){
$this->checkFuel($fuel); $this->checkFuel($fuel);