package net.wayward_realms.waywardprofessions;

import net.wayward_realms.waywardlib.character.CharacterPlugin;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.RegisteredServiceProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class BlockBreakListener implements Listener {

    private final WaywardProfessions plugin;

    public BlockBreakListener(WaywardProfessions plugin) {
        this.plugin = plugin;
    }

    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        RegisteredServiceProvider<CharacterPlugin> characterPluginProvider = Bukkit.getServer().getServicesManager().getRegistration(CharacterPlugin.class);
        if (characterPluginProvider != null) {
            CharacterPlugin characterPlugin = characterPluginProvider.getProvider();
            net.wayward_realms.waywardlib.character.Character character = characterPlugin.getActiveCharacter(event.getPlayer());
            Material material = event.getBlock().getType();
            List<ItemStack> drops = new ArrayList<>();
            Random random = new Random();
            for (ItemStack drop : event.getBlock().getDrops()) {
                int miningEfficiency = plugin.getMiningEfficiency(character, event.getBlock().getType());
                int amount = (int) ((double) (random.nextInt(100) <= 30 ? (miningEfficiency > 10 ? random.nextInt(miningEfficiency) : random.nextInt(10)) : 25) * (4D / 100D) * (double) drop.getAmount());
                drops.add(new ItemStack(drop.getType(), amount));
            }
            event.setCancelled(true);
            event.getBlock().setType(Material.AIR);
            for (ItemStack drop : drops) {
                event.getBlock().getWorld().dropItem(event.getBlock().getLocation().add(0.5D, 0.5D, 0.5D), drop);
            }
            plugin.setMiningEfficiency(character, material, plugin.getMiningEfficiency(character, material) + (random.nextInt(100) <= 30 ? random.nextInt(3) : 0));
        }
    }
}

@@ -105,16 +105,75 @@
 Plugin;%0A
+import net.wayward_realms.waywardlib.professions.ToolType;%0A
 import o
@@ -1843,16 +1843,711 @@
 (true);%0A
+            if (event.getPlayer().getItemInHand() != null) %7B%0A                if (ToolType.getToolType(event.getPlayer().getItemInHand().getType()) != null) %7B%0A                    ToolType toolType = ToolType.getToolType(event.getPlayer().getItemInHand().getType());%0A                    switch (toolType) %7B%0A                        case SWORD: event.getPlayer().getItemInHand().setDurability((short) (event.getPlayer().getItemInHand().getDurability() + 2)); break;%0A                        case PICKAXE:case AXE:case SPADE: event.getPlayer().getItemInHand().setDurability((short) (event.getPlayer().getItemInHand().getDurability() + 1)); break;%0A                    %7D%0A                %7D%0A            %7D%0A
         

Decrease durability when breaking blocks