diff options
author | Anthony Hilyard <anthony.hilyard@gmail.com> | 2022-01-25 09:18:04 -0800 |
---|---|---|
committer | Anthony Hilyard <anthony.hilyard@gmail.com> | 2022-01-25 09:18:04 -0800 |
commit | 0cd83156930cf320578e09fafa17069bedd8230f (patch) | |
tree | 63e9d0322326126d4beaa497e06050ba905f555c /src/main/java/com/anthonyhilyard/iceberg/config/IcebergConfig.java | |
parent | 7a3df325b5bb294c2836082f58b719c70792ed61 (diff) | |
download | Iceberg-0cd83156930cf320578e09fafa17069bedd8230f.tar.gz Iceberg-0cd83156930cf320578e09fafa17069bedd8230f.tar.bz2 Iceberg-0cd83156930cf320578e09fafa17069bedd8230f.zip |
Rewrote new config system, added support for Config Menus for Forge mod.
Diffstat (limited to 'src/main/java/com/anthonyhilyard/iceberg/config/IcebergConfig.java')
-rw-r--r-- | src/main/java/com/anthonyhilyard/iceberg/config/IcebergConfig.java | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/main/java/com/anthonyhilyard/iceberg/config/IcebergConfig.java b/src/main/java/com/anthonyhilyard/iceberg/config/IcebergConfig.java new file mode 100644 index 0000000..e27e08d --- /dev/null +++ b/src/main/java/com/anthonyhilyard/iceberg/config/IcebergConfig.java @@ -0,0 +1,89 @@ +package com.anthonyhilyard.iceberg.config; + +import javax.annotation.Nonnull; + +import com.anthonyhilyard.iceberg.Loader; +import com.electronwill.nightconfig.core.Config; + +import org.apache.commons.lang3.tuple.Pair; + +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.ModLoadingContext; +import net.minecraftforge.fml.common.Mod.EventBusSubscriber; +import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus; +import net.minecraftforge.fml.config.ModConfig; +import net.minecraftforge.fml.event.config.ModConfigEvent; + +@EventBusSubscriber(modid = Loader.MODID, bus = Bus.MOD) +public abstract class IcebergConfig<T extends IcebergConfig<?>> +{ + private static IcebergConfigSpec SPEC = null; + private static IcebergConfig<?> INSTANCE = null; + private static String modId = null; + private static boolean registered = false; + + protected abstract <I extends IcebergConfig<?>> void setInstance(I instance); + protected void onLoad() {} + protected void onReload() {} + + static + { + Config.setInsertionOrderPreserved(true); + } + + @SubscribeEvent + private static void onLoadEvent(ModConfigEvent.Loading event) + { + if (modId != null && INSTANCE != null && event.getConfig().getModId().contentEquals(modId)) + { + INSTANCE.onLoad(); + } + } + + @SubscribeEvent + private static void onReloadEvent(ModConfigEvent.Reloading event) + { + if (modId != null && INSTANCE != null && event.getConfig().getModId().contentEquals(modId)) + { + INSTANCE.onReload(); + } + } + + public static final boolean register(Class<? extends IcebergConfig<?>> superClass, @Nonnull String modId) + { + if (registered) + { + return false; + } + + IcebergConfig.modId = modId; + + Pair<IcebergConfig<?>, IcebergConfigSpec> specPair = new IcebergConfigSpec.Builder().finish((builder) -> + { + IcebergConfig<?> result = null; + try + { + result = (IcebergConfig<?>)superClass.getConstructor(IcebergConfigSpec.Builder.class).newInstance(builder); + } + catch (Exception e) + { + Loader.LOGGER.warn("Failed to register configuration: {}", e); + } + return result; + }); + + if (specPair.getRight() == null || specPair.getLeft() == null) + { + return false; + } + + SPEC = specPair.getRight(); + INSTANCE = specPair.getLeft(); + INSTANCE.setInstance(specPair.getLeft()); + + ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, SPEC); + + registered = true; + return true; + } +} |