Durability Plugin PLEASE HELP

Discussion in 'Spigot Plugin Development' started by MaxJacobsYT123, May 14, 2017.

  1. So, This is my current code, and for some reason, whenever I give damage and or take damage, even though my armor is at full durability, it still warns me. Can anyone please help?
    Code (Text):
    package me.plugin.uses;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageByBlockEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Uses extends JavaPlugin implements Listener {
       
        @EventHandler(
                ignoreCancelled = false,
                priority = EventPriority.LOW)
        public void checkBlock(EntityDamageByBlockEvent e) {
            if(e.getEntity() instanceof Player) {
                Player p = (Player) e.getEntity();
               
                for(ItemStack is : p.getInventory().getArmorContents()) {
                    if(is.getDurability() < 0.25 * 100) {
                        p.sendMessage("Your " + getNameOfArmour(is) + " is low on durability!");
                    }
                }
            }
        }

        @EventHandler(
                ignoreCancelled = false,
                priority = EventPriority.LOW)
        public void checkEntity(EntityDamageByEntityEvent e) {
            if(e.getEntity() instanceof Player) {
                Player p = (Player) e.getEntity();
             
                for(ItemStack is : p.getInventory().getArmorContents()) {
                    if(is.getDurability() < 0.25 * 100) {
                        p.sendMessage("Your " + getNameOfArmour(is) + " is low!");
                    }
                }
            }
            if(e.getDamager() instanceof Player) {
                Player p = (Player) e.getDamager();
                for(ItemStack is : p.getInventory().getArmorContents()) {
                    if(is.getDurability() < 0.25 * 100) {
                        p.sendMessage("Your " + getNameOfArmour(is) + " is low!");
                    }
                }
            }
        }
         
        public String getNameOfArmour(ItemStack is) {
            Material m = is.getType();

            // If the material is a helmet of some sort
            if(m.toString().toLowerCase().contains("helmet")) return "helmet";

            // If the material is a chestplate of some sort
            if(m.toString().toLowerCase().contains("chestplate")) return "chestplate";

            // If the material are leggings of some sort
            if(m.toString().toLowerCase().contains("leggings")) return "helmet";

            // If the material are boots of some sort
            if(m.toString().toLowerCase().contains("boots")) return "boots";
            //Just in case
            return "";
        }
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            if (e.getAction() == Action.LEFT_CLICK_AIR) {
                    if (e.getItem() == new ItemStack(Material.STONE_SWORD)) {
                        if (e.getItem().getDurability() < 0.25 * 100) {
                            e.getPlayer().sendMessage("Your Stone Sword is low on durability!");
                        }
                    }
                    if (e.getItem() == new ItemStack(Material.WOOD_SWORD)) {
                        if (e.getItem().getDurability() < 0.25 * 100) {
                            e.getPlayer().sendMessage("Your Wooden Sword is low on durability!");
                        }
                    }
                    if (e.getItem() == new ItemStack(Material.IRON_SWORD)) {
                        if (e.getItem().getDurability() < 0.25 * 100) {
                            e.getPlayer().sendMessage("Your Iron Sword is low on durability!");
                        }
                    }
                    if (e.getItem() == new ItemStack(Material.GOLD_SWORD)) {
                        if (e.getItem().getDurability() < 0.25 * 100) {
                            e.getPlayer().sendMessage("Your Golden Sword is low on durability!");
                        }
                    }
                    if (e.getItem() == new ItemStack(Material.DIAMOND_SWORD)) {
                        if (e.getItem().getDurability() < 0.25 * 100) {
                            e.getPlayer().sendMessage("Your Diamond Sword is low on durability!");
                        }
                    }
                    if (e.getItem() == new ItemStack(Material.BOW)) {
                        if (e.getItem().getDurability() < 0.25 * 100) {
                            e.getPlayer().sendMessage("Your Bow Sword is low on durability!");
                        }
                    }
                }
            }
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
    }



     
     
  2. WAS

    WAS

    Don't you already have a topic going for this same problem? Again I think with short values you need to look into valueOf.