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

Solved Fill a chest with item

Comments in 'Plugin Development' started by deot, Nov 12, 2015.

  1. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    Okay, I searched through forum, but I can't find an exact solution for this...
    How can i set items to a chest?
    Im doing like this: is this correct?
    PHP:
    public function fillChest(){
      
    $coord = new Vector3(12 3456);
      
    $chest $this->level->getTile($coord);
      
    $chest->getInventory()->addItem(Item::get(258,1));
    }
    Correct?? :p
    Last edited: Nov 12, 2015
  2. zombie_Power
    Offline

    zombie_Power Active Member

    Joined:
    Sep 7, 2015
    Posts:
    239
    Minecraft User:
    Zombie_Power
    PHP:
    $chest->getInventory()->setItem($slotItem::get(add whatever here);
    deot likes this.
  3. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    Code:
    12.11 02:26:23 [Server] INFO Fatal error: Call to a member function getInventory() on null in phar:///plugins/MinedoxCore-Smash_v1.0.0.phar/src/MDCore/MDCore.php on line 368
    There's a problem with $chest... where did I wrong? Pls help.... :p
  4. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    That means level does not exist. First getBlock() then getTile() maybe?
  5. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    PHP:
    $this->level $this->getServer()->getDefaultLevel();
    This is how I get the level...
    I still don't know why it crashed.... getBlock()->getTile()?? Is this possible? xD getBlock and getTile? LOL
  6. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    653
    Plugins:
    1
    Minecraft User:
    jojoe77777
    No, getTile() accepts a Vector3.
    deot likes this.
  7. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    I figured out... There's something wrong with my coordinates... zzz
    How can I get coordinates correctly?
    HamIsGoodie and Legoboy0215 like this.
  8. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    653
    Plugins:
    1
    Minecraft User:
    jojoe77777
    To start with, delete the space (look below)
  9. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    You have to floor() the given values.
    I know there are some "not cleen" things in this code, but it fills the chest.
    PHP:
    $position $player->getPosition(); //replace with the vector you use to get the chest
                    
    $position instanceof Position;
                    
    $chest $position->getLevel()->getBlock(new Vector3(floor($position->getX()), 30 + (69 64), floor($position->getZ())));
                    
    $position->getLevel()->setBlock(new Vector3(floor($position->getX()), 30 + (69 64), floor($position->getZ())), new Block(Block::CHEST), truetrue);
                    
    $nbt = new Compound("", [new Enum("Items", []),new String("id"Tile::CHEST),new Int("x"floor($position->getX())),new Int("y"floor($position->getY())),new Int("z"floor($position->getZ()))]); // here floor() is important, in the other lines not, but still, i used it
                    
    $nbt->Items->setTagType(NBT::TAG_Compound);
                    
    $tile Tile::createTile("Chest"$sender->getLevel()->getChunk($position->getX() >> 4$position->getZ() >> 4), $nbt);
                    if(!(
    $tile instanceof \pocketmine\tile\Chest)) continue;
                    
    $tile->getInventory()->addItem(new Item(Item::ICE02), new Item(Item::BUCKET101));
                    
    by the way, with this code i create a new chest at the players feet. So you might need to modify the code.
  10. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    Nope :) My code is working, just the coordinates i obtained is wrong :) Anyway, still thank you but i already fixed it yesterday :p
  11. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Oh, okay. Looked like this wasn't totally solved :D
  12. zombie_Power
    Offline

    zombie_Power Active Member

    Joined:
    Sep 7, 2015
    Posts:
    239
    Minecraft User:
    Zombie_Power
    That code won't work... As you cant use Item(Item::BUCKET);
    Please use Item:get . It is more efficient and less likely to get changed in the future
  13. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    What a wonder THAT it works. ;). Check it before you post. and get() wont work, because it has meta and i wanted to keep it short.
    Item::BUCKET just returns the id, as Item->get() does.
    zombie_Power likes this.
  14. zombie_Power
    Offline

    zombie_Power Active Member

    Joined:
    Sep 7, 2015
    Posts:
    239
    Minecraft User:
    Zombie_Power
    Yes that may be true, I am just saying that Item::get is more efficient:p
  15. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    It is? i don't think so. Item::BUCKET will get the id directly from the Item list (Item[]), item get will call a subfunction of Item::class, which will then return the id. I am sure that 1 step less will not change the performance alot, but a task less is always less time.
  16. deot
    Offline

    deot Active Member Plugin Developer

    Joined:
    Apr 22, 2015
    Posts:
    219
    Plugins:
    1
    Minecraft User:
    deot
    What do u mean by wasnt totally solved? Yes, it is totally solved :)
    Messages are sent to damager and victim now, i tested it :)
    HamIsGoodie and thebigsmileXD like this.
  17. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Whoops, then i was mistaken, sry! :D

Share This Page

Advertisement