Solved When i add an item to an inventory, it vanishes (and kind of want a better code for this)

Discussion in 'Plugin Development' started by JigokuSaru, Apr 20, 2015.

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

    JigokuSaru

    Ok so my code seems to not show any new items it adds and im not sure why
    Code:
     
    @EventHandler
       public void onInventoryCLose(InventoryCloseEvent e){
         if(e.getInventory() != null){
           if(e.getInventory().getName().equalsIgnoreCase(inv.getName())){
             if(e.getInventory().getItem(0) != null){
               ItemStack is0 = new ItemStack(e.getInventory().getItem(0));
               if(e.getInventory().getItem(9) == null){
                 e.getPlayer().getInventory().addItem(is0);
                 e.getInventory().setItem(0,new ItemStack(Material.AIR));             
               }
               if(e.getInventory().getItem(9) != null){
                 ItemStack pm1 = e.getInventory().getItem(9);
                 String type = pm1.getType().toString();
                 String amount = String.valueOf(pm1.getAmount());
                 List<String> lore = new ArrayList<String>();
                 lore.add(new String(amount+" "+type).toLowerCase().replace("_", " " ));
                 e.getInventory().setItem(0,setName(is0, null,lore ));
                 e.getPlayer().getInventory().addItem(pm1);
                 e.getInventory().setItem(9,new ItemStack(Material.AIR));
    If there is a better way to right this i would sure love to know what it is... I am unsure about this.
     
  2. Offline

    Koobaczech

    First thing i have to say is the player inventory is 9 slots, ranging from 0-8. SO change all 9s to 8s. Next i think you should get a player from the event e so you can work with that players inventory instead of directly from the event
     
  3. Offline

    JigokuSaru

    The inventory is 27 slots and the e.getInventory is not a players inventory so thetes no player to it...
    @Koobaczech
     
    Last edited: Apr 20, 2015
  4. Offline

    au2001

    @JigokuSaru Try something like:
    Code:
    @EventHandler
    public void onInventoryClose (InventoryCloseEvent e) {
        if (e.getInventory() != null) {
            Inventory i = e.getInventory();
            if (i.getName().equalsIgnoreCase(inv.getName())) {
                if (i.getItem(0) != null) {
                    Inventory pi = e.getPlayer().getInventory();
                    ItemStack is0 = new ItemStack(i.getItem(0));
                    if (i.getItem(9) == null) {
                        pi.addItem(is0);
                        i.setItem(0, new ItemStack(Material.AIR));
                    }
                    if (i.getItem(9) != null) {
                        ItemStack pm1 = i.getItem(9);
                        String type = pm1.getType().toString();
                        String amount = String.valueOf(pm1.getAmount());
                        List<String> lore = new ArrayList<String>();
                        lore.add(new String(amount + " " + type).toLowerCase().replace('_', ' '));
                        i.setItem(0, setName(is0, null, lore));
                        pi.addItem(pm1);
                        i.setItem(9, new ItemStack(Material.AIR));
                    }
                    ((Player) e.getPlayer()).updateInventory();
                }
            }
        }
    }
     
  5. Offline

    JigokuSaru

    @au2001ok but will this work if the item already has a lore/enchantment?
     
  6. Offline

    mine-care

    @au2001 try avoiding spoon feeding people that most of the time leads to a copy paste and they gain nuthing out of it, let them experiment a little bit :- )
     
    CodePlaysMinecraft likes this.
  7. Offline

    JigokuSaru

    @mine-care
    in all honesty I think its more of a correction dince it was off my original code at the top
     
    au2001 likes this.
  8. Offline

    au2001

    @mine-care ok then :D but I only added one line to his code :p


    @JigokuSaru This will work with any item.
    That is a well known issue in bukkit (it might be on purpose, but I doubt it).
    When you modify a player's inventory, you need to update it.
    You do that by using player.updateInventory();
    So you would add this line once you're done with modifying the player's inv.
    This will make all the "vanished" items appear, and should fix everything.

    I hope this helped you,
    - au2001
     
    mine-care likes this.
  9. Offline

    JigokuSaru

  10. @JigokuSaru If the problem is solved, make sure to set this thread to solved by going to Thread Tools (at the top) > Edit Title > Prefix > Solved. See this for more information. :)
     
  11. Offline

    JigokuSaru

    @CodePlaysMinecraft
    I cant change that since my phone wont let me, im on a phone. Sorry if this sounds rude, but if its seems solved and you see that it was stated then just take heed and no need to comment, It is very difficult to change that for some people i think till they acquire internet on a computer of some sort
     
  12. Offline

    nverdier

    CodePlaysMinecraft likes this.
  13. Offline

    JigokuSaru

    @nverdier
    lol so far ur always there for me XD
     
Thread Status:
Not open for further replies.

Share This Page