Method not working?

Discussion in 'Plugin Development' started by plisov, Aug 16, 2017.

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

    plisov

    Hi,
    I made a method that's supposed to add a set of strings into a list and then return it in the method. It's working fine however whenever I try to check and see if an item's lore contains one of the strings in the list, it doesn't work. Here is what I have.

    Method class
    Code:
    package me.plisov.ventureland.enchantments;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    
    public class ItemLores {
    
        public static List<String> lores = new ArrayList<String>();
    
        public static String manaRegen = ChatColor.GRAY + "Mana Regeneration";
    
        public static String getLores() {
    
            lores.add(manaRegen);
    
            return lores.toString();
        }
    }
    
    Check
    Code:
    
                                                if (player.getInventory().getItemInMainHand().getItemMeta().getLore()
                                                        .contains(ItemLores.getLores())) {
                                                    player.sendMessage("WORKING!!!");
                                                }
    I also printed out the list and it shows that one line, Mana Regeneration, multiple times. So what I'm thinking is the check is checking if the item's lore has all of those strings and not just one.

    Any help is much appreciated :)
     
  2. Offline

    Zombie_Striker

    @plisov
    Every time you call getLores, it adds a new line each time. Either create a new arraylist for each lore, or only add the line once.

    You are checking if the lore contains the memory location of the Arraylist object, not the actual line. If you want to check if "manaRegen" is in the lore, get that string instead of the lores.toString()
     
  3. Offline

    plisov

    The thing with that is that I'm planning to add multiple lines to the list. Not just that one. What do you mean add the line once? Isn't that what I'm doing?
     
  4. Offline

    Zombie_Striker

    @plisov
    Nope. The arraylist is the same instance every time getLore is called, so that line of lore will be added every time getLore is called.

    If you want to check this, print out lores.size(). What you will see is, every time getLore is called, the size will increase by 1. To fix this, use the static function (like below) to add the line:
    Code:
    static{
    lores.add(manaRegen);
    }
    Because this is a static method, this will only be called once, so the line will be added only once.

    If you want multiple lines of lore, store one line somewhere that can easily identify the item. You don't need to check if all the lines of lore are the same; you just need to check for one.
     
  5. Offline

    plisov

    That's what I've been trying to do all along :p
    Ill try what you said

    @Zombie_Striker
    EDIT

    Here is the run down of what I'm trying to do. I'm making an enchantment plugin where you click the enchantment onto your weapon and it stays on it. I have that working. However I'm also trying to make it so that when you click another different enchantment onto the weapon, it'll check if it has a previous enchantment on it, which would be the strings in the array and if it does, replace that old enchantment with the new enchantment. These enchantments are just strings in the lore. So what I'm having trouble with is checking to see if the item's lore contains any of the strings from the list. How could I achieve this?
     
    Last edited: Aug 17, 2017
  6. Offline

    Zombie_Striker

    @plisov
    Why not just create a new enchantment object instead of using stings? If you do that, all you need to do is loop through all the custom enchantments you crated and check if it is applied to the sword using ItemStack#containsEnchantment().

    In case you do not know how to create your own enchantment:
    https://bukkit.org/threads/how-to-make-an-item-glow-with-no-enchant.374594/
    (Although this was for creating an enchantment without a name, all you need to do is change the getName method to return your enchantment's name)
     
  7. Offline

    plisov

    I've created the enchantment object and its working fine however how do I make it show the name of the enchantment and have the Glow() take a string and not an int?
     
  8. Offline

    Zombie_Striker

    @plisov
    The int represents the enchantment's ID. You cant replace this. Just replace this with some number high enough so it does not conflict with any of the existing enchantments.

    What what be the purpose of the string?

    To give the enchantment a name, change the getName() method so that it returns the enchantment's name.
     
  9. Offline

    plisov

    How exactly do you set the enchantment's name? I changed the getName method's return to be this.getName();

    EDIT:
    Oh so for every enchantment, I'd have to make a new class

    I thought there was a really easy way to get all my custom enchantments or the strings and then check all at once if the item contained any of those enchantments.
    @Zombie_Striker
     
    Last edited: Aug 17, 2017
  10. Offline

    Zombie_Striker

    @plisov
    1. calling this.getName would just be the method calling itself, creating a never ending loop. Use something like the following
      Code:
      public String getName(){
         return "myEnchantment's name"
      }
    2. Yes.
    3. Well, for looping through a list of all your custom enchantments should be easy. Just have it where, in the onEnable, you register the enchantment and then add it to a List. From there, when you need to check if the item has one of the enchantments, for loop through the list and check if the itemstack#containsEnchantment. If this is true for any of the enchantments, then the item already had one of the enchantments.
     
  11. Offline

    plisov

    Ah thanks. I seem to have gotten the enchantment to add to the item and now i'm trying to replace the enchantment with a new enchantment. It doesnt seem to work. All it does is it adds the new enchantment to the item without removing the old one. Here is the method that is supposed to replace the enchantment but it prints out 1 and Error
    Code:
    package me.plisov.ventureland.enchantments;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class EnchantmentsAPI {
    
        public static void replaceEnchantment(Player player, Enchantment currentEnch, Enchantment newEnch) {
    
            ItemStack item = player.getInventory().getItemInMainHand();
            ItemMeta itemMeta = item.getItemMeta();
    
            player.sendMessage("1");
           
            if (itemMeta.hasEnchant(currentEnch)) {
                item.removeEnchantment(currentEnch);
                item.addEnchantment(newEnch, 1);
    
                player.sendMessage("2");
    
                if (itemMeta.getLore().contains(currentEnch.getName())) {
    
                    player.sendMessage("3");
    
                    for (String line : itemMeta.getLore()) {
    
                        player.sendMessage("4");
                        if (line.contains(currentEnch.getName())) {
    
                            player.sendMessage("5");
                           
                            List<String> lore = new ArrayList<String>();
                            lore = itemMeta.getLore();
                           
                            itemMeta.getLore().remove(line);
                           
                            line = newEnch.getName();
                           
                            itemMeta.setLore(lore);
    
                            player.sendMessage("6");
                        }
                    }
                }
            } else {
    
                player.sendMessage("Error");
                return;
            }
        }
    }
    
    Here is where I actually use the method
    Code:
    
                if (nameTagMeta.getLore().contains(ItemLores.xpBoost)) {
    
                    List<String> nameTagLore;
                    if (itemMeta.hasLore()) {
                        nameTagLore = itemMeta.getLore();
                    } else {
                        nameTagLore = new ArrayList<String>();
                    }
                    if (nameTagLore.contains(ItemLores.xpBoost)) {
                        return;
    
                    }
    
                    ManaRegeneration manaRegen = new ManaRegeneration(1000);
                    XPBoost xpBoost = new XPBoost(1001);
                   
                    EnchantmentsAPI.replaceEnchantment(player, manaRegen, xpBoost);
    
                    // int chance = LoreHelper.readLoreSuccessChance(bookMeta);
    
                    // SuccessMethod method = new SuccessMethod(chance);
                    // It needs to be its own instance, so the chance stays the same
    
                    // if(method.success()) {
                   
                    itemMeta.addEnchant(xpBoost, 1, true);
    
                    event.setCancelled(true);
                    nameTagLore.add(xpBoost.getName());
    
                    // Glow glow = new Glow(32);
                    // itemMeta.addEnchant(glow, 1, true);
    
                    itemMeta.setLore(nameTagLore);
                    event.setCursor(new ItemStack(Material.AIR, 1));
                    if (itemMeta.getLore() == null) {
                        return;
                    }
                   
                    item.setItemMeta(itemMeta);
    
                    // } else {
    
                    // player.sendMessage("Failed");
    
                    // }
                }
            }
        }
    How would I fix the method to make it work?
     
Thread Status:
Not open for further replies.

Share This Page