ItemStack#isSimilar problem

Discussion in 'Spigot Plugin Development' started by DrOreo002, May 27, 2018.

  1. So I have this code on my InventoryClick event
    Code (Text):
    if (e.getCurrentItem().isSimilar(paginatedInventory.getNextButton().getItem())) {
                    System.out.println("Pass"); // Not called
                    GUIButton button = paginatedInventory.getNextButton();
                    button.getAction().onClick(e);
                    return;
                }
    And you might me wondering, what is the nextButton looks like?. Well here it is

    Code (Text):
    this.backButton = new GUIButton(new ItemStack(Material.ARROW));
            this.nextButton = new GUIButton(new CustomItem(Material.ARROW, "&7[ &aPage Selanjutnya &7]")).setAction(event -> {
                System.out.println("Next page...");
                Player player = (Player) event.getWhoClicked();
                nextPage(player);
            });
            inven.setItem(next, nextButton.getItem());
            inven.setItem(back, backButton.getItem());
    My GUIButton class (Ignore the not make sense things here pls)
    Code (Text):
    public class GUIButton extends ItemStack {

        private ButtonListener action;
        private ItemStack item;

        public GUIButton(ItemStack item) {
            super(item);
            this.item = item;
        }

        public GUIButton setAction(ButtonListener action) {
            this.action = action;
            return this;
        }

        public GUIButton name(String name) {
            ItemMeta meta = getItemMeta();
            meta.setDisplayName(color(name));
            setItemMeta(meta);
            return this;
        }

        public ButtonListener getAction() {
            return action;
        }

        public ItemStack getItem() {
            return item;
        }

        private String color(String text) {
            return ChatColor.translateAlternateColorCodes('&', text);
        }
    }
    And the problem is, it say that its not similar.., why?
     
  2. You are creating new items for every player. Are you sure, that they are all similar and that you are not using the name(str) method in between those calls?
     
  3. New items for every player?. I don't remember that I do that

    Btw, yes its similar
     
  4. You are unnecessarily making CustomItem and GUIButton extend ItemStack. You then need to handle the extensions for ItemStack correctly (since your CustomItem doesn't seem to be setting the parent ItemStack data correctly).
     
    • Agree Agree x 1
  5. In your GUIButton#name method, you are not applying the changes to the `item` variable but you are using it. Either apply those changes or use `paginatedInventory.getNextButton()` instead of `paginatedInventory.getNextButton().getItem()`. I would remove the variable completely.
     
  6. Alright. I've updated the code. Here's the class now. (Apparently its still not working)
    Code (Text):
    public class GUIButton {

        private ButtonListener action;
        private ItemStack item;

        public GUIButton(ItemStack item) {
            this.item = item;
        }

        public GUIButton setAction(ButtonListener action) {
            this.action = action;
            return this;
        }

        public ButtonListener getAction() {
            return action;
        }

        public ItemStack getItem() {
            return item;
        }
    }
    Code (Text):
    public class CustomItem extends ItemStack {

        public CustomItem(Material mat) {
            super(mat);
        }

        public CustomItem(Material mat, String name) {
            super(mat);
            ItemMeta meta = getItemMeta();
            meta.setDisplayName(color(name));
            setItemMeta(meta);
        }

        private String color(String text) {
            return ChatColor.translateAlternateColorCodes('&', text);
        }
    }
    The rest of the code are still the same.
     
  7. Btw, I've added a debug message. Just to make sure if those item is similar or not

    same2.png
    Here's the result. Why?

    Edit (Update)

    same3.png
     
  8. Use the source code of .isSimilar() to figure out what makes it return false.
     
  9. How can I see the source code?
     
  10. Most IDEs have an option to view the declaration and implementation or a library's method. Try right clicking the method and find an option for them.