Add Items to and Specific Players Chest Inventory.

Discussion in 'Plugin Development' started by iTzVeri, Dec 19, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    iTzVeri

    Is there a Way to add an Item to an Inventory which is not the Players own inventory. Here is what i tried:
    Code:
      
    @EventHandler
        public void onInteract(PlayerInteractEvent e){
            Player p = e.getPlayer();
            Inventory inv = Bukkit.createInventory(p.getPlayer(), 18, "ยง6testy");
            if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
                if(e.getClickedBlock() != null){
                    if(e.getClickedBlock().getType().equals(Material.ENDER_CHEST)){
                            e.setCancelled(true);
                            Random r = new Random();
                            int i = r.nextInt(3);
                            ItemStack is = new ItemStack(Material.ENCHANTED_BOOK);
                            ItemMeta im = is.getItemMeta();
                         
                            if(i == 0){
                                inv.addItem(is);
                            }
                            if(i == 1){
                                inv.addItem( is);
                            }
                            if(i == 2){
                                inv.addItem( is);
                            }
                           //and so on!
                            //TODO
                         
    
                            p.updateInventory();
                        }
                    }
                }
            }
    And here is where I opened the Inventory. It opens the inventory but its empty...

    Code:
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(e.getItem() != null){
                    if(e.getItem().getType() == Material.BLAZE_ROD){
                        p.openInventory(inv);
                    }
                }
            }
    It is the same event(same method). Also the title of the inventory is correct. But it wont set the Items into that inventory.

    Thanks for your help and sorry for my bad spelling. I'm not realy confident with the English Language.. ;)
     
    Last edited: Dec 19, 2014
  2. Offline

    Skionz

    @iTzVeri Try canceling the event. I believe it is opening the player's ender chest right now.
     
  3. Offline

    iTzVeri

    @Skionz No it is canceled in my code. Also where I opened the inventory it is triggered by th Blaze Rod rightclick. So it opens the right inventory but with no items in it.

    @Skionz Nvm , i made it :) thanks for your help anyways :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
Thread Status:
Not open for further replies.

Share This Page