From 1cfd9e690580677409d2f2069679a10534a50e89 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 2 May 2025 21:24:27 +0800 Subject: [PATCH 1/2] Load all config nodes before calling onSetValue --- .../core/config/handle/BaseConfigurationHandle.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/mvplugins/multiverse/core/config/handle/BaseConfigurationHandle.java b/src/main/java/org/mvplugins/multiverse/core/config/handle/BaseConfigurationHandle.java index 5f68cea9b..6e9d15495 100644 --- a/src/main/java/org/mvplugins/multiverse/core/config/handle/BaseConfigurationHandle.java +++ b/src/main/java/org/mvplugins/multiverse/core/config/handle/BaseConfigurationHandle.java @@ -30,7 +30,7 @@ public abstract class BaseConfigurationHandle { protected final @Nullable Logger logger; protected final @NotNull NodeGroup nodes; protected final @Nullable ConfigMigrator migrator; - protected final @NotNull Map nodeValueMap; + protected final @NotNull Map nodeValueMap; protected C config; @@ -80,9 +80,12 @@ protected void setUpNodes() { if (node instanceof ValueNode valueNode) { var value = deserializeNodeFromConfig(valueNode); nodeValueMap.put(valueNode, value); - valueNode.onSetValue(value, value); } }); + + nodeValueMap.forEach((valueNode, value) -> { + valueNode.onSetValue(value, value); + }); } protected T deserializeNodeFromConfig(ValueNode node) { From a4e7282a0b3d492e796e351b41f535366fea991d Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 2 May 2025 21:24:46 +0800 Subject: [PATCH 2/2] Implement `bukkit-yml-path` config option --- .../multiverse/core/MultiverseCore.java | 2 - .../multiverse/core/config/CoreConfig.java | 8 +++ .../core/config/CoreConfigNodes.java | 8 +++ .../multiverse/core/utils/FileUtils.java | 50 +++++++++++-------- src/test/resources/configs/fresh_config.yml | 1 + 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/mvplugins/multiverse/core/MultiverseCore.java b/src/main/java/org/mvplugins/multiverse/core/MultiverseCore.java index c21252fdf..7f40ddee4 100644 --- a/src/main/java/org/mvplugins/multiverse/core/MultiverseCore.java +++ b/src/main/java/org/mvplugins/multiverse/core/MultiverseCore.java @@ -13,10 +13,8 @@ import io.vavr.control.Try; import jakarta.inject.Inject; import jakarta.inject.Provider; -import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.serialization.ConfigurationSerialization; -import org.bukkit.plugin.ServicePriority; import org.jetbrains.annotations.NotNull; import org.jvnet.hk2.annotations.Service; diff --git a/src/main/java/org/mvplugins/multiverse/core/config/CoreConfig.java b/src/main/java/org/mvplugins/multiverse/core/config/CoreConfig.java index 2cbc26b6a..7eb34c518 100644 --- a/src/main/java/org/mvplugins/multiverse/core/config/CoreConfig.java +++ b/src/main/java/org/mvplugins/multiverse/core/config/CoreConfig.java @@ -495,6 +495,14 @@ public Try setShowLegacyAliases(boolean showLegacyAliases) { return configHandle.set(configNodes.showLegacyAliases, showLegacyAliases); } + public Try setBukkitYmlPath(String bukkitYmlPath) { + return configHandle.set(configNodes.bukkitYmlPath, bukkitYmlPath); + } + + public String getBukkitYmlPath() { + return configHandle.get(configNodes.bukkitYmlPath); + } + /** * {@inheritDoc} */ diff --git a/src/main/java/org/mvplugins/multiverse/core/config/CoreConfigNodes.java b/src/main/java/org/mvplugins/multiverse/core/config/CoreConfigNodes.java index 12f22c1cc..3e2619912 100644 --- a/src/main/java/org/mvplugins/multiverse/core/config/CoreConfigNodes.java +++ b/src/main/java/org/mvplugins/multiverse/core/config/CoreConfigNodes.java @@ -364,7 +364,15 @@ private N node(N node) { .comment("") .build()); + final ConfigNode bukkitYmlPath = node(ConfigNode.builder("misc.bukkit-yml-path", String.class) + .comment("Change this if you use a custom path for the bukkit.yml file with `--bukkit-settings` startup flag.") + .comment("Note: this config option needs a server restart to take effect.") + .defaultValue("bukkit.yml") + .name("bukkit-yml-path") + .build()); + final ConfigNode globalDebug = node(ConfigNode.builder("misc.global-debug", Integer.class) + .comment("") .comment("This is our debug flag to help identify issues with Multiverse.") .comment("If you are having issues with Multiverse, please set this to 3 and then post your log to pastebin.com") .comment("Otherwise, there's no need to touch this. If not instructed by a wiki page or developer.") diff --git a/src/main/java/org/mvplugins/multiverse/core/utils/FileUtils.java b/src/main/java/org/mvplugins/multiverse/core/utils/FileUtils.java index 078aff376..40bd52b0d 100644 --- a/src/main/java/org/mvplugins/multiverse/core/utils/FileUtils.java +++ b/src/main/java/org/mvplugins/multiverse/core/utils/FileUtils.java @@ -9,6 +9,7 @@ import java.io.File; import java.io.IOException; +import java.lang.management.ManagementFactory; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; import java.util.Collections; @@ -19,9 +20,11 @@ import com.dumptruckman.minecraft.util.Logging; import io.vavr.control.Try; import jakarta.inject.Inject; +import org.bukkit.Bukkit; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jvnet.hk2.annotations.Service; +import org.mvplugins.multiverse.core.config.CoreConfig; import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES; @@ -31,32 +34,16 @@ @Service public final class FileUtils { + private final CoreConfig config; private final File serverFolder; - private final File bukkitYml; - private final File serverProperties; + private File bukkitYml; + private File serverProperties; @Inject - FileUtils() { + FileUtils(CoreConfig config) { + this.config = config; this.serverFolder = new File(System.getProperty("user.dir")); Logging.finer("Server folder: " + this.serverFolder); - this.bukkitYml = findFileFromServerDirectory("bukkit.yml"); - this.serverProperties = findFileFromServerDirectory("server.properties"); - } - - private @Nullable File findFileFromServerDirectory(String fileName) { - File[] files; - try { - files = this.serverFolder.listFiles((file, s) -> s.equalsIgnoreCase(fileName)); - } catch (Exception e) { - Logging.severe("Could not read from server directory. Unable to locate file: %s", fileName); - Logging.severe(e.getMessage()); - return null; - } - if (files != null && files.length == 1) { - return files[0]; - } - Logging.warning("Unable to locate file from server directory: %s", fileName); - return null; } /** @@ -74,6 +61,10 @@ public File getServerFolder() { * @return The bukkit.yml file if exist, else null. */ public @Nullable File getBukkitConfig() { + if (this.bukkitYml == null) { + this.bukkitYml = findFileFromServerDirectory(config.getBukkitYmlPath()); + Logging.finer("Bukkit.yml: " + this.bukkitYml); + } return this.bukkitYml; } @@ -83,9 +74,26 @@ public File getServerFolder() { * @return The server.properties file if exist, else null. */ public @Nullable File getServerProperties() { + if (this.serverProperties == null) { + this.serverProperties = findFileFromServerDirectory("server.properties"); + Logging.finer("server.properties: %s", this.serverProperties); + } return this.serverProperties; } + private @Nullable File findFileFromServerDirectory(String fileName) { + if (this.serverFolder == null) { + Logging.warning("Unable to locate server directory."); + return null; + } + File file = new File(this.serverFolder, fileName); + if (!file.exists()) { + Logging.warning("Unable to locate file from server directory: %s", fileName); + return null; + } + return file; + } + /** * Deletes the given folder completely. * diff --git a/src/test/resources/configs/fresh_config.yml b/src/test/resources/configs/fresh_config.yml index cb3d4ab4a..88f373fea 100644 --- a/src/test/resources/configs/fresh_config.yml +++ b/src/test/resources/configs/fresh_config.yml @@ -38,6 +38,7 @@ command: show-legacy-aliases: false misc: + bukkit-yml-path: bukkit.yml global-debug: 0 debug-permissions: false silent-start: false