Durability Check

Discussion in 'Plugin Development' started by Side8StarLite, Oct 14, 2016.

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

    Side8StarLite

    Hello, I've been trying to develop a plugin based on a friend's request that checks a player's tool and armor durability, and warns them if it's getting low. So far, I've gotten the tool check done for the most part, but I've got some questions:
    1) Mainly, is there any easier way to check if the player has taken durability on an item such as some listener "onItemUse" or something? I know you can use PlayerInteractEvent, but I always have to check if a) The player is holding an item, b) If the item is a tool, c) If they're actually destroying the block, ca) If they aren't destroying the block, are they holding a shovel or hoe that takes durability in any other way, and cb) If the block is modifiable by the specified tool. Not to mention there's shearing to involve too.

    2) If there isn't, is there at least a function that checks if the item is a tool (eg shovel, hoe, sword, pick)?

    Here's my code for now, pretty messy, so any suggestions would be appreciated:
    Code:
    package me.rebeltrooper;
    
    import io.netty.util.internal.chmv8.ConcurrentHashMapV8.Action;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerShearEntityEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class ItemListener implements Listener
    {
        public ItemListener(ItemDurabilityCheck plugin)
        {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler
        public void onBlockBreak(BlockBreakEvent e)
        {
            ItemStack item = e.getPlayer().getItemInHand();
            if (!isTool(item.getType()))
                return;
           
            checkDurability(item, e.getPlayer());
        }
       
        @EventHandler
        public void onToolUse(PlayerInteractEvent e)
        {
            if (e.getItem() == null)
                return;
           
            Material mat = e.getItem().getType();
            ItemStack item = e.getItem();
            if (e.getAction() == org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK)
            {
                if (mat == Material.WOOD_SPADE ||
                        mat == Material.IRON_SPADE ||
                        mat == Material.GOLD_SPADE ||
                        mat == Material.DIAMOND_SPADE)
                {
                    if (e.getClickedBlock().getType() == Material.GRASS)
                    {
                        checkDurability(item, e.getPlayer());
                        return;
                    }
                }
    
                if (mat == Material.WOOD_HOE ||
                        mat == Material.IRON_HOE ||
                        mat == Material.GOLD_HOE ||
                        mat == Material.DIAMOND_HOE)
                {
                    if (e.getClickedBlock().getType() == Material.GRASS ||
                            e.getClickedBlock().getType() == Material.DIRT)
                    {
                        checkDurability(item, e.getPlayer());
                        return;
                    }
                }
            }
        }
    
        @EventHandler
        public void onShear(PlayerShearEntityEvent e)
        {
            ItemStack item = e.getPlayer().getItemInHand();
            checkDurability(item, e.getPlayer());
        }
       
        public boolean isTool(Material item)
        {
            Material m[] = {
                    Material.WOOD_AXE, Material.WOOD_PICKAXE, Material.WOOD_SPADE, Material.WOOD_HOE, Material.WOOD_SWORD,
                    Material.IRON_AXE, Material.IRON_PICKAXE, Material.IRON_SPADE, Material.IRON_HOE, Material.IRON_SWORD,
                    Material.GOLD_AXE, Material.GOLD_PICKAXE, Material.GOLD_SPADE, Material.GOLD_HOE, Material.GOLD_SWORD,
                    Material.DIAMOND_AXE, Material.DIAMOND_PICKAXE, Material.DIAMOND_SPADE, Material.DIAMOND_HOE, Material.DIAMOND_SWORD
            };
           
            for (Material test: m)
            {
                if (item == test)
                    return true;
            }
            return false;
        }
       
        public void checkDurability(ItemStack item, Player player)
        {
            short tenPercent = (short)(item.getType().getMaxDurability() * 0.9);
            short fivePercent = (short)(item.getType().getMaxDurability() * 0.95);
            if (item.getDurability() == tenPercent)
            {
                Bukkit.broadcastMessage(ChatColor.GREEN + "Item is at 10% durability!");
                player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 30, 1);
                return;
               
            }
               
            if (item.getDurability() == fivePercent)
            {
                Bukkit.broadcastMessage(ChatColor.GREEN + "Item is at 5% durability!");
                player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 30, 1);
                return;
            }
           
        }
    
    }
    
    I'm more used to C++ than I am with Java, so I still needs some getting used to. Hope you understand!
     
  2. Offline

    Zombie_Striker

    If you want to learn more about Bukkit's class/events/method, this is the link you need. Please, bookmark/create a file with this link for when you have questions like this:
    https://hub.spigotmc.org/javadocs/bukkit/

    Under Events/Player, you will find the PlayerItemDamageEvent, which is called when an item changes its durability. Listen for this.
     
  3. Offline

    Side8StarLite

    Thank you! I'll make sure to do that! And yes I've been scouring the javadocs for that kind of function, but just couldn't find one that I needed (still trying to get used to navigating the site). Nice to have some expert advice!
     
  4. Offline

    Zombie_Striker

    @Side8StarLite
    If your problem has been solved, mark this thread as solved.
     
  5. Offline

    Side8StarLite

    It says "PlayerItemDamageEvent cannot be resolved to a type" and it has a red squiggly line under it. And it doesn't say any imports under it either.
     

    Attached Files:

  6. Offline

    Zombie_Striker

  7. Offline

    Side8StarLite

    1.9.4 CraftBukkit
     
Thread Status:
Not open for further replies.

Share This Page