Solved Issue with using placeholders in item lore

Discussion in 'Spigot Plugin Development' started by Drakonn, Jun 12, 2018.

Thread Status:
Not open for further replies.
  1. Hey, I am having a bit of an issue with my code. I am trying to have an itemstack, and in that itemstacks lore have %currentmin% and %currentmax% which when the gui is opened will be replaced with integers. This works for the first time the gui is opened however every time after that when I get the item its lore does not contain %currentmin% and %currentmax% it instead contains the previous values. I am really not sure what could be causing this as I only do AbstractItem.getItem() wich just returns the itemstack then I edit that itemstack, I never go back and set the itemstack in AbstractItem so next time I invoke AbstractItem.getItem() I would think It should give me a fresh item? Anyway here is all the relevant code.

    Code (Text):

        public static ItemStack setRangePlaceHolders(ItemStack item, Player player)
        {
            Bukkit.broadcastMessage("setting range placeholder for " + item.toString());
            List<String> lore = item.getItemMeta().getLore();
            List<String> newLore = new ArrayList<>();
            ItemMeta meta = item.getItemMeta();
            String minRange = "0";
            String maxRange = String.valueOf(ConfigManager.range);
            if(Wild.getInstance().maxRanges.containsKey(player.getUniqueId())) {
                maxRange = String.valueOf(Wild.getInstance().maxRanges.get(player.getUniqueId()));
                Bukkit.broadcastMessage("set maxrange to "+ String.valueOf(Wild.getInstance().maxRanges.get(player.getUniqueId())));
            }

            if(Wild.getInstance().minRanges.containsKey(player.getUniqueId())) {
                minRange = String.valueOf(Wild.getInstance().minRanges.get(player.getUniqueId()));
                Bukkit.broadcastMessage("set minrange to "+ String.valueOf(Wild.getInstance().minRanges.get(player.getUniqueId())));
            }

            for(String string : lore)
            {
                string = string.replaceAll("%currentmin%", minRange);
                string = string.replaceAll("%currentmax%", maxRange);
                newLore.add(string);
            }

            meta.setLore(newLore);
            item.setItemMeta(meta);
            return item;
        }
     

    Code (Text):
        public void openInventory(Player player)
        {
            List<AbstractItem> guiItems = ItemManager.getMainGuiItems();
            Inventory inv = Bukkit.createInventory(null, ItemManager.mainInvSize, ItemManager.mainInvName);

            for(AbstractItem guiItem : guiItems)
            {
                ItemStack item = guiItem.getItem();
                if(!player.hasPermission(guiItem.getPermission()))
                    item = Util.changeAccess(item, ConfigManager.accessFalse);

                if(player.hasPermission(guiItem.getPermission()))
                    item = Util.changeAccess(item, ConfigManager.accessTrue);

                item = Util.setRangePlaceHolders(item, player);

                inv.setItem(guiItem.getInvSlot(), item);
            }

            player.openInventory(inv);
        }

    Code (Text):
        private int invSlot;
        private ItemStack item;
        private ItemType type;
        private int cost;
        private String permission;
        private static List<AbstractItem> items = new ArrayList<>();

        public AbstractItem(ItemStack item, int invSlot, ItemType type, int cost, String permission) {
            this.item = item;
            this.invSlot = invSlot;
            this.type = type;
            this.cost = cost;
            this.permission = permission;
            items.add(this);
        }

        public static AbstractItem getAbstractItem(ItemStack item) {
            return items.stream().filter(abstractItem -> abstractItem.getItem().getItemMeta().getDisplayName().equalsIgnoreCase(item.getItemMeta().getDisplayName())
            && abstractItem.getItem().getType().equals(item.getType()))
                    .findFirst().orElse(null);
        }

        public int getInvSlot() { return invSlot; }

        public ItemStack getItem() { return item; }

        public ItemType getType() { return type; }

        public int getCost() { return cost; }

        public String getPermission() { return permission; }
     
  2. Change the "getItem()" method in AbstractItem to be "return item.clone();". You are, in fact, getting the same ItemStack, not a new one.
     
    • Winner Winner x 1
  3. Thanks for the help :)
     
Thread Status:
Not open for further replies.