Skip to content

Commit

Permalink
Integrate config any time (#82)
Browse files Browse the repository at this point in the history
* feat: integrate ConfigAnyTime

* docs: correct javadoc of configanytime

* refactor: fix config anytime mod container name

* fix: inject config anytime container manually
  • Loading branch information
kappa-maintainer authored Feb 29, 2024
1 parent 2ef257b commit 1970f0b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ mcp_version = 20240102.171137
special_source = net.md-5:SpecialSource:1.9.0
special_source_mcp = net.md-5:SpecialSource:1.8.5
binary_patcher = net.minecraftforge:binarypatcher:1.1.1:fatjar
installer_tools_version = 1.3.0
installer_tools_version = 1.4.1

# Libraries
asm_version = 9.6
asm_deprecated = 7.1
bouncepad_version = 0.4.37-cursed
bouncepad_version = 0.4.38-cursed
lwjgl_version = 3.3.4-27-CLEANROOM

# Sets default memory used for Gradle commands. Can be overridden by user or command line properties.
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/cleanroommc/configanytime/ConfigAnytime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.cleanroommc.configanytime;

import net.minecraftforge.common.config.Config;
import net.minecraftforge.common.config.ConfigManager;

@Deprecated
public class ConfigAnytime {
/**
* A wrapper of {@link ConfigManager#register(Class)}, created for compatibility.
* @param configClass configuration class that is annotated with {@link Config}
*/
public static void register(Class<?> configClass) {
ConfigManager.register(configClass);
}
}
29 changes: 29 additions & 0 deletions src/main/java/net/minecraftforge/common/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;

import net.minecraft.launchwrapper.Launch;
import net.minecraftforge.common.config.Config.Comment;
import net.minecraftforge.common.config.Config.LangKey;
import net.minecraftforge.common.config.Config.Name;
Expand Down Expand Up @@ -377,4 +378,32 @@ private static String getName(Field f)
return f.getName();
}

/**
* Register configuration class that is annotated with {@link Config} here for it to be processed immediately with saving and loading supported.
* Preferably call this method in a static init block at the very end of your configuration class.
* @param configClass configuration class that is annotated with {@link Config}
*/
public static void register(Class<?> configClass) {
if (Launch.classLoader.isClassLoaded("net.minecraftforge.fml.common.Loader")) {
if (Loader.instance().hasReachedState(LoaderState.PREINITIALIZATION)) {
throw new RuntimeException("Please call this method before pre-init!");
}
}
Config config = configClass.getAnnotation(Config.class);
String modId = config.modid();
Set<Class<?>> modConfigClasses = MOD_CONFIG_CLASSES.computeIfAbsent(modId, k -> Sets.newHashSet());
modConfigClasses.add(configClass);
File configDir = new File(Launch.minecraftHome, "config");
File configFile = new File(configDir, config.name() + ".cfg");
Configuration cfg = CONFIGS.get(configFile.getAbsolutePath());
if (cfg == null) {
cfg = new Configuration(configFile);
cfg.load();
CONFIGS.put(configFile.getAbsolutePath(), cfg);
}
sync(cfg, configClass, modId, config.category(), true, null);

cfg.save();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.minecraftforge.fml.common;

import com.google.common.eventbus.EventBus;

public class ConfigAnytimeContainer extends DummyModContainer{
public ConfigAnytimeContainer() {
super(new ModMetadata());
ModMetadata meta = this.getMetadata();
meta.modId = "configanytime";
meta.name = "ConfigAnytime";
meta.description = "Allows Forge configurations to be setup at any point in time.";
meta.version = "2.0";
meta.authorList.add("Rongmario");
}

@Override
public boolean registerBus(EventBus bus, LoadController controller) {
bus.register(this);
return true;
}
}
1 change: 1 addition & 0 deletions src/main/java/net/minecraftforge/fml/common/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ private ModDiscoverer identifyMods(List<String> additionalContainers)
mods.add(minecraft);
// Add in the MCP mod container
mods.add(new InjectedModContainer(mcp,new File("minecraft.jar")));
mods.add(new ConfigAnytimeContainer());
for (String cont : injectedContainers)
{
ModContainer mc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public MixinContainer() {
meta.modId = "mixinbooter";
meta.name = "MixinBooter";
meta.description = "A Mixin library and loader.";
meta.version = "100.0";
meta.logoFile = "/icon.png";
meta.version = "10.0";
meta.authorList.add("Rongmario");
}

Expand Down

0 comments on commit 1970f0b

Please sign in to comment.