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

Get chest inv?..

Comments in 'Plugin Development' started by dxm_hippie, Jan 19, 2016.

  1. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    Somethings not right /: i get no items from inside the chest..any suggestions?

    PHP:
    public function getChestDrops($chest$p){
            
    $tile $chest->getLevel()->getTile(new Vector3($chest->x,$chest->y,$chest->z));
            if(
    $tile instanceof Chest){
                foreach(
    $tile->getInventory() as $item){
                    if(
    $p->getInventory()->canAddItem(Item::get($item->getId()))){
                           
    $p->getInventory()->addItem(Item::get($item->getId()));
                    }else{
                          
    $p->getLevel()->dropItem($chestItem::get($item->getId()));
                       }
                }
            }
        }
  2. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    The foreach should be
    PHP:
    foreach($tile->getInventory()->getContents() as $item)
    This is also better,if not it will only check if you can add one of the stack of item in the chest
    PHP:
    if($p->getInventory()->canAddItem($item)){
    dxm_hippie likes this.
  3. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    Thank you :) i shouldnt have the getId() with canadditem right?
  4. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    Yes, but your code has a little issue.
    Lets say in the chest has 64 apples, and the player can only take 5 more apples, your code only checks if the player can add 1 more apple, and you only add 1 apple to the player after that, i guess thats not what you want?
  5. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    Ah..No thats no good lol i was thinking it would check one item at a time for some reason. So i need to get the count some how then compare it manually?
  6. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    If im not wrong, addItem will return the items it could not add so i guess you can just do
    PHP:
    public function getChestDrops($chest$p){
            
    $tile $chest->getLevel()->getTile(new Vector3($chest->x,$chest->y,$chest->z));
            if(
    $tile instanceof Chest){
                foreach(
    $tile->getInventory()->getContents as $item){
                    
    $cantAdd $p->getInventory()->addItem($item);
                    if(
    count($cantAdd)){
                          foreach(
    $cantAdd as $drop$p->getLevel()->dropItem($chest$drop);
                    }
                }
            }
        }
  7. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    Worked but..that had some duplication issues going on :p ill just drop the item like a normal person lol thanks for the help!

Share This Page

Advertisement