How to check for an itemstack for armor contents

Discussion in 'Plugin Development' started by blok601, Sep 22, 2015.

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

    blok601

    The title is a bit confusing. I want to check if a player is wearing diamond boots with a specific item meta, and if they are add a potion effect. I have everything working, I just need to know how to check for a specific item meta.

    Thanks!

    -blok

    Here is my code, I am trying to give them a potion effect when they are wearing the boots.

    Code:
    package me.blok601.meta;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Main extends JavaPlugin{
       
        public void onEnable(){
            getLogger().info("Meta has been enabled!");
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
                public void run(){
                    for(Player p : Bukkit.getOnlinePlayers()){
                        ItemStack boots = new ItemStack(Material.DIAMOND_BOOTS, 1);
                        List<String> lore = new ArrayList<String>();
                        ItemMeta meta = boots.getItemMeta();
                        meta.setDisplayName("Test");
                        lore.add(ChatColor.GREEN + "Test!");
                        meta.setLore(lore);
                        boots.setItemMeta(meta);
                        if(p.getInventory().getBoots() == boots){
                            p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 1));
                        }
                       
                       
                    }
                }
            }, 20, 20);
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]){
            if(cmd.getName().equalsIgnoreCase("tester")){
                Player player = (Player) sender;
                ItemStack boots = new ItemStack(Material.DIAMOND_BOOTS, 1);
                List<String> lore = new ArrayList<String>();
                ItemMeta meta = boots.getItemMeta();
                meta.setDisplayName("Test");
                lore.add(ChatColor.GREEN + "Test!");
                meta.setLore(lore);
                boots.setItemMeta(meta);
                player.getInventory().addItem(boots);
            }
            return false;
           
        }
    
    }
    
     
    Last edited: Sep 22, 2015
  2. @blok601
    Don't use == when comaring Objects, in most cases this will return false. Use Object#equals(otherObject) instead. Also, I wouldn't do it like this. You should first check if the item is not null and then check the item's type and metadata.
     
    blok601 likes this.
  3. Offline

    blok601

    Sorry complete noob here. I understand checking if it is null, but how would I do it? I was thinking of creating an event..
     
  4. Offline

    RoboticPlayer

    I think within your loop of online players, loop through the armor contents, check if they are wearing diamond boots with the lore that you want, then run your code. Also a side-note, there is no reason to log when the plugin is enabled, Bukkit does this automatically.

    In your onCommand, CHECK BEFORE CASTING SENDER TO PLAYER!!!
     
    blok601 likes this.
  5. Offline

    blok601

    But then how would you check every single player?
     
  6. Offline

    tkuiyeager1

    @blok601 try use this:
    Code:
    if(p.getInventory().getBoots().getType() == Material.DIAMOND_BOOTS && p.getInventory().getBoots().getItemMeta().getDisplayName().equals("Name")) {
                    //You Can Add Here Your Potion Effect
                    p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 20, 20));
                }
     
  7. Offline

    RoboticPlayer

    You literally already have the code done for that.
    Code:
    for (Player p : Bukkit.getServer().getOnlinePlayers()) {
        for (ItemStack armor : p.getInventory().getArmorContents() {
            if (armor.getBoots() == ItemStack.DIAMOND_BOOTS) {
                if (armor.getBoots().getItemMeta == boots) {
                    // Your code to add potion effects
                }
            }
        }
    }
    Please note that the above code is untested and may not work completely as expected.
     
  8. Offline

    rbrick

  9. Offline

    blok601

    worked perfectly. Thank you! I just needed to work on the timing, I kept crashing my server with the runnable xD
     
  10. Offline

    tkuiyeager1

Thread Status:
Not open for further replies.

Share This Page