Test for lore

Status
This thread has been locked.

Plus.

livin
Supreme
Feedback score
16
Posts
311
Reactions
158
Resources
0
Hey, I'm trying to make a Pill plugin where when you click an item, it runs something, but I'm trying to make it so it only runs if there is a certain lore on an item. I want to test for multiple lines, not just one.

My code so far:
Everything is working perfectly, but it's just I want to add that feature but idk how.

Code:
        if (p.getItemInHand().getType() == Material.PUMPKIN_SEEDS) {
            if (p.getItemInHand().getItemMeta().getDisplayName().contains("§b§lSpeed Pill")) {
                if(p.getItemInHand().getItemMeta().getLore().equals("§b§lEffects:\n§fSpeed II\n\n§eRight click to swallow!"));
                if (p.getItemInHand().getAmount() >= 2) {
                    p.sendMessage("§cYou can not take multiple pills at once! Seperate them!");
                } else {
                    p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 700, 1));
                    p.sendMessage("§bYou have taken a §fSpeed Pill §band gained §f§lSpeed II §bfor §f§l35 seconds");
                    p.setItemInHand(air);
                    p.playSound(p.getLocation(), Sound.FIZZ, 1, 1);
                    Bukkit.getWorld("world").playEffect(p.getLocation(), Effect.MOBSPAWNER_FLAMES, 20303);
                    p.playSound(p.getLocation(), Sound.EAT, 1, 1);

                }
            }

        }
    }
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

William

Developer
Supreme
Feedback score
10
Posts
389
Reactions
189
Resources
0
Hey, I'm trying to make a Pill plugin where when you click an item, it runs something, but I'm trying to make it so it only runs if there is a certain lore on an item. I want to test for multiple lines, not just one.

My code so far:
Everything is working perfectly, but it's just I want to add that feature but idk how.

Code:
        if (p.getItemInHand().getType() == Material.PUMPKIN_SEEDS) {
            if (p.getItemInHand().getItemMeta().getDisplayName().contains("§b§lSpeed Pill")) {
                if(p.getItemInHand().getItemMeta().getLore().equals("§b§lEffects:\n§fSpeed II\n\n§eRight click to swallow!"));
                if (p.getItemInHand().getAmount() >= 2) {
                    p.sendMessage("§cYou can not take multiple pills at once! Seperate them!");
                } else {
                    p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 700, 1));
                    p.sendMessage("§bYou have taken a §fSpeed Pill §band gained §f§lSpeed II §bfor §f§l35 seconds");
                    p.setItemInHand(air);
                    p.playSound(p.getLocation(), Sound.FIZZ, 1, 1);
                    Bukkit.getWorld("world").playEffect(p.getLocation(), Effect.MOBSPAWNER_FLAMES, 20303);
                    p.playSound(p.getLocation(), Sound.EAT, 1, 1);

                }
            }

        }
    }
player.getItemInHand().getItemMeta().getLore().contains("Lore here") i think
 

William

Developer
Supreme
Feedback score
10
Posts
389
Reactions
189
Resources
0
if (player.getItemInHand().getType() == Material.PUMPKIN_SEEDS && player.getItemInHand().getItemMeta().getLore().contains("lore1") && player.getItemInHand().getItemMeta().getLore().contains("lore2")){
 

Fantasy

Feedback score
0
Posts
40
Reactions
11
Resources
0
Java:
List<String> lores = player.getItemInHand().getLore();
for (String lore : lores) {
      if (lore.contains("test") {
      # Code here.
      }
}
 

LordOfTime

Feedback score
15
Posts
603
Reactions
245
Resources
0
Java:
List<String> lores = player.getItemInHand().getLore();
for (String lore : lores) {
      if (lore.contains("test") {
      # Code here.
      }
}

First, you're forgetting to get meta... You can't just get lore straight from the item (at least not that I'm aware of). Second, code like this will throw nullPointers if the item's meta or lore doesn't exist, so a safe way to do this would be to do either try / catch, or use nested if's if you like that way better (I.E. if (item.hasItemMeta()) { if (item.getItemMeta().hasLore()) { etc)
 

Fantasy

Feedback score
0
Posts
40
Reactions
11
Resources
0
First, you're forgetting to get meta... You can't just get lore straight from the item (at least not that I'm aware of). Second, code like this will throw nullPointers if the item's meta or lore doesn't exist, so a safe way to do this would be to do either try / catch, or use nested if's if you like that way better (I.E. if (item.hasItemMeta()) { if (item.getItemMeta().hasLore()) { etc)
I only showed him a way to loop through each lore of the item and check if that specific lore contains something, how he wants to check if the item even has a lore or item meta is up to him.
 
Status
This thread has been locked.
Top