diff options
Diffstat (limited to 'src/main/java/io/github/moulberry/repo/data/NEUIngredient.java')
-rw-r--r-- | src/main/java/io/github/moulberry/repo/data/NEUIngredient.java | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/main/java/io/github/moulberry/repo/data/NEUIngredient.java b/src/main/java/io/github/moulberry/repo/data/NEUIngredient.java new file mode 100644 index 0000000..9f7d62c --- /dev/null +++ b/src/main/java/io/github/moulberry/repo/data/NEUIngredient.java @@ -0,0 +1,63 @@ +package io.github.moulberry.repo.data; + +import com.google.gson.*; +import lombok.Getter; + +import java.lang.reflect.Type; + +@Getter +public class NEUIngredient { + String itemId; + int amount; + public static final String NEU_SENTINEL_EMPTY = "NEU_SENTINEL_EMPTY"; + public static final NEUIngredient SENTINEL_EMPTY = new NEUIngredient(); + + static { + SENTINEL_EMPTY.itemId = NEU_SENTINEL_EMPTY; + SENTINEL_EMPTY.amount = 0; + } + + private NEUIngredient() { + } + + public static NEUIngredient fromItem(NEUItem item, int count) { + NEUIngredient ingredient = new NEUIngredient(); + ingredient.amount = count; + ingredient.itemId = item.getSkyblockItemId(); + return ingredient; + } + + public static NEUIngredient fromString(String string) { + String[] parts = string.split(":"); + NEUIngredient ingredient = new NEUIngredient(); + if (parts.length == 2) { + ingredient.amount = Integer.parseInt(parts[1]); + } else if (parts.length == 1) { + ingredient.amount = 1; + } else { + throw new IllegalArgumentException("Could not parse ingredient " + string); + } + ingredient.itemId = parts[0]; + if (NEU_SENTINEL_EMPTY.equals(ingredient.itemId)) + return SENTINEL_EMPTY; + return ingredient; + } + + public static class Serializer implements JsonDeserializer<NEUIngredient> { + + @Override + public NEUIngredient deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + + if (!json.isJsonPrimitive()) throw new JsonParseException("Expected string for ingredient, found " + json); + JsonPrimitive p = json.getAsJsonPrimitive(); + if (!p.isString()) throw new JsonParseException("Expected string for ingredient, found " + json); + String asString = json.getAsString(); + return fromString(asString); + } + } + + @Override + public String toString() { + return String.format("NEUIngredient{%s:%d}", itemId, amount); + } +} |