Get all chest in the world and add items into the chest

Discussion in 'Development' started by Vaxrp, Feb 14, 2019.

  1. Vaxrp

    Vaxrp Witch

    Messages:
    73
    GitHub:
    Vaxrp
    Basically, I want to be able to get ALL the chests that are ALREADY placed on the map and give items to them. I honestly don't know how to go about doing that. They are going to be specific items but randomly. Thanks
     
  2. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    177
    GitHub:
    diamondgamermcpe
    PHP:
    /** @var Level $level */
    /** @var Item $item */
    foreach($level->getTiles() as $tile) {
        if(
    $tile instanceof Chest) {
            
    $tile->getInventory()->addItem($item);
        }
    }
     
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,572
    GitHub:
    muqsit
    That would work if all chunks of the world are loaded.

    It would be better (in terms of memory and CPU) if this was solved by a script (outside pocketmine) which does that, but that might require a few reflection hacks if you're going to use pocketmine's level provider.
     
  4. KielKing

    KielKing Zombie

    Messages:
    260
    GitHub:
    kielking
    if you're working on a minigame, I recommend you just save the position of the chests
     
  5. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    707
    GitHub:
    xXNiceYT
    No if I would him I would get all tiles and set items in it when game start
     
  6. KielKing

    KielKing Zombie

    Messages:
    260
    GitHub:
    kielking
     
  7. Phqzing

    Phqzing Spider

    Messages:
    9
    GitHub:
    phqzing
    how do I randomize the items that get put in the chest?
     
  8. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    115
    [QUOTE = "DiamondGamer30, postagem: 60917, membro: 1548"]
    PHP:
    / ** @var Level level * /
    / ** @var 
    Item item * /
    foreach ($ 
    level-> getTiles () as $ tile) {
        if ($ 
    tile instanceof Chest) {
            $ 
    tile-> getInventory () -> addItem ($ item);
        }
    }
    [/QUOTE]
    The ChunkLoadEvent event Maybe it is more efficient because it will apply the items in all the chests that are loaded, just a suggestion[/PHP]
     
    Last edited: May 21, 2021
  9. wolfdale

    wolfdale Zombie Pigman

    Messages:
    536
    If its just for a game where block cannot be broken it would be better to add item on inventory open? Then save the coords of the holder and clear at end of game. Dont add item if coords already exist
    Make use of rand or array_rand for choosing items in an array
     

Share This Page

  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.