Solved ItemStack#IsSimilar() not working?

Discussion in 'Spigot Plugin Development' started by ratquaza, Feb 5, 2020.

  1. Currently trying to create a Buy Sign that players can make, but I'm having issues with detecting if an ItemStack in an inventory is similar to another one?

    Previously, I used ItemStack#equals() to compare items, which would work but the amount would be an issue, so I switched to use ItemStack#IsSimilar() and now it's not working at all.

    The basic functionality of my code is a player creates a sign, and then right clicks it with the item they'd like to sell. Right clicking the sign again would open an inventory to place stock. If items placed in the inventory don't match the sold item, they'll be given back to the player.

    Here's the Buy Shop class:

    Code (Java):
    public class BuyShop {
       
    public static HashMap<Location, BuyShop> shops = new HashMap<>();


        public UUID owner;
        public ItemStack item;
        public int price;
        public Location location;

        public Inventory inventory;
        private int itemCount;

        private BuyShop() {
            this.inventory = Bukkit.createInventory(null, 54);
        }

        private BuyShop(Player owner, ItemStack item, int price) {
            this.owner = owner.getUniqueId();
            this.item = item;
            this.price = price;
            this.inventory = Bukkit.createInventory(null, 54);
            updateItemCount();
        }

        public void updateItemCount() {
            itemCount = 0;
            for (ItemStack i : inventory.getContents()) {
                if (i != null && i.isSimilar(item)) {
                    itemCount += i.getAmount();
                }
            }
        }

        public void remove() {
            Player playerOwner = Bukkit.getPlayer(owner);
            for (int i = 0; i < inventory.getSize(); i++) {
                ItemStack item = inventory.getItem(i);
                if (item != null) {
                    playerOwner.getInventory().addItem(item);
                    inventory.clear(i);
                }
            }
            shops.remove(location);
        }
    }
    (Removed functions that are irrelevant)

    Now, the event handler that listens to closing inventories:

    Code (Java):

        private static HashMap<Player, Location> interactedShops = new HashMap<>(); // This updates in another listener

       
        @EventHandler
        public void onInventoryClose(InventoryCloseEvent e) {
            if (interactedShops.containsKey(e.getPlayer()) && BuyShop.shops.containsKey(interactedShops.get(e.getPlayer()))) {
                Sign sign = ((Sign) e.getPlayer().getWorld().getBlockAt(interactedShops.get(e.getPlayer())).getState());
                BuyShop shop = BuyShop.shops.get(interactedShops.get(e.getPlayer()));
                if (shop.inventory.equals(e.getInventory())) {
                    for (int i = 0; i < shop.inventory.getSize(); i++) {
                        if (shop.inventory.getItem(i) != null && !shop.inventory.getItem(i).isSimilar(shop.item)) {
                            e.getPlayer().getInventory().addItem(shop.inventory.getItem(i));
                            System.out.println(shop.inventory.getItem(i).isSimilar(shop.item)); // Always false
                            shop.inventory.clear(i);
                        }
                    }
                    shop.updateItemCount();
                    sign.setLine(3, "Stock: " + shop.getCount());
                    sign.update();
                }
            }
        }
    Anyone know what's going on?
     
  2. Wait, so where and what is your problem exactly? Are all the ItemStack#isSimilair() checks not working, or a specific one?
     
  3. Just as you posted this, I figured out the issue. It seemed to be when I would set the Item value in the BuyShop instance, I wouldn't clone the item but instead use the direct copy of it which would cause it to be false.