From d1442fe35abf7e8e90087b1920935e52da34255b Mon Sep 17 00:00:00 2001 From: Timo Ley Date: Sat, 9 Nov 2024 20:41:40 +0100 Subject: [PATCH] chore: move registries out of worldtype --- .../java/dev/tilera/cwg/ClassicWorldgen.java | 13 +- .../java/dev/tilera/cwg/WorldTypeCustom.java | 171 +----------------- .../api/generator/IChunkManagerFactory.java | 2 + .../tilera/cwg/options/GeneratorRegistry.java | 31 ++++ .../tilera/cwg/options/OptionRegistry.java | 151 ++++++++++++++++ 5 files changed, 198 insertions(+), 170 deletions(-) create mode 100644 src/main/java/dev/tilera/cwg/options/GeneratorRegistry.java create mode 100644 src/main/java/dev/tilera/cwg/options/OptionRegistry.java diff --git a/src/main/java/dev/tilera/cwg/ClassicWorldgen.java b/src/main/java/dev/tilera/cwg/ClassicWorldgen.java index e8566f8..33a480e 100644 --- a/src/main/java/dev/tilera/cwg/ClassicWorldgen.java +++ b/src/main/java/dev/tilera/cwg/ClassicWorldgen.java @@ -10,6 +10,7 @@ import cpw.mods.fml.common.event.FMLServerStoppedEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import dev.tilera.cwg.api.CwgGlobals; import dev.tilera.cwg.api.hooks.IHookProvider; +import dev.tilera.cwg.api.options.IGeneratorOptionRegistry; import dev.tilera.cwg.api.utils.BooleanOption; import dev.tilera.cwg.api.utils.IntOption; import dev.tilera.cwg.api.utils.StringOption; @@ -26,6 +27,8 @@ import dev.tilera.cwg.hooks.SwissCavegenHook; import dev.tilera.cwg.noisegen.NoiseGeneratorOctavesFarlands; import dev.tilera.cwg.options.ChunkManagerOption; import dev.tilera.cwg.options.ConfigProvider; +import dev.tilera.cwg.options.GeneratorRegistry; +import dev.tilera.cwg.options.OptionRegistry; import dev.tilera.cwg.proxy.CommonProxy; import dev.tilera.cwg.vanilla.SingleBiomeChunkManagerFactory; import dev.tilera.cwg.vanilla.VanillaChunkManagerFactory; @@ -42,7 +45,7 @@ public class ClassicWorldgen { public static final WorldTypeCustom CUSTOM = new WorldTypeCustom(); public static final WorldTypeClassic CLASSIC = new WorldTypeClassic(); - public static final ConfigProvider CONFIG = new ConfigProvider(CUSTOM); + public static ConfigProvider CONFIG; public static BiomeGenBase[] biomeCache = new BiomeGenBase[256]; @Mod.Instance @@ -53,10 +56,12 @@ public class ClassicWorldgen { @EventHandler public void preInit(FMLPreInitializationEvent event) { Config.initConfig(); - CustomDimensions.INSTANCE = new CustomDimensions(CUSTOM); - CwgGlobals.setOptionRegistry(CUSTOM); + IGeneratorOptionRegistry optionRegistry = new OptionRegistry(); + CONFIG = new ConfigProvider(optionRegistry); + CustomDimensions.INSTANCE = new CustomDimensions(optionRegistry); + CwgGlobals.setOptionRegistry(optionRegistry); CwgGlobals.setDefaultProvider(CONFIG); - CwgGlobals.setGeneratorRegistry(CUSTOM); + CwgGlobals.setGeneratorRegistry(new GeneratorRegistry()); CwgGlobals.setHookRegistry(new HookRegistry()); CwgGlobals.setCurrentState(null); } diff --git a/src/main/java/dev/tilera/cwg/WorldTypeCustom.java b/src/main/java/dev/tilera/cwg/WorldTypeCustom.java index 9b14735..f856a3d 100644 --- a/src/main/java/dev/tilera/cwg/WorldTypeCustom.java +++ b/src/main/java/dev/tilera/cwg/WorldTypeCustom.java @@ -1,37 +1,16 @@ package dev.tilera.cwg; -import java.util.Base64; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.Map.Entry; -import java.util.stream.Collectors; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; - import dev.tilera.cwg.api.CwgGlobals; import dev.tilera.cwg.api.generator.AbstractChunkManager; import dev.tilera.cwg.api.generator.IChunkManagerFactory; -import dev.tilera.cwg.api.generator.IChunkManagerRegistry; import dev.tilera.cwg.api.options.IGeneratorOptionProvider; import dev.tilera.cwg.api.options.IGeneratorOptionRegistry; -import dev.tilera.cwg.api.options.IOption; -import dev.tilera.cwg.options.OptionProvider; import net.minecraft.world.World; import net.minecraft.world.WorldType; import net.minecraft.world.biome.WorldChunkManager; import net.minecraft.world.chunk.IChunkProvider; -public class WorldTypeCustom extends WorldType implements IGeneratorOptionRegistry, IChunkManagerRegistry { - - private Map> optionRegistry = new HashMap<>(); - private Map chunkManagerRegistry = new HashMap<>(); - private Gson gson = new GsonBuilder().create(); +public class WorldTypeCustom extends WorldType { public WorldTypeCustom() { super("cwg"); @@ -39,16 +18,17 @@ public class WorldTypeCustom extends WorldType implements IGeneratorOptionRegist @Override public WorldChunkManager getChunkManager(World world) { + IGeneratorOptionRegistry registry = CwgGlobals.getOptionRegistry(); String opts = world.getWorldInfo().getGeneratorOptions(); IGeneratorOptionProvider options; if (opts.isEmpty()) { - options = this; + options = registry; } else { try { - options = decodeOptions(opts); + options = registry.decodeOptions(opts); } catch (Exception e) { e.printStackTrace(); - options = this; + options = registry; } } AbstractChunkManager manager = options.getValue("cwg:generator", IChunkManagerFactory.class).createChunkManager(options, world); @@ -65,145 +45,4 @@ public class WorldTypeCustom extends WorldType implements IGeneratorOptionRegist } } - @Override - public void registerOption(IOption option) { - optionRegistry.put(option.getID(), option); - } - - @Override - public Set getRegisteredOptions() { - return optionRegistry.keySet(); - } - - @Override - public String encodeOptions(IGeneratorOptionProvider provider) { - JsonObject json = new JsonObject(); - IChunkManagerFactory generator = provider.getValue("cwg:generator", IChunkManagerFactory.class); - for (String id : this.getRegisteredOptions()) { - IOption o = optionRegistry.get(id); - if (o == null) { - continue; - } else if (generator != null && o.isGeneratorSpecific() && !generator.hasSpecificOption(o)) { - continue; - } else if (o.getType() == Integer.class) { - json.add(id, new JsonPrimitive(provider.getInt(id))); - } else if (o.getType() == Double.class) { - json.add(id, new JsonPrimitive(provider.getDouble(id))); - } else if (o.getType() == Boolean.class) { - json.add(id, new JsonPrimitive(provider.getBoolean(id))); - } else if (o.getType() == String.class) { - json.add(id, new JsonPrimitive(provider.getString(id))); - } else { - String enc = o.toRepresentation(id, provider); - json.add(id, new JsonPrimitive(enc)); - } - } - String jsn = gson.toJson(json); - return Base64.getEncoder().encodeToString(jsn.getBytes()); - } - - @Override - public IGeneratorOptionProvider decodeOptions(String options) { - String jsn = new String(Base64.getDecoder().decode(options)); - JsonObject json = gson.fromJson(jsn, JsonObject.class); - OptionProvider provider = new OptionProvider(this); - for (Entry e : json.entrySet()) { - if (!e.getValue().isJsonPrimitive()) continue; - JsonPrimitive value = e.getValue().getAsJsonPrimitive(); - IOption o = optionRegistry.get(e.getKey()); - if (o == null) { - continue; - } else if (o.getType() == Integer.class && value.isNumber()) { - provider.putInt(o.getID(), value.getAsInt()); - } else if (o.getType() == Double.class && value.isNumber()) { - provider.putDouble(o.getID(), value.getAsDouble()); - } else if (o.getType() == Boolean.class && value.isBoolean()) { - provider.putBoolean(o.getID(), value.getAsBoolean()); - } else if (o.getType() == String.class && value.isString()) { - provider.putString(o.getID(), value.getAsString()); - } else if (value.isString()) { - provider.putValue(o.getID(), o.fromRepresentation(value.getAsString())); - } - } - return provider; - } - - @Override - public boolean isRegistered(String id, Class type) { - IOption o = optionRegistry.get(id); - return o != null && o.getType() == type; - } - - @SuppressWarnings("unchecked") - private T getOption(String id, Class type, T defaultValue) { - IOption o = optionRegistry.get(id); - if (o == null) { - return defaultValue; - } else if (o.getType().isAssignableFrom(type)) { - return (T) o.getDefault(); - } - return defaultValue; - } - - @Override - public Integer getInt(String id) { - return getOption(id, Integer.class, null); - } - - @Override - public String getString(String id) { - return getOption(id, String.class, null); - } - - @Override - public Double getDouble(String id) { - return getOption(id, Double.class, null); - } - - @Override - public Boolean getBoolean(String id) { - return getOption(id, Boolean.class, false); - } - - @Override - public T getValue(String id, Class type) { - return getOption(id, type, null); - } - - @Override - public void registerChunkManager(IChunkManagerFactory factory) { - chunkManagerRegistry.put(factory.getID(), factory); - } - - @Override - public IChunkManagerFactory getFactory(String id) { - return chunkManagerRegistry.get(id); - } - - @Override - public List getChunkManagers() { - return chunkManagerRegistry.values().stream().collect(Collectors.toList()); - } - - @SuppressWarnings("unchecked") - @Override - public IOption getOption(String id, Class type) { - IOption opt = optionRegistry.get(id); - if (opt != null && opt.getType() == type) { - return (IOption) optionRegistry.get(id); - } else { - return null; - } - } - - @Override - public Class getOptionType(String id) { - IOption opt = optionRegistry.get(id); - if (opt != null) { - return opt.getType(); - } else { - return null; - } - } - } diff --git a/src/main/java/dev/tilera/cwg/api/generator/IChunkManagerFactory.java b/src/main/java/dev/tilera/cwg/api/generator/IChunkManagerFactory.java index 604fe0a..2cdfad7 100644 --- a/src/main/java/dev/tilera/cwg/api/generator/IChunkManagerFactory.java +++ b/src/main/java/dev/tilera/cwg/api/generator/IChunkManagerFactory.java @@ -14,4 +14,6 @@ public interface IChunkManagerFactory { boolean hasSpecificOption(IOption option); + default void onRegister() {} + } diff --git a/src/main/java/dev/tilera/cwg/options/GeneratorRegistry.java b/src/main/java/dev/tilera/cwg/options/GeneratorRegistry.java new file mode 100644 index 0000000..4ce7081 --- /dev/null +++ b/src/main/java/dev/tilera/cwg/options/GeneratorRegistry.java @@ -0,0 +1,31 @@ +package dev.tilera.cwg.options; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import dev.tilera.cwg.api.generator.IChunkManagerFactory; +import dev.tilera.cwg.api.generator.IChunkManagerRegistry; + +public class GeneratorRegistry implements IChunkManagerRegistry { + + private Map registry = new HashMap<>(); + + @Override + public void registerChunkManager(IChunkManagerFactory factory) { + registry.put(factory.getID(), factory); + factory.onRegister(); + } + + @Override + public IChunkManagerFactory getFactory(String id) { + return registry.get(id); + } + + @Override + public List getChunkManagers() { + return registry.values().stream().collect(Collectors.toList()); + } + +} diff --git a/src/main/java/dev/tilera/cwg/options/OptionRegistry.java b/src/main/java/dev/tilera/cwg/options/OptionRegistry.java new file mode 100644 index 0000000..1e7267e --- /dev/null +++ b/src/main/java/dev/tilera/cwg/options/OptionRegistry.java @@ -0,0 +1,151 @@ +package dev.tilera.cwg.options; + +import java.util.Base64; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; + +import dev.tilera.cwg.api.generator.IChunkManagerFactory; +import dev.tilera.cwg.api.options.IGeneratorOptionProvider; +import dev.tilera.cwg.api.options.IGeneratorOptionRegistry; +import dev.tilera.cwg.api.options.IOption; + +public class OptionRegistry implements IGeneratorOptionRegistry { + + private Map> registry = new HashMap<>(); + private Gson gson = new GsonBuilder().create(); + + @Override + public void registerOption(IOption option) { + registry.put(option.getID(), option); + } + + @Override + public Set getRegisteredOptions() { + return registry.keySet(); + } + + @Override + public String encodeOptions(IGeneratorOptionProvider provider) { + JsonObject json = new JsonObject(); + IChunkManagerFactory generator = provider.getValue("cwg:generator", IChunkManagerFactory.class); + for (String id : this.getRegisteredOptions()) { + IOption o = registry.get(id); + if (o == null) { + continue; + } else if (generator != null && o.isGeneratorSpecific() && !generator.hasSpecificOption(o)) { + continue; + } else if (o.getType() == Integer.class) { + json.add(id, new JsonPrimitive(provider.getInt(id))); + } else if (o.getType() == Double.class) { + json.add(id, new JsonPrimitive(provider.getDouble(id))); + } else if (o.getType() == Boolean.class) { + json.add(id, new JsonPrimitive(provider.getBoolean(id))); + } else if (o.getType() == String.class) { + json.add(id, new JsonPrimitive(provider.getString(id))); + } else { + String enc = o.toRepresentation(id, provider); + json.add(id, new JsonPrimitive(enc)); + } + } + String jsn = gson.toJson(json); + return Base64.getEncoder().encodeToString(jsn.getBytes()); + } + + @Override + public IGeneratorOptionProvider decodeOptions(String options) { + String jsn = new String(Base64.getDecoder().decode(options)); + JsonObject json = gson.fromJson(jsn, JsonObject.class); + OptionProvider provider = new OptionProvider(this); + for (Entry e : json.entrySet()) { + if (!e.getValue().isJsonPrimitive()) continue; + JsonPrimitive value = e.getValue().getAsJsonPrimitive(); + IOption o = registry.get(e.getKey()); + if (o == null) { + continue; + } else if (o.getType() == Integer.class && value.isNumber()) { + provider.putInt(o.getID(), value.getAsInt()); + } else if (o.getType() == Double.class && value.isNumber()) { + provider.putDouble(o.getID(), value.getAsDouble()); + } else if (o.getType() == Boolean.class && value.isBoolean()) { + provider.putBoolean(o.getID(), value.getAsBoolean()); + } else if (o.getType() == String.class && value.isString()) { + provider.putString(o.getID(), value.getAsString()); + } else if (value.isString()) { + provider.putValue(o.getID(), o.fromRepresentation(value.getAsString())); + } + } + return provider; + } + + @Override + public boolean isRegistered(String id, Class type) { + IOption o = registry.get(id); + return o != null && o.getType() == type; + } + + @SuppressWarnings("unchecked") + private T getOption(String id, Class type, T defaultValue) { + IOption o = registry.get(id); + if (o == null) { + return defaultValue; + } else if (o.getType().isAssignableFrom(type)) { + return (T) o.getDefault(); + } + return defaultValue; + } + + @Override + public Integer getInt(String id) { + return getOption(id, Integer.class, null); + } + + @Override + public String getString(String id) { + return getOption(id, String.class, null); + } + + @Override + public Double getDouble(String id) { + return getOption(id, Double.class, null); + } + + @Override + public Boolean getBoolean(String id) { + return getOption(id, Boolean.class, false); + } + + @Override + public T getValue(String id, Class type) { + return getOption(id, type, null); + } + + @SuppressWarnings("unchecked") + @Override + public IOption getOption(String id, Class type) { + IOption opt = registry.get(id); + if (opt != null && opt.getType() == type) { + return (IOption) registry.get(id); + } else { + return null; + } + } + + @Override + public Class getOptionType(String id) { + IOption opt = registry.get(id); + if (opt != null) { + return opt.getType(); + } else { + return null; + } + } + +}