use a custom collection of serializers instead of the default list

This commit is contained in:
nossr50 2019-04-24 21:01:33 -07:00
parent e8ea502f77
commit 63e905db6c
2 changed files with 36 additions and 18 deletions

View File

@ -44,6 +44,7 @@ import com.gmail.nossr50.config.skills.alchemy.PotionConfig;
import com.gmail.nossr50.config.treasure.ExcavationTreasureConfig;
import com.gmail.nossr50.config.treasure.FishingTreasureConfig;
import com.gmail.nossr50.config.treasure.HerbalismTreasureConfig;
import com.gmail.nossr50.datatypes.experience.FormulaType;
import com.gmail.nossr50.datatypes.party.PartyFeature;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SubSkillType;
@ -55,6 +56,7 @@ import com.gmail.nossr50.skills.salvage.salvageables.SalvageableManager;
import com.gmail.nossr50.util.experience.ExperienceMapManager;
import com.google.common.collect.Maps;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializerCollection;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializers;
import org.bukkit.Material;
@ -82,6 +84,10 @@ public final class ConfigManager {
private SalvageableManager salvageableManager;
private BonusDropManager bonusDropManager;
/* CUSTOM SERIALIZERS */
private TypeSerializerCollection customSerializers;
/* MOD MANAGERS */
//TODO: Add these back when modded servers become a thing again
@ -305,12 +311,24 @@ public final class ConfigManager {
DEFAULT_SERIALIZERS.registerType(TypeToken.of(Pattern.class), new PatternSerializer());
*/
customSerializers = TypeSerializers.newCollection();
mcMMO.p.getLogger().info("Registering custom type serializers for Configurate...");
TypeSerializers.getDefaultSerializers().registerType(new TypeToken<Material>() {}, new CustomEnumValueSerializer());
TypeSerializers.getDefaultSerializers().registerType(new TypeToken<PartyFeature>() {}, new CustomEnumValueSerializer());
TypeSerializers.getDefaultSerializers().registerType(TypeToken.of(Repairable.class), new RepairableSerializer());
TypeSerializers.getDefaultSerializers().registerType(TypeToken.of(Salvageable.class), new SalvageableSerializer());
TypeSerializers.getDefaultSerializers().registerType(TypeToken.of(MinecraftMaterialWrapper.class), new MinecraftMaterialWrapperSerializer());
customSerializers.registerType(new TypeToken<Material>() {}, new CustomEnumValueSerializer());
customSerializers.registerType(new TypeToken<PartyFeature>() {}, new CustomEnumValueSerializer());
customSerializers.registerType(new TypeToken<FormulaType>() {}, new CustomEnumValueSerializer());
customSerializers.registerType(TypeToken.of(Repairable.class), new RepairableSerializer());
customSerializers.registerType(TypeToken.of(Salvageable.class), new SalvageableSerializer());
customSerializers.registerType(TypeToken.of(MinecraftMaterialWrapper.class), new MinecraftMaterialWrapperSerializer());
}
/**
* Gets the serializers registered and used by mcMMO
* This includes all default serializers
* @return our custom serializers
*/
public TypeSerializerCollection getCustomSerializers() {
return customSerializers;
}
private void registerSkillConfig(PrimarySkillType primarySkillType, Class clazz)

View File

@ -62,7 +62,9 @@ public class SerializedConfigLoader<T> {
"\nYou can also consult the new official wiki" +
"\nhttps://mcmmo.org/wiki - Keep in mind the wiki is a WIP and may not have information about everything in mcMMO!";
private static final ConfigurationOptions LOADER_OPTIONS = ConfigurationOptions.defaults().setHeader(CONFIG_HEADER);
private ConfigurationOptions configurationOptions;
private CommentedConfigurationNode data;
private CommentedConfigurationNode fileData;
private final String ROOT_NODE_ADDRESS;
@ -78,16 +80,6 @@ public class SerializedConfigLoader<T> {
*/
private HoconConfigurationLoader loader;
/**
* A node representation of "whats actually in the file".
*/
private CommentedConfigurationNode fileData = SimpleCommentedConfigurationNode.root(LOADER_OPTIONS);
/**
* A node representation of {@link #fileData}, merged with the data of {@link #parent}.
*/
private CommentedConfigurationNode data = SimpleCommentedConfigurationNode.root(LOADER_OPTIONS);
/**
* The mapper instance used to populate the config instance
*/
@ -104,7 +96,15 @@ public class SerializedConfigLoader<T> {
Files.createFile(path);
}
this.loader = HoconConfigurationLoader.builder().setPath(path).build();
configurationOptions = ConfigurationOptions.defaults().setSerializers(mcMMO.getConfigManager().getCustomSerializers()).setHeader(CONFIG_HEADER);
data = SimpleCommentedConfigurationNode.root(configurationOptions);
fileData = SimpleCommentedConfigurationNode.root(configurationOptions);
this.loader = HoconConfigurationLoader.builder()
.setPath(path)
.setDefaultOptions(configurationOptions)
.build();
this.configMapper = ObjectMapper.forClass(clazz).bindToNew();
reload();
@ -128,7 +128,7 @@ public class SerializedConfigLoader<T> {
public boolean save() {
try {
// save from the mapped object --> node
CommentedConfigurationNode saveNode = SimpleCommentedConfigurationNode.root(LOADER_OPTIONS);
CommentedConfigurationNode saveNode = SimpleCommentedConfigurationNode.root(configurationOptions);
this.configMapper.serialize(saveNode.getNode(ROOT_NODE_ADDRESS));
// before saving this config, remove any values already declared with the same value on the parent