Add Serializer for Repairable

This commit is contained in:
nossr50 2019-03-22 14:13:36 -07:00
parent bf8e3d5a08
commit 0f98d9babe
5 changed files with 64 additions and 87 deletions

View File

@ -3,6 +3,7 @@ package com.gmail.nossr50.config;
import com.gmail.nossr50.config.collectionconfigs.SalvageConfig;
import com.gmail.nossr50.config.experience.ExperienceConfig;
import com.gmail.nossr50.config.hocon.CustomEnumValueSerializer;
import com.gmail.nossr50.config.hocon.RepairableSerializer;
import com.gmail.nossr50.config.hocon.SerializedConfigLoader;
import com.gmail.nossr50.config.hocon.admin.ConfigAdmin;
import com.gmail.nossr50.config.hocon.antiexploit.ConfigExploitPrevention;
@ -46,6 +47,7 @@ import com.gmail.nossr50.config.treasure.HerbalismTreasureConfig;
import com.gmail.nossr50.datatypes.party.PartyFeature;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.skills.repair.repairables.Repairable;
import com.gmail.nossr50.skills.repair.repairables.SimpleRepairable;
import com.gmail.nossr50.skills.repair.repairables.SimpleRepairableManager;
import com.gmail.nossr50.skills.salvage.salvageables.Salvageable;
@ -213,6 +215,7 @@ public final class ConfigManager {
TypeSerializers.getDefaultSerializers().registerType(new TypeToken<Material>() {}, new CustomEnumValueSerializer());
TypeSerializers.getDefaultSerializers().registerType(new TypeToken<PartyFeature>() {}, new CustomEnumValueSerializer());
TypeSerializers.getDefaultSerializers().registerType(TypeToken.of(SimpleRepairable.class), new RepairableSerializer());
mcMMO.p.getLogger().info("Deserializing configs...");
//TODO: Not sure about the order of MainConfig

View File

@ -0,0 +1,59 @@
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 org.bukkit.Material;
public class RepairableSerializer implements TypeSerializer<SimpleRepairable> {
/*
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 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) */
Material item = value.getNode("Item").getValue(TypeToken.of(Material.class));
Material repairItem = value.getNode("Item-Used-To-Repair").getValue(TypeToken.of(Material.class));
int minimumQuantity = value.getNode("Minimum-Quantity-Used-To-Repair").getValue(TypeToken.of(Integer.class));
int minimumLevel = value.getNode("Skill-Level-Required-To-Repair").getValue(TypeToken.of(Integer.class));
double xpMultiplier = value.getNode("XP-Multiplier").getValue(TypeToken.of(Double.class));
return new SimpleRepairable(item, repairItem, minimumQuantity, minimumLevel, xpMultiplier);
}
@Override
public void serialize(TypeToken<?> type, SimpleRepairable obj, ConfigurationNode value) throws ObjectMappingException {
value.getNode("Item").setValue(obj.getItemMaterial());
value.getNode("Item-Used-To-Repair").setValue(obj.getRepairMaterial());
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());
}
}

View File

@ -26,7 +26,7 @@ import com.gmail.nossr50.runnables.player.PlayerProfileLoadingTask;
import com.gmail.nossr50.runnables.player.PowerLevelUpdatingTask;
import com.gmail.nossr50.runnables.skills.BleedTimerTask;
import com.gmail.nossr50.skills.alchemy.Alchemy;
import com.gmail.nossr50.skills.repair.repairables.RepairableManager;
import com.gmail.nossr50.skills.repair.repairables.SimpleRepairableManager;
import com.gmail.nossr50.skills.salvage.salvageables.SalvageableManager;
import com.gmail.nossr50.util.*;
import com.gmail.nossr50.util.blockmeta.chunkmeta.ChunkManager;
@ -354,7 +354,7 @@ public class mcMMO extends JavaPlugin {
return placeStore;
}
public static RepairableManager getRepairableManager() {
public static SimpleRepairableManager getRepairableManager() {
return configManager.getSimpleRepairableManager();
}

View File

@ -1,23 +0,0 @@
/*
package com.gmail.nossr50.skills.repair.repairables;
import com.gmail.nossr50.datatypes.skills.ItemType;
import com.gmail.nossr50.datatypes.skills.MaterialType;
import org.bukkit.Material;
public class RepairableFactory {
public static Repairable getRepairable(Material itemMaterial, Material repairMaterial, byte repairMetadata, int minimumQuantity, short maximumDurability) {
return getRepairable(itemMaterial, repairMaterial, repairMetadata, null, 0, minimumQuantity, maximumDurability, ItemType.OTHER, MaterialType.OTHER, 1);
}
public static Repairable getRepairable(Material itemMaterial, Material repairMaterial, byte repairMetadata, int minimumLevel, int minimumQuantity, short maximumDurability, ItemType repairItemType, MaterialType repairMaterialType, double xpMultiplier) {
return getRepairable(itemMaterial, repairMaterial, repairMetadata, null, minimumLevel, minimumQuantity, maximumDurability, repairItemType, repairMaterialType, xpMultiplier);
}
public static Repairable getRepairable(Material itemMaterial, Material repairMaterial, byte repairMetadata, String repairMaterialPrettyName, int minimumLevel, int minimumQuantity, short maximumDurability, ItemType repairItemType, MaterialType repairMaterialType, double xpMultiplier) {
// TODO: Add in loading from config what type of repairable we want.
return new SimpleRepairable(itemMaterial, repairMaterial, repairMetadata, repairMaterialPrettyName, minimumLevel, minimumQuantity, maximumDurability, repairItemType, repairMaterialType, xpMultiplier);
}
}
*/

View File

@ -1,62 +0,0 @@
/*
package com.gmail.nossr50.skills.repair.repairables;
import com.gmail.nossr50.config.Unload;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.List;
public interface RepairableManager extends Unload {
*/
/**
* Register a repairable with the RepairManager
*
* @param repairable Repairable to register
*//*
public void registerRepairable(Repairable repairable);
*/
/**
* Register a list of repairables with the RepairManager
*
* @param repairables List<Repairable> to register
*//*
public void registerRepairables(List<Repairable> repairables);
*/
/**
* Checks if an item is repairable
*
* @param type Material to check if repairable
*
* @return true if repairable, false if not
*//*
public boolean isRepairable(Material type);
*/
/**
* Checks if an item is repairable
*
* @param itemStack Item to check if repairable
*
* @return true if repairable, false if not
*//*
public boolean isRepairable(ItemStack itemStack);
*/
/**
* Gets the repairable with this type
*
* @param type Material of the repairable to look for
*
* @return the repairable, can be null
*//*
public Repairable getRepairable(Material type);
}
*/