Solved Can't remove ItemStack of a inventory

Discussion in 'Plugin Development' started by TheStarkPvP, Jul 22, 2016.

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

    TheStarkPvP

    Hey! I'm having a problem: I've created a ItemStack and added him to one inventory, but I'm not able to remove. My code:

    Code:
    // Coins class
    
    public static ItemStack Galeao(ItemStack item){
            ItemStack newItem = item;
            item = new ItemStack(Material.INK_SACK, 1, (short) 11);
            ItemMeta itemmeta = item.getItemMeta();
            itemmeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            ArrayList<String> itemlore = new ArrayList<>();
            itemlore.add("Vale 17 Sicles");
            itemmeta.setDisplayName("§6Galeão");
            itemmeta.setLore(itemlore);
            newItem.setItemMeta(itemmeta);
            return newItem;
        }
    
    // Listener class
    // There's a InteractEvent with a action right click sign check. (I didn't forget EventHandler)
    if(s.getLine(0).equals(ChatColor.DARK_BLUE + "[HPEconomy]")){
           if(s.getLine(1).equals("Remove")){
                  if(s.getLine(2).equals("1 Galeão")){
    p.getInventory().remove(Coins.Galeao(new ItemStack(Material.INK_SACK, 1, (short) 11)));
                  }
           }
    }
    
    // I'm using this to add the ItemStack to the inventory:
    p.getInventory().addItem(Coins.Galeao(new ItemStack(Material.INK_SACK, 1, (short) 11)));
    Well... I don't see why it don't work, as I'm using the same ItemStack to add the item and remove it.
    Any help is appreciated, thanks!
     
  2. Offline

    Zombie_Striker

    @TheStarkPvP
    That is because you are trying to remove a new itemstack. How can you remove something that has just been created? You need to use the instance that is already in the inventory.
     
  3. Offline

    MordorKing78

    You're trying to remove a new itemstack, that's not possible.

    EDIT: Sorry I did not see that @Zombie_Striker had already covered this, anyways that's your problem ;)
     
  4. Offline

    TheStarkPvP

    Hey! Thank you both for help, but I still have a problem. I don't know how to use the ItemStack without a new, when I try it, the eclipse tell me to make a new method ItemStack(Material, short), and I think is not really needed. I will be grateful if someone help me!
     
  5. @TheStarkPvP
    What you need to do is define the ItemStack as a variable. Then use that variable to both remove and add the item.
     
  6. Offline

    TheStarkPvP

    @AlvinB
    So, I did:

    Code:
    ItemStack nis = Coins.Nuque(new ItemStack(Material.INK_SACK, 1, (short) 14));
    
    //code
    
    RightClickSign event...
    SignLinesCheck...
    
    if(!(p.getInventory().containsAtLeast(nis, 29))){
                                        p.sendMessage(ChatColor.RED + "Você não tem nuques o suficiente para realizar esta transação.");
                                    } else
                                    if(p.getInventory().containsAtLeast(nis, 29)){
                                        nis = Coins.Nuque(new ItemStack(Material.INK_SACK, 29, (short) 14));
                                        p.getInventory().removeItem(nis);
    
    }
    And I'm still receiving the message "Você não tem nuques o suficiente para realizar esta transação."
    Someone may help?
     
  7. @TheStarkPvP
    Don't redefine your "nis" variable right before you use it.
     
  8. Offline

    TheStarkPvP

    @AlvinB I need remove 29 "nis" but the initial nis have a amount of 1, and the removeItem method don't accept one int, just a ItemStack
     
  9. @TheStarkPvP
    Where do you add the item to your inventory?
     
  10. Offline

    TheStarkPvP

    @AlvinB
    Code:
    if(p.hasPermission("hpeconomy.opuse")){
                        if(s.getLine(0).equals(ChatColor.DARK_BLUE + "[HPEconomy]")){
                            if(s.getLine(1).equals("Ganhar")){
                                if(s.getLine(2).equals("1 Galeão")){
                                    p.getInventory().addItem(gis);
                                }
                                if(s.getLine(2).equals("1 Sicle")){
                                    p.getInventory().addItem(sis);
                                }
                                if(s.getLine(2).equals("1 Nuque")){
                                    p.getInventory().addItem(nis);
                                }
                            }
                        }
                    }
     
  11. @TheStarkPvP
    Alright, so if you want to remove it, it has to be the exact same ItemStack you add, you can't modify it anywhere.
     
  12. Offline

    TheStarkPvP

    @AlvinB
    But I add "nis" and remove "nis" is the same itemstack, right?

    EDIT:

    Af, is not like I can use "(old ItemStack(...))", I just know how to use "(new ItemStack(...))" and I don't find anywhere how to use ItemStack without the new :/
     
    Last edited: Jul 23, 2016
  13. Offline

    Zombie_Striker

    The word you're looking for may be "Ninja'd" ;)
    I am not suggestion you reference old objects or create new objects. Instead, loop through the player's inventory and find the object you want to remove. After that, use the .remove() method on that itemstack.
     
  14. Offline

    TheStarkPvP

    @Zombie_Striker
    Ok, so I did it:
    Code:
    // PlayerInteractEvent
            ItemStack galleon1 = Coins.Galeao(new ItemStack(Material.INK_SACK, 1, (short) 11));
            ItemStack sickle17 = Coins.Sicle(new ItemStack(Material.INK_SACK, 17, (short) 7));
            Player p = e.getPlayer();
            int GalleonSlot = 0;
            int GalleonAmount = 0;
            int SicleSlot = 0;
            int SicleAmount = 0;
            if(p.getInventory().contains(Material.INK_SACK)){
                if(p.getInventory().getItem(p.getInventory().first(Material.INK_SACK)).getItemMeta().getDisplayName().equals("§6Galeão")){
                    GalleonSlot = p.getInventory().first(Material.INK_SACK);
                    GalleonAmount = p.getInventory().getItem(GalleonSlot).getAmount();
                } else
                if(p.getInventory().getItem(p.getInventory().first(Material.INK_SACK)).getItemMeta().getDisplayName().equals("§7Sicle")){
                    SicleSlot = p.getInventory().first(Material.INK_SACK);
                    SicleAmount = p.getInventory().getItem(SicleSlot).getAmount();
                }
    
    // Checking if the right clicked block is a sign...
    
    if(s.getLine(0).equals(ChatColor.DARK_BLUE + "[Converter]")){
                            if(s.getLine(1).equals("1 Galeão")){
                            if(s.getLine(2).equals("para")){
                            if(s.getLine(3).equals("17 Sicles")){
                                if(p.getInventory().contains(Material.INK_SACK)){
                                    if(GalleonAmount == 0){
                                        p.sendMessage("§cVocê não possui galeões o suficiente para realizar esta transação!");
                                        e.setCancelled(true);
                                    } else
                                    if(GalleonAmount >= 1){
                                        p.getInventory().setItem(GalleonSlot, Coins.Galeao(new ItemStack(Material.INK_SACK, GalleonAmount - 1, (short) 11)));
                                        p.getInventory().addItem(sickle17);
                                        p.sendMessage("§aTransação realizada com sucesso!");
                                    }
                                }
                            }
                        }
                    }
                }
    Is "working", but I just can use the sign if the "galleon1" is the first Material.INK_SACK in the inventory, so, if it is in the second slot of the hotbar and the first is free and I use the sign once, it will add "sickle17" in the first slot and I just can use again if I move the "sickle17".
    Also, I need to know if there's something like (or better) p.updateInventory(); that's deprecated.
    Thanks!
     
    Last edited: Jul 23, 2016
  15. @TheStarkPvP
    Can you clarify what your code is supposed to do?
     
  16. Offline

    Zombie_Striker

    @TheStarkPvP
    A few points about your code:
    1. If you want to get the item that is in the player's hand, use "Player#getItemIn(Main)Hand". I assumed that you just wanted to get any item in my previous post.
    2. If the amount is equal to one, subtracting 1 from the amount will not remove the item. You need to actually use the .remove command if you want to remove the item.
    3. Stick to Java Naming conventions
    4. Make sure that the player has the item in their hand before you create any variables. No need to create four ints and two itemstacks if the player is not even holding anything.
     
  17. Offline

    TheStarkPvP

    @AlvinB
    Code:
    // PlayerInteractEvent
    // Here, I did some ItemStacks to make it eazy after
            ItemStack galleon1 = Coins.Galeao(new ItemStack(Material.INK_SACK, 1, (short) 11));
            ItemStack sickle17 = Coins.Sicle(new ItemStack(Material.INK_SACK, 17, (short) 7));
    
            Player p = e.getPlayer();
    
    // Then I've created some ints, to know the slot and amount of galleons/sickles
            int GalleonSlot = 0;
            int GalleonAmount = 0;
            int SicleSlot = 0;
            int SicleAmount = 0;
    
    // Here, I check if the player contains Ink Sack
            if(p.getInventory().contains(Material.INK_SACK)){
                
    // Then I see the first slot (because I don't know a way to see in all inventory) that have a Ink_Sack with the name "§6Galeão"
    if(p.getInventory().getItem(p.getInventory().first(Material.INK_SACK)).getItemMeta().getDisplayName().equals("§6Galeão")){
    
    // And I set the GalleonSlot and the amount of Galleons that I have
                    GalleonSlot = p.getInventory().first(Material.INK_SACK);
                    GalleonAmount = p.getInventory().getItem(GalleonSlot).getAmount();
                } else
                
    // Same thing...
    if(p.getInventory().getItem(p.getInventory().first(Material.INK_SACK)).getItemMeta().getDisplayName().equals("§7Sicle")){
                    SicleSlot = p.getInventory().first(Material.INK_SACK);
                    SicleAmount = p.getInventory().getItem(SicleSlot).getAmount();
                }
    // Checking if the right clicked block is a sign...
    // Check if the sign is the sign I want
    if(s.getLine(0).equals(ChatColor.DARK_BLUE + "[Converter]")){
                            if(s.getLine(1).equals("1 Galeão")){
                            if(s.getLine(2).equals("para")){
                            if(s.getLine(3).equals("17 Sicles")){
    // Check if player have Ink sack
                                if(p.getInventory().contains(Material.INK_SACK)){
    
    // Check if the amount of galleons is zero, and if that is then I cancel the event
                                    if(GalleonAmount == 0){
                                        p.sendMessage("§cVocê não possui galeões o suficiente para realizar esta transação!");
                                        e.setCancelled(true);
                                    } else
    // Check if the amount of galleons is higher or one, and then I set the amount to GalleonAmount - 1 and add 17 Sickles, and msg that worked.
                                    if(GalleonAmount >= 1){
                                        p.getInventory().setItem(GalleonSlot, Coins.Galeao(new ItemStack(Material.INK_SACK, GalleonAmount - 1, (short) 11)));
                                        p.getInventory().addItem(sickle17);
                                        p.sendMessage("§aTransação realizada com sucesso!");
                                    }
                                }
                            }
                        }
                    }
                }

    @Zombie_Striker
    The point is that I don't want to force the item to be in the hand, it can be in any place of the inventory, and the variables are working (even if the number of galleons is one). By the way I'm using craftbukkit 1.8, so all hands are the main hand.
    Thanks!
     
  18. @TheStarkPvP
    You can use Inventory.contains(ItemStack) to check if an item is in the inventory, or if you don't want to test for all the properties of an ItemStack, you can loop through the inventory using an enhanced foor loop and check each item for your specific conditions.
     
  19. Offline

    TheStarkPvP

    @AlvinB
    May you give a example?
     
  20. @TheStarkPvP
    Alright so,
    Code:java
    1. Inventory.getContents()
    returns an ItemStack array. Loop through this array using an enhanced for loop and check for the conditions you want.
     
  21. Offline

    TheStarkPvP

    Well... I have tried a lot today to do this plugin, but don't success, I will just make a plugin request and if someone finish it for me, I will study the code. Thanks anyway for try!
     
  22. Offline

    Hyperenci

    Well even if they make it for you that doesnt mean youll have access to the source code.
     
  23. Offline

    TheStarkPvP

    That's true, but when playing I will try to think what they've used to code everything.
     
Thread Status:
Not open for further replies.

Share This Page