package in.twizmwaz.cardinal.module.modules.toolRepair;

import in.twizmwaz.cardinal.module.Module;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack;

import java.util.Set;

public class ToolRepair implements Module {

    private final Set<Material> materials;

    protected ToolRepair(final Set<Material> materials) {
        this.materials = materials;
    }

    @Override
    public void unload() {
        HandlerList.unregisterAll(this);
    }

    @EventHandler
    public void onPlayerPickupItem(PlayerPickupItemEvent event) {
        if (!event.isCancelled()) {
            ItemStack item1 = event.getItem().getItemStack();
            if (materials.contains(item1.getType())) {
                if (event.getPlayer().getInventory().contains(item1.getType())) {
                    ItemStack toRepair = null;
                    boolean itemsMatch = false;
                    for (ItemStack item2 : event.getPlayer().getInventory().getContents()) {
                        if (item2 != null) {
                            if (item1.getType() == item2.getType()) {
                                if (item1.getEnchantments() != null && item2.getEnchantments() != null) {
                                    if (item1.getEnchantments().equals(item2.getEnchantments())) {
                                        itemsMatch = true;
                                    }
                                } else {
                                    if (item1.getEnchantments() == null && item2.getEnchantments() == null) {
                                        itemsMatch = true;
                                    }
                                }
                                if (itemsMatch) {
                                    toRepair = item2;
                                }
                            }
                        }
                    }
                    if (itemsMatch) {
                        event.setCancelled(true);
                        event.getItem().remove();
                        event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.ITEM_PICKUP, 0.1F, 1);
                        toRepair.setDurability((short) (toRepair.getDurability() - (item1.getType().getMaxDurability() - item1.getDurability()) < 0 ? 0 : toRepair.getDurability() - (item1.getType().getMaxDurability() - item1.getDurability())));
                    }
                }
            }
        }
    }
}

@@ -854,32 +854,12 @@
 e())
-) %7B%0A                if (
+ && 
 even
@@ -920,107 +920,8 @@
 ) %7B%0A
-                    ItemStack toRepair = null;%0A                    boolean itemsMatch = false;%0A    
     
@@ -1017,36 +1017,32 @@
                 
-    
 if (item2 != nul
@@ -1046,916 +1046,65 @@
 null
-) %7B%0A                            if (item1.getType() == item2.getType()) %7B%0A                                if (item1.getEnchantments() != null && item2.getEnchantments() != null) %7B%0A                                    if (item1.getEnchantments().equals(item2.getEnchantments())) %7B%0A                                        itemsMatch = true;%0A                                    %7D%0A                                %7D else %7B%0A                                    if (item1.getEnchantments() == null && item2.getEnchantments() == null) %7B%0A                                        itemsMatch = true;%0A                                    %7D%0A                                %7D%0A                                if (itemsMatch) %7B%0A                                    toRepair = item2;%0A                                %7D%0A                            %7D%0A                        %7D%0A                    %7D%0A                    if (itemsMatch
+ && toMaxDurability(item1).equals(toMaxDurability(item2))
 ) %7B%0A
@@ -1345,48 +1345,26 @@
     
-toRepair.setDurability((short) (toRepair
+int result = item2
 .get
@@ -1442,173 +1442,379 @@
 y())
- %3C 0 ? 0 : toRepair.getDurability() - (item1.getType().getMaxDurability() - item1.getDurability())));%0A                    %7D%0A                %7D%0A            %7D%0A        %7D
+;%0A                        item2.setDurability((short) (result %3C 0 ? 0 : result));%0A                        break;%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    public ItemStack toMaxDurability(ItemStack item) %7B%0A        ItemStack item2 = new ItemStack(item);%0A        item2.setDurability(item.getType().getMaxDurability());%0A        return item2;
 %0A   

Fix tool repair module merging items that shouldn't merge