From 3806dcbd5d6ac0a19eee65319ebac7742a42a5e6 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Mon, 8 Apr 2019 01:34:36 -0700 Subject: [PATCH] Salvageable serializer --- .../config/hocon/RepairableSerializer.java | 25 +++--- .../config/hocon/SalvageableSerializer.java | 79 +++++++++++++++++++ 2 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/config/hocon/SalvageableSerializer.java diff --git a/src/main/java/com/gmail/nossr50/config/hocon/RepairableSerializer.java b/src/main/java/com/gmail/nossr50/config/hocon/RepairableSerializer.java index 2d14a813d..999289ccd 100644 --- a/src/main/java/com/gmail/nossr50/config/hocon/RepairableSerializer.java +++ b/src/main/java/com/gmail/nossr50/config/hocon/RepairableSerializer.java @@ -11,6 +11,11 @@ import java.util.List; import java.util.Optional; public class RepairableSerializer implements TypeSerializer { + public static final String ITEM = "Item"; + public static final String ITEMS_USED_TO_REPAIR = "Items-Used-To-Repair"; + public static final String MINIMUM_QUANTITY_USED_TO_REPAIR = "Minimum-Quantity-Used-To-Repair"; + public static final String OVERRIDE_LEVEL_REQUIREMENT = "Override-Level-Requirement"; + public static final String XP_MULTIPLIER = "XP-Multiplier"; /* TypeTokens are obtained in two ways @@ -40,8 +45,8 @@ public class RepairableSerializer implements TypeSerializer { /* Repairable(Material itemMaterial, Material repairMaterial, int minimumQuantity, int minimumLevel, double xpMultiplier) */ - String item = value.getNode("Item").getValue(TypeToken.of(String.class)); - List repairItems = value.getNode("Items-Used-To-Repair").getValue(new TypeToken>() {}); + String item = value.getNode(ITEM).getValue(TypeToken.of(String.class)); + List repairItems = value.getNode(ITEMS_USED_TO_REPAIR).getValue(new TypeToken>() {}); /*String itemConstant = HOCONUtil.deserializeENUMName(value.getNode("Item").getString()); @@ -50,9 +55,9 @@ public class RepairableSerializer implements TypeSerializer { Material item = (Material) getEnum(itemConstant, TypeToken.of(Material.class)); Material repairItem = (Material) getEnum(repairConstant, TypeToken.of(Material.class));*/ - int minimumQuantity = value.getNode("Minimum-Quantity-Used-To-Repair").getValue(TypeToken.of(Integer.class)); - int minimumLevel = value.getNode("Override-Level-Requirement").getValue(TypeToken.of(Integer.class)); - double xpMultiplier = value.getNode("XP-Multiplier").getValue(TypeToken.of(Double.class)); + int minimumQuantity = value.getNode(MINIMUM_QUANTITY_USED_TO_REPAIR).getValue(TypeToken.of(Integer.class)); + int minimumLevel = value.getNode(OVERRIDE_LEVEL_REQUIREMENT).getValue(TypeToken.of(Integer.class)); + double xpMultiplier = value.getNode(XP_MULTIPLIER).getValue(TypeToken.of(Double.class)); return new Repairable(item, repairItems, minimumQuantity, minimumLevel, xpMultiplier); } @@ -62,11 +67,11 @@ public class RepairableSerializer implements TypeSerializer { /*value.getNode("Item").setValue(HOCONUtil.serializeENUMName(obj.getItemMaterial().getKey().getKey())); value.getNode("Item-Used-To-Repair").setValue(HOCONUtil.serializeENUMName(obj.getRepairMaterials().getKey().getKey()));*/ - value.getNode("Item").setValue(obj.getItemMaterial().getKey().toString()); - value.getNode("Items-Used-To-Repair").setValue(obj.getRepairMaterialsRegistryKeys()); - value.getNode("Minimum-Quantity-Used-To-Repair").setValue(obj.getMinimumQuantity()); - value.getNode("Skill-Level-Required-To-Repair").setValue(obj.getMinimumLevel()); - value.getNode("XP-Multiplier").setValue(obj.getXpMultiplier()); + value.getNode(ITEM).setValue(obj.getItemMaterial().getKey().toString()); + value.getNode(ITEMS_USED_TO_REPAIR).setValue(obj.getRepairMaterialsRegistryKeys()); + value.getNode(MINIMUM_QUANTITY_USED_TO_REPAIR).setValue(obj.getMinimumQuantity()); + value.getNode(OVERRIDE_LEVEL_REQUIREMENT).setValue(obj.getMinimumLevel()); + value.getNode(XP_MULTIPLIER).setValue(obj.getXpMultiplier()); } private Enum getEnum(String enumConstant, TypeToken type) throws ObjectMappingException diff --git a/src/main/java/com/gmail/nossr50/config/hocon/SalvageableSerializer.java b/src/main/java/com/gmail/nossr50/config/hocon/SalvageableSerializer.java new file mode 100644 index 000000000..00f3a8d67 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/config/hocon/SalvageableSerializer.java @@ -0,0 +1,79 @@ +package com.gmail.nossr50.config.hocon; + +import com.gmail.nossr50.skills.repair.repairables.Repairable; +import com.gmail.nossr50.skills.salvage.salvageables.Salvageable; +import com.google.common.reflect.TypeToken; +import ninja.leaping.configurate.ConfigurationNode; +import ninja.leaping.configurate.objectmapping.ObjectMappingException; +import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; +import ninja.leaping.configurate.util.EnumLookup; + +import java.util.List; +import java.util.Optional; + +public class SalvageableSerializer implements TypeSerializer { + public static final String ITEM_NODE_NAME = "Item"; + public static final String ITEM_RETURNED_BY_SALVAGE = "Item-Returned-By-Salvage"; + public static final String MAXIMUM_QUANTITY_RETURNED = "Maximum-Quantity-Returned"; + public static final String OVERRIDE_LEVEL_REQUIREMENT = "Override-Level-Requirement"; + + /* + TypeTokens are obtained in two ways + + For Raw basic classes: + + TypeToken stringTok = TypeToken.of(String.class); + TypeToken intTok = TypeToken.of(Integer.class); + + For Generics: + + TypeToken> stringListTok = new TypeToken>() {}; + + Wildcard example: + + TypeToken> wildMapTok = new TypeToken>() {}; + + */ + + + @Override + public Salvageable deserialize(TypeToken type, ConfigurationNode value) throws ObjectMappingException { + + /* + CONFIG_REPAIRABLES_DEFAULTS.add(new Repairable(WOODEN_SWORD, OAK_PLANKS, 1, 0, .25D)); + */ + + /* Repairable(Material itemMaterial, Material repairMaterial, int minimumQuantity, int minimumLevel, double xpMultiplier) */ + + String item = value.getNode(ITEM_NODE_NAME).getValue(TypeToken.of(String.class)); + String itemReturnedBySalvage = value.getNode(ITEM_RETURNED_BY_SALVAGE).getValue(new TypeToken() {}); + int maximumQuantityReturned = value.getNode(MAXIMUM_QUANTITY_RETURNED).getValue(TypeToken.of(Integer.class)); + int minimumLevel = value.getNode(OVERRIDE_LEVEL_REQUIREMENT).getValue(TypeToken.of(Integer.class)); + + return new Salvageable(item, itemReturnedBySalvage, maximumQuantityReturned, minimumLevel); + } + + @Override + public void serialize(TypeToken type, Salvageable obj, ConfigurationNode value) { + + value.getNode(ITEM_NODE_NAME).setValue(obj.getItemMaterial().getKey().toString()); + value.getNode(ITEM_RETURNED_BY_SALVAGE).setValue(obj.getSalvagedItemMaterial()); + value.getNode(MAXIMUM_QUANTITY_RETURNED).setValue(obj.getMaximumQuantity()); + value.getNode(OVERRIDE_LEVEL_REQUIREMENT).setValue(obj.getMinimumLevel()); + } + + private Enum getEnum(String enumConstant, TypeToken type) throws ObjectMappingException + { + //noinspection RedundantCast + Optional ret = (Optional) EnumLookup.lookupEnum(type.getRawType().asSubclass(Enum.class), + enumConstant); // XXX: intellij says this cast is optional but it isnt + + if (!ret.isPresent()) { + throw new ObjectMappingException("Invalid enum constant provided for " + enumConstant + ": " + + "Expected a value of enum " + type + ", got " + enumConstant); + } + + return ret.get(); + } + +}