Is there a way to remove a chest tile after closing the chest inventory? I found no function in the API. :/ *Edit: I don't want to remove it. Only replace it with the old block
What you are trying to do depends on whether or not you set the tile with or without the block. Can you show proof of a previous attempt?
PHP: $tile = Tile::createTile("Chest", $level = $player->getLevel(), new CompoundTag("", [new StringTag("id", Tile::CHEST),new IntTag("x", (int) $player->x),new IntTag("y", (int) $player->y - 2),new IntTag("z", (int) $player->z)]));$block = Block::get(Block::CHEST);$block->x = (int) $tile->x;$block->y = (int) $tile->y;$block->z = (int) $tile->z;$block->level = $level;$block->level->sendBlocks([$player], [$block]);$inventory = new ChestInventory($tile);$tile->spawnTo($player);$player->addWindow($inventory); But if the inventory gets closed the tile is there (only for $player and not for all). And if the player clicks on it the tile disappears. But how can I automatically remove it?
You already have the tile defined, so you can do $player->level->removeTile($tile). How you listen for it would be a bit trickier. Maybe add a certain NBT tag to the tile, so you can determine if it's that specific tile? Maybe assign an ID to it as an IntTag, then save the tile in an associative array with the ID as it's key. Or, you could just give the tile a simple identifier that lets you know if it is your chest tile or a normal chest tile. If it isn't a normal one, you could remove it.
PHP: public function onInventoryClose(InventoryCloseEvent $event){ $block = $event->getInventory()->getHolder(); if(!$block instanceof Block) return; $block->getLevel()->getTile($block)->close();} You obviously want to check if its the right chest first.
Since you are sending the block updates to the inventory viewer(s) only, (I assume this as you've copied the code from PlayerVaults; I feel special), it'd make sense to send the air block to the inventory viewer(s) only. Don't forget to close the tile. Consequences: There will be too many unnecessary tile entities that are yet to be removed. More tile entities = more server load. Possible duplication: If you forget to close the tile, then place a block in place of the tile and break it, the block will close the tile when it breaks and drop the tile's inventory contents. PHP: /** @var InventoryCloseEvent $event */$tile = $event->getInventory()->getHolder();$players = $event->getInventory()->getViewers();$block = Block::get(Block::AIR);$block->x = $tile->x;$block->y = $tile->y;$block->z = $tile->z;$block->level = $tile->getLevel();$tile->getLevel()->sendBlocks($players, [$block]);$tile->close();
But what if the tile replaced a block that isn't air? I don't want to destroy my world Could I just send a update packet for that block to replace it to the real one?
A tile (a.k.a. "tile entity") simply means "extra NBT data located at a specific block location". It is completely irrelevant to the block, although gameplay mechanism in the block may vary due to the tile entity. Therefore, creating a tile does not affect the block at its block location, and direct (non-gameplay) changes to the block do not affect the tile either.