package com.gmail.nossr50.config.hocon; import com.gmail.nossr50.skills.repair.repairables.SimpleRepairable; 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 RepairableSerializer implements TypeSerializer { /* 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 SimpleRepairable deserialize(TypeToken type, ConfigurationNode value) throws ObjectMappingException { /* CONFIG_REPAIRABLES_DEFAULTS.add(new SimpleRepairable(WOODEN_SWORD, OAK_PLANKS, 1, 0, .25D)); */ /* SimpleRepairable(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 itemConstant = HOCONUtil.deserializeENUMName(value.getNode("Item").getString()); String repairConstant = HOCONUtil.deserializeENUMName(value.getNode("Item-Used-To-Repair").getString()); 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)); return new SimpleRepairable(item, repairItems, minimumQuantity, minimumLevel, xpMultiplier); } @Override public void serialize(TypeToken type, SimpleRepairable obj, ConfigurationNode value) throws ObjectMappingException { /*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()); } private Enum getEnum(String enumConstant, TypeToken type) throws ObjectMappingException { 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(); } }