Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved Problem getting chest with getTile()

Comments in 'Plugin Development' started by AppPhil, Oct 6, 2015.

  1. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
    Hey there,

    for a minigame I have to fill chests with items.
    I want to get the chests with $level->getTile($chestPos) but that returns null sometimes and I really don't know why, because I am sure that there is a chest at the position.

    Maybe someone knows this problem or can help me.
    Andrey Nazarchuk likes this.
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    • That chunk may not be loaded
    • That chunk may be unloaded
    • Some error made the tile be deleted, for example, setting blocks through a world editor.
    Andrey Nazarchuk and AppPhil like this.
  3. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
    And how can I load the chunk where the Position of the Tile is in?
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Say, if your position is saved in a Vector3 $v and Level object in $lv:
    PHP:
    $lv->loadChunk($v->>> 4$v->>> 4);
    >> 4 converts the X coordinate of the block into the X order of the chunk.
    Andrey Nazarchuk and AppPhil like this.
  5. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
    I tried to load the chunk but that doesn't seem to be the problem. I will try creating the tile in the code now.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    The chunk will be unloaded if nobody is using it.
    Did you get tile immediately after loading chunk?
    Andrey Nazarchuk and AppPhil like this.
  7. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
    Yes in the line after that.
    Looks like this:
    PHP:
    $level->loadChunk($chestPositions[1]->>> 4$chestPositions[1]->>> 4);
    $chest1 $level->getTile($chestPositions[1]);
    $inv $chest1->getInventory();
    Edit:
    I have some chests on the same level, where no players are while I'm testing, and there I don't get the error. Maybe it could be because I placed the chests where the error comes with 1.6?

    Edit2:
    I changed some of the old chests placed in 1.4 (or something before 1.6) to new chests and there I can get the tile. So that's not the problem.

    Edit3:
    When I do getBlockIdAt(the position of the chest) it gives 0, so there seems to be nothing. But why?
    Last edited: Oct 6, 2015
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    #3: that means the chunk is not loaded. Can you answer my question? Did you check it immediately after unloading chink?

    BTW, is your array an int/float array?
    Andrey Nazarchuk and AppPhil like this.
  9. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
    Now I've added this code before the getTile():
    PHP:
    $block = new \pocketmine\block\Chest();
    $level->setBlock($chestPositions[1], $blocktruetrue);
    $this->main->getLogger()->info("TTT: ".$level->getBlockIdAt($chestPositions[1]->x$chestPositions[1]->y$chestPositions[1]->z));
                
    $nbt = new Compound("", [
                new 
    Enum("Items", []),
                new 
    String("id"Tile::CHEST),
                new 
    Int("x"$this->x),
                new 
    Int("y"$this->y),
                new 
    Int("z"$this->z)
            ]);
    $nbt->Items->setTagType(NBT::TAG_Compound);
    $tile Tile::createTile("Chest"$level->getChunk($this->>> 4$this->>> 4), $nbt);
    So there must be a tile but the getTile() function still return null.
  10. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
    Yes I checked if the chunk is loaded with this:
    PHP:
    $chunkLoaded $level->isChunkLoaded($chestPositions[1]->>> 4$chestPositions[1]->>> 4);
    if(
    $chunkLoaded) {
         
    $this->main->getLogger()->info("Loaded");
    }
    $chest1 $level->getTile($chestPositions[1]);
    The array is an int => Vector3 array. So $chestPositions[1] is a Vector3.
  11. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
    I really don't understand it. I tried to open the chest and there's no problem with that. There is no difference between all the chests I placed but the new chests can't be getted by getTile().
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Is it a double chest?
    Andrey Nazarchuk and AppPhil like this.
  13. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Just for debug, can you dump or in some way output the return result of getTiles for the level? (We just need the array with the coordinates in it for investigating)
    Andrey Nazarchuk and AppPhil like this.
  15. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
    I'm not sure what I have to do so I've printed out var_dump($lv->getTiles()).
    Then the console logged this: (to long to post here) http://pastebin.com/jgLEqak5

    If it's not that what you need, tell me what I have to do so that you can help me.
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Simplify it by converting them all into coords only :p and see if your tile exists there.
    Andrey Nazarchuk and AppPhil like this.
  17. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
    Thank you soo much! I found out that the tiles have different positions than the positions I was searching for them. But I don't know why I wrote the wrong positions, because I teleported me to the positions and then I was standing in the chest.
    PEMapModder and Taha_The_Hacker like this.
  18. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    LOL xD
    AppPhil likes this.
  19. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Like my posts please :p
    Andrey Nazarchuk and AppPhil like this.
  20. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    You already got enough likes (4,213 likes) :p
    PEMapModder and SeifValkierks like this.

Share This Page

Advertisement