Avoid repeated calls to getItemInHand() in drop item handler

This commit is contained in:
Dylan K. Taylor 2023-03-21 00:04:29 +00:00
parent 097632902a
commit ccd288d7fa
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -398,18 +398,19 @@ class InGamePacketHandler extends PacketHandler{
} }
$inventory = $this->player->getInventory(); $inventory = $this->player->getInventory();
$heldItemStack = TypeConverter::getInstance()->coreItemStackToNet($inventory->getItemInHand()); $heldItem = $inventory->getItemInHand();
$heldItemStack = TypeConverter::getInstance()->coreItemStackToNet($heldItem);
//because the client doesn't tell us the expected itemstack ID, we have to deep-compare our known //because the client doesn't tell us the expected itemstack ID, we have to deep-compare our known
//itemstack info with the one the client sent. This is costly, but we don't have any other option :( //itemstack info with the one the client sent. This is costly, but we don't have any other option :(
if($heldItemStack->getCount() < $droppedItemStack->getCount() || !$heldItemStack->equalsWithoutCount($droppedItemStack)){ if($heldItemStack->getCount() < $droppedItemStack->getCount() || !$heldItemStack->equalsWithoutCount($droppedItemStack)){
return false; return false;
} }
$newHeldItem = $inventory->getItemInHand(); //this modifies $heldItem
$droppedItem = $newHeldItem->pop($droppedItemStack->getCount()); $droppedItem = $heldItem->pop($droppedItemStack->getCount());
$builder = new TransactionBuilder(); $builder = new TransactionBuilder();
$builder->getInventory($inventory)->setItem($inventory->getHeldItemIndex(), $newHeldItem); $builder->getInventory($inventory)->setItem($inventory->getHeldItemIndex(), $heldItem);
$builder->addAction(new DropItemAction($droppedItem)); $builder->addAction(new DropItemAction($droppedItem));
$transaction = new InventoryTransaction($this->player, $builder->generateActions()); $transaction = new InventoryTransaction($this->player, $builder->generateActions());