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

Solved Chest problem

Discussion in 'Development' started by aTmG, Aug 4, 2018.

  1. aTmG

    aTmG Silverfish

    Messages:
    22
    GitHub:
    ATMG
    I'm foreaching a level's tiles and checking if they are an instanceof Chest but when I var_dump("chest") after the check, I get less strings then there are chests in the level. Why is this happening?
     
    Muqsit likes this.
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Can I see the foreach loop?
     
  3. aTmG

    aTmG Silverfish

    Messages:
    22
    GitHub:
    ATMG
    PHP:
    foreach($level->getTiles() as $tile){
        if(
    $tile instanceof Chest){
              
    var_dump("chest");
        }
    }
     
  4. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    My guess is that Level.php only stores Tiles that are in loaded chunks, since almost everything is handled by each chunk. I may very well be wrong. What are you trying to do with each tile?
     
    Muqsit likes this.
  5. aTmG

    aTmG Silverfish

    Messages:
    22
    GitHub:
    ATMG
    I am trying to refill them
     
  6. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Would TreasureChest work? I'm not sure about how to resolve your problem, though somebody else might.
     
  7. aTmG

    aTmG Silverfish

    Messages:
    22
    GitHub:
    ATMG
    TreasureChest wouldn't work for what I am trying to do
     
  8. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    You can refill the chests in chunk load event
     
  9. aTmG

    aTmG Silverfish

    Messages:
    22
    GitHub:
    ATMG
    I'm doing skywars though so I don't think that would work (sorry for late reply)
     
  10. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    Check other sw plugin for help

    Like https://github.com/Muqsit/SkyWars
     
    Muqsit likes this.
  11. aTmG

    aTmG Silverfish

    Messages:
    22
    GitHub:
    ATMG
    Using that plugin without any modifications still gives me that problem. It's the one I was modifying
     
  12. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    Maybe you haven’t load Level or get all levels

    Try it but not sure if it will work

    PHP:
    use pocketmine\tile\Chest;

    foreach(
    $this->getServer()->getLevels() as $level){
    foreach(
    $level->getTiles() as $tile){
    if(
    $tile instanceof Chest){
    //stuff
    }
    }
    }
     
  13. aTmG

    aTmG Silverfish

    Messages:
    22
    GitHub:
    ATMG
    The plugin does the same thing but $level is defined in another location
     
  14. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You can't refill ALL the chests in a level, but you can try these methods:
    • Write a list of coordinates where you've put the chests at, and loop through those coordinates.
    • Use chunk loaders.
    I'd recommend going with the second option. A ChunkLoader keeps the chunk loaded until it exists. Players for example, are a type of chunk loaders.

    Add chunk loaders across your map, then remove them when the game is over (if you don't remove them, the chunks will stay loaded until the level unloads, which is generally during server shutdown).

    To create a ChunkLoader class, create a class that implements the ChunkLoader interface.
    PHP:
    <?php
    class MyChunkLoader implements ChunkLoader{
    }
    Don't forget — this is an interface, you will need to fill in the required class details.

    Once you do that, it's fairly simple to add and remove them.
    PHP:
    /** @var ChunkLoader $loader */
    /** @var Vector3 $pos */

    $chunkX $pos->getFloorX() >> 4;
    $chunkZ $pos->getFloorZ() >> 4;

    //Add ChunkLoader:
    $level->registerChunkLoader($loader$chunkX$chunkZ);

    //Remove ChunkLoader:
    $level->unregisterChunkLoader($loader$chunkX$chunkZ);
     
  15. aTmG

    aTmG Silverfish

    Messages:
    22
    GitHub:
    ATMG
    Are there any examples of these I can look at?
     
  16. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    PHP:
        /**
         * @param ChunkLoadEvent $event
         */
        
    public function onLoadChunk(ChunkLoadEvent $event) : void{
            
    $chunk $event->getChunk();
            foreach(
    $chunk->getTiles() as $tile) {
                if (
    $tile instanceof Chest) {
                    if(!
    in_array($tile->getId(),$this->filledTiles)) {
                        
    $this->filledTiles[] = $tile->getId();
                        if (
    $tile->getLevel()->getId() == $tile->getLevel()->getId()) {
                            
    $tile->getInventory()->clearAll();
                            
    $slotToFill 0;
                            for (
    $i 0$i <= 7$i++) {
                                
    $items = [Item::get(Item::IRON_SWORD), Item::get(Item::IRON_BOOTS)];
                                
    $slotToFill++;
                                
    $tile->getInventory()->setItem($slotToFill$a);
                            }
                        }
                    }
                }
            }
        }
     
    corytortoise 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.