0

What I want to do is some kit, whenever I place a chest, it will fill items inside it. This is my code so far.

public function onBlockPlace(BlockPlaceEvent $event): void {
        $dataConfig = CustomConfig::get("data.yml");
        $block = $event->getBlock();
        $item = $event->getItem();
        $player = $event->getPlayer();
        $server = $player->getServer();
        $world = $player->getWorld();


        if ($item->getId() === ItemIds::CHEST && $item->getMeta() !== 0) {
            $chest = new Chest($world, $block->getPosition());
            $kits = $dataConfig->getConfig()->getAll(true);
            $meta = $item->getMeta();


            // add items on the chest
            foreach ($kits as $id) {
                $kit = $dataConfig->getConfig()->get($id);
                if ($kit["meta"] === $meta) {
                    $chest->setName($kit["display"]);
                    $items = $dataConfig->getConfig()->getNested("$id.chest.items");


                    foreach ($items as $key => $value) {
                        $itemKit = ItemKit::fromArray($value, $key);
                        $chest->getInventory()->addItem($itemKit->getItem());
                    }


                    break;
                }
            }


            $world->addTile($chest);
            $world->setBlock($block->getPosition(), $block);
            $item->pop();
            $event->cancel();
        }
    }

It would add the items on the chest, and show the chest in the world, the problem is that now the default behavior is gone, for example, the blockplace sound, pairevent, etc. But if I were to remove the event.cancel(), and have this:

            $world->addTile($chest);
            // $world->setBlock($block->getPosition(), $block);
            // $item->pop();
            // $event->cancel();

The title of the chest is the name of the item. Like for example the name of the chest item is Awesome kit, I want the title of the chest tile to be just Awesome, but what happened is the title is the Awesome kit.

This is the result of those two:

            $world->addTile($chest);
            $world->setBlock($block->getPosition(), $block);
            $item->pop();
            $event->cancel();

Image1

            $world->addTile($chest);
            // $world->setBlock($block->getPosition(), $block);
            // $item->pop();
            // $event->cancel();

Image2

How do I achieve the first one, but still having the default behavior?

AdolfJames Urian
  • 97
  • 1
  • 1
  • 9

2 Answers2

0

I found an old GitHub bug issue from 2017: https://github.com/pmmp/PocketMine-MP/issues/1554

Apparently the PMMP Team couldn't replicate it, so the issue got closed.

LolXDDev
  • 63
  • 1
  • 5
0

I would extend the Chest Block, then register it to override existing chests. In the postPlace function in the chest block, after it checks for pairing, you can modify items in the inventory and change the name there without canceling the event.

xSuper
  • 1