Salvageable serializer

This commit is contained in:
nossr50 2019-04-08 01:34:36 -07:00
parent b67bc45e9a
commit 3806dcbd5d
2 changed files with 94 additions and 10 deletions

View File

@ -11,6 +11,11 @@ import java.util.List;
import java.util.Optional;
public class RepairableSerializer implements TypeSerializer<Repairable> {
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> {
/* Repairable(Material itemMaterial, Material repairMaterial, int minimumQuantity, int minimumLevel, double xpMultiplier) */
String item = value.getNode("Item").getValue(TypeToken.of(String.class));
List<String> repairItems = value.getNode("Items-Used-To-Repair").getValue(new TypeToken<List<String>>() {});
String item = value.getNode(ITEM).getValue(TypeToken.of(String.class));
List<String> repairItems = value.getNode(ITEMS_USED_TO_REPAIR).getValue(new TypeToken<List<String>>() {});
/*String itemConstant = HOCONUtil.deserializeENUMName(value.getNode("Item").getString());
@ -50,9 +55,9 @@ public class RepairableSerializer implements TypeSerializer<Repairable> {
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<Repairable> {
/*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

View File

@ -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<Salvageable> {
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<String> stringTok = TypeToken.of(String.class);
TypeToken<Integer> intTok = TypeToken.of(Integer.class);
For Generics:
TypeToken<List<String>> stringListTok = new TypeToken<List<String>>() {};
Wildcard example:
TypeToken<Map<?, ?>> wildMapTok = new TypeToken<Map<?, ?>>() {};
*/
@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<String>() {});
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<Enum> 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();
}
}