Added API method Item->equalsExact() and removed some boilerplate code

This commit is contained in:
Dylan K. Taylor 2017-09-15 16:48:46 +01:00
parent 0e51820dfb
commit bd64172750
4 changed files with 14 additions and 4 deletions

View File

@ -2238,7 +2238,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$item = $this->inventory->getItemInHand();
$oldItem = clone $item;
if($this->level->useItemOn($blockVector, $item, $face, $packet->trData->clickPos, $this, true)){
if(!$item->equals($oldItem) or $item->getCount() !== $oldItem->getCount()){
if(!$item->equalsExact($oldItem)){
$this->inventory->setItemInHand($item);
$this->inventory->sendHeldItem($this->hasSpawned);
}
@ -2270,7 +2270,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
if($this->canInteract($blockVector->add(0.5, 0.5, 0.5), $this->isCreative() ? 13 : 6) and $this->level->useBreakOn($blockVector, $item, $this, true)){
if($this->isSurvival()){
if(!$item->equals($oldItem) or $item->getCount() !== $oldItem->getCount()){
if(!$item->equalsExact($oldItem)){
$this->inventory->setItemInHand($item);
$this->inventory->sendHeldItem($this->hasSpawned);
}

View File

@ -189,7 +189,7 @@ class SimpleInventoryTransaction implements InventoryTransaction{
$sortedThisLoop = 0;
foreach($list as $i => $action){
$actionSource = $action->getSourceItem();
if($actionSource->equals($lastTargetItem) and $actionSource->getCount() === $lastTargetItem->getCount()){
if($actionSource->equalsExact($lastTargetItem)){
$lastTargetItem = $action->getTargetItem();
unset($list[$i]);
$sortedThisLoop++;

View File

@ -75,7 +75,7 @@ class SlotChangeAction extends InventoryAction{
*/
public function isValid(Player $source) : bool{
$check = $this->inventory->getItem($this->inventorySlot);
return $check->equals($this->sourceItem) and $check->getCount() === $this->sourceItem->getCount();
return $check->equalsExact($this->sourceItem);
}
/**

View File

@ -883,6 +883,16 @@ class Item implements ItemIds, \JsonSerializable{
return false;
}
/**
* Returns whether the specified item stack has the same ID, damage, NBT and count as this item stack.
* @param Item $other
*
* @return bool
*/
final public function equalsExact(Item $other) : bool{
return $this->equals($other, true, true) and $this->count === $other->count;
}
/**
* @deprecated Use {@link Item#equals} instead, this method will be removed in the future.
*