Skip to content

Commit

Permalink
Add field name as parameter to the global change callback
Browse files Browse the repository at this point in the history
  • Loading branch information
xpple committed Sep 12, 2024
1 parent eb667a9 commit 48ee07d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import dev.xpple.betterconfig.impl.BetterConfigImpl;
import dev.xpple.betterconfig.impl.BetterConfigInternals;
import dev.xpple.betterconfig.impl.ModConfigImpl;
import dev.xpple.betterconfig.util.TriConsumer;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

Expand All @@ -22,7 +22,7 @@ public final class ModConfigBuilder<S, C> {
private final GsonBuilder builder = new GsonBuilder().serializeNulls().enableComplexMapKeySerialization();
private final Map<Class<?>, Function<C, ? extends ArgumentType<?>>> arguments = new HashMap<>();

private BiConsumer<Object, Object> globalChangeHook = (oldValue, newValue) -> {};
private TriConsumer<String, Object, Object> globalChangeHook = (config, oldValue, newValue) -> {};

public ModConfigBuilder(String modId, Class<?> configsClass) {
this.modId = modId;
Expand Down Expand Up @@ -103,13 +103,13 @@ public <T> ModConfigBuilder<S, C> registerTypeHierarchy(Class<T> type, TypeAdapt

/**
* Register a callback that will be called whenever any config value is updated. The first
* parameter of the callback is the old value, the second parameter is the new value. Both values
* are deep copies of the updated config, so they can be modified without care for the config's
* value.
* parameter of the callback is the name of the config that was changed. The second parameter is
* the old value and the third parameter is the new value. Both values are deep copies of the
* updated config, so they can be modified without care for the config's value.
* @param hook the callback
* @return the current builder instance
*/
public ModConfigBuilder<S, C> registerGlobalChangeHook(BiConsumer<Object, Object> hook) {
public ModConfigBuilder<S, C> registerGlobalChangeHook(TriConsumer<String, Object, Object> hook) {
this.globalChangeHook = hook;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,6 @@ static void onChange(ModConfigImpl<?, ?> modConfig, Field field, CheckedRunnable
updater.run();
Object newValue = modConfig.deepCopy(field.get(null), field.getGenericType());
onChange.accept(oldValue, newValue);
modConfig.getGlobalChangeHook().accept(oldValue, newValue);
modConfig.getGlobalChangeHook().accept(field.getName(), oldValue, newValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import dev.xpple.betterconfig.api.Config;
import dev.xpple.betterconfig.util.CheckedBiConsumer;
import dev.xpple.betterconfig.util.CheckedConsumer;
import dev.xpple.betterconfig.util.TriConsumer;

import java.io.BufferedWriter;
import java.io.IOException;
Expand Down Expand Up @@ -64,9 +65,9 @@ public class ModConfigImpl<S, C> implements ModConfig {
private final Gson inlineGson;
private final Map<Class<?>, Function<C, ? extends ArgumentType<?>>> arguments;

private final BiConsumer<Object, Object> globalChangeHook;
private final TriConsumer<String, Object, Object> globalChangeHook;

public ModConfigImpl(String modId, Class<?> configsClass, Gson gson, Map<Class<?>, Function<C, ? extends ArgumentType<?>>> arguments, BiConsumer<Object, Object> globalChangeHook) {
public ModConfigImpl(String modId, Class<?> configsClass, Gson gson, Map<Class<?>, Function<C, ? extends ArgumentType<?>>> arguments, TriConsumer<String, Object, Object> globalChangeHook) {
this.modId = modId;
this.configsClass = configsClass;
this.gson = gson.newBuilder().setPrettyPrinting().create();
Expand Down Expand Up @@ -129,7 +130,7 @@ Map<String, BiConsumer<Object, Object>> getOnChangeCallbacks() {
return this.onChangeCallbacks;
}

BiConsumer<Object, Object> getGlobalChangeHook() {
TriConsumer<String, Object, Object> getGlobalChangeHook() {
return this.globalChangeHook;
}

Expand Down
16 changes: 16 additions & 0 deletions common/src/main/java/dev/xpple/betterconfig/util/TriConsumer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.xpple.betterconfig.util;

import java.util.Objects;

@FunctionalInterface
public interface TriConsumer<T, U, V> {
void accept(T k, U v, V s);

default TriConsumer<T, U, V> andThen(TriConsumer<? super T, ? super U, ? super V> after) {
Objects.requireNonNull(after);
return (t, u, v) -> {
accept(t, u, v);
after.accept(t, u, v);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void onInitializeServer() {
.registerTypeHierarchy(Block.class, new BlockAdapter(), BlockWrappedArgumentType::block)
.registerTypeHierarchy(BlockInput.class, new BlockStateAdapter(), BlockStateArgument::block)
.registerTypeHierarchy((Class<StructureType<?>>) (Class) StructureType.class, new StructureAdapter(), StructureArgumentType::structure)
.registerGlobalChangeHook((oldValue, newValue) -> System.out.println("Old: " + oldValue + ", new: " + newValue))
.registerGlobalChangeHook((config, oldValue, newValue) -> System.out.println(config + " was updated | old: " + oldValue + ", new: " + newValue))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void onInitializeClient() {
.registerTypeHierarchy(Block.class, new BlockAdapter(), BlockArgumentType::block)
.registerTypeHierarchy(BlockInput.class, new BlockStateAdapter(), BlockStateArgument::block)
.registerTypeHierarchy((Class<StructureType<?>>) (Class) StructureType.class, new StructureAdapter(), StructureArgumentType::structure)
.registerGlobalChangeHook((oldValue, newValue) -> System.out.println("Old: " + oldValue + ", new: " + newValue))
.registerGlobalChangeHook((config, oldValue, newValue) -> System.out.println(config + " was updated | old: " + oldValue + ", new: " + newValue))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void onEnable() {
.registerType(Material.class, new MaterialAdapter(), BlockMaterialArgumentType::block)
.registerTypeHierarchy(BlockState.class, new BlockStateAdapter(), ArgumentTypes::blockState)
.registerTypeHierarchy(Structure.class, new StructureAdapter(), StructureArgumentType::structure)
.registerGlobalChangeHook((oldValue, newValue) -> System.out.println("Old: " + oldValue + ", new: " + newValue))
.registerGlobalChangeHook((config, oldValue, newValue) -> System.out.println(config + " was updated | old: " + oldValue + ", new: " + newValue))
.build();
}
}

0 comments on commit 48ee07d

Please sign in to comment.