pmmp / PocketMine-MP

A server software for Minecraft: Bedrock Edition in PHP
https://pmmp.io
GNU Lesser General Public License v3.0
3.28k stars 1.56k forks source link

Storing local variables when Item::onAttackEntity is called #6186

Closed Aboshxm2 closed 11 months ago

Aboshxm2 commented 11 months ago

Issue description

When overriding onAttackEntity for custom items, it is not possible to store variables when the method is called.

OS and versions

Plugins

Crashdump, backtrace or other files

$id = ItemTypeIds::newId();
        $item = (new class(new ItemIdentifier($id)) extends Item {
            public int $myVariable = 0;

            public function onAttackEntity(Entity $victim, array &$returnedItems): bool
            {
                $this->myVariable = 1;
                return false;
            }
        });

        $this->getServer()->getPluginManager()->registerEvent(EntityDamageByEntityEvent::class, function (EntityDamageByEntityEvent $event) use ($id): void {
            $attacker = $event->getDamager();
            if(!$attacker instanceof Player) return;

            $item = $attacker->getInventory()->getItemInHand();
            if($item->getTypeId() === $id) {
                $attacker->sendMessage((string)$item->myVariable);
            }
        }, EventPriority::MONITOR, $this);

image

Aboshxm2 commented 11 months ago

It seems that because the item is equal to the old item (it's actually not), the item won't be replaced in the inventory.

So, I don't know, the Item::equalsExact should be overridable

github-actions[bot] commented 11 months ago

Hi, we only accept bug reports on this issue tracker, but this issue looks like a support request.

Instead of creating an issue, try the following:

Docs | Discord | Forums