Solved How would you check for armor on a player?

Discussion in 'Bukkit Help' started by MineralSmasher, Jul 20, 2017.

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

    MineralSmasher

    Hello, I am brand new to developing plugins and Bukkit in general, and I am attempting to create a plugin that has needs to check if a player is wearing certain armor pieces, and if so will be able to activate a certain ability. I feel as if this has been accomplished before, but no amount of searching led me to an answer ;-;. My plugin so far has been just to accomplish the basic code before making more specific items, so I'm trying to do this:
    The player must be wearing a full set of gold armor.
    The player must right click a bone with the lore "test"
    If the above events occur, an ender pearl will be thrown and teleport the player.
    I was able to code the last two conditions successfully, but I have no clue on how to check if a player has a full set of gold armor on. I downloaded an ArmorEquipEvent <Edit by Moderator: Redacted not allowed paid resource url> external jar as well. I am trying to achieve this without checking each second if the player is wearing the armor pieces. This is my code so far:

    Code:
    package me.mineralsmasher;
    
    import java.util.List;
    
    import org.bukkit.Material;
    import org.bukkit.entity.EnderPearl;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import com.codingforcookies.armorequip.ArmorEquipEvent;
    
    public class PlayerListener implements Listener {
      
        public PlayerListener(CustomItemsMS plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
        @EventHandler
        public void onHold(PlayerInteractEvent e, ArmorEquipEvent a) {  
            Player player = e.getPlayer();
            ItemStack item = player.getInventory().getItemInMainHand();
            ItemStack[] armor = e.getPlayer().getInventory().getArmorContents();
            if(a.getNewArmorPiece() != null && a.getNewArmorPiece().getType() != Material.AIR) {
                if (armor[0].getType() == Material.GOLD_HELMET
                        && armor[1].getType() == Material.GOLD_CHESTPLATE
                        && armor[2].getType() == Material.GOLD_LEGGINGS
                        && armor[3].getType() == Material.GOLD_BOOTS) {
                            if (item != null) {
                                ItemMeta meta = item.getItemMeta();
                                if (meta != null) {
                                    List<String> lore = meta.getLore();
                                    if (lore != null) {
                                            if (lore.toString().contains("test")) {
                                                player.launchProjectile(EnderPearl.class);
                                            }
                                    }
                                }
                            }
                }
            }
        }
    }
    I'm pretty sure there is nothing wrong with implementing the plugin or anything, but my java knowledge is basic, so any help is greatly appreciated! :)
     
    Last edited by a moderator: Feb 10, 2021
  2. Offline

    Machine Maker

    @MineralSmasher
    1. No need for the ArmorEquipEvent. The Bukkit API has easy ways of getting the armor. You're also using the event incorrectly, but that doesn't matter so much.

    2. Get the players inventory and then you can get the helmet, chestplate, leggings, or boots.
    Code:
    e.getPlayer().getInventory().getHelmet() //returns the ItemStack of the helmet slot
     
  3. Offline

    MineralSmasher

    I feel kinda stupid.... Thank you so much!
     
  4. Offline

    Machine Maker

Thread Status:
Not open for further replies.

Share This Page