protocol ItemStack: added equals() method to compare net itemstacks directly

this will be needed for more than just this little check once item NBT gets cleaned out properly, since we'll need to compare object equality by network layer stuff instead of internals (where different network objects might deserialize to the same internal items).
This commit is contained in:
Dylan K. Taylor 2020-08-02 23:22:39 +01:00
parent 2e0f7102e8
commit 1525001565
2 changed files with 16 additions and 3 deletions

View File

@ -180,12 +180,12 @@ class TypeConverter{
* @throws \UnexpectedValueException
*/
public function createInventoryAction(NetworkInventoryAction $action, Player $player) : ?InventoryAction{
$old = TypeConverter::getInstance()->netItemStackToCore($action->oldItem);
$new = TypeConverter::getInstance()->netItemStackToCore($action->newItem);
if($old->equalsExact($new)){
if($action->oldItem->equals($action->newItem)){
//filter out useless noise in 1.13
return null;
}
$old = TypeConverter::getInstance()->netItemStackToCore($action->oldItem);
$new = TypeConverter::getInstance()->netItemStackToCore($action->newItem);
switch($action->sourceType){
case NetworkInventoryAction::SOURCE_CONTAINER:
if($action->windowId === ContainerIds::UI and $action->inventorySlot > 0){

View File

@ -97,4 +97,17 @@ final class ItemStack{
public function getShieldBlockingTick() : ?int{
return $this->shieldBlockingTick;
}
public function equals(ItemStack $itemStack) : bool{
return
$this->id === $itemStack->id &&
$this->meta === $itemStack->meta &&
$this->count === $itemStack->count &&
$this->canPlaceOn === $itemStack->canPlaceOn &&
$this->canDestroy === $itemStack->canDestroy &&
$this->shieldBlockingTick === $itemStack->shieldBlockingTick && (
$this->nbt === $itemStack->nbt || //this covers null === null and fast object identity
($this->nbt !== null && $itemStack->nbt !== null && $this->nbt->equals($itemStack->nbt))
);
}
}