Solved Looping through lore.

Discussion in 'Spigot Plugin Development' started by JacksonHogan, Jun 18, 2016.

Thread Status:
Not open for further replies.
  1. I'm trying to loop through a lore of an item, this is what I've got:

    Code (Text):
    ItemStack item = new ItemStack(player.getInventory().getBoots());
                        ItemMeta itemmeta = item.getItemMeta();
                        item.setItemMeta(itemmeta);
                        if(itemmeta.getLore() != null){
                            for(String s : itemmeta.getLore()){
                                if(ChatColor.stripColor(s).contains("Speed I")){
                                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100, 0));
                                    break;
                                }
                               
                                if(ChatColor.stripColor(s).contains("Speed II")){
                                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100, 1));
                                    break;
                                }
                               
                                if(ChatColor.stripColor(s).contains("Speed III")){
                                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100, 2));
                                    break;
                                }
                            }
                        }
    The issue is... When i'm wearing boots with the lore: "Speed II" It only gives me speed 1.
     
  2. Choco

    Moderator

    Obviously :p Because Speed II contains Speed I. Instead of using .contains(), use .equals()
    If you really require the use of .contains(), you could remove the "break" statement
     
    • Funny Funny x 1
  3. Oh I didn't think about that! XD you could also make it check for III then II then I
     
    • Agree Agree x 1
  4. Choco

    Moderator

    You know what, that's probably even better :D
     
  5. That's what i was thinking, But if i use .equals() wouldn't that check if all the lore's on the item equal that?
     
  6. Choco

    Moderator

    It would only check the iteration you're currently on. For example, "s.equals("Speed I")". Though @Trophonix's idea was really good
     
    • Like Like x 1
  7. If you use .equals it will only work if it says only "Speed III" on the line you're testing. It won't work if you put "Gives you Speed III" in the lore.
     
  8. Ok, I just used this:
     
    • Like Like x 1
Thread Status:
Not open for further replies.