Player: ensure that PlayerItemHeldEvent is called when the contents of the held slot changes

in PM3, this was done by implicitly relying on the client to send a MobEquipmentPacket selecting the same hotbar slot when the slot contents changes.
In PM4, we avoid relying on this, and fire the event directly when the listener detects a held slot change.
This ensures that the behaviour remains consistent regardless of what the client starts doing in the future.

closes #4905
This commit is contained in:
Dylan K. Taylor 2022-03-22 16:52:55 +00:00
parent 4a94cb85a2
commit ea33a04d00
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -313,6 +313,16 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$this->setNameTag($this->username);
}
private function callDummyItemHeldEvent() : void{
$slot = $this->inventory->getHeldItemIndex();
$event = new PlayerItemHeldEvent($this, $this->inventory->getItem($slot), $slot);
$event->call();
//TODO: this event is actually cancellable, but cancelling it here has no meaningful result, so we
//just ignore it. We fire this only because the content of the held slot changed, not because the
//held slot index changed. We can't prevent that from here, and nor would it be sensible to.
}
protected function initEntity(CompoundTag $nbt) : void{
parent::initEntity($nbt);
$this->addDefaultWindows();
@ -321,10 +331,16 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
function(Inventory $unused, int $slot) : void{
if($slot === $this->inventory->getHeldItemIndex()){
$this->setUsingItem(false);
$this->callDummyItemHeldEvent();
}
},
function() : void{
function(Inventory $unused, array $changedSlots) : void{
$this->setUsingItem(false);
$heldSlot = $this->inventory->getHeldItemIndex();
if(isset($changedSlots[$heldSlot])){
$this->callDummyItemHeldEvent();
}
}
));