Item lore not updating

Discussion in 'Plugin Development' started by CheesyFreezy, Jan 1, 2016.

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

    CheesyFreezy

    Heey developers,
    Like the title says, the lore on an item is not updating. I'm making custom durability which works fine for the rest, but the lore isn't updating. I debugged some stuff and the lore is adding durability but it's not being added on the item.

    CODE:
    Code:
    public static void addDurability(ItemStack item, int amount) {
            String[] commaLine = ChatColor.stripColor(item.getItemMeta().getLore().toString()).split(",");
           
            int line = 0;
           
            if(Core.weaponList.contains(item.getType())) {
                line = 3;
            } else if(Core.armorList.contains(item.getType())) {
                line = 4;
            }
           
            if(commaLine[line].contains("[") && commaLine[line].contains("]")) {
                int currentDurability = getNumbers(commaLine[line]);
               
                ItemMeta meta = item.getItemMeta();
                List<String> currentLore = meta.getLore();
                if((currentDurability + amount) > 1500) {
                    currentLore.set(line, ChatColor.GRAY + "[DURABILITY: " + "1500" + "]");
                } else {
                    currentLore.set(line, ChatColor.GRAY + "[DURABILITY: " + (currentDurability + amount) + "]");
                }
               
                meta.setLore(currentLore);
                item.setItemMeta(meta);
            }
        }
     
  2. Offline

    mcdorli

    I think, if you pass a variable to a function, it just copies it, and uses that, not the same one. Try to return the item, and where you use this function, do something like
    Code:
    item = addDurability(item, 10)
    
    And please, don't abuse static
     
  3. Offline

    adam753

    @mcdorli
    Nope, that's C/C++. In Java, non-primary data types are references.

    @CheesyFreezy
    This looks like it should work so you should debug and find out what's happening in the code. Maybe one of your if-statements isn't doing what you expect.
     
  4. Offline

    mcdorli

    That's, why I put there the bold think word.
     
  5. Offline

    CheesyFreezy

    It's running everything all fine, but the itemstack that i'm trying to change is still the old itemstack.

    I also made a removeDurability, and that is working good, but the addDurability isn't.

    Code:
    public static void removeDurability(Player player, ItemStack item, int amount) {
            String[] commaLine = ChatColor.stripColor(item.getItemMeta().getLore().toString()).split(",");
           
            if(Core.weaponList.contains(item.getType())) {
                if(commaLine[3].contains("[") && commaLine[3].contains("]")) {
                    int currentDurability = getNumbers(commaLine[3]);
                   
                    if(currentDurability <= 1) {
                        player.getInventory().remove(item);
                    } else {
                        ItemMeta weaponMeta = item.getItemMeta();
                        List<String> currentLore = item.getItemMeta().getLore();
                        currentLore.set(3, ChatColor.GRAY + "[DURABILITY: " + (currentDurability - amount) + "]");
                       
                        weaponMeta.setLore(currentLore);
                        item.setItemMeta(weaponMeta);
                    }
                }
            } else if(Core.armorList.contains(item.getType())) {
                if(commaLine[4].contains("[") && commaLine[4].contains("]")) {
                    int currentDurability = getNumbers(commaLine[4]);
                   
                    if(currentDurability <= 1) {
                        player.getInventory().remove(item);
                    } else {
                        ItemMeta weaponMeta = item.getItemMeta();
                        List<String> currentLore = item.getItemMeta().getLore();
                        currentLore.set(4, ChatColor.GRAY + "[DURABILITY: " + (currentDurability - amount) + "]");
                       
                        weaponMeta.setLore(currentLore);
                        item.setItemMeta(weaponMeta);
                    }
                }
            }
        }
     
  6. Offline

    Zombie_Striker

    @CheesyFreezy
    If the itemstack is in an inventory or in the players hand, you have to update the inventory. Either use Inventory.update() or player.updateInventory() (Forget which it is).
     
  7. Offline

    CheesyFreezy

    I made a drag and drop system where you can drag and drop leather on (as an example) a leather helmet and it will gain durability from that. But if I do player.updateInventory() the clicked item will disappear.

    EDIT: I tested it out with another guy and it didn't happend to him, only to me.
     
    Last edited: Jan 2, 2016
  8. Offline

    teej107

    Everything in Java is pass-by-value and not pass-by-reference.
    That being said,
    this would be a way to overcome that however since you aren't changing the reference or value, you don't need to return or re-assign anything.

    @CheesyFreezy Try doing some rubberduck debugging.
     
  9. Offline

    adam753

    @teej107
    Well now that's just confusing.
     
Thread Status:
Not open for further replies.

Share This Page