Skip to content

Commit

Permalink
config modification api
Browse files Browse the repository at this point in the history
  • Loading branch information
Treetrain1 authored Oct 16, 2023
1 parent 4cae4e1 commit 36a67fb
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ public abstract class Config<T> {

private final String modId;
private final Path path;
private final boolean supportsModification;
private final Class<T> config;
private T configInstance;
private final T defaultInstance;

public Config(String modId, Class<T> config, Path path) {
public Config(String modId, Class<T> config, Path path, boolean supportsModification) {
this.modId = modId;
this.path = path;
this.supportsModification = supportsModification;
this.config = config;
try {
this.defaultInstance = this.configInstance = config.getConstructor().newInstance();
Expand All @@ -51,7 +53,16 @@ public Path path() {
return this.path;
}

public boolean supportsModification() {
return this.supportsModification;
}

public T config() {
if (this.supportsModification()) return ConfigModification.modifyConfig(this.configClass(), this.instance());
return this.instance();
}

public T instance() {
return this.configInstance;
}

Expand All @@ -69,4 +80,5 @@ public Class<T> configClass() {

public abstract void save();
public abstract boolean load();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.frozenblock.lib.config.api.instance;

import net.frozenblock.config.api.registry.ConfigRegistry;
import java.util.function.Consumer;

public record ConfigModification<T>(Config<T> instance, Consumer<T> modifications) {
public static <T> T modifyConfig(Config<T> config, T original) {
// clone
T instance = config.configClass().getConstructor().newInstance();
copyInto(original, instance);


// modify
for (ConfigModification modification : ConfigRegistry.getModificationsForConfig(config)) {
modification.modifications.accept(instance);
}
return instance;
}

private static <T> void copyInto(T source, T destination) {
Class<?> clazz = source.getClass();
while (!clazz.equals(Object.class)) {
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
try {
field.set(destination, field.get(source));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
clazz = clazz.getSuperclass();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,27 @@ public class JsonConfig<T> extends Config<T> {
private final JsonType type;

public JsonConfig(String modId, Class<T> config) {
this(modId, config, JsonType.JSON);
this(modId, config, true);
}

public JsonConfig(String modId, Class<T> config, JsonType type) {
this(modId, config, makePath(modId, type.getSerializedName()), type);
this(modId, config, type, true);
}

public JsonConfig(String modId, Class<T> config, Path path, JsonType type) {
super(modId, config, path);
this(modId, config, path, type, true);
}

public JsonConfig(String modId, Class<T> config, boolean supportsModification) {
this(modId, config, JsonType.JSON, supportsModification);
}

public JsonConfig(String modId, Class<T> config, JsonType type, boolean supportsModification) {
this(modId, config, makePath(modId, type.getSerializedName()), type, supportsModification);
}

public JsonConfig(String modId, Class<T> config, Path path, JsonType type, boolean supportsModification) {
super(modId, config, path, supportsModification);
var janksonBuilder = Jankson.builder();

this.jankson = ConfigSerialization.createJankson(janksonBuilder, modId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public TomlConfig(String modId, Class<T> config, TomlWriter.Builder builder) {
}

public TomlConfig(String modId, Class<T> config, Path path, TomlWriter.Builder builder) {
super(modId, config, path);
super(modId, config, path, true);
this.tomlWriter = builder.build();

if (this.load()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class ConfigRegistry {
private static final Map<String, List<TypedEntryType<?>>> MOD_TYPED_ENTRY_REGISTRY = new Object2ObjectOpenHashMap<>();
private static final List<TypedEntryType<?>> TYPED_ENTRY_REGISTRY = new ObjectArrayList<>();

private static final Map<Config<?>, List<ConfigModification<?>>> MODIFICATION_REGISTRY = new Object2ObjectOpenHashMap<>();

public static <T> Config<T> register(Config<T> config) {
if (CONFIG_REGISTRY.contains(config)) {
throw new IllegalStateException("Config already registered.");
Expand Down Expand Up @@ -76,4 +78,14 @@ public static Collection<TypedEntryType<?>> getTypedEntryTypesForMod(String modI
public static Collection<TypedEntryType<?>> getAllTypedEntryTypes() {
return List.copyOf(TYPED_ENTRY_REGISTRY);
}

public static <T> ConfigModification<T> register(Config<T> config, ConfigModification<T> modification) {
if (!contains(config)) throw new IllegalStateException("Config " + config + " not in registry!");
MODIFICATION_REGISTRY.computeIfAbsent(config, a -> new ArrayList<>()).add(modification);
return modification;
}

public static <T> Collection<ConfigModification<T>> getModificationsForConfig(Config<T> config) {
return (Collection<ConfigModification<T>>) (Collection) Map.copyOf(MODIFICATION_REGISTRY).getOrDefault(config, new ArrayList<>());
}
}

0 comments on commit 36a67fb

Please sign in to comment.