Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate config any time #82

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConfigAnytimeContainer

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;
}
}
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
Loading