Solved How do you remove the item ability to degrade it's durability?

Discussion in 'Plugin Development' started by CahyaAziz, Jan 11, 2021.

  1. Offline

    CahyaAziz

    Code:
    package me.cahya.BreakRnd.Listeners;
    
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffectType;
    
    import me.cahya.BreakRnd.Main;
    
    public class BreakRnd implements Listener {
     
        private Main plugin;
     
        public PotionEffectType[] pots;
     
        public BreakRnd(Main plugin) {
            this.plugin = plugin;
            this.pots = PotionEffectType.values();
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
    
        @EventHandler
        public void breakEvent(BlockBreakEvent e) {
         
            Player p = e.getPlayer();
            Random rnd = new Random();
            Material i = p.getInventory().getItemInMainHand().getType();
            int chance = rnd.nextInt(100);
         
            p.updateInventory();
            if (i.equals(null))
                return;
            if (i.equals(Material.AIR))
                return;
            if (i == Material.WOODEN_AXE || i == Material.WOODEN_HOE || i == Material.WOODEN_PICKAXE || i == Material.WOODEN_SHOVEL ||
                    i == Material.STONE_AXE || i == Material.STONE_HOE || i == Material.STONE_PICKAXE || i == Material.STONE_SHOVEL ||
                    i == Material.GOLDEN_AXE || i == Material.GOLDEN_HOE || i == Material.GOLDEN_PICKAXE || i == Material.GOLDEN_SHOVEL ||
                    i == Material.IRON_AXE || i == Material.IRON_HOE || i == Material.IRON_PICKAXE || i == Material.IRON_SHOVEL ||
                    i == Material.DIAMOND_AXE || i == Material.DIAMOND_HOE || i == Material.DIAMOND_PICKAXE || i == Material.DIAMOND_SHOVEL)
                if (chance <= 10)
                p.getInventory().removeItem(new ItemStack(i));
            p.updateInventory();
    I am new to plugin development and it may seem easy to you guys but I'm trying to code myself a plugin that when you breaks blocks with a tool, it has a 10% chance of breaking the tool. The 10% break tool part i've done it but Whenever the tools lost durability (where you see the green bar at the bottom) my code doesn't work anymore.

    EDIT : I figure it out with the PlayerItemDamageEvent and putting setDamage()
     
    Last edited: Jan 11, 2021
  2. Offline

    SecretX

    Just a tip, you can reduce the checks inside if by using:

    Code:
    if (Material.matchMaterial("hoe") || Material.matchMaterial("axe") || Material.matchMaterial("pickaxe") || Material.matchMaterial("shovel")) {
        // Do stuff
    }
     

Share This Page