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

Chest / inventoryEvent

Comments in 'Plugin Development' started by MinePocket Servers, Nov 21, 2015.

  1. MinePocket Servers
    Offline

    MinePocket Servers Active Member

    Joined:
    Mar 6, 2014
    Posts:
    69
    Minecraft User:
    MinePocket
    Background: Making a hg plugin that will work with imported maps.
    Problem: Chests! Need to find a way to deal with chests
    Solution:
    1) Add items to chests on the fly. When player opens chest (triggered by invopenevent) check if chest is in an array. if not then add items in (randomly selected from a list). If in array then do nothing.
    2) Same as above but skip adding items to chests and directly give players items from a random list. If in the array then do nothing.

    HELP:

    How do I specifically detect chest inv open or interact with a chest? I could use on touch then scan for chests but im sure theres a better way.
    If i go route 1: how do I add items to a chest? How to clear items in chest? Whats the best way to add a chest to the array (was thinking coords)
    If i go route 2: How would I set chest gui canceled and auto close the chest?
  2. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    Route 1:
    PHP:
    public function onTouch(PlayerInteractEvent $event){
        
    $b $event->getBlock();
        if(
    $b->getId() === Block::CHEST){
            
    $t $b->getLevel()->getTile($b);
            if(
    $t instanceof Chest){
                if(
    in_array($findme$array)){//you can store coord like $array[] = "$x $y $z";, then replace this with   if(in_array($b->x." "$b->y." ".$b->z, $array))
                    
    for($i=0;$i $t->getSize();$i++){//add items
                        
    $t->getInventory()->addItem(Item::get($itemlist[mt_rand(0,count($itemlist) - 1)]));//store item IDs in $itemlist
                    
    }
                }
            }
        }
    }
    Route 2:
    PHP:
    public function onTouch(PlayerInteractEvent $event){
        
    $b $event->getBlock();
        
    $p $event->getPlayer();
        if(
    $b->getId() === Block::CHEST){
            
    $t $b->getLevel()->getTile($b);
            if(
    $t instanceof Chest){
                if(
    in_array($findme$array)){//you can store coord like $array[] = "$x $y $z";, then replace this with   if(in_array($b->x." "$b->y." ".$b->z, $array))
                    
    $event->setCancelled();
                    for(
    $i=0;$i $t->getSize();$i++){//add items
                        
    $p->getInventory()->addItem(Item::get($itemlist[mt_rand(0,count($itemlist) - 1)]));//store item IDs in $itemlist
                    
    }
                }
            }
        }
    }
    Last edited: Nov 24, 2015
  3. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
  4. MinePocket Servers
    Offline

    MinePocket Servers Active Member

    Joined:
    Mar 6, 2014
    Posts:
    69
    Minecraft User:
    MinePocket
    Wow thanks alot!
  5. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Its better to use InventoryOpenEvent

Share This Page

Advertisement