1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Remove tile

Discussion in 'Development' started by LucGamesDE, Jun 6, 2017.

  1. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    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
     
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    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?
     
  3. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    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->2),
    new 
    IntTag("z", (int) $player->z)
    ]));
    $block Block::get(Block::CHEST);
    $block->= (int) $tile->x;
    $block->= (int) $tile->y;
    $block->= (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?
     
  4. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    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.
     
  5. Aviv

    Aviv Baby Zombie

    Messages:
    156
    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.
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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->$tile->x;
    $block->$tile->y;
    $block->$tile->z;
    $block->level $tile->getLevel();
    $tile->getLevel()->sendBlocks($players, [$block]);
    $tile->close();
     
  7. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    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?
     
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    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.
     
    jasonwynn10 likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.