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

Add global change hook #11

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
* @Config(onChange = "exampleOnChange")
* public static String exampleString = "defaultString";
* public static void exampleOnChange(String oldValue, String newValue) {
* System.out.println("Old: " + oldValue + ", new: " + newValue);
* LOGGER.info("exampleOnChange was updated | old: {}, new: {}", oldValue, newValue);
* }
* }
* </pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.xpple.betterconfig.api;

import org.jetbrains.annotations.ApiStatus;

/**
* An event object that is created whenever any config value is updated.
* @param config the name of the config that was changed
* @param oldValue the config's old value
* @param newValue the config's new value
*/
public record GlobalChangeEvent(String config, Object oldValue, Object newValue) {
@ApiStatus.Internal
public GlobalChangeEvent {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

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

Expand All @@ -21,6 +22,8 @@ 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 Consumer<GlobalChangeEvent> globalChangeHook = event -> {};

public ModConfigBuilder(String modId, Class<?> configsClass) {
this.modId = modId;
this.configsClass = configsClass;
Expand Down Expand Up @@ -98,12 +101,23 @@ public <T> ModConfigBuilder<S, C> registerTypeHierarchy(Class<T> type, TypeAdapt
return this;
}

/**
* Register a callback that will be called whenever any config value is updated. See
* {@link GlobalChangeEvent} for the event context that is available in the callback.
* @param hook the callback
* @return the current builder instance
*/
public ModConfigBuilder<S, C> registerGlobalChangeHook(Consumer<GlobalChangeEvent> hook) {
this.globalChangeHook = hook;
return this;
}

/**
* Finalise the registration process.
* @throws IllegalArgumentException when a configuration already exists for this mod
*/
public void build() {
ModConfigImpl<?, ?> modConfig = new ModConfigImpl<>(this.modId, this.configsClass, this.builder.create(), this.arguments);
ModConfigImpl<?, ?> modConfig = new ModConfigImpl<>(this.modId, this.configsClass, this.builder.create(), this.arguments, this.globalChangeHook);
if (BetterConfigImpl.getModConfigs().putIfAbsent(this.modId, modConfig) == null) {
BetterConfigInternals.init(modConfig);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.xpple.betterconfig.BetterConfigCommon;
import dev.xpple.betterconfig.api.Config;
import dev.xpple.betterconfig.api.GlobalChangeEvent;
import dev.xpple.betterconfig.util.CheckedRunnable;

import java.io.BufferedReader;
Expand Down Expand Up @@ -403,5 +404,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(new GlobalChangeEvent(field.getName(), oldValue, newValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.xpple.betterconfig.BetterConfigCommon;
import dev.xpple.betterconfig.api.GlobalChangeEvent;
import dev.xpple.betterconfig.api.ModConfig;
import dev.xpple.betterconfig.api.Config;
import dev.xpple.betterconfig.util.CheckedBiConsumer;
Expand All @@ -27,6 +28,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

Expand Down Expand Up @@ -64,12 +66,15 @@ public class ModConfigImpl<S, C> implements ModConfig {
private final Gson inlineGson;
private final Map<Class<?>, Function<C, ? extends ArgumentType<?>>> arguments;

public ModConfigImpl(String modId, Class<?> configsClass, Gson gson, Map<Class<?>, Function<C, ? extends ArgumentType<?>>> arguments) {
private final Consumer<GlobalChangeEvent> globalChangeHook;

public ModConfigImpl(String modId, Class<?> configsClass, Gson gson, Map<Class<?>, Function<C, ? extends ArgumentType<?>>> arguments, Consumer<GlobalChangeEvent> globalChangeHook) {
this.modId = modId;
this.configsClass = configsClass;
this.gson = gson.newBuilder().setPrettyPrinting().create();
this.inlineGson = gson;
this.arguments = arguments;
this.globalChangeHook = globalChangeHook;
}

@Override
Expand Down Expand Up @@ -126,6 +131,10 @@ Map<String, BiConsumer<Object, Object>> getOnChangeCallbacks() {
return this.onChangeCallbacks;
}

Consumer<GlobalChangeEvent> getGlobalChangeHook() {
return this.globalChangeHook;
}

@SuppressWarnings("unchecked")
public Function<C, ? extends ArgumentType<?>> getArgument(Class<?> type) {
return this.arguments.getOrDefault(type, (Function<C, ? extends ArgumentType<?>>) defaultArguments.get(type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ private static void privateSetter(String string) {
@Config(onChange = "onChange")
public static List<String> exampleOnChange = new ArrayList<>(List.of("xpple, earthcomputer"));
private static void onChange(List<String> oldValue, List<String> newValue) {
System.out.println("Old: " + oldValue + ", new: " + newValue);
BetterConfigCommon.LOGGER.info("exampleOnChange was updated | old: {}, new: {}", oldValue, newValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +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(event -> BetterConfigCommon.LOGGER.info("{} was updated | old: {}, new: {}", event.config(), event.oldValue(), event.newValue()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +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(event -> BetterConfigCommon.LOGGER.info("{} was updated | old: {}, new: {}", event.config(), event.oldValue(), event.newValue()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ private static void privateSetter(String string) {
@Config(onChange = "onChange")
public static List<String> exampleOnChange = new ArrayList<>(List.of("xpple, earthcomputer"));
private static void onChange(List<String> oldValue, List<String> newValue) {
System.out.println("Old: " + oldValue + ", new: " + newValue);
BetterConfigCommon.LOGGER.info("exampleOnChange was updated | old: {}, new: {}", oldValue, newValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +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(event -> BetterConfigCommon.LOGGER.info("{} was updated | old: {}, new: {}", event.config(), event.oldValue(), event.newValue()))
.build();
}
}
Loading