package cz.creeper.customitemlibrary.registry;

import com.google.common.base.Preconditions;
import lombok.Getter;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;

import java.util.Optional;

public class DurabilityIdentifier {
    @Getter
    private final ItemType itemType;
    @Getter
    private final int durability;

    public DurabilityIdentifier(ItemStackSnapshot itemStackSnapshot) {
        itemType = itemStackSnapshot.getType();
        durability = itemStackSnapshot.get(Keys.ITEM_DURABILITY)
                .orElseThrow(() -> new IllegalArgumentException("This item does not have a durability."));
    }

    public DurabilityIdentifier(ItemStack itemStack) {
        itemType = itemStack.getItem();
        durability = itemStack.get(Keys.ITEM_DURABILITY)
            .orElseThrow(() -> new IllegalArgumentException("This item does not have a durability."));
    }

    public DurabilityIdentifier(ItemType itemType, int durability) {
        Optional<Integer> numberOfUses = CustomToolDefinition.getNumberOfUses(ItemStack.of(itemType, 1));
        Preconditions.checkArgument(numberOfUses.isPresent(), "This item type does not have a durability.");
        Preconditions.checkArgument(durability >= 0 && durability < numberOfUses.get(),
            "Durability out of bounds. Min: 0; Max: " + (numberOfUses.get() - 1) + "; Provided: " + durability);

        this.itemType = itemType;
        this.durability = durability;
    }

    public static DurabilityIdentifier parse(String string) {
        int separatorIndex = string.lastIndexOf(CustomToolDefinition.ID_SEPARATOR);
        String id = string.substring(0, separatorIndex);
        String rawDurability = id.substring(separatorIndex + 1);
        ItemType itemType = Sponge.getRegistry().getType(ItemType.class, id)
                .orElseThrow(() -> new IllegalArgumentException("Could not find an ItemType for id '" + id + "'."));
        int durability = Integer.valueOf(rawDurability);

        return new DurabilityIdentifier(itemType, durability);
    }

    @Override
    public String toString() {
        return itemType.getName() + CustomToolDefinition.ID_SEPARATOR + durability;
    }

    @Override
    public int hashCode() {
        return toString().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return toString().equals(obj.toString());
    }
}

@@ -416,16 +416,73 @@
 ifier %7B%0A
+    public static final char DURABILITY_SEPARATOR = '@';%0A
     @Get
@@ -477,32 +477,32 @@
 @';%0A    @Getter%0A
-
     private fina
@@ -1121,32 +1121,85 @@
 ity.%22));%0A    %7D%0A%0A
+    @SuppressWarnings(%22OptionalGetWithoutIsPresent%22)%0A
     public Durab
@@ -1857,39 +1857,26 @@
 IndexOf(
-CustomToolDefinition.ID
+DURABILITY
 _SEPARAT
@@ -2409,31 +2409,18 @@
 ) + 
-CustomToolDefinition.ID
+DURABILITY
 _SEP
@@ -2584,24 +2584,24 @@
 ject obj) %7B%0A
-
         retu
@@ -2594,32 +2594,69 @@
 %7B%0A        return
+ getClass().equals(obj.getClass()) &&
  toString().equa

change durability separator to align with Sponge formatting