Solved Compare ItemStacks

Discussion in 'Plugin Development' started by AntonioC94, Apr 19, 2020.

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

    AntonioC94

    Hello there,

    I just have a question and is ¿How can I compare an ItemStack dropped with an ItemSack saved in ArrayList?
    I made a method to create a ItemStack and save that ItemStack in ArrayList with a custom name, I just want to cancell the drop of that item but I have always a "false" when I check if that items are equals.

    Code:
        @EventHandler
        public void cancelDropp(PlayerDropItemEvent event) {
            Player player = event.getPlayer();
            ItemStack item= event.getItemDrop().getItemStack();
            if(main.hat.contains(item)){
                event.setCancelled(true);
                player.sendMessage("You can not");
            }
        }
    
    Edit: Version 1.15.2
     
  2. Offline

    alant7_

    Hello,
    You might have to compare itemstack with each one in the array list using method 'compare(another itemstack)'. If that does not work then try comparing item's lore with ones in the array list.
     
  3. Offline

    KarimAKL

    @AntonioC94 Is the amount different? If so, you'll have to use isSimilar(ItemStack) instead of equals(Object).
     
  4. Offline

    AntonioC94

    @alant7_ Thanks I will try to use the lore :D
    @KarimAKL No, The amount will be just one always

    EDIT:
    Just for ask but... The "easy" way to do that is compare the lore right? and.. I think its simple but how can I do that?
    I mean, set the lore like
    Code:
    itemMeta.setLore(Arrays.asList(name));
    
    and then compare the lore of the dropped item and the item in the ArrayList, but how exactly?

    Something like this?
    Code:java
    1.  
    2. public void cancelarDrop(PlayerDropItemEvent e) {
    3. Player p = e.getPlayer();
    4. ItemStack dropped = e.getItemDrop().getItemStack();
    5. ItemMeta meta = (ItemMeta) dropped.getItemMeta().getLore();
    6. if (dropped != null) {
    7. if (meta.equals(*******)) { //Im not sure about what to put here hehe
    8. }
    9. }
    10. }
    11.  


    SOLVED: I just checked if the item to drop has a custom lore , is not necessary to check if that item is on arraylist
     
    Last edited: Apr 20, 2020
Thread Status:
Not open for further replies.

Share This Page