From 63e905db6c9c521cf172d579589eb51c7709a6f0 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Wed, 24 Apr 2019 21:01:33 -0700 Subject: [PATCH] use a custom collection of serializers instead of the default list --- .../gmail/nossr50/config/ConfigManager.java | 28 +++++++++++++++---- .../config/hocon/SerializedConfigLoader.java | 26 ++++++++--------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/config/ConfigManager.java b/src/main/java/com/gmail/nossr50/config/ConfigManager.java index 94d475af0..7f2079127 100644 --- a/src/main/java/com/gmail/nossr50/config/ConfigManager.java +++ b/src/main/java/com/gmail/nossr50/config/ConfigManager.java @@ -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() {}, new CustomEnumValueSerializer()); - TypeSerializers.getDefaultSerializers().registerType(new TypeToken() {}, 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() {}, new CustomEnumValueSerializer()); + customSerializers.registerType(new TypeToken() {}, new CustomEnumValueSerializer()); + customSerializers.registerType(new TypeToken() {}, 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) diff --git a/src/main/java/com/gmail/nossr50/config/hocon/SerializedConfigLoader.java b/src/main/java/com/gmail/nossr50/config/hocon/SerializedConfigLoader.java index f0c09726e..2ada0563e 100644 --- a/src/main/java/com/gmail/nossr50/config/hocon/SerializedConfigLoader.java +++ b/src/main/java/com/gmail/nossr50/config/hocon/SerializedConfigLoader.java @@ -62,7 +62,9 @@ public class SerializedConfigLoader { "\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 { */ 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 { 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 { 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