mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 08:17:34 +00:00
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:
parent
2e0f7102e8
commit
1525001565
@ -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){
|
||||
|
@ -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))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user