Checking what an item's lore contains?

Discussion in 'Spigot Plugin Development' started by Oahu, Jun 11, 2017.

  1. Okay, so basically I am trying to give a player a potion effect if they have certain armor. Could someone tell me how I would check what the lore of the specified contains?

    Here is my code:

    Code (Text):
    package us.oahudev.regenarmor;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scheduler.BukkitRunnable;


    /**
    * Created by aidan on 6/10/2017.
    */
    public class LoreRunnable extends BukkitRunnable {

        private final RegenArmor plugin;

        public LoreRunnable(RegenArmor plugin) {
            this.plugin = plugin;
        }

        @Override
        public void run() {
            for (Player p : Bukkit.getServer().getOnlinePlayers()) {
                if ((p.getInventory().getHelmet() == null) || (p.getInventory().getChestplate() == null)
                        || (p.getInventory().getLeggings() == null) || (p.getInventory().getBoots() == null)) {
                    return;
                }

                if (p.getInventory().getHelmet().getItemMeta() == null || p.getInventory().getChestplate().getItemMeta() == null
                        || p.getInventory().getLeggings().getItemMeta() == null || p.getInventory().getBoots().getItemMeta() == null) {
                    return;
                }

                if (p.getInventory().getHelmet().getItemMeta().getLore() == null || p.getInventory().getChestplate().getItemMeta().getLore() == null
                        || p.getInventory().getLeggings().getItemMeta().getLore() == null || p.getInventory().getBoots().getItemMeta().getLore() == null) {
                    return;
                }

                if ((p.getInventory().getHelmet().getItemMeta().getLore().contains("§cRegen 1"))
                        || (p.getInventory().getChestplate().getItemMeta().getLore().contains("§cRegen 1"))
                        || (p.getInventory().getLeggings().getItemMeta().getLore().contains("§cRegen 1"))
                        || (p.getInventory().getBoots().getItemMeta().getLore().contains("§cRegen 1"))) {
                    p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1000000000, 0));
                } else if ((p.getInventory().getHelmet().getItemMeta().getLore().contains("§cRegen 2"))
                        || (p.getInventory().getChestplate().getItemMeta().getLore().contains("§cRegen 2"))
                        || (p.getInventory().getLeggings().getItemMeta().getLore().contains("§cRegen 2"))
                        || (p.getInventory().getBoots().getItemMeta().getLore().contains("§cRegen 2"))) {
                    p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1000000000, 1));
                } else if ((p.getInventory().getHelmet().getItemMeta().getLore().contains("§cRegen 3"))
                        || (p.getInventory().getChestplate().getItemMeta().getLore().contains("§cRegen 3"))
                        || (p.getInventory().getLeggings().getItemMeta().getLore().contains("§cRegen 3"))
                        || (p.getInventory().getBoots().getItemMeta().getLore().contains("§cRegen 3"))) {
                    p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1000000000, 2));
                } else if ((p.getInventory().getHelmet().getItemMeta().getLore().contains("§cRegen 4"))
                        || (p.getInventory().getChestplate().getItemMeta().getLore().contains("§cRegen 4"))
                        || (p.getInventory().getLeggings().getItemMeta().getLore().contains("§cRegen 4"))
                        || (p.getInventory().getBoots().getItemMeta().getLore().contains("§cRegen 4"))) {
                    p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1000000000, 3));
                } else if ((p.getInventory().getHelmet().getItemMeta().getLore().contains("§cRegen 5"))
                        || (p.getInventory().getChestplate().getItemMeta().getLore().contains("§cRegen 5"))
                        || (p.getInventory().getLeggings().getItemMeta().getLore().contains("§cRegen 5"))
                        || (p.getInventory().getBoots().getItemMeta().getLore().contains("§cRegen 5"))) {
                    p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1000000000, 4));
                }

                if ((!p.getInventory().getHelmet().getItemMeta().getLore().contains("§cRegen 1"))
                        || (!p.getInventory().getChestplate().getItemMeta().getLore().contains("§cRegen 1"))
                        || (!p.getInventory().getLeggings().getItemMeta().getLore().contains("§cRegen 1"))
                        || (!p.getInventory().getBoots().getItemMeta().getLore().contains("§cRegen 1"))
                        || (!p.getInventory().getHelmet().getItemMeta().getLore().contains("§cRegen 2"))
                        || (!p.getInventory().getChestplate().getItemMeta().getLore().contains("§cRegen 2"))
                        || (!p.getInventory().getLeggings().getItemMeta().getLore().contains("§cRegen 2"))
                        || (!p.getInventory().getBoots().getItemMeta().getLore().contains("§cRegen 2"))
                        || (!p.getInventory().getHelmet().getItemMeta().getLore().contains("§cRegen 3"))
                        || (!p.getInventory().getChestplate().getItemMeta().getLore().contains("§cRegen 3"))
                        || (!p.getInventory().getLeggings().getItemMeta().getLore().contains("§cRegen 3"))
                        || (!p.getInventory().getBoots().getItemMeta().getLore().contains("§cRegen 3"))
                        || (!p.getInventory().getHelmet().getItemMeta().getLore().contains("§cRegen 4"))
                        || (!p.getInventory().getChestplate().getItemMeta().getLore().contains("§cRegen 4"))
                        || (!p.getInventory().getLeggings().getItemMeta().getLore().contains("§cRegen 4"))
                        || (!p.getInventory().getBoots().getItemMeta().getLore().contains("§cRegen 4"))
                        || (!p.getInventory().getHelmet().getItemMeta().getLore().contains("§cRegen 5"))
                        || (!p.getInventory().getChestplate().getItemMeta().getLore().contains("§cRegen 5"))
                        || (!p.getInventory().getLeggings().getItemMeta().getLore().contains("§cRegen 5"))
                        || (!p.getInventory().getBoots().getItemMeta().getLore().contains("§cRegen 5"))) {
                    if (p.hasPotionEffect(PotionEffectType.REGENERATION)) {
                        p.removePotionEffect(PotionEffectType.REGENERATION);
                    }
                }
            }
        }
    }
     
     
  2. Why are you not using a for loop?

    Code (Text):

    for(int i = 1; i <= 5; i++) {
    if(!(p.getInventory().getHelmet().getItemMeta().getLore().contains("§cRegen " + i) || p.getInventory().getChestplate.getItemMeta().getLore().contains("§cRegen " + i) ||
    p.getLeggings().getItemMeta().getLore().contains("§cRegen " + i) || p.getBoots().getItemMeta().getLore().contains("§cRegen " + i))) {
      // EXECUTE YOUR CODE HERE
      }
    }
     
     
    #2 DamaTheDev, Jun 11, 2017
    Last edited: Jun 12, 2017
  3. Fixed that.

    Do you know how to check what a lore contains? Because that is the only part of my code that isn't working.
     
  4. Yes of course it is not working. Contains checks whether ab arraylist contains an object. This means the object instances need to be the same and in this case even if strings match they still are a different instance of an object so they won't match (because using "Regen..." you are creatng a new instance instead of using an already created instance with the same text). Loop through every line in the lore and then do equalsIgnoreCase or contains or whatever to compare the strings.
     
  5. That's not right.
    The method contains checks for equal objects and not for the same object as you can see here in the JavaDocs: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#contains-java.lang.Object-
    So it should work as long as the strings are equal.
     
  6. Update: I am still looking for a solution.
     
  7. One thing to note, that if any single piece of armor is not set(or null), nothing after will execute.
     
  8. I think it is because of the color codes. Instead of using the '§' symbol, try this:
    Code (Text):
    p.getInventory().getHelmet().getItemMeta().getLore().contains(ChatColor.RED + "Regen 1")
    EDIT: Don't just do it like that for that one line, continue it all the way down. :)
     
  9. Or just do this when you check
    Code (Text):
    ChatColor.stripColor("");
    Also you can use another for loop instead of going through the Boots, Leggings, Chestplate, and Helmet. Instead
    Code (Text):
    for(ItemStack itemStack : p.getInventory().getArmorContents()) {
        //Code
    }
     
  10. You would actually need to start at 1. Starting at 0 adds redundancy as there is nothing to do when Regen == 0.

    Code (Text):
    for(int i = 1; i < 6; i++) {
        //Code
    }
     
  11. I just rushed the loop, didn't really check actual values, but yep. Thanks for the clarification.
     
    • Like Like x 1
  12. Hey, the way you do that is not really optimized. You have to iterate the armor of the player, check the lore and set effect to the player.

    Code (Text):
    // you have to check if clicked item is an armor piece ^^
    // Iterate armor contents
    for(ItemStack armor : player.getInventory().getArmorContents()) {
        for (int i = 0; i < 5; i++) {
            // check if item has item meta, to avoid error on click on a normal armor piece
            if (!armor.hasItemMeta()) return;
            // check if lore contains "Regen (1|2|3|4|5)"
            if (!armor.getItemMeta().getLore().contains("Regen " + (i + 1))) return;
           
            // add potion effect to the player
            player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1000000000, i));
        }
    }

    This code should work but all the time when you click on an armor piece a loop will be threw, I suggest you to do that with if condition. ;)