From 0163ed0319be62c3c14691661168643b32e56dc6 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Wed, 17 Jan 2024 14:38:33 +0100 Subject: [PATCH] Simplify config event posting logic (remove ConfigConfig) --- .../main/java/net/neoforged/fml/Bindings.java | 5 --- .../net/neoforged/fml/IBindingsProvider.java | 2 - .../java/net/neoforged/fml/ModContainer.java | 2 +- .../fml/config/ConfigFileTypeHandler.java | 3 +- .../neoforged/fml/config/ConfigTracker.java | 9 ++-- .../neoforged/fml/config/IConfigEvent.java | 44 ------------------- .../net/neoforged/fml/config/ModConfig.java | 8 +++- .../fml/event/config/ModConfigEvent.java | 4 +- 8 files changed, 15 insertions(+), 62 deletions(-) delete mode 100644 loader/src/main/java/net/neoforged/fml/config/IConfigEvent.java diff --git a/loader/src/main/java/net/neoforged/fml/Bindings.java b/loader/src/main/java/net/neoforged/fml/Bindings.java index f64523e91..182df9e38 100644 --- a/loader/src/main/java/net/neoforged/fml/Bindings.java +++ b/loader/src/main/java/net/neoforged/fml/Bindings.java @@ -7,7 +7,6 @@ import cpw.mods.modlauncher.util.ServiceLoaderUtils; import net.neoforged.bus.api.IEventBus; -import net.neoforged.fml.config.IConfigEvent; import net.neoforged.fml.loading.FMLLoader; import java.util.ServiceLoader; @@ -33,8 +32,4 @@ public static Supplier getForgeBus() { public static Supplier getMessageParser() { return INSTANCE.provider.getMessageParser(); } - - public static Supplier getConfigConfiguration() { - return INSTANCE.provider.getConfigConfiguration(); - } } diff --git a/loader/src/main/java/net/neoforged/fml/IBindingsProvider.java b/loader/src/main/java/net/neoforged/fml/IBindingsProvider.java index 31cfa677c..13b9d6fc1 100644 --- a/loader/src/main/java/net/neoforged/fml/IBindingsProvider.java +++ b/loader/src/main/java/net/neoforged/fml/IBindingsProvider.java @@ -6,12 +6,10 @@ package net.neoforged.fml; import net.neoforged.bus.api.IEventBus; -import net.neoforged.fml.config.IConfigEvent; import java.util.function.Supplier; public interface IBindingsProvider { Supplier getForgeBusSupplier(); Supplier getMessageParser(); - Supplier getConfigConfiguration(); } diff --git a/loader/src/main/java/net/neoforged/fml/ModContainer.java b/loader/src/main/java/net/neoforged/fml/ModContainer.java index fddbde09f..ba258c972 100644 --- a/loader/src/main/java/net/neoforged/fml/ModContainer.java +++ b/loader/src/main/java/net/neoforged/fml/ModContainer.java @@ -183,7 +183,7 @@ public void addConfig(final ModConfig modConfig) { * Accept an arbitrary event for processing by the mod. Posted to {@link #getEventBus()}. * @param e Event to accept */ - protected final void acceptEvent(T e) { + public final void acceptEvent(T e) { IEventBus bus = getEventBus(); if (bus == null) return; diff --git a/loader/src/main/java/net/neoforged/fml/config/ConfigFileTypeHandler.java b/loader/src/main/java/net/neoforged/fml/config/ConfigFileTypeHandler.java index c188698af..64c69e19c 100644 --- a/loader/src/main/java/net/neoforged/fml/config/ConfigFileTypeHandler.java +++ b/loader/src/main/java/net/neoforged/fml/config/ConfigFileTypeHandler.java @@ -11,6 +11,7 @@ import com.electronwill.nightconfig.core.io.ParsingException; import com.electronwill.nightconfig.core.io.WritingMode; import com.mojang.logging.LogUtils; +import net.neoforged.fml.event.config.ModConfigEvent; import net.neoforged.fml.loading.FMLConfig; import net.neoforged.fml.loading.FMLPaths; import org.apache.commons.io.FilenameUtils; @@ -167,7 +168,7 @@ public void run() { } LOGGER.debug(CONFIG, "Config file {} changed, sending notifies", this.modConfig.getFileName()); this.modConfig.getSpec().afterReload(); - IConfigEvent.reloading(this.modConfig).post(); + this.modConfig.postConfigEvent(ModConfigEvent.Reloading::new); } } } diff --git a/loader/src/main/java/net/neoforged/fml/config/ConfigTracker.java b/loader/src/main/java/net/neoforged/fml/config/ConfigTracker.java index 88ae85256..2a66e02f9 100644 --- a/loader/src/main/java/net/neoforged/fml/config/ConfigTracker.java +++ b/loader/src/main/java/net/neoforged/fml/config/ConfigTracker.java @@ -9,6 +9,7 @@ import com.electronwill.nightconfig.core.file.CommentedFileConfig; import com.mojang.logging.LogUtils; import java.nio.file.Files; +import net.neoforged.fml.event.config.ModConfigEvent; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.Marker; @@ -77,7 +78,7 @@ private void openConfig(final ModConfig config, final Path configBasePath, @Null final Path basePath = resolveBasePath(config, configBasePath, configOverrideBasePath); final CommentedFileConfig configData = ConfigFileTypeHandler.TOML.reader(basePath).apply(config); config.setConfigData(configData); - IConfigEvent.loading(config).post(); + config.postConfigEvent(ModConfigEvent.Loading::new); config.save(); } @@ -86,9 +87,7 @@ private void closeConfig(final ModConfig config) { LOGGER.trace(CONFIG, "Closing config file type {} at {} for {}", config.getType(), config.getFileName(), config.getModId()); // stop the filewatcher before we save the file and close it, so reload doesn't fire ConfigFileTypeHandler.TOML.unload(config); - var unloading = IConfigEvent.unloading(config); - if (unloading != null) - unloading.post(); + config.postConfigEvent(ModConfigEvent.Unloading::new); config.save(); config.setConfigData(null); } @@ -99,7 +98,7 @@ public void loadDefaultServerConfigs() { final CommentedConfig commentedConfig = CommentedConfig.inMemory(); modConfig.getSpec().correct(commentedConfig); modConfig.setConfigData(commentedConfig); - IConfigEvent.loading(modConfig).post(); + modConfig.postConfigEvent(ModConfigEvent.Loading::new); }); } diff --git a/loader/src/main/java/net/neoforged/fml/config/IConfigEvent.java b/loader/src/main/java/net/neoforged/fml/config/IConfigEvent.java deleted file mode 100644 index b264914a7..000000000 --- a/loader/src/main/java/net/neoforged/fml/config/IConfigEvent.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) Forge Development LLC and contributors - * SPDX-License-Identifier: LGPL-2.1-only - */ - -package net.neoforged.fml.config; - -import net.neoforged.bus.api.Event; -import net.neoforged.bus.api.IEventBus; -import net.neoforged.fml.Bindings; - -import java.util.function.Function; - -import org.jetbrains.annotations.Nullable; - -public interface IConfigEvent { - record ConfigConfig(Function loading, Function reloading, @Nullable Function unloading) {} - - ConfigConfig CONFIGCONFIG = Bindings.getConfigConfiguration().get(); - - static IConfigEvent reloading(ModConfig modConfig) { - return CONFIGCONFIG.reloading().apply(modConfig); - } - static IConfigEvent loading(ModConfig modConfig) { - return CONFIGCONFIG.loading().apply(modConfig); - } - @Nullable static IConfigEvent unloading(ModConfig modConfig) { - return CONFIGCONFIG.unloading() == null ? null : CONFIGCONFIG.unloading().apply(modConfig); - } - ModConfig getConfig(); - - default void post() { - IEventBus bus = getConfig().container.getEventBus(); - - if (bus != null) { - bus.post(self()); - } - } - - @SuppressWarnings("unchecked") - default T self() { - return (T) this; - } -} diff --git a/loader/src/main/java/net/neoforged/fml/config/ModConfig.java b/loader/src/main/java/net/neoforged/fml/config/ModConfig.java index 7aea0dc30..03f6c76e6 100644 --- a/loader/src/main/java/net/neoforged/fml/config/ModConfig.java +++ b/loader/src/main/java/net/neoforged/fml/config/ModConfig.java @@ -9,11 +9,13 @@ import com.electronwill.nightconfig.core.file.FileConfig; import com.electronwill.nightconfig.toml.TomlFormat; import net.neoforged.fml.ModContainer; +import net.neoforged.fml.event.config.ModConfigEvent; import net.neoforged.fml.loading.StringUtils; import java.io.ByteArrayInputStream; import java.nio.file.Path; import java.util.Locale; +import java.util.function.Function; public class ModConfig { @@ -76,7 +78,11 @@ public Path getFullPath() { public void acceptSyncedConfig(byte[] bytes) { setConfigData(TomlFormat.instance().createParser().parse(new ByteArrayInputStream(bytes))); - IConfigEvent.reloading(this).post(); + postConfigEvent(ModConfigEvent.Reloading::new); + } + + void postConfigEvent(Function constructor) { + container.acceptEvent(constructor.apply(this)); } public enum Type { diff --git a/loader/src/main/java/net/neoforged/fml/event/config/ModConfigEvent.java b/loader/src/main/java/net/neoforged/fml/event/config/ModConfigEvent.java index 04fe19864..463a2f7af 100644 --- a/loader/src/main/java/net/neoforged/fml/event/config/ModConfigEvent.java +++ b/loader/src/main/java/net/neoforged/fml/event/config/ModConfigEvent.java @@ -6,18 +6,16 @@ package net.neoforged.fml.event.config; import net.neoforged.bus.api.Event; -import net.neoforged.fml.config.IConfigEvent; import net.neoforged.fml.config.ModConfig; import net.neoforged.fml.event.IModBusEvent; -public class ModConfigEvent extends Event implements IModBusEvent, IConfigEvent { +public class ModConfigEvent extends Event implements IModBusEvent { private final ModConfig config; ModConfigEvent(final ModConfig config) { this.config = config; } - @Override public ModConfig getConfig() { return config; }