From e3145398474b02c1ffbf4b4faefbf9ecbbc2f3f4 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Tue, 7 Mar 2023 23:32:09 +0800 Subject: [PATCH 01/37] dep: Bump CommentedConfiguration to 1.0.1 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 9a0a0d61..5351c7fb 100644 --- a/build.gradle +++ b/build.gradle @@ -68,7 +68,7 @@ dependencies { api('me.main__.util:SerializationConfig:1.7') { exclude group: 'org.bukkit', module: 'bukkit' } - api('io.github.townyadvanced.commentedconfiguration:CommentedConfiguration:1.0.0') { + api('io.github.townyadvanced.commentedconfiguration:CommentedConfiguration:1.0.1') { exclude group: 'org.spigotmc', module: 'spigot-api' } From c76652f0a20981ab1ebf8329123178203d9375e3 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Tue, 7 Mar 2023 23:33:35 +0800 Subject: [PATCH 02/37] feat: Test out new commentedconfiguration --- .../MultiverseCore/MultiverseCore.java | 3 + .../MultiverseCore/api/NewNMVConfig.java | 182 +++++++++++++++ .../configuration/DefaultMVConfig.java | 21 ++ .../configuration/MVConfigNodes.java | 170 ++++++++++++++ .../utils/settings/MVSettings.java | 209 ++++++++++++++++++ .../settings/migration/MigrateVersion.java | 38 ++++ .../settings/migration/MigratorAction.java | 7 + .../migration/MoveMigratorAction.java | 21 ++ .../utils/settings/node/MVCommentedNode.java | 55 +++++ .../utils/settings/node/MVValueNode.java | 53 +++++ .../utils/settings/node/NodesCollection.java | 84 +++++++ 11 files changed, 843 insertions(+) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/api/NewNMVConfig.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigrateVersion.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigratorAction.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodesCollection.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index 880e8d20..73aee163 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -41,6 +41,7 @@ import com.onarandombox.MultiverseCore.commands.RemoveCommand; import com.onarandombox.MultiverseCore.commands.TeleportCommand; import com.onarandombox.MultiverseCore.commands.UnloadCommand; import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; +import com.onarandombox.MultiverseCore.configuration.DefaultMVConfig; import com.onarandombox.MultiverseCore.destination.DestinationsProvider; import com.onarandombox.MultiverseCore.destination.core.AnchorDestination; import com.onarandombox.MultiverseCore.destination.core.BedDestination; @@ -358,6 +359,8 @@ public class MultiverseCore extends JavaPlugin implements MVCore { */ @Override public void loadConfigs() { + new DefaultMVConfig(this); + // Now grab the Configuration Files. this.multiverseConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "config.yml")); InputStream resourceURL = this.getClass().getResourceAsStream("/defaults/config.yml"); diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/NewNMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/NewNMVConfig.java new file mode 100644 index 00000000..0304c51c --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/api/NewNMVConfig.java @@ -0,0 +1,182 @@ +package com.onarandombox.MultiverseCore.api; + +public interface NewNMVConfig { + + /** + * Sets firstSpawnWorld. + * @param firstSpawnWorld The new value. + */ + void setFirstSpawnWorld(String firstSpawnWorld); + + /** + * Gets firstSpawnWorld. + * @return firstSpawnWorld. + */ + String getFirstSpawnWorld(); + + /** + * Sets version. + * @param version The new value. + */ + void setVersion(int version); + + /** + * Gets version. + * @return version. + */ + double getVersion(); + + /** + * Sets globalDebug. + * @param globalDebug The new value. + */ + void setGlobalDebug(int globalDebug); + + /** + * Gets globalDebug. + * @return globalDebug. + */ + int getGlobalDebug(); + + /** + * Sets displayPermErrors. + * @param displayPermErrors The new value. + */ + void setDisplayPermErrors(boolean displayPermErrors); + + /** + * Gets displayPermErrors. + * @return displayPermErrors. + */ + boolean getDisplayPermErrors(); + + /** + * Sets firstSpawnOverride. + * @param firstSpawnOverride The new value. + */ + void setFirstSpawnOverride(boolean firstSpawnOverride); + + /** + * Gets firstSpawnOverride. + * @return firstSpawnOverride. + */ + boolean getFirstSpawnOverride(); + + /** + * Sets teleportIntercept. + * @param teleportIntercept The new value. + */ + void setTeleportIntercept(boolean teleportIntercept); + + /** + * Gets teleportIntercept. + * @return teleportIntercept. + */ + boolean getTeleportIntercept(); + + /** + * Sets prefixChat. + * @param prefixChat The new value. + */ + void setPrefixChat(boolean prefixChat); + + /** + * Gets prefixChat. + * @return prefixChat. + */ + boolean getPrefixChat(); + + /** + * Sets prefixChatFormat. + * @param prefixChatFormat The new value. + */ + void setPrefixChatFormat(String prefixChatFormat); + + /** + * Gets prefixChatFormat. + * @return prefixChatFormat. + */ + String getPrefixChatFormat(); + + /** + * Sets enforceAccess. + * @param enforceAccess The new value. + */ + void setEnforceAccess(boolean enforceAccess); + + /** + * Gets enforceAccess. + * @return enforceAccess. + */ + boolean getEnforceAccess(); + + /** + * Sets whether to suppress startup messages. + * + * @param silentStart true to suppress messages. + */ + void setSilentStart(boolean silentStart); + + /** + * Whether we are suppressing startup messages. + * + * @return true if we are suppressing startup messages. + */ + boolean getSilentStart(); + + /** + * Sets whether or not to let Bukkit determine portal search radius on its own or if Multiverse should give input. + * + * @param useDefaultPortalSearch True to let Bukkit determine portal search radius on its own. + */ + void setUseDefaultPortalSearch(boolean useDefaultPortalSearch); + + /** + * Gets whether or not Bukkit will be determining portal search radius on its own or if Multiverse should help. + * + * @return True means Bukkit will use its own default values. + */ + boolean isUsingDefaultPortalSearch(); + + /** + * Sets the radius at which vanilla style portals will be searched for to connect to worlds together. + * + * @param searchRadius The portal search radius. + */ + void setPortalSearchRadius(int searchRadius); + + /** + * Gets the radius at which vanilla style portals will be searched for to connect to worlds together. + * + * @return The portal search radius. + */ + int getPortalSearchRadius(); + + /** + * Gets whether or not the automatic purge of entities is enabled. + * + * @return True if automatic purge is enabled. + */ + boolean isAutoPurgeEnabled(); + + /** + * Sets whether or not the automatic purge of entities is enabled. + * + * @param autopurge True if automatic purge should be enabled. + */ + void setAutoPurgeEnabled(boolean autopurge); + + /** + * Gets whether or not the donation/patreon messages are shown. + * + * @return True if donation/patreon messages should be shown. + */ + boolean isShowingDonateMessage(); + + /** + * Sets whether or not the donation/patreon messages are shown. + * + * @param idonotwanttodonate True if donation/patreon messages should be shown. + */ + void setShowDonateMessage(boolean idonotwanttodonate); +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java new file mode 100644 index 00000000..2ceb2f5f --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java @@ -0,0 +1,21 @@ +package com.onarandombox.MultiverseCore.configuration; + +import java.nio.file.Path; + +import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.utils.settings.MVSettings; + +public class DefaultMVConfig { + + private final MVSettings settings; + + public DefaultMVConfig(MultiverseCore core) { + Path configPath = Path.of(core.getDataFolder().getPath(), "config2.yml"); + settings = MVSettings.builder(configPath) + .logger(core.getLogger()) + .defaultNodes(MVConfigNodes.getNodes()) + .build(); + settings.load(); + settings.save(); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java new file mode 100644 index 00000000..96a926b4 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java @@ -0,0 +1,170 @@ +package com.onarandombox.MultiverseCore.configuration; + +import java.util.ArrayList; +import java.util.List; + +import com.onarandombox.MultiverseCore.utils.settings.node.MVCommentedNode; +import com.onarandombox.MultiverseCore.utils.settings.node.MVValueNode; +import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; + +public class MVConfigNodes { + private static final List nodes = new ArrayList<>(); + + public static List getNodes() { + return nodes; + } + + private static N node(N node) { + nodes.add(node); + return node; + } + + private static final MVCommentedNode HEADER = node(MVCommentedNode.builder("world") // TODO hacky way to get the header to the top of the file + .comment("####################################################################################################") + .comment("# #") + .comment("# █▀▄▀█ █░█ █░░ ▀█▀ █ █░█ █▀▀ █▀█ █▀ █▀▀   █▀▀ █▀█ █▀█ █▀▀ #") + .comment("# █░▀░█ █▄█ █▄▄ ░█░ █ ▀▄▀ ██▄ █▀▄ ▄█ ██▄   █▄▄ █▄█ █▀▄ ██▄ #") + .comment("# #") + .comment("# #") + .comment("# WIKI: https://github.com/Multiverse/Multiverse-Core/wiki #") + .comment("# DISCORD: https://discord.gg/NZtfKky #") + .comment("# BUG REPORTS: https://github.com/Multiverse/Multiverse-Core/issues #") + .comment("# #") + .comment("# #") + .comment("# Each option in this file is documented and explained here: #") + .comment("# ==> https://github.com/Multiverse/Multiverse-Core/wiki/config.yml #") + .comment("# #") + .comment("# #") + .comment("# New options are added to this file automatically. If you manually made changes #") + .comment("# to this file while your server is running, please run `/mv reload` command. #") + .comment("# #") + .comment("####################################################################################################") + .comment("") + .comment("") + .build()); + +// private static final MVCommentedNode WORLD_HEADER = node(MVCommentedNode.builder("world") +// .comment("") +// .comment("") +// .build()); + + public static final MVValueNode ENFORCE_ACCESS = node(MVValueNode.builder("world.enforce-access", Boolean.class) + .comment("This setting will prevent players from entering worlds they don't have access to.") + .comment("If this is set to false, players will be able to enter any world they want.") + .comment("If this is set to true, players will only be able to enter worlds they have") + .comment("the `mv.access.` permission.") + .defaultValue(false) + .build()); + + public static final MVValueNode ENFORCE_GAMEMODE = node(MVValueNode.builder("world.enforce-gamemode", Boolean.class) + .comment("") + .comment("Sets whether Multiverse will should enforce gamemode on world change.") + .comment("If enabled, players will be forced into the gamemode of the world they are entering, unless they have") + .comment("the `mv.bypass.gamemode.` permission.") + .defaultValue(true) + .build()); + + public static final MVValueNode AUTO_PURGE_ENTITIES = node(MVValueNode.builder("world.auto-purge-entities", Boolean.class) + .comment("") + .comment("Sets whether Multiverse will purge mobs and entities with be automatically.") + .defaultValue(false) + .build()); + + public static final MVValueNode TELEPORT_INTERCEPT = node(MVValueNode.builder("world.teleport-intercept", Boolean.class) + .comment("") + .comment("If this is set to true, Multiverse will enforce access permissions for all teleportation,") + .comment("including teleportation from other plugins.") + .defaultValue(true) + .build()); + + private static final MVCommentedNode SPAWN_HEADER = node(MVCommentedNode.builder("spawn") + .comment("") + .comment("") + .build()); + + public static final MVValueNode FIRST_SPAWN_OVERRIDE = node(MVValueNode.builder("spawn.first-spawn-override", Boolean.class) + .comment("Sets whether Multiverse will override the first spawn location of a world.") + .comment("If enabled, Multiverse will set the first spawn location of a world to the spawn location of the world.") + .comment("If disabled, it will default to server.properties settings.") + .defaultValue(true) + .build()); + + public static final MVValueNode FIRST_SPAWN_LOCATION = node(MVValueNode.builder("spawn.first-spawn-location", String.class) + .comment("") + .comment("Sets the world that Multiverse will use as the location for players that first join the server.") + .comment("This only applies if first-spawn-override is set to true.") + .defaultValue("") + .build()); + + private static final MVCommentedNode PORTAL_HEADER = node(MVCommentedNode.builder("portal") + .comment("") + .comment("") + .build()); + + public static final MVValueNode USE_CUSTOM_PORTAL_SEARCH = node(MVValueNode.builder("portal.use-custom-portal-search", Boolean.class) + .comment("This config option defines whether or not Multiverse should interfere with's Bukkit's default portal search radius.") + .comment("Setting it to false would mean you want to simply let Bukkit decides the search radius itself.") + .defaultValue(false) + .build()); + + public static final MVValueNode CUSTOM_PORTAL_SEARCH_RADIUS = node(MVValueNode.builder("portal.custom-portal-search-radius", Integer.class) + .comment("") + .comment("This config option defines the search radius Multiverse should use when searching for a portal.") + .comment("This only applies if use-custom-portal-search is set to true.") + .defaultValue(128) + .build()); + + private static final MVCommentedNode MESSAGING_HEADER = node(MVCommentedNode.builder("messaging") + .comment("") + .comment("") + .build()); + + public static final MVValueNode ENABLE_CHAT_PREFIX = node(MVValueNode.builder("messaging.enable-chat-prefix", Boolean.class) + .comment("This config option defines whether or not Multiverse should prefix the chat with the world name.") + .comment("This only applies if use-custom-portal-search is set to true.") + .defaultValue(false) + .build()); + + public static final MVValueNode CHAT_PREFIX_FORMAT = node(MVValueNode.builder("messaging.chat-prefix-format", String.class) + .comment("") + .comment("This config option defines the format Multiverse should use when prefixing the chat with the world name.") + .comment("This only applies if enable-chat-prefix is set to true.") + .defaultValue("[%world%]%chat%") + .build()); + + private static final MVCommentedNode MISC_HEADER = node(MVCommentedNode.builder("misc") + .comment("") + .comment("") + .build()); + + public static final MVValueNode GLOBAL_DEBUG = node(MVValueNode.builder("misc.global-debug", Integer.class) + .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.") + .comment(" 0 = Off, No debug messages") + .comment(" 1 = fine") + .comment(" 2 = finer") + .comment(" 3 = finest") + .defaultValue(0) + .build()); + + public static final MVValueNode SLIENT_START = node(MVValueNode.builder("misc.silent-start", Boolean.class) + .comment("") + .comment("If true, the startup console messages will no longer show.") + .defaultValue(false) + .build()); + + public static final MVValueNode I_DONT_WANT_TO_DONATE = node(MVValueNode.builder("misc.i-dont-want-to-donate", Boolean.class) + .comment("") + .comment("If you don't want to donate, you can set this to true and Multiverse will stop nagging you.") + .defaultValue(false) + .build()); + + public static final MVValueNode VERSION = node(MVValueNode.builder("version", Double.class) + .comment("") + .comment("") + .comment("This just signifies the version number so we can see what version of config you have.") + .comment("NEVER TOUCH THIS VALUE") + .defaultValue(5.0D) + .build()); +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java new file mode 100644 index 00000000..a8cf70fe --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java @@ -0,0 +1,209 @@ +package com.onarandombox.MultiverseCore.utils.settings; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.logging.Logger; + +import com.dumptruckman.minecraft.util.Logging; +import com.google.common.base.Strings; +import io.github.townyadvanced.commentedconfiguration.CommentedConfiguration; +import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; +import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; +import io.github.townyadvanced.commentedconfiguration.setting.ValueNode; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.FileConfigurationOptions; +import org.bukkit.configuration.file.YamlConfigurationOptions; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class MVSettings { + + public static Builder builder(String configPath) { + return new Builder(configPath); + } + + public static Builder builder(Path configPath) { + return new Builder(configPath); + } + + protected final Path configPath; + protected final Logger logger; + protected final List defaultNodes; + + protected CommentedConfiguration config; + + /** + * Creates a new MVSettings instance that makes use of CommentedConfiguration. + * + * @param configPath The path to the configuration file. + * @param logger The Logger to use for error messages. + * @param defaultNodes The default node values to add to the configuration. + */ + protected MVSettings(@NotNull Path configPath, @Nullable Logger logger, @Nullable List defaultNodes) { + this.configPath = configPath; + this.defaultNodes = defaultNodes; + this.logger = logger; + } + + /** + * Loads the configuration. + * + * @return True if the configuration was loaded successfully, false otherwise. + */ + public boolean load() { + if (!createConfigFile()) { + return false; + } + this.config = new CommentedConfiguration(configPath, logger); + if (!config.load()) { + return false; + } + addDefaultNodes(); + return true; + } + + /** + * Create a new config file if file does not exist + * + * @return True if file exist or created successfully, otherwise false. + */ + protected boolean createConfigFile() { + File configFile = configPath.toFile(); + if (configFile.exists()) { + return true; + } + try { + if (!configFile.createNewFile()) { + return false; + } + } catch (IOException e) { + return false; + } + return true; + } + + /** + * Adds default node values to the configuration if they are not already present. + */ + protected void addDefaultNodes() { + if (defaultNodes == null || defaultNodes.isEmpty()) { + return; + } + + CommentedConfiguration tempConfig = new CommentedConfiguration(configPath, logger); + for (CommentedNode node : defaultNodes) { + if (node.getComments().length > 0) { + tempConfig.addComment(node.getPath(), node.getComments()); + } + if (node instanceof ValueNode) { + ValueNode valueNode = (ValueNode) node; + tempConfig.set(node.getPath(), get(valueNode)); + } + } + + this.config = tempConfig; + } + + /** + * Saves the configuration. + */ + public void save() { + config.save(); + } + + /** + * Gets the value of a node, if the node has a default value, it will be returned if the node is not found. + * + * @param node The node to get the value of. + * @return The value of the node. + */ + public Object get(@NotNull ValueNode node) { + return config.get(node.getPath(), node.getDefaultValue()); + } + + /** + * Gets the value of a node, if the node has a default value, it will be returned if the node is not found. + * + * @param node The node to get the value of. + * @param type The type of the node value. + * @param The type of the node value. + * @return The value of the node. + */ + public T get(@NotNull ValueNode node, Class type) { + return config.getObject(node.getPath(), type, (T) node.getDefaultValue()); + } + + /** + * Gets the value of a node, if the node has a default value, it will be returned if the node is not found. + * + * @param node The node to get the value of. + * @param The type of the node value. + * @return The value of the node. + */ + public T get(@NotNull TypedValueNode node) { + return config.getObject(node.getPath(), node.getType(), node.getDefaultValue()); + } + + /** + * Sets the value of a node, if the validator is not null, it will be tested first. + * + * @param node The node to set the value of. + * @param value The value to set. + */ + public void set(@NotNull ValueNode node, Object value) { + config.set(node.getPath(), value); + } + + /** + * Sets the value of a node, if the validator is not null, it will be tested first. + * + * @param node The node to set the value of. + * @param value The value to set. + * @param The type of the node value. + */ + public void set(@NotNull TypedValueNode node, T value) { + config.set(node.getPath(), value); + } + + /** + * Gets the configuration object. + * + * @return The configuration object. + */ + public @NotNull CommentedConfiguration getConfig() { + return config; + } + + public static class Builder { + + private final Path configPath; + private Logger logger; + private List defaultNodes; + + public Builder(String configPath) { + this.configPath = Path.of(configPath); + } + + public Builder(Path configPath) { + this.configPath = configPath; + } + + public Builder logger(@Nullable Logger logger) { + this.logger = logger; + return this; + } + + public Builder defaultNodes(@Nullable List defaultNodes) { + this.defaultNodes = defaultNodes; + return this; + } + + public MVSettings build() { + return new MVSettings(configPath, logger, defaultNodes); + } + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigrateVersion.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigrateVersion.java new file mode 100644 index 00000000..6918a9d5 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigrateVersion.java @@ -0,0 +1,38 @@ +package com.onarandombox.MultiverseCore.utils.settings.migration; + +import java.util.ArrayList; +import java.util.List; + +import com.onarandombox.MultiverseCore.utils.settings.MVSettings; + +public class MigrateVersion { + private final double version; + private final List actions; + + protected MigrateVersion(double version, List actions) { + this.version = version; + this.actions = actions; + } + + public void migrate(MVSettings settings) { + actions.forEach(action -> action.migrate(settings)); + } + + public static class Builder { + private final double version; + private final List actions = new ArrayList<>(); + + public Builder(double version) { + this.version = version; + } + + public Builder addAction(MigratorAction action) { + actions.add(action); + return this; + } + + public MigrateVersion build() { + return new MigrateVersion(version, actions); + } + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigratorAction.java new file mode 100644 index 00000000..84b560c7 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigratorAction.java @@ -0,0 +1,7 @@ +package com.onarandombox.MultiverseCore.utils.settings.migration; + +import com.onarandombox.MultiverseCore.utils.settings.MVSettings; + +public interface MigratorAction { + void migrate(MVSettings settings); +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java new file mode 100644 index 00000000..dd15e7f8 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java @@ -0,0 +1,21 @@ +package com.onarandombox.MultiverseCore.utils.settings.migration; + +import com.onarandombox.MultiverseCore.utils.settings.MVSettings; + +public class MoveMigratorAction implements MigratorAction { + private final String fromPath; + private final String toPath; + + public MoveMigratorAction(String fromPath, String toPath) { + this.fromPath = fromPath; + this.toPath = toPath; + } + + @Override + public void migrate(MVSettings settings) { + Object value = settings.getConfig().get(fromPath); + if (value != null) { + settings.getConfig().set(toPath, value); + } + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java new file mode 100644 index 00000000..8c06d15d --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java @@ -0,0 +1,55 @@ +package com.onarandombox.MultiverseCore.utils.settings.node; + +import java.util.ArrayList; +import java.util.List; + +import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; +import org.jetbrains.annotations.NotNull; + +public class MVCommentedNode implements CommentedNode { + + public static Builder builder(String path) { + return new Builder<>(path); + } + + protected final String path; + protected final String[] comments; + + protected MVCommentedNode(String path, String[] comments) { + this.path = path; + this.comments = comments; + } + + @Override + public @NotNull String getPath() { + return path; + } + + @Override + public @NotNull String[] getComments() { + return comments; + } + + public static class Builder { + protected final String path; + protected final List comments; + + protected Builder(String path) { + this.path = path; + this.comments = new ArrayList<>(); + } + + public B comment(@NotNull String comment) { + if (!comment.isEmpty() && !comment.trim().startsWith("#")) { + // Automatically add a comment prefix if the comment doesn't start with one. + comment = "# " + comment; + } + this.comments.add(comment); + return (B) this; + } + + public MVCommentedNode build() { + return new MVCommentedNode(path, comments.toArray(new String[0])); + } + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java new file mode 100644 index 00000000..c96b6fe6 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java @@ -0,0 +1,53 @@ +package com.onarandombox.MultiverseCore.utils.settings.node; + +import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class MVValueNode extends MVCommentedNode implements TypedValueNode { + + public static Builder builder(String path, Class type) { + return new Builder<>(path, type); + } + + protected final Class type; + + private final T defaultValue; + + protected MVValueNode(String path, String[] comments, Class type, T defaultValue) { + super(path, comments); + this.type = type; + this.defaultValue = defaultValue; + } + + @Override + public @NotNull Class getType() { + return type; + } + + @Override + public @Nullable T getDefaultValue() { + return defaultValue; + } + + public static class Builder> extends MVCommentedNode.Builder { + + protected final Class type; + protected T defaultValue; + + public Builder(String path, Class type) { + super(path); + this.type = type; + } + + public B defaultValue(T defaultValue) { + this.defaultValue = defaultValue; + return (B) this; + } + + @Override + public MVValueNode build() { + return new MVValueNode<>(path, comments.toArray(new String[0]), type, defaultValue); + } + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodesCollection.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodesCollection.java new file mode 100644 index 00000000..d06493a6 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodesCollection.java @@ -0,0 +1,84 @@ +package com.onarandombox.MultiverseCore.utils.settings.node; + +import java.util.Collection; +import java.util.Iterator; + +import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; +import org.jetbrains.annotations.NotNull; + +public class NodesCollection implements Collection { + private final Collection nodes; + + public NodesCollection(Collection nodes) { + this.nodes = nodes; + } + + + @Override + public int size() { + return nodes.size(); + } + + @Override + public boolean isEmpty() { + return nodes.isEmpty(); + } + + @Override + public boolean contains(Object o) { + return nodes.contains(o); + } + + @NotNull + @Override + public Iterator iterator() { + return nodes.iterator(); + } + + @NotNull + @Override + public Object[] toArray() { + return nodes.toArray(); + } + + @NotNull + @Override + public T[] toArray(@NotNull T[] ts) { + return nodes.toArray(ts); + } + + @Override + public boolean add(CommentedNode commentedNode) { + return nodes.add(commentedNode); + } + + @Override + public boolean remove(Object o) { + return nodes.remove(o); + } + + @Override + public boolean containsAll(@NotNull Collection collection) { + return nodes.containsAll(collection); + } + + @Override + public boolean addAll(@NotNull Collection collection) { + return nodes.addAll(collection); + } + + @Override + public boolean removeAll(@NotNull Collection collection) { + return nodes.removeAll(collection); + } + + @Override + public boolean retainAll(@NotNull Collection collection) { + return nodes.retainAll(collection); + } + + @Override + public void clear() { + nodes.clear(); + } +} From 9ce648d06a3534f8d1b783ef4415f32b6d3fb9f2 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Thu, 16 Mar 2023 11:18:31 +0800 Subject: [PATCH 03/37] refactor: Version migrator code --- .../utils/settings/MVSettings.java | 7 --- .../settings/migration/ConfigMigrator.java | 46 +++++++++++++++++++ ...grateVersion.java => VersionMigrator.java} | 12 +++-- 3 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java rename src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/{MigrateVersion.java => VersionMigrator.java} (75%) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java index a8cf70fe..f7eef533 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java @@ -2,21 +2,14 @@ package com.onarandombox.MultiverseCore.utils.settings; import java.io.File; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; import java.nio.file.Path; -import java.util.Arrays; import java.util.List; import java.util.logging.Logger; -import com.dumptruckman.minecraft.util.Logging; -import com.google.common.base.Strings; import io.github.townyadvanced.commentedconfiguration.CommentedConfiguration; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; import io.github.townyadvanced.commentedconfiguration.setting.ValueNode; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.FileConfigurationOptions; -import org.bukkit.configuration.file.YamlConfigurationOptions; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java new file mode 100644 index 00000000..e41eb368 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java @@ -0,0 +1,46 @@ +package com.onarandombox.MultiverseCore.utils.settings.migration; + +import java.util.ArrayList; +import java.util.List; + +import com.onarandombox.MultiverseCore.utils.settings.MVSettings; +import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; + +public class ConfigMigrator { + + private final TypedValueNode versionNode; + private final List versionMigrators; + + public ConfigMigrator(TypedValueNode versionNode, List versionMigrators) { + this.versionNode = versionNode; + this.versionMigrators = versionMigrators; + } + + public void migrate(MVSettings settings) { + double versionNumber = settings.get(versionNode); + for (VersionMigrator versionMigrator : versionMigrators) { + if (versionNumber < versionMigrator.getVersion()) { + versionMigrator.migrate(settings); + } + } + } + + public static class Builder { + private final TypedValueNode versionNode; + private final List versionMigrators; + + public Builder(TypedValueNode versionNode) { + this.versionNode = versionNode; + this.versionMigrators = new ArrayList<>(); + } + + public Builder addVersionMigrator(VersionMigrator versionMigrator) { + versionMigrators.add(versionMigrator); + return this; + } + + public ConfigMigrator build() { + return new ConfigMigrator(versionNode, versionMigrators); + } + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigrateVersion.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/VersionMigrator.java similarity index 75% rename from src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigrateVersion.java rename to src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/VersionMigrator.java index 6918a9d5..c6be421b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigrateVersion.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/VersionMigrator.java @@ -5,11 +5,11 @@ import java.util.List; import com.onarandombox.MultiverseCore.utils.settings.MVSettings; -public class MigrateVersion { +public class VersionMigrator { private final double version; private final List actions; - protected MigrateVersion(double version, List actions) { + protected VersionMigrator(double version, List actions) { this.version = version; this.actions = actions; } @@ -18,6 +18,10 @@ public class MigrateVersion { actions.forEach(action -> action.migrate(settings)); } + public double getVersion() { + return version; + } + public static class Builder { private final double version; private final List actions = new ArrayList<>(); @@ -31,8 +35,8 @@ public class MigrateVersion { return this; } - public MigrateVersion build() { - return new MigrateVersion(version, actions); + public VersionMigrator build() { + return new VersionMigrator(version, actions); } } } From db0e9f31425183716963b4b636c4d5d959f3a26b Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Sat, 18 Mar 2023 23:09:10 +0800 Subject: [PATCH 04/37] feat: Implement migration from old mv config.yml --- .../MultiverseCore/MultiverseCore.java | 4 +- .../MultiverseCore/api/NewMVConfig.java | 15 ++ .../MultiverseCore/api/NewNMVConfig.java | 182 ------------------ .../configuration/DefaultMVConfig.java | 69 ++++++- .../utils/settings/MVSettings.java | 30 ++- .../settings/migration/ConfigMigrator.java | 6 + .../migration/InvertBoolMigratorAction.java | 20 ++ .../migration/MoveMigratorAction.java | 18 +- .../settings/migration/VersionMigrator.java | 4 + 9 files changed, 156 insertions(+), 192 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/api/NewMVConfig.java delete mode 100644 src/main/java/com/onarandombox/MultiverseCore/api/NewNMVConfig.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/InvertBoolMigratorAction.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index 73aee163..a484a9dc 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -359,7 +359,9 @@ public class MultiverseCore extends JavaPlugin implements MVCore { */ @Override public void loadConfigs() { - new DefaultMVConfig(this); + DefaultMVConfig defaultMVConfig = new DefaultMVConfig(this); + defaultMVConfig.load(); + defaultMVConfig.save(); // Now grab the Configuration Files. this.multiverseConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "config.yml")); diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/NewMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/NewMVConfig.java new file mode 100644 index 00000000..7c65f1c3 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/api/NewMVConfig.java @@ -0,0 +1,15 @@ +package com.onarandombox.MultiverseCore.api; + +public interface NewMVConfig { + /** + * Sets enforceAccess. + * @param enforceAccess The new value. + */ + void setEnforceAccess(boolean enforceAccess); + + /** + * Gets enforceAccess. + * @return enforceAccess. + */ + boolean getEnforceAccess(); +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/NewNMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/NewNMVConfig.java deleted file mode 100644 index 0304c51c..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/api/NewNMVConfig.java +++ /dev/null @@ -1,182 +0,0 @@ -package com.onarandombox.MultiverseCore.api; - -public interface NewNMVConfig { - - /** - * Sets firstSpawnWorld. - * @param firstSpawnWorld The new value. - */ - void setFirstSpawnWorld(String firstSpawnWorld); - - /** - * Gets firstSpawnWorld. - * @return firstSpawnWorld. - */ - String getFirstSpawnWorld(); - - /** - * Sets version. - * @param version The new value. - */ - void setVersion(int version); - - /** - * Gets version. - * @return version. - */ - double getVersion(); - - /** - * Sets globalDebug. - * @param globalDebug The new value. - */ - void setGlobalDebug(int globalDebug); - - /** - * Gets globalDebug. - * @return globalDebug. - */ - int getGlobalDebug(); - - /** - * Sets displayPermErrors. - * @param displayPermErrors The new value. - */ - void setDisplayPermErrors(boolean displayPermErrors); - - /** - * Gets displayPermErrors. - * @return displayPermErrors. - */ - boolean getDisplayPermErrors(); - - /** - * Sets firstSpawnOverride. - * @param firstSpawnOverride The new value. - */ - void setFirstSpawnOverride(boolean firstSpawnOverride); - - /** - * Gets firstSpawnOverride. - * @return firstSpawnOverride. - */ - boolean getFirstSpawnOverride(); - - /** - * Sets teleportIntercept. - * @param teleportIntercept The new value. - */ - void setTeleportIntercept(boolean teleportIntercept); - - /** - * Gets teleportIntercept. - * @return teleportIntercept. - */ - boolean getTeleportIntercept(); - - /** - * Sets prefixChat. - * @param prefixChat The new value. - */ - void setPrefixChat(boolean prefixChat); - - /** - * Gets prefixChat. - * @return prefixChat. - */ - boolean getPrefixChat(); - - /** - * Sets prefixChatFormat. - * @param prefixChatFormat The new value. - */ - void setPrefixChatFormat(String prefixChatFormat); - - /** - * Gets prefixChatFormat. - * @return prefixChatFormat. - */ - String getPrefixChatFormat(); - - /** - * Sets enforceAccess. - * @param enforceAccess The new value. - */ - void setEnforceAccess(boolean enforceAccess); - - /** - * Gets enforceAccess. - * @return enforceAccess. - */ - boolean getEnforceAccess(); - - /** - * Sets whether to suppress startup messages. - * - * @param silentStart true to suppress messages. - */ - void setSilentStart(boolean silentStart); - - /** - * Whether we are suppressing startup messages. - * - * @return true if we are suppressing startup messages. - */ - boolean getSilentStart(); - - /** - * Sets whether or not to let Bukkit determine portal search radius on its own or if Multiverse should give input. - * - * @param useDefaultPortalSearch True to let Bukkit determine portal search radius on its own. - */ - void setUseDefaultPortalSearch(boolean useDefaultPortalSearch); - - /** - * Gets whether or not Bukkit will be determining portal search radius on its own or if Multiverse should help. - * - * @return True means Bukkit will use its own default values. - */ - boolean isUsingDefaultPortalSearch(); - - /** - * Sets the radius at which vanilla style portals will be searched for to connect to worlds together. - * - * @param searchRadius The portal search radius. - */ - void setPortalSearchRadius(int searchRadius); - - /** - * Gets the radius at which vanilla style portals will be searched for to connect to worlds together. - * - * @return The portal search radius. - */ - int getPortalSearchRadius(); - - /** - * Gets whether or not the automatic purge of entities is enabled. - * - * @return True if automatic purge is enabled. - */ - boolean isAutoPurgeEnabled(); - - /** - * Sets whether or not the automatic purge of entities is enabled. - * - * @param autopurge True if automatic purge should be enabled. - */ - void setAutoPurgeEnabled(boolean autopurge); - - /** - * Gets whether or not the donation/patreon messages are shown. - * - * @return True if donation/patreon messages should be shown. - */ - boolean isShowingDonateMessage(); - - /** - * Sets whether or not the donation/patreon messages are shown. - * - * @param idonotwanttodonate True if donation/patreon messages should be shown. - */ - void setShowDonateMessage(boolean idonotwanttodonate); -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java index 2ceb2f5f..8ce2b27f 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java @@ -1,21 +1,84 @@ package com.onarandombox.MultiverseCore.configuration; +import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.api.NewMVConfig; import com.onarandombox.MultiverseCore.utils.settings.MVSettings; +import com.onarandombox.MultiverseCore.utils.settings.migration.ConfigMigrator; +import com.onarandombox.MultiverseCore.utils.settings.migration.InvertBoolMigratorAction; +import com.onarandombox.MultiverseCore.utils.settings.migration.MoveMigratorAction; +import com.onarandombox.MultiverseCore.utils.settings.migration.VersionMigrator; -public class DefaultMVConfig { +public class DefaultMVConfig implements NewMVConfig { + public static final String CONFIG_FILENAME = "config2.yml"; + private final Path configPath; private final MVSettings settings; public DefaultMVConfig(MultiverseCore core) { - Path configPath = Path.of(core.getDataFolder().getPath(), "config2.yml"); + configPath = Path.of(core.getDataFolder().getPath(), CONFIG_FILENAME); + + migrateFromOldConfig(); + settings = MVSettings.builder(configPath) .logger(core.getLogger()) .defaultNodes(MVConfigNodes.getNodes()) + .migrator(ConfigMigrator.builder(MVConfigNodes.VERSION) + .addVersionMigrator(VersionMigrator.builder(5.0) + .addAction(MoveMigratorAction.of("multiverse-configuration.enforceaccess", "world.enforce-access")) + .addAction(MoveMigratorAction.of("multiverse-configuration.prefixchat", "messaging.enable-chat-prefix")) + .addAction(MoveMigratorAction.of("multiverse-configuration.prefixchatformat", "messaging.chat-prefix-format")) + .addAction(MoveMigratorAction.of("multiverse-configuration.teleportintercept", "world.teleport-intercept")) + .addAction(MoveMigratorAction.of("multiverse-configuration.firstspawnoverride", "spawn.first-spawn-override")) + //.addAction(MoveMigratorAction.of("multiverse-configuration.displaypermerrors", "")) + .addAction(MoveMigratorAction.of("multiverse-configuration.globaldebug", "misc.global-debug")) + .addAction(MoveMigratorAction.of("multiverse-configuration.silentstart", "misc.silent-start")) + .addAction(MoveMigratorAction.of("multiverse-configuration.firstspawnworld", "worlds.first-spawn-location")) + .addAction(MoveMigratorAction.of("multiverse-configuration.defaultportalsearch", "portals.use-custom-portal-search")) + .addAction(InvertBoolMigratorAction.of("portals.use-custom-portal-search")) + .addAction(MoveMigratorAction.of("multiverse-configuration.portalsearchradius", "portals.custom-portal-search-radius")) + .addAction(MoveMigratorAction.of("multiverse-configuration.autopurge", "world.auto-purge-entities")) + .addAction(MoveMigratorAction.of("multiverse-configuration.idonotwanttodonate", "misc.i-dont-want-to-donate")) + .build()) + .build()) .build(); - settings.load(); + } + + private void migrateFromOldConfig() { + String content; + try { + content = Files.readString(configPath); + } catch (IOException e) { + return; + } + if (content.contains("version: 2.5")) { + content = content.replace("==: com.onarandombox.MultiverseCore.MultiverseCoreConfiguration", ""); + } + try { + Files.writeString(configPath, content); + } catch (IOException e) { + // ignore + } + } + + public boolean load() { + return settings.load(); + } + + public void save() { settings.save(); } + + @Override + public void setEnforceAccess(boolean enforceAccess) { + settings.set(MVConfigNodes.ENFORCE_ACCESS, enforceAccess); + } + + @Override + public boolean getEnforceAccess() { + return settings.get(MVConfigNodes.ENFORCE_ACCESS); + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java index f7eef533..3bcb8606 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java @@ -6,10 +6,12 @@ import java.nio.file.Path; import java.util.List; import java.util.logging.Logger; +import com.onarandombox.MultiverseCore.utils.settings.migration.ConfigMigrator; import io.github.townyadvanced.commentedconfiguration.CommentedConfiguration; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; import io.github.townyadvanced.commentedconfiguration.setting.ValueNode; +import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -27,6 +29,8 @@ public class MVSettings { protected final Logger logger; protected final List defaultNodes; + protected final ConfigMigrator migrator; + protected CommentedConfiguration config; /** @@ -35,11 +39,13 @@ public class MVSettings { * @param configPath The path to the configuration file. * @param logger The Logger to use for error messages. * @param defaultNodes The default node values to add to the configuration. + * @param migrator */ - protected MVSettings(@NotNull Path configPath, @Nullable Logger logger, @Nullable List defaultNodes) { + protected MVSettings(@NotNull Path configPath, @Nullable Logger logger, @Nullable List defaultNodes, ConfigMigrator migrator) { this.configPath = configPath; this.defaultNodes = defaultNodes; this.logger = logger; + this.migrator = migrator; } /** @@ -55,6 +61,7 @@ public class MVSettings { if (!config.load()) { return false; } + migrateConfig(); addDefaultNodes(); return true; } @@ -79,6 +86,10 @@ public class MVSettings { return true; } + protected void migrateConfig() { + migrator.migrate(this); + } + /** * Adds default node values to the configuration if they are not already present. */ @@ -108,6 +119,10 @@ public class MVSettings { config.save(); } + public boolean isLoaded() { + return config != null; + } + /** * Gets the value of a node, if the node has a default value, it will be returned if the node is not found. * @@ -177,6 +192,8 @@ public class MVSettings { private Logger logger; private List defaultNodes; + private ConfigMigrator migrator; + public Builder(String configPath) { this.configPath = Path.of(configPath); } @@ -185,6 +202,10 @@ public class MVSettings { this.configPath = configPath; } + public Builder logger(@NotNull Plugin plugin) { + return logger(plugin.getLogger()); + } + public Builder logger(@Nullable Logger logger) { this.logger = logger; return this; @@ -195,8 +216,13 @@ public class MVSettings { return this; } + public Builder migrator(@Nullable ConfigMigrator migrator) { + this.migrator = migrator; + return this; + } + public MVSettings build() { - return new MVSettings(configPath, logger, defaultNodes); + return new MVSettings(configPath, logger, defaultNodes, migrator); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java index e41eb368..5a87a2de 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java @@ -3,10 +3,14 @@ package com.onarandombox.MultiverseCore.utils.settings.migration; import java.util.ArrayList; import java.util.List; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.utils.settings.MVSettings; import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; public class ConfigMigrator { + public static Builder builder(TypedValueNode versionNode) { + return new Builder(versionNode); + } private final TypedValueNode versionNode; private final List versionMigrators; @@ -19,7 +23,9 @@ public class ConfigMigrator { public void migrate(MVSettings settings) { double versionNumber = settings.get(versionNode); for (VersionMigrator versionMigrator : versionMigrators) { + Logging.info("Checking if config needs to be migrated from version " + versionNumber + " to " + versionMigrator.getVersion()); if (versionNumber < versionMigrator.getVersion()) { + Logging.info("Migrating config from version " + versionNumber + " to " + versionMigrator.getVersion()); versionMigrator.migrate(settings); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/InvertBoolMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/InvertBoolMigratorAction.java new file mode 100644 index 00000000..ccd76d99 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/InvertBoolMigratorAction.java @@ -0,0 +1,20 @@ +package com.onarandombox.MultiverseCore.utils.settings.migration; + +import com.onarandombox.MultiverseCore.utils.settings.MVSettings; + +public class InvertBoolMigratorAction implements MigratorAction { + public static InvertBoolMigratorAction of(String path) { + return new InvertBoolMigratorAction(path); + } + + private final String path; + + public InvertBoolMigratorAction(String path) { + this.path = path; + } + + @Override + public void migrate(MVSettings settings) { + settings.getConfig().set(path, !settings.getConfig().getBoolean(path)); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java index dd15e7f8..a8c8e39b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java @@ -1,8 +1,15 @@ package com.onarandombox.MultiverseCore.utils.settings.migration; +import java.util.Optional; + +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.utils.settings.MVSettings; public class MoveMigratorAction implements MigratorAction { + public static MoveMigratorAction of(String fromPath, String toPath) { + return new MoveMigratorAction(fromPath, toPath); + } + private final String fromPath; private final String toPath; @@ -13,9 +20,12 @@ public class MoveMigratorAction implements MigratorAction { @Override public void migrate(MVSettings settings) { - Object value = settings.getConfig().get(fromPath); - if (value != null) { - settings.getConfig().set(toPath, value); - } + Logging.info(String.valueOf(settings.getConfig().get(fromPath))); + Optional.ofNullable(settings.getConfig().get(fromPath)) + .ifPresent(value -> { + Logging.info("Moving " + fromPath + " to " + toPath); + settings.getConfig().set(toPath, value); + settings.getConfig().set(fromPath, null); + }); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/VersionMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/VersionMigrator.java index c6be421b..3796a3d0 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/VersionMigrator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/VersionMigrator.java @@ -6,6 +6,10 @@ import java.util.List; import com.onarandombox.MultiverseCore.utils.settings.MVSettings; public class VersionMigrator { + public static Builder builder(double version) { + return new Builder(version); + } + private final double version; private final List actions; From 783038ffb469339f4fb54faddfef6a7cd93d73ff Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Sat, 18 Mar 2023 23:27:57 +0800 Subject: [PATCH 05/37] feat: Implement get/set methods from previous MVConfig class --- .../MultiverseCore/api/NewMVConfig.java | 142 ++++++++++++++++++ .../configuration/DefaultMVConfig.java | 110 ++++++++++++++ .../configuration/MVConfigNodes.java | 2 +- 3 files changed, 253 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/NewMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/NewMVConfig.java index 7c65f1c3..a900605a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/NewMVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/NewMVConfig.java @@ -12,4 +12,146 @@ public interface NewMVConfig { * @return enforceAccess. */ boolean getEnforceAccess(); + + /** + * Sets whether or not the automatic purge of entities is enabled. + * + * @param autopurge True if automatic purge should be enabled. + */ + void setAutoPurgeEnabled(boolean autopurge); + + /** + * Gets whether or not the automatic purge of entities is enabled. + * + * @return True if automatic purge is enabled. + */ + boolean isAutoPurgeEnabled(); + + /** + * Sets teleportIntercept. + * @param teleportIntercept The new value. + */ + void setTeleportIntercept(boolean teleportIntercept); + + /** + * Gets teleportIntercept. + * @return teleportIntercept. + */ + boolean getTeleportIntercept(); + + /** + * Sets firstSpawnOverride. + * @param firstSpawnOverride The new value. + */ + void setFirstSpawnOverride(boolean firstSpawnOverride); + + /** + * Gets firstSpawnOverride. + * @return firstSpawnOverride. + */ + boolean getFirstSpawnOverride(); + + /** + * Sets firstSpawnWorld. + * @param firstSpawnWorld The new value. + */ + void setFirstSpawnWorld(String firstSpawnWorld); + + /** + * Gets firstSpawnWorld. + * @return firstSpawnWorld. + */ + String getFirstSpawnWorld(); + + /** + * Sets whether or not to let Bukkit determine portal search radius on its own or if Multiverse should give input. + * + * @param useDefaultPortalSearch True to let Bukkit determine portal search radius on its own. + */ + void setUseDefaultPortalSearch(boolean useDefaultPortalSearch); + + /** + * Gets whether or not Bukkit will be determining portal search radius on its own or if Multiverse should help. + * + * @return True means Bukkit will use its own default values. + */ + boolean isUsingDefaultPortalSearch(); + + /** + * Sets the radius at which vanilla style portals will be searched for to connect to worlds together. + * + * @param searchRadius The portal search radius. + */ + void setPortalSearchRadius(int searchRadius); + + /** + * Gets the radius at which vanilla style portals will be searched for to connect to worlds together. + * + * @return The portal search radius. + */ + int getPortalSearchRadius(); + + /** + * Sets prefixChat. + * @param prefixChat The new value. + */ + void setPrefixChat(boolean prefixChat); + + /** + * Gets prefixChat. + * @return prefixChat. + */ + boolean getPrefixChat(); + + /** + * Sets prefixChatFormat. + * @param prefixChatFormat The new value. + */ + void setPrefixChatFormat(String prefixChatFormat); + + /** + * Gets prefixChatFormat. + * @return prefixChatFormat. + */ + String getPrefixChatFormat(); + + /** + * Sets globalDebug. + * @param globalDebug The new value. + */ + void setGlobalDebug(int globalDebug); + + /** + * Gets globalDebug. + * @return globalDebug. + */ + int getGlobalDebug(); + + /** + * Sets whether to suppress startup messages. + * + * @param silentStart true to suppress messages. + */ + void setSilentStart(boolean silentStart); + + /** + * Whether we are suppressing startup messages. + * + * @return true if we are suppressing startup messages. + */ + boolean getSilentStart(); + + /** + * Sets whether or not the donation/patreon messages are shown. + * + * @param idonotwanttodonate True if donation/patreon messages should be shown. + */ + void setShowDonateMessage(boolean idonotwanttodonate); + + /** + * Gets whether or not the donation/patreon messages are shown. + * + * @return True if donation/patreon messages should be shown. + */ + boolean isShowingDonateMessage(); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java index 8ce2b27f..0173e0ee 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java @@ -81,4 +81,114 @@ public class DefaultMVConfig implements NewMVConfig { public boolean getEnforceAccess() { return settings.get(MVConfigNodes.ENFORCE_ACCESS); } + + @Override + public void setAutoPurgeEnabled(boolean autopurge) { + settings.set(MVConfigNodes.AUTO_PURGE_ENTITIES, autopurge); + } + + @Override + public boolean isAutoPurgeEnabled() { + return settings.get(MVConfigNodes.AUTO_PURGE_ENTITIES); + } + + @Override + public void setTeleportIntercept(boolean teleportIntercept) { + settings.set(MVConfigNodes.TELEPORT_INTERCEPT, teleportIntercept); + } + + @Override + public boolean getTeleportIntercept() { + return settings.get(MVConfigNodes.TELEPORT_INTERCEPT); + } + + @Override + public void setFirstSpawnOverride(boolean firstSpawnOverride) { + settings.set(MVConfigNodes.FIRST_SPAWN_OVERRIDE, firstSpawnOverride); + } + + @Override + public boolean getFirstSpawnOverride() { + return settings.get(MVConfigNodes.FIRST_SPAWN_OVERRIDE); + } + + @Override + public void setFirstSpawnWorld(String firstSpawnWorld) { + settings.set(MVConfigNodes.FIRST_SPAWN_LOCATION, firstSpawnWorld); + } + + @Override + public String getFirstSpawnWorld() { + return settings.get(MVConfigNodes.FIRST_SPAWN_LOCATION); + } + + @Override + public void setUseDefaultPortalSearch(boolean useDefaultPortalSearch) { + settings.set(MVConfigNodes.USE_CUSTOM_PORTAL_SEARCH, !useDefaultPortalSearch); + } + + @Override + public boolean isUsingDefaultPortalSearch() { + return !settings.get(MVConfigNodes.USE_CUSTOM_PORTAL_SEARCH); + } + + @Override + public void setPortalSearchRadius(int searchRadius) { + settings.set(MVConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS, searchRadius); + } + + @Override + public int getPortalSearchRadius() { + return settings.get(MVConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS); + } + + @Override + public void setPrefixChat(boolean prefixChat) { + settings.set(MVConfigNodes.ENABLE_CHAT_PREFIX, prefixChat); + } + + @Override + public boolean getPrefixChat() { + return settings.get(MVConfigNodes.ENABLE_CHAT_PREFIX); + } + + @Override + public void setPrefixChatFormat(String prefixChatFormat) { + settings.set(MVConfigNodes.CHAT_PREFIX_FORMAT, prefixChatFormat); + } + + @Override + public String getPrefixChatFormat() { + return settings.get(MVConfigNodes.CHAT_PREFIX_FORMAT); + } + + @Override + public void setGlobalDebug(int globalDebug) { + settings.set(MVConfigNodes.GLOBAL_DEBUG, globalDebug); + } + + @Override + public int getGlobalDebug() { + return settings.get(MVConfigNodes.GLOBAL_DEBUG); + } + + @Override + public void setSilentStart(boolean silentStart) { + settings.set(MVConfigNodes.SILENT_START, silentStart); + } + + @Override + public boolean getSilentStart() { + return settings.get(MVConfigNodes.SILENT_START); + } + + @Override + public void setShowDonateMessage(boolean idonotwanttodonate) { + settings.set(MVConfigNodes.I_DONT_WANT_TO_DONATE, idonotwanttodonate); + } + + @Override + public boolean isShowingDonateMessage() { + return settings.get(MVConfigNodes.I_DONT_WANT_TO_DONATE); + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java index 96a926b4..aa5d73b7 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java @@ -148,7 +148,7 @@ public class MVConfigNodes { .defaultValue(0) .build()); - public static final MVValueNode SLIENT_START = node(MVValueNode.builder("misc.silent-start", Boolean.class) + public static final MVValueNode SILENT_START = node(MVValueNode.builder("misc.silent-start", Boolean.class) .comment("") .comment("If true, the startup console messages will no longer show.") .defaultValue(false) From 6cc169cb559d315d872a9058c2aa1be3c31a0de4 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Wed, 22 Mar 2023 22:48:24 +0800 Subject: [PATCH 06/37] feat: Fully replace with new config api --- .../MultiverseCore/MultiverseCore.java | 57 +--- .../MultiverseCoreConfiguration.java | 316 ------------------ .../MultiverseCore/api/MVConfig.java | 187 +++++------ .../MultiverseCore/api/NewMVConfig.java | 157 --------- .../configuration/DefaultMVConfig.java | 15 +- .../configuration/MVConfigNodes.java | 2 +- .../utils/settings/MVSettings.java | 12 +- .../settings/migration/ConfigMigrator.java | 8 + .../MultiverseCore/world/SimpleMVWorld.java | 3 +- .../world/SimpleMVWorldManager.java | 3 +- 10 files changed, 111 insertions(+), 649 deletions(-) delete mode 100644 src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java delete mode 100644 src/main/java/com/onarandombox/MultiverseCore/api/NewMVConfig.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index a484a9dc..27234463 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -7,12 +7,7 @@ package com.onarandombox.MultiverseCore; -import java.io.BufferedReader; import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -32,8 +27,8 @@ import com.onarandombox.MultiverseCore.commands.ConfirmCommand; import com.onarandombox.MultiverseCore.commands.CreateCommand; import com.onarandombox.MultiverseCore.commands.DebugCommand; import com.onarandombox.MultiverseCore.commands.DeleteCommand; -import com.onarandombox.MultiverseCore.commands.ImportCommand; import com.onarandombox.MultiverseCore.commands.GameruleCommand; +import com.onarandombox.MultiverseCore.commands.ImportCommand; import com.onarandombox.MultiverseCore.commands.LoadCommand; import com.onarandombox.MultiverseCore.commands.RegenCommand; import com.onarandombox.MultiverseCore.commands.ReloadCommand; @@ -68,9 +63,6 @@ import com.onarandombox.MultiverseCore.utils.metrics.MetricsConfigurator; import com.onarandombox.MultiverseCore.world.SimpleMVWorldManager; import com.onarandombox.MultiverseCore.world.WorldProperties; import me.main__.util.SerializationConfig.SerializationConfig; -import org.bukkit.configuration.Configuration; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -95,8 +87,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore { private final MVWorldManager worldManager = new SimpleMVWorldManager(this); // Configurations - private FileConfiguration multiverseConfig; - private volatile MultiverseCoreConfiguration config; + private DefaultMVConfig config; // Listeners private MVChatListener chatListener; @@ -126,7 +117,6 @@ public class MultiverseCore extends JavaPlugin implements MVCore { Logging.init(this); // Register our config classes - SerializationConfig.registerAll(MultiverseCoreConfiguration.class); SerializationConfig.registerAll(WorldProperties.class); SerializationConfig.initLogging(Logging.getLogger()); } @@ -138,7 +128,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore { public void onEnable() { // Load our configs first as we need them for everything else. this.loadConfigs(); - if (this.multiverseConfig == null) { + if (this.config == null) { Logging.severe("Your configs were not loaded."); Logging.severe("Please check your configs and restart the server."); this.getServer().getPluginManager().disablePlugin(this); @@ -359,35 +349,10 @@ public class MultiverseCore extends JavaPlugin implements MVCore { */ @Override public void loadConfigs() { - DefaultMVConfig defaultMVConfig = new DefaultMVConfig(this); - defaultMVConfig.load(); - defaultMVConfig.save(); + config = new DefaultMVConfig(this); + config.load(); + config.save(); - // Now grab the Configuration Files. - this.multiverseConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "config.yml")); - InputStream resourceURL = this.getClass().getResourceAsStream("/defaults/config.yml"); - - // Read in our default config with UTF-8 now - Configuration coreDefaults; - try { - coreDefaults = YamlConfiguration.loadConfiguration(new BufferedReader(new InputStreamReader(resourceURL, "UTF-8"))); - this.multiverseConfig.setDefaults(coreDefaults); - } catch (UnsupportedEncodingException e) { - Logging.severe("Couldn't load default config with UTF-8 encoding. Details follow:"); - e.printStackTrace(); - Logging.severe("Default configs NOT loaded."); - } - - this.multiverseConfig.options().copyDefaults(false); - this.multiverseConfig.options().copyHeader(true); - - MultiverseCoreConfiguration wantedConfig = null; - try { - wantedConfig = (MultiverseCoreConfiguration) multiverseConfig.get("multiverse-configuration"); - } catch (Exception ignore) { - } finally { - config = ((wantedConfig == null) ? new MultiverseCoreConfiguration() : wantedConfig); - } this.worldManager.loadWorldConfig(new File(getDataFolder(), "worlds.yml")); int level = Logging.getDebugLevel(); @@ -402,14 +367,8 @@ public class MultiverseCore extends JavaPlugin implements MVCore { */ @Override public boolean saveMVConfig() { - try { - this.multiverseConfig.set("multiverse-configuration", getMVConfig()); - this.multiverseConfig.save(new File(getDataFolder(), "config.yml")); - return true; - } catch (IOException e) { - Logging.severe("Could not save Multiverse config.yml config. Please check your file permissions."); - return false; - } + this.config.save(); + return true; } /** diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java deleted file mode 100644 index 6be5659c..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java +++ /dev/null @@ -1,316 +0,0 @@ -package com.onarandombox.MultiverseCore; - -import java.util.Map; - -import com.dumptruckman.minecraft.util.Logging; -import com.onarandombox.MultiverseCore.api.MVConfig; -import com.onarandombox.MultiverseCore.event.MVDebugModeEvent; -import me.main__.util.SerializationConfig.NoSuchPropertyException; -import me.main__.util.SerializationConfig.Property; -import me.main__.util.SerializationConfig.SerializationConfig; -import org.bukkit.Bukkit; - -/** - * Our configuration. - */ -public class MultiverseCoreConfiguration extends SerializationConfig implements MVConfig { - private static MultiverseCoreConfiguration instance; - - /** - * Sets the statically saved instance. - * @param instance The new instance. - */ - public static void setInstance(MultiverseCoreConfiguration instance) { - MultiverseCoreConfiguration.instance = instance; - } - - /** - * @return True if the static instance of config is set. - */ - public static boolean isSet() { - return instance != null; - } - - /** - * Gets the statically saved instance. - * @return The statically saved instance. - */ - @Deprecated - public static MultiverseCoreConfiguration getInstance() { - if (instance == null) - throw new IllegalStateException("The instance wasn't set!"); - return instance; - } - - @Property - private volatile boolean enforceaccess; - @Property - private volatile boolean prefixchat; - @Property - private volatile String prefixchatformat; - @Property - private volatile boolean teleportintercept; - @Property - private volatile boolean firstspawnoverride; - @Property - private volatile boolean displaypermerrors; - @Property - private volatile int globaldebug; - @Property - private volatile boolean silentstart; - @Property - private volatile double version; - @Property - private volatile String firstspawnworld; - @Property - private volatile boolean defaultportalsearch; - @Property - private volatile int portalsearchradius; - @Property - private volatile boolean autopurge; - @Property - private volatile boolean idonotwanttodonate; - - public MultiverseCoreConfiguration() { - super(); - MultiverseCoreConfiguration.setInstance(this); - } - - public MultiverseCoreConfiguration(Map values) { - super(values); - MultiverseCoreConfiguration.setInstance(this); - } - - /** - * {@inheritDoc} - */ - @Override - protected void setDefaults() { - // BEGIN CHECKSTYLE-SUPPRESSION: MagicNumberCheck - enforceaccess = false; - prefixchat = false; - prefixchatformat = "[%world%]%chat%"; - teleportintercept = true; - firstspawnoverride = true; - displaypermerrors = true; - globaldebug = 0; - this.version = 2.9; - silentstart = false; - defaultportalsearch = true; - portalsearchradius = 128; - autopurge = true; - idonotwanttodonate = false; - // END CHECKSTYLE-SUPPRESSION: MagicNumberCheck - } - - /** - * {@inheritDoc} - */ - @Override - public boolean setConfigProperty(String property, String value) { - try { - return this.setProperty(property, value, true); - } catch (NoSuchPropertyException e) { - return false; - } - } - - // And here we go: - - /** - * {@inheritDoc} - */ - @Override - public boolean getEnforceAccess() { - return this.enforceaccess; - } - - /** - * {@inheritDoc} - */ - @Override - public void setEnforceAccess(boolean enforceAccess) { - this.enforceaccess = enforceAccess; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean getPrefixChat() { - return this.prefixchat; - } - - /** - * {@inheritDoc} - */ - @Override - public void setPrefixChat(boolean prefixChat) { - this.prefixchat = prefixChat; - } - - /** - * {@inheritDoc} - */ - @Override - public String getPrefixChatFormat() { - return this.prefixchatformat; - } - - /** - * {@inheritDoc} - */ - @Override - public void setPrefixChatFormat(String prefixChatFormat) { - this.prefixchatformat = prefixChatFormat; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean getTeleportIntercept() { - return this.teleportintercept; - } - - /** - * {@inheritDoc} - */ - @Override - public void setTeleportIntercept(boolean teleportIntercept) { - this.teleportintercept = teleportIntercept; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean getFirstSpawnOverride() { - return this.firstspawnoverride; - } - - /** - * {@inheritDoc} - */ - @Override - public void setFirstSpawnOverride(boolean firstSpawnOverride) { - this.firstspawnoverride = firstSpawnOverride; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean getDisplayPermErrors() { - return this.displaypermerrors; - } - - /** - * {@inheritDoc} - */ - @Override - public void setDisplayPermErrors(boolean displayPermErrors) { - this.displaypermerrors = displayPermErrors; - } - - /** - * {@inheritDoc} - */ - @Override - public int getGlobalDebug() { - return this.globaldebug; - } - - /** - * {@inheritDoc} - */ - @Override - public void setGlobalDebug(int globalDebug) { - this.globaldebug = globalDebug; - Logging.setDebugLevel(globalDebug); - Bukkit.getPluginManager().callEvent(new MVDebugModeEvent(globalDebug)); - } - - /** - * {@inheritDoc} - */ - @Override - public double getVersion() { - return this.version; - } - - /** - * {@inheritDoc} - */ - @Override - public void setVersion(int version) { - this.version = version; - } - - /** - * {@inheritDoc} - */ - @Override - public String getFirstSpawnWorld() { - return this.firstspawnworld; - } - - /** - * {@inheritDoc} - */ - @Override - public void setFirstSpawnWorld(String firstSpawnWorld) { - this.firstspawnworld = firstSpawnWorld; - } - - @Override - public void setSilentStart(boolean silentStart) { - Logging.setShowingConfig(!silentStart); - this.silentstart = silentStart; - } - - @Override - public boolean getSilentStart() { - return silentstart; - } - - @Override - public void setUseDefaultPortalSearch(boolean useDefaultPortalSearch) { - defaultportalsearch = useDefaultPortalSearch; - } - - @Override - public boolean isUsingDefaultPortalSearch() { - return defaultportalsearch; - } - - @Override - public void setPortalSearchRadius(int searchRadius) { - this.portalsearchradius = searchRadius; - } - - @Override - public int getPortalSearchRadius() { - return portalsearchradius; - } - - @Override - public boolean isAutoPurgeEnabled() { - return autopurge; - } - - @Override - public void setAutoPurgeEnabled(boolean autopurge) { - this.autopurge = autopurge; - } - - @Override - public boolean isShowingDonateMessage() { - return !idonotwanttodonate; - } - - @Override - public void setShowDonateMessage(boolean showDonateMessage) { - this.idonotwanttodonate = !showDonateMessage; - } -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java index 237aeae8..8b158fc3 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java @@ -1,78 +1,31 @@ package com.onarandombox.MultiverseCore.api; -import org.bukkit.configuration.serialization.ConfigurationSerializable; - -/** - * The configuration of MultiverseCore. - */ -public interface MVConfig extends ConfigurationSerializable { +public interface MVConfig { /** - * Sets a property using a {@link String}. - * @param property The name of the property. - * @param value The value. - * @return True on success, false if the operation failed. + * Sets enforceAccess. + * @param enforceAccess The new value. */ - boolean setConfigProperty(String property, String value); + void setEnforceAccess(boolean enforceAccess); /** - * Sets firstSpawnWorld. - * @param firstSpawnWorld The new value. + * Gets enforceAccess. + * @return enforceAccess. */ - void setFirstSpawnWorld(String firstSpawnWorld); + boolean getEnforceAccess(); /** - * Gets firstSpawnWorld. - * @return firstSpawnWorld. + * Sets whether or not the automatic purge of entities is enabled. + * + * @param autopurge True if automatic purge should be enabled. */ - String getFirstSpawnWorld(); + void setAutoPurgeEnabled(boolean autopurge); /** - * Sets version. - * @param version The new value. + * Gets whether or not the automatic purge of entities is enabled. + * + * @return True if automatic purge is enabled. */ - void setVersion(int version); - - /** - * Gets version. - * @return version. - */ - double getVersion(); - - /** - * Sets globalDebug. - * @param globalDebug The new value. - */ - void setGlobalDebug(int globalDebug); - - /** - * Gets globalDebug. - * @return globalDebug. - */ - int getGlobalDebug(); - - /** - * Sets displayPermErrors. - * @param displayPermErrors The new value. - */ - void setDisplayPermErrors(boolean displayPermErrors); - - /** - * Gets displayPermErrors. - * @return displayPermErrors. - */ - boolean getDisplayPermErrors(); - - /** - * Sets firstSpawnOverride. - * @param firstSpawnOverride The new value. - */ - void setFirstSpawnOverride(boolean firstSpawnOverride); - - /** - * Gets firstSpawnOverride. - * @return firstSpawnOverride. - */ - boolean getFirstSpawnOverride(); + boolean isAutoPurgeEnabled(); /** * Sets teleportIntercept. @@ -87,54 +40,28 @@ public interface MVConfig extends ConfigurationSerializable { boolean getTeleportIntercept(); /** - * Sets prefixChat. - * @param prefixChat The new value. + * Sets firstSpawnOverride. + * @param firstSpawnOverride The new value. */ - void setPrefixChat(boolean prefixChat); + void setFirstSpawnOverride(boolean firstSpawnOverride); /** - * Gets prefixChat. - * @return prefixChat. + * Gets firstSpawnOverride. + * @return firstSpawnOverride. */ - boolean getPrefixChat(); - - /** - * Sets prefixChatFormat. - * @param prefixChatFormat The new value. - */ - void setPrefixChatFormat(String prefixChatFormat); + boolean getFirstSpawnOverride(); /** - * Gets prefixChatFormat. - * @return prefixChatFormat. + * Sets firstSpawnWorld. + * @param firstSpawnWorld The new value. */ - String getPrefixChatFormat(); + void setFirstSpawnWorld(String firstSpawnWorld); /** - * Sets enforceAccess. - * @param enforceAccess The new value. + * Gets firstSpawnWorld. + * @return firstSpawnWorld. */ - void setEnforceAccess(boolean enforceAccess); - - /** - * Gets enforceAccess. - * @return enforceAccess. - */ - boolean getEnforceAccess(); - - /** - * Sets whether to suppress startup messages. - * - * @param silentStart true to suppress messages. - */ - void setSilentStart(boolean silentStart); - - /** - * Whether we are suppressing startup messages. - * - * @return true if we are suppressing startup messages. - */ - boolean getSilentStart(); + String getFirstSpawnWorld(); /** * Sets whether or not to let Bukkit determine portal search radius on its own or if Multiverse should give input. @@ -165,25 +92,54 @@ public interface MVConfig extends ConfigurationSerializable { int getPortalSearchRadius(); /** - * Gets whether or not the automatic purge of entities is enabled. - * - * @return True if automatic purge is enabled. + * Sets prefixChat. + * @param prefixChat The new value. */ - boolean isAutoPurgeEnabled(); + void setPrefixChat(boolean prefixChat); /** - * Sets whether or not the automatic purge of entities is enabled. - * - * @param autopurge True if automatic purge should be enabled. + * Gets prefixChat. + * @return prefixChat. */ - void setAutoPurgeEnabled(boolean autopurge); + boolean getPrefixChat(); /** - * Gets whether or not the donation/patreon messages are shown. - * - * @return True if donation/patreon messages should be shown. + * Sets prefixChatFormat. + * @param prefixChatFormat The new value. */ - boolean isShowingDonateMessage(); + void setPrefixChatFormat(String prefixChatFormat); + + /** + * Gets prefixChatFormat. + * @return prefixChatFormat. + */ + String getPrefixChatFormat(); + + /** + * Sets globalDebug. + * @param globalDebug The new value. + */ + void setGlobalDebug(int globalDebug); + + /** + * Gets globalDebug. + * @return globalDebug. + */ + int getGlobalDebug(); + + /** + * Sets whether to suppress startup messages. + * + * @param silentStart true to suppress messages. + */ + void setSilentStart(boolean silentStart); + + /** + * Whether we are suppressing startup messages. + * + * @return true if we are suppressing startup messages. + */ + boolean getSilentStart(); /** * Sets whether or not the donation/patreon messages are shown. @@ -191,4 +147,11 @@ public interface MVConfig extends ConfigurationSerializable { * @param idonotwanttodonate True if donation/patreon messages should be shown. */ void setShowDonateMessage(boolean idonotwanttodonate); + + /** + * Gets whether or not the donation/patreon messages are shown. + * + * @return True if donation/patreon messages should be shown. + */ + boolean isShowingDonateMessage(); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/NewMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/NewMVConfig.java deleted file mode 100644 index a900605a..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/api/NewMVConfig.java +++ /dev/null @@ -1,157 +0,0 @@ -package com.onarandombox.MultiverseCore.api; - -public interface NewMVConfig { - /** - * Sets enforceAccess. - * @param enforceAccess The new value. - */ - void setEnforceAccess(boolean enforceAccess); - - /** - * Gets enforceAccess. - * @return enforceAccess. - */ - boolean getEnforceAccess(); - - /** - * Sets whether or not the automatic purge of entities is enabled. - * - * @param autopurge True if automatic purge should be enabled. - */ - void setAutoPurgeEnabled(boolean autopurge); - - /** - * Gets whether or not the automatic purge of entities is enabled. - * - * @return True if automatic purge is enabled. - */ - boolean isAutoPurgeEnabled(); - - /** - * Sets teleportIntercept. - * @param teleportIntercept The new value. - */ - void setTeleportIntercept(boolean teleportIntercept); - - /** - * Gets teleportIntercept. - * @return teleportIntercept. - */ - boolean getTeleportIntercept(); - - /** - * Sets firstSpawnOverride. - * @param firstSpawnOverride The new value. - */ - void setFirstSpawnOverride(boolean firstSpawnOverride); - - /** - * Gets firstSpawnOverride. - * @return firstSpawnOverride. - */ - boolean getFirstSpawnOverride(); - - /** - * Sets firstSpawnWorld. - * @param firstSpawnWorld The new value. - */ - void setFirstSpawnWorld(String firstSpawnWorld); - - /** - * Gets firstSpawnWorld. - * @return firstSpawnWorld. - */ - String getFirstSpawnWorld(); - - /** - * Sets whether or not to let Bukkit determine portal search radius on its own or if Multiverse should give input. - * - * @param useDefaultPortalSearch True to let Bukkit determine portal search radius on its own. - */ - void setUseDefaultPortalSearch(boolean useDefaultPortalSearch); - - /** - * Gets whether or not Bukkit will be determining portal search radius on its own or if Multiverse should help. - * - * @return True means Bukkit will use its own default values. - */ - boolean isUsingDefaultPortalSearch(); - - /** - * Sets the radius at which vanilla style portals will be searched for to connect to worlds together. - * - * @param searchRadius The portal search radius. - */ - void setPortalSearchRadius(int searchRadius); - - /** - * Gets the radius at which vanilla style portals will be searched for to connect to worlds together. - * - * @return The portal search radius. - */ - int getPortalSearchRadius(); - - /** - * Sets prefixChat. - * @param prefixChat The new value. - */ - void setPrefixChat(boolean prefixChat); - - /** - * Gets prefixChat. - * @return prefixChat. - */ - boolean getPrefixChat(); - - /** - * Sets prefixChatFormat. - * @param prefixChatFormat The new value. - */ - void setPrefixChatFormat(String prefixChatFormat); - - /** - * Gets prefixChatFormat. - * @return prefixChatFormat. - */ - String getPrefixChatFormat(); - - /** - * Sets globalDebug. - * @param globalDebug The new value. - */ - void setGlobalDebug(int globalDebug); - - /** - * Gets globalDebug. - * @return globalDebug. - */ - int getGlobalDebug(); - - /** - * Sets whether to suppress startup messages. - * - * @param silentStart true to suppress messages. - */ - void setSilentStart(boolean silentStart); - - /** - * Whether we are suppressing startup messages. - * - * @return true if we are suppressing startup messages. - */ - boolean getSilentStart(); - - /** - * Sets whether or not the donation/patreon messages are shown. - * - * @param idonotwanttodonate True if donation/patreon messages should be shown. - */ - void setShowDonateMessage(boolean idonotwanttodonate); - - /** - * Gets whether or not the donation/patreon messages are shown. - * - * @return True if donation/patreon messages should be shown. - */ - boolean isShowingDonateMessage(); -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java index 0173e0ee..17c72f20 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java @@ -4,16 +4,18 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; -import com.onarandombox.MultiverseCore.api.NewMVConfig; +import com.onarandombox.MultiverseCore.api.MVConfig; import com.onarandombox.MultiverseCore.utils.settings.MVSettings; import com.onarandombox.MultiverseCore.utils.settings.migration.ConfigMigrator; import com.onarandombox.MultiverseCore.utils.settings.migration.InvertBoolMigratorAction; import com.onarandombox.MultiverseCore.utils.settings.migration.MoveMigratorAction; import com.onarandombox.MultiverseCore.utils.settings.migration.VersionMigrator; -public class DefaultMVConfig implements NewMVConfig { - public static final String CONFIG_FILENAME = "config2.yml"; +public class DefaultMVConfig implements MVConfig { + public static final String CONFIG_FILENAME = "config.yml"; + public static final double CONFIG_VERSION = 5.0; private final Path configPath; private final MVSettings settings; @@ -21,10 +23,10 @@ public class DefaultMVConfig implements NewMVConfig { public DefaultMVConfig(MultiverseCore core) { configPath = Path.of(core.getDataFolder().getPath(), CONFIG_FILENAME); - migrateFromOldConfig(); + migrateFromOldConfigFile(); settings = MVSettings.builder(configPath) - .logger(core.getLogger()) + .logger(Logging.getLogger()) .defaultNodes(MVConfigNodes.getNodes()) .migrator(ConfigMigrator.builder(MVConfigNodes.VERSION) .addVersionMigrator(VersionMigrator.builder(5.0) @@ -47,13 +49,14 @@ public class DefaultMVConfig implements NewMVConfig { .build(); } - private void migrateFromOldConfig() { + private void migrateFromOldConfigFile() { String content; try { content = Files.readString(configPath); } catch (IOException e) { return; } + // Remove the old config section if it is still in the old ConfigurationSerializable. if (content.contains("version: 2.5")) { content = content.replace("==: com.onarandombox.MultiverseCore.MultiverseCoreConfiguration", ""); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java index aa5d73b7..d6d7f91e 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java @@ -165,6 +165,6 @@ public class MVConfigNodes { .comment("") .comment("This just signifies the version number so we can see what version of config you have.") .comment("NEVER TOUCH THIS VALUE") - .defaultValue(5.0D) + .defaultValue(DefaultMVConfig.CONFIG_VERSION) .build()); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java index 3bcb8606..f983bca1 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java @@ -36,10 +36,10 @@ public class MVSettings { /** * Creates a new MVSettings instance that makes use of CommentedConfiguration. * - * @param configPath The path to the configuration file. - * @param logger The Logger to use for error messages. - * @param defaultNodes The default node values to add to the configuration. - * @param migrator + * @param configPath The path to the configuration file. + * @param logger The Logger to use for error messages. + * @param defaultNodes The default node values to add to the configuration. + * @param migrator The migrator to use for migrating the configuration. */ protected MVSettings(@NotNull Path configPath, @Nullable Logger logger, @Nullable List defaultNodes, ConfigMigrator migrator) { this.configPath = configPath; @@ -177,6 +177,10 @@ public class MVSettings { config.set(node.getPath(), value); } + public void setDefault(@NotNull TypedValueNode node) { + config.set(node.getPath(), node.getDefaultValue()); + } + /** * Gets the configuration object. * diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java index 5a87a2de..c8ca6d98 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java @@ -29,12 +29,20 @@ public class ConfigMigrator { versionMigrator.migrate(settings); } } + + settings.setDefault(versionNode); } public static class Builder { private final TypedValueNode versionNode; private final List versionMigrators; + /** + * Creates a new builder for a ConfigMigrator. + * + * @param versionNode The node that stores the version number of the config. + * Default value should be the current latest version number. + */ public Builder(TypedValueNode versionNode) { this.versionNode = versionNode; this.versionMigrators = new ArrayList<>(); diff --git a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java index 0790c668..8df6ed58 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java @@ -14,7 +14,6 @@ import java.util.UUID; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; -import com.onarandombox.MultiverseCore.MultiverseCoreConfiguration; import com.onarandombox.MultiverseCore.api.BlockSafety; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; @@ -282,7 +281,7 @@ public class SimpleMVWorld implements MVWorld { } world.setSpawnFlags(allowMonsters, allowAnimals); } - if (MultiverseCoreConfiguration.getInstance().isAutoPurgeEnabled()) { + if (plugin.getMVConfig().isAutoPurgeEnabled()) { plugin.getMVWorldManager().getTheWorldPurger().purgeWorld(SimpleMVWorld.this); } return super.validateChange(property, newValue, oldValue, object); diff --git a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java index 214f3496..27a79b8b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java @@ -26,7 +26,6 @@ import java.util.stream.Collectors; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; -import com.onarandombox.MultiverseCore.MultiverseCoreConfiguration; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; @@ -482,7 +481,7 @@ public class SimpleMVWorldManager implements MVWorldManager { return false; } SimpleMVWorld world = new SimpleMVWorld(plugin, cbworld, mvworld); - if (MultiverseCoreConfiguration.getInstance().isAutoPurgeEnabled()) { + if (plugin.getMVConfig().isAutoPurgeEnabled()) { this.worldPurger.purgeWorld(world); } this.worlds.put(worldName, world); From 9ae2cddfda5255ecc8251917b9ee27cccf124501 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Wed, 22 Mar 2023 22:53:16 +0800 Subject: [PATCH 07/37] refactor: Flip boolean for custom portal search config --- .../com/onarandombox/MultiverseCore/api/MVConfig.java | 4 ++-- .../MultiverseCore/configuration/DefaultMVConfig.java | 8 ++++---- .../MultiverseCore/listeners/MVEntityListener.java | 2 +- .../MultiverseCore/listeners/MVPlayerListener.java | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java index 8b158fc3..46029486 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java @@ -68,14 +68,14 @@ public interface MVConfig { * * @param useDefaultPortalSearch True to let Bukkit determine portal search radius on its own. */ - void setUseDefaultPortalSearch(boolean useDefaultPortalSearch); + void setUseCustomPortalSearch(boolean useDefaultPortalSearch); /** * Gets whether or not Bukkit will be determining portal search radius on its own or if Multiverse should help. * * @return True means Bukkit will use its own default values. */ - boolean isUsingDefaultPortalSearch(); + boolean isUsingCustomPortalSearch(); /** * Sets the radius at which vanilla style portals will be searched for to connect to worlds together. diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java index 17c72f20..16e6c2b4 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java @@ -126,13 +126,13 @@ public class DefaultMVConfig implements MVConfig { } @Override - public void setUseDefaultPortalSearch(boolean useDefaultPortalSearch) { - settings.set(MVConfigNodes.USE_CUSTOM_PORTAL_SEARCH, !useDefaultPortalSearch); + public void setUseCustomPortalSearch(boolean useDefaultPortalSearch) { + settings.set(MVConfigNodes.USE_CUSTOM_PORTAL_SEARCH, useDefaultPortalSearch); } @Override - public boolean isUsingDefaultPortalSearch() { - return !settings.get(MVConfigNodes.USE_CUSTOM_PORTAL_SEARCH); + public boolean isUsingCustomPortalSearch() { + return settings.get(MVConfigNodes.USE_CUSTOM_PORTAL_SEARCH); } @Override diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java index 0828bf40..d9894c56 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java @@ -115,7 +115,7 @@ public class MVEntityListener implements Listener { if (event.isCancelled() || event.getTo() == null) { return; } - if (!this.plugin.getMVConfig().isUsingDefaultPortalSearch()) { + if (this.plugin.getMVConfig().isUsingCustomPortalSearch()) { event.setSearchRadius(this.plugin.getMVConfig().getPortalSearchRadius()); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java index 3bd1f2ae..960eb517 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java @@ -292,7 +292,7 @@ public class MVPlayerListener implements Listener { + "' was allowed to go to '" + event.getTo().getWorld().getName() + "' because enforceaccess is off."); } - if (!this.plugin.getMVConfig().isUsingDefaultPortalSearch()) { + if (this.plugin.getMVConfig().isUsingCustomPortalSearch()) { event.setSearchRadius(this.plugin.getMVConfig().getPortalSearchRadius()); } } From 7759a37b4ec8105e7eded05e9eff13a4ad611bd0 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Thu, 23 Mar 2023 23:59:19 +0800 Subject: [PATCH 08/37] feat: Implement config names and basic config command --- .../MultiverseCore/MultiverseCore.java | 8 +-- .../MultiverseCore/api/MVConfig.java | 8 +++ .../commands/ConfigCommand.java | 50 +++++++++++++++ .../configuration/DefaultMVConfig.java | 27 +++++++- .../configuration/MVConfigNodes.java | 8 ++- .../utils/settings/MVSettings.java | 63 ++++++++++++++----- .../utils/settings/node/MVValueNode.java | 24 +++++-- .../utils/settings/node/NamedValueNode.java | 7 +++ .../{NodesCollection.java => NodeGroup.java} | 51 +++++++++++++-- 9 files changed, 214 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java rename src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/{NodesCollection.java => NodeGroup.java} (52%) diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index 9590c94f..631c7495 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -23,6 +23,7 @@ import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.commands.CheckCommand; import com.onarandombox.MultiverseCore.commands.CloneCommand; +import com.onarandombox.MultiverseCore.commands.ConfigCommand; import com.onarandombox.MultiverseCore.commands.ConfirmCommand; import com.onarandombox.MultiverseCore.commands.CreateCommand; import com.onarandombox.MultiverseCore.commands.DebugCommand; @@ -89,7 +90,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore { private final MVWorldManager worldManager = new SimpleMVWorldManager(this); // Configurations - private DefaultMVConfig config; + private MVConfig config; // Listeners private MVChatListener chatListener; @@ -196,6 +197,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore { this.commandManager = new MVCommandManager(this); this.commandManager.registerCommand(new CheckCommand(this)); this.commandManager.registerCommand(new CloneCommand(this)); + this.commandManager.registerCommand(new ConfigCommand(this)); this.commandManager.registerCommand(new ConfirmCommand(this)); this.commandManager.registerCommand(new CreateCommand(this)); this.commandManager.registerCommand(new DebugCommand(this)); @@ -369,9 +371,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore { */ @Override public void loadConfigs() { - config = new DefaultMVConfig(this); - config.load(); - config.save(); + config = DefaultMVConfig.init(this); this.worldManager.loadWorldConfig(new File(getDataFolder(), "worlds.yml")); diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java index 46029486..1180de5c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java @@ -1,6 +1,14 @@ package com.onarandombox.MultiverseCore.api; public interface MVConfig { + boolean load(); + + void save(); + + Object getProperty(String name); + + void setProperty(String name, Object value); + /** * Sets enforceAccess. * @param enforceAccess The new value. diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java new file mode 100644 index 00000000..2888e6c4 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java @@ -0,0 +1,50 @@ +package com.onarandombox.MultiverseCore.commands; + +import co.aikar.commands.BukkitCommandIssuer; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Optional; +import co.aikar.commands.annotation.Single; +import co.aikar.commands.annotation.Subcommand; +import co.aikar.commands.annotation.Syntax; +import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.api.MVConfig; +import org.jetbrains.annotations.NotNull; + +@CommandAlias("mv") +public class ConfigCommand extends MultiverseCoreCommand { + private final MVConfig config; + + public ConfigCommand(@NotNull MultiverseCore plugin) { + super(plugin); + this.config = plugin.getMVConfig(); + } + + @Subcommand("config") + @CommandPermission("multiverse.core.config") + @CommandCompletion("@mvconfig") //TODO + @Syntax(" [new-value]") + @Description("") //TODO + public void onConfigCommand(BukkitCommandIssuer issuer, + + @Syntax("") + @Description("") //TODO + String name, + + @Optional + @Single + @Syntax("[new-value]") + @Description("") //TODO + String value + ) { + if (value == null) { + issuer.sendMessage(name + "is currently set to " + config.getProperty(name)); + return; + } + config.setProperty(name, value); + config.save(); + issuer.sendMessage("Set " + name + " to " + value); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java index 16e6c2b4..a8988b27 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java @@ -17,6 +17,19 @@ public class DefaultMVConfig implements MVConfig { public static final String CONFIG_FILENAME = "config.yml"; public static final double CONFIG_VERSION = 5.0; + /** + * Creates a new DefaultMVConfig instance and loads the configuration automatically. + * + * @param core The MultiverseCore instance. + * @return The new DefaultMVConfig instance. + */ + public static DefaultMVConfig init(MultiverseCore core) { + var config = new DefaultMVConfig(core); + config.load(); + config.save(); + return config; + } + private final Path configPath; private final MVSettings settings; @@ -27,7 +40,7 @@ public class DefaultMVConfig implements MVConfig { settings = MVSettings.builder(configPath) .logger(Logging.getLogger()) - .defaultNodes(MVConfigNodes.getNodes()) + .nodes(MVConfigNodes.getNodes()) .migrator(ConfigMigrator.builder(MVConfigNodes.VERSION) .addVersionMigrator(VersionMigrator.builder(5.0) .addAction(MoveMigratorAction.of("multiverse-configuration.enforceaccess", "world.enforce-access")) @@ -67,14 +80,26 @@ public class DefaultMVConfig implements MVConfig { } } + @Override public boolean load() { return settings.load(); } + @Override public void save() { settings.save(); } + @Override + public Object getProperty(String name) { + return settings.get(name); + } + + @Override + public void setProperty(String name, Object value) { + settings.set(name, value); + } + @Override public void setEnforceAccess(boolean enforceAccess) { settings.set(MVConfigNodes.ENFORCE_ACCESS, enforceAccess); diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java index d6d7f91e..3ce3ebfc 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java @@ -1,16 +1,18 @@ package com.onarandombox.MultiverseCore.configuration; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import com.onarandombox.MultiverseCore.utils.settings.node.MVCommentedNode; import com.onarandombox.MultiverseCore.utils.settings.node.MVValueNode; +import com.onarandombox.MultiverseCore.utils.settings.node.NodeGroup; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; public class MVConfigNodes { - private static final List nodes = new ArrayList<>(); + private static final NodeGroup nodes = new NodeGroup(); - public static List getNodes() { + public static NodeGroup getNodes() { return nodes; } @@ -54,6 +56,7 @@ public class MVConfigNodes { .comment("If this is set to true, players will only be able to enter worlds they have") .comment("the `mv.access.` permission.") .defaultValue(false) + .name("enforce-access") .build()); public static final MVValueNode ENFORCE_GAMEMODE = node(MVValueNode.builder("world.enforce-gamemode", Boolean.class) @@ -166,5 +169,6 @@ public class MVConfigNodes { .comment("This just signifies the version number so we can see what version of config you have.") .comment("NEVER TOUCH THIS VALUE") .defaultValue(DefaultMVConfig.CONFIG_VERSION) + .name(null) .build()); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java index f983bca1..fe9898df 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java @@ -3,10 +3,11 @@ package com.onarandombox.MultiverseCore.utils.settings; import java.io.File; import java.io.IOException; import java.nio.file.Path; -import java.util.List; +import java.util.Collection; import java.util.logging.Logger; import com.onarandombox.MultiverseCore.utils.settings.migration.ConfigMigrator; +import com.onarandombox.MultiverseCore.utils.settings.node.NodeGroup; import io.github.townyadvanced.commentedconfiguration.CommentedConfiguration; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; @@ -27,7 +28,7 @@ public class MVSettings { protected final Path configPath; protected final Logger logger; - protected final List defaultNodes; + protected final NodeGroup nodes; protected final ConfigMigrator migrator; @@ -38,12 +39,12 @@ public class MVSettings { * * @param configPath The path to the configuration file. * @param logger The Logger to use for error messages. - * @param defaultNodes The default node values to add to the configuration. + * @param nodes All the node path and values for the configuration. * @param migrator The migrator to use for migrating the configuration. */ - protected MVSettings(@NotNull Path configPath, @Nullable Logger logger, @Nullable List defaultNodes, ConfigMigrator migrator) { + protected MVSettings(@NotNull Path configPath, @Nullable Logger logger, @NotNull NodeGroup nodes, ConfigMigrator migrator) { this.configPath = configPath; - this.defaultNodes = defaultNodes; + this.nodes = nodes; this.logger = logger; this.migrator = migrator; } @@ -86,6 +87,9 @@ public class MVSettings { return true; } + /** + * Migration of the configuration based on {@link ConfigMigrator}. + */ protected void migrateConfig() { migrator.migrate(this); } @@ -94,17 +98,16 @@ public class MVSettings { * Adds default node values to the configuration if they are not already present. */ protected void addDefaultNodes() { - if (defaultNodes == null || defaultNodes.isEmpty()) { + if (nodes.isEmpty()) { return; } CommentedConfiguration tempConfig = new CommentedConfiguration(configPath, logger); - for (CommentedNode node : defaultNodes) { + for (CommentedNode node : nodes) { if (node.getComments().length > 0) { tempConfig.addComment(node.getPath(), node.getComments()); } - if (node instanceof ValueNode) { - ValueNode valueNode = (ValueNode) node; + if (node instanceof ValueNode valueNode) { tempConfig.set(node.getPath(), get(valueNode)); } } @@ -133,6 +136,18 @@ public class MVSettings { return config.get(node.getPath(), node.getDefaultValue()); } + /** + * Get the value of the node by name. + * + * @param name The name of the node to get the value of. + * @return The value of the node. + */ + public Object get(@NotNull String name) { + return nodes.findNode(name) + .map(node -> (node instanceof ValueNode valueNode) ? get(valueNode) : null) + .orElse(null); + } + /** * Gets the value of a node, if the node has a default value, it will be returned if the node is not found. * @@ -166,6 +181,20 @@ public class MVSettings { config.set(node.getPath(), value); } + /** + * Set the value of the node by name. + * + * @param name The name of the node to set the value of. + * @param value The value to set. + */ + public void set(@NotNull String name, Object value) { + nodes.findNode(name).ifPresent(node -> { + if (node instanceof ValueNode valueNode) { + set(valueNode, value); + } + }); + } + /** * Sets the value of a node, if the validator is not null, it will be tested first. * @@ -177,12 +206,18 @@ public class MVSettings { config.set(node.getPath(), value); } + /** + * Sets the default value of a node. + * + * @param node The node to set the default value of. + * @param The type of the node value. + */ public void setDefault(@NotNull TypedValueNode node) { config.set(node.getPath(), node.getDefaultValue()); } /** - * Gets the configuration object. + * Gets the inner configuration object. * * @return The configuration object. */ @@ -194,7 +229,7 @@ public class MVSettings { private final Path configPath; private Logger logger; - private List defaultNodes; + private NodeGroup nodes; private ConfigMigrator migrator; @@ -215,8 +250,8 @@ public class MVSettings { return this; } - public Builder defaultNodes(@Nullable List defaultNodes) { - this.defaultNodes = defaultNodes; + public Builder nodes(@Nullable NodeGroup nodes) { + this.nodes = nodes; return this; } @@ -226,7 +261,7 @@ public class MVSettings { } public MVSettings build() { - return new MVSettings(configPath, logger, defaultNodes, migrator); + return new MVSettings(configPath, logger, nodes, migrator); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java index c96b6fe6..b97e0c84 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java @@ -1,23 +1,23 @@ package com.onarandombox.MultiverseCore.utils.settings.node; -import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public class MVValueNode extends MVCommentedNode implements TypedValueNode { +public class MVValueNode extends MVCommentedNode implements NamedValueNode { public static Builder builder(String path, Class type) { return new Builder<>(path, type); } protected final Class type; + protected final T defaultValue; + protected final String name; - private final T defaultValue; - - protected MVValueNode(String path, String[] comments, Class type, T defaultValue) { + protected MVValueNode(String path, String[] comments, Class type, T defaultValue, String name) { super(path, comments); this.type = type; this.defaultValue = defaultValue; + this.name = name; } @Override @@ -30,14 +30,21 @@ public class MVValueNode extends MVCommentedNode implements TypedValueNode return defaultValue; } + @Override + public String getName() { + return name; + } + public static class Builder> extends MVCommentedNode.Builder { protected final Class type; protected T defaultValue; + private String name; public Builder(String path, Class type) { super(path); this.type = type; + this.name = path; } public B defaultValue(T defaultValue) { @@ -45,9 +52,14 @@ public class MVValueNode extends MVCommentedNode implements TypedValueNode return (B) this; } + public B name(String name) { + this.name = name; + return (B) this; + } + @Override public MVValueNode build() { - return new MVValueNode<>(path, comments.toArray(new String[0]), type, defaultValue); + return new MVValueNode<>(path, comments.toArray(new String[0]), type, defaultValue, name); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java new file mode 100644 index 00000000..712f3418 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java @@ -0,0 +1,7 @@ +package com.onarandombox.MultiverseCore.utils.settings.node; + +import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; + +public interface NamedValueNode extends TypedValueNode { + String getName(); +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodesCollection.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodeGroup.java similarity index 52% rename from src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodesCollection.java rename to src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodeGroup.java index d06493a6..187f5d71 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodesCollection.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodeGroup.java @@ -1,18 +1,51 @@ package com.onarandombox.MultiverseCore.utils.settings.node; +import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.Iterator; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import com.dumptruckman.minecraft.util.Logging; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; import org.jetbrains.annotations.NotNull; -public class NodesCollection implements Collection { +public class NodeGroup implements Collection { private final Collection nodes; + private final Map nodesMap; - public NodesCollection(Collection nodes) { - this.nodes = nodes; + public NodeGroup() { + this.nodes = new ArrayList<>(); + this.nodesMap = new HashMap<>(); } + public NodeGroup(Collection nodes) { + this.nodes = nodes; + this.nodesMap = new HashMap<>(nodes.size()); + nodes.forEach(this::addNodeIndex); + } + + private void addNodeIndex(CommentedNode node) { + if (node instanceof NamedValueNode namedValueNode && namedValueNode.getName() != null) { + nodesMap.put(namedValueNode.getName(), node); + } + } + + private void removeNodeIndex(CommentedNode node) { + if (node instanceof NamedValueNode namedValueNode) { + nodesMap.remove(namedValueNode.getName()); + } + } + + public Collection getNames() { + return nodesMap.keySet(); + } + + public Optional findNode(String name) { + return Optional.ofNullable(nodesMap.get(name)); + } @Override public int size() { @@ -49,12 +82,20 @@ public class NodesCollection implements Collection { @Override public boolean add(CommentedNode commentedNode) { - return nodes.add(commentedNode); + if (nodes.add(commentedNode)) { + addNodeIndex(commentedNode); + return true; + } + return false; } @Override public boolean remove(Object o) { - return nodes.remove(o); + if (nodes.remove(o) && o instanceof CommentedNode) { + removeNodeIndex((CommentedNode) o); + return true; + } + return false; } @Override From d321851f0df7e80e5a45b018171b93ba04da5729 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 24 Mar 2023 00:14:16 +0800 Subject: [PATCH 09/37] fix: Donation and migration of various node path --- .../MultiverseCore/MultiverseCore.java | 14 +++++++++++--- .../configuration/DefaultMVConfig.java | 17 +++++++++-------- .../configuration/MVConfigNodes.java | 10 +++------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index 631c7495..3469b17b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -11,6 +11,7 @@ import java.io.File; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.logging.Logger; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.anchor.AnchorManager; @@ -66,10 +67,12 @@ import com.onarandombox.MultiverseCore.utils.metrics.MetricsConfigurator; import com.onarandombox.MultiverseCore.world.SimpleMVWorldManager; import com.onarandombox.MultiverseCore.world.WorldProperties; import me.main__.util.SerializationConfig.SerializationConfig; +import org.bukkit.ChatColor; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPluginLoader; +import org.jetbrains.annotations.NotNull; /** * The implementation of the Multiverse-{@link MVCore}. @@ -249,10 +252,9 @@ public class MultiverseCore extends JavaPlugin implements MVCore { */ private void logEnableMessage() { Logging.config("Version %s (API v%s) Enabled - By %s", this.getDescription().getVersion(), PROTOCOL, getAuthors()); - if (getMVConfig().isShowingDonateMessage()) { - getLogger().config("Help dumptruckman keep this project alive. Become a patron! https://www.patreon.com/dumptruckman"); - getLogger().config("One time donations are also appreciated: https://www.paypal.me/dumptruckman"); + Logging.config("Help dumptruckman keep this project alive. Become a patron! https://www.patreon.com/dumptruckman"); + Logging.config("One time donations are also appreciated: https://www.paypal.me/dumptruckman"); } } @@ -333,6 +335,12 @@ public class MultiverseCore extends JavaPlugin implements MVCore { return this.pluginCount; } + @NotNull + @Override + public Logger getLogger() { + return Logging.getLogger(); + } + /** * {@inheritDoc} */ diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java index a8988b27..5dba9bea 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java @@ -51,12 +51,13 @@ public class DefaultMVConfig implements MVConfig { //.addAction(MoveMigratorAction.of("multiverse-configuration.displaypermerrors", "")) .addAction(MoveMigratorAction.of("multiverse-configuration.globaldebug", "misc.global-debug")) .addAction(MoveMigratorAction.of("multiverse-configuration.silentstart", "misc.silent-start")) - .addAction(MoveMigratorAction.of("multiverse-configuration.firstspawnworld", "worlds.first-spawn-location")) - .addAction(MoveMigratorAction.of("multiverse-configuration.defaultportalsearch", "portals.use-custom-portal-search")) - .addAction(InvertBoolMigratorAction.of("portals.use-custom-portal-search")) - .addAction(MoveMigratorAction.of("multiverse-configuration.portalsearchradius", "portals.custom-portal-search-radius")) + .addAction(MoveMigratorAction.of("multiverse-configuration.firstspawnworld", "spawn.first-spawn-location")) + .addAction(MoveMigratorAction.of("multiverse-configuration.defaultportalsearch", "portal.use-custom-portal-search")) + .addAction(InvertBoolMigratorAction.of("portal.use-custom-portal-search")) + .addAction(MoveMigratorAction.of("multiverse-configuration.portalsearchradius", "portal.custom-portal-search-radius")) .addAction(MoveMigratorAction.of("multiverse-configuration.autopurge", "world.auto-purge-entities")) - .addAction(MoveMigratorAction.of("multiverse-configuration.idonotwanttodonate", "misc.i-dont-want-to-donate")) + .addAction(MoveMigratorAction.of("multiverse-configuration.idonotwanttodonate", "misc.show-donation-message")) + .addAction(InvertBoolMigratorAction.of("misc.show-donation-message")) .build()) .build()) .build(); @@ -211,12 +212,12 @@ public class DefaultMVConfig implements MVConfig { } @Override - public void setShowDonateMessage(boolean idonotwanttodonate) { - settings.set(MVConfigNodes.I_DONT_WANT_TO_DONATE, idonotwanttodonate); + public void setShowDonateMessage(boolean showDonateMessage) { + settings.set(MVConfigNodes.SHOW_DONATION_MESSAGE, showDonateMessage); } @Override public boolean isShowingDonateMessage() { - return settings.get(MVConfigNodes.I_DONT_WANT_TO_DONATE); + return settings.get(MVConfigNodes.SHOW_DONATION_MESSAGE); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java index 3ce3ebfc..c1c6d762 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java @@ -1,9 +1,5 @@ package com.onarandombox.MultiverseCore.configuration; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - import com.onarandombox.MultiverseCore.utils.settings.node.MVCommentedNode; import com.onarandombox.MultiverseCore.utils.settings.node.MVValueNode; import com.onarandombox.MultiverseCore.utils.settings.node.NodeGroup; @@ -157,10 +153,10 @@ public class MVConfigNodes { .defaultValue(false) .build()); - public static final MVValueNode I_DONT_WANT_TO_DONATE = node(MVValueNode.builder("misc.i-dont-want-to-donate", Boolean.class) + public static final MVValueNode SHOW_DONATION_MESSAGE = node(MVValueNode.builder("misc.show-donation-message", Boolean.class) .comment("") - .comment("If you don't want to donate, you can set this to true and Multiverse will stop nagging you.") - .defaultValue(false) + .comment("If you don't want to donate, you can set this to false and Multiverse will stop nagging you.") + .defaultValue(true) .build()); public static final MVValueNode VERSION = node(MVValueNode.builder("version", Double.class) From e3e3c039c35f791e363488800c849b38a8fb1cc3 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 24 Mar 2023 13:00:29 +0800 Subject: [PATCH 10/37] feat: Fully implement config command --- .../MultiverseCore/api/MVConfig.java | 2 +- .../commands/ConfigCommand.java | 27 ++++++++++--- .../commandtools/MVCommandCompletions.java | 2 + .../commandtools/MVCommandContexts.java | 40 +++++++++++++++++++ .../commandtools/context/MVConfigValue.java | 13 ++++++ .../configuration/DefaultMVConfig.java | 4 +- .../configuration/MVConfigNodes.java | 12 ++++++ .../utils/settings/MVSettings.java | 13 +++--- 8 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/commandtools/context/MVConfigValue.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java index 1180de5c..c3692d2c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java @@ -7,7 +7,7 @@ public interface MVConfig { Object getProperty(String name); - void setProperty(String name, Object value); + boolean setProperty(String name, Object value); /** * Sets enforceAccess. diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java index 2888e6c4..1d9246a7 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java @@ -11,6 +11,7 @@ import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Syntax; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVConfig; +import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue; import org.jetbrains.annotations.NotNull; @CommandAlias("mv") @@ -24,7 +25,7 @@ public class ConfigCommand extends MultiverseCoreCommand { @Subcommand("config") @CommandPermission("multiverse.core.config") - @CommandCompletion("@mvconfig") //TODO + @CommandCompletion("@mvconfigs") @Syntax(" [new-value]") @Description("") //TODO public void onConfigCommand(BukkitCommandIssuer issuer, @@ -37,14 +38,30 @@ public class ConfigCommand extends MultiverseCoreCommand { @Single @Syntax("[new-value]") @Description("") //TODO - String value + MVConfigValue value ) { if (value == null) { - issuer.sendMessage(name + "is currently set to " + config.getProperty(name)); + showConfigValue(issuer, name); + return; + } + updateConfigValue(issuer, name, value.getValue()); + } + + private void showConfigValue(BukkitCommandIssuer issuer, String name) { + Object currentValue = config.getProperty(name); + if (currentValue == null) { + issuer.sendMessage("No such config option: " + name); + return; + } + issuer.sendMessage(name + "is currently set to " + config.getProperty(name)); + } + + private void updateConfigValue(BukkitCommandIssuer issuer, String name, Object value) { + if (!config.setProperty(name, value)) { + issuer.sendMessage("Unable to set " + name + " to " + value); return; } - config.setProperty(name, value); config.save(); - issuer.sendMessage("Set " + name + " to " + value); + issuer.sendMessage("Successfully set " + name + " to " + value); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java index ff5671d8..70973d87 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java @@ -19,6 +19,7 @@ import com.google.common.collect.Sets; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; +import com.onarandombox.MultiverseCore.configuration.MVConfigNodes; import com.onarandombox.MultiverseCore.destination.ParsedDestination; import org.bukkit.GameRule; import org.jetbrains.annotations.NotNull; @@ -38,6 +39,7 @@ public class MVCommandCompletions extends PaperCommandCompletions { registerAsyncCompletion("destinations", this::suggestDestinations); registerAsyncCompletion("flags", this::suggestFlags); registerStaticCompletion("gamerules", this::suggestGamerules); + registerStaticCompletion("mvconfigs", MVConfigNodes.getNodes().getNames()); registerAsyncCompletion("mvworlds", this::suggestMVWorlds); setDefaultCompletion("destinations", ParsedDestination.class); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java index dc3d1a07..5a7eba2e 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java @@ -1,6 +1,7 @@ package com.onarandombox.MultiverseCore.commandtools; import java.util.HashSet; +import java.util.Optional; import java.util.Set; import co.aikar.commands.BukkitCommandExecutionContext; @@ -12,11 +13,16 @@ import com.google.common.base.Strings; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.commandtools.context.GameRuleValue; +import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue; +import com.onarandombox.MultiverseCore.configuration.MVConfigNodes; import com.onarandombox.MultiverseCore.destination.ParsedDestination; import com.onarandombox.MultiverseCore.display.filters.ContentFilter; import com.onarandombox.MultiverseCore.display.filters.DefaultContentFilter; import com.onarandombox.MultiverseCore.display.filters.RegexContentFilter; import com.onarandombox.MultiverseCore.utils.PlayerFinder; +import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; +import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; +import io.github.townyadvanced.commentedconfiguration.setting.ValueNode; import org.bukkit.GameRule; import org.bukkit.entity.Player; @@ -32,6 +38,7 @@ public class MVCommandContexts extends PaperCommandContexts { registerContext(ParsedDestination.class, this::parseDestination); registerContext(GameRule.class, this::parseGameRule); registerContext(GameRuleValue.class, this::parseGameRuleValue); + registerContext(MVConfigValue.class, this::parseMVConfigValue); registerIssuerAwareContext(MVWorld.class, this::parseMVWorld); registerIssuerAwareContext(MVWorld[].class, this::parseMVWorldArray); registerIssuerAwareContext(Player.class, this::parsePlayer); @@ -97,6 +104,39 @@ public class MVCommandContexts extends PaperCommandContexts { return new GameRuleValue(resolvedValue); } + private MVConfigValue parseMVConfigValue(BukkitCommandExecutionContext context) { + String configName = (String) context.getResolvedArg(String.class); + if (Strings.isNullOrEmpty(configName)) { + throw new InvalidCommandArgument("No config name specified."); + } + Optional node = MVConfigNodes.getNodes().findNode(configName); + if (node.isEmpty()) { + throw new InvalidCommandArgument("The config " + configName + " is not valid."); + } + + String valueString = context.getFirstArg(); + if (Strings.isNullOrEmpty(valueString)) { + throw new InvalidCommandArgument("No config value specified."); + } + + if (!(node.get() instanceof TypedValueNode)) { + context.popFirstArg(); + return new MVConfigValue(valueString); + } + + ContextResolver resolver = getResolver(((TypedValueNode) node.get()).getType()); + if (resolver == null) { + context.popFirstArg(); + return new MVConfigValue(valueString); + } + + Object resolvedValue = resolver.getContext(context); + if (resolvedValue == null) { + throw new InvalidCommandArgument("The config value " + valueString + " is not valid for config " + configName + "."); + } + return new MVConfigValue(resolvedValue); + } + private MVWorld parseMVWorld(BukkitCommandExecutionContext context) { String resolve = context.getFlagValue("resolve", ""); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/context/MVConfigValue.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/context/MVConfigValue.java new file mode 100644 index 00000000..03d2c0aa --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/context/MVConfigValue.java @@ -0,0 +1,13 @@ +package com.onarandombox.MultiverseCore.commandtools.context; + +public class MVConfigValue { + private final Object value; + + public MVConfigValue(Object value) { + this.value = value; + } + + public Object getValue() { + return value; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java index 5dba9bea..79933337 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java @@ -97,8 +97,8 @@ public class DefaultMVConfig implements MVConfig { } @Override - public void setProperty(String name, Object value) { - settings.set(name, value); + public boolean setProperty(String name, Object value) { + return settings.set(name, value); } @Override diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java index c1c6d762..5e3b97c9 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java @@ -61,12 +61,14 @@ public class MVConfigNodes { .comment("If enabled, players will be forced into the gamemode of the world they are entering, unless they have") .comment("the `mv.bypass.gamemode.` permission.") .defaultValue(true) + .name("enforce-gamemode") .build()); public static final MVValueNode AUTO_PURGE_ENTITIES = node(MVValueNode.builder("world.auto-purge-entities", Boolean.class) .comment("") .comment("Sets whether Multiverse will purge mobs and entities with be automatically.") .defaultValue(false) + .name("auto-purge-entities") .build()); public static final MVValueNode TELEPORT_INTERCEPT = node(MVValueNode.builder("world.teleport-intercept", Boolean.class) @@ -74,6 +76,7 @@ public class MVConfigNodes { .comment("If this is set to true, Multiverse will enforce access permissions for all teleportation,") .comment("including teleportation from other plugins.") .defaultValue(true) + .name("teleport-intercept") .build()); private static final MVCommentedNode SPAWN_HEADER = node(MVCommentedNode.builder("spawn") @@ -86,6 +89,7 @@ public class MVConfigNodes { .comment("If enabled, Multiverse will set the first spawn location of a world to the spawn location of the world.") .comment("If disabled, it will default to server.properties settings.") .defaultValue(true) + .name("first-spawn-override") .build()); public static final MVValueNode FIRST_SPAWN_LOCATION = node(MVValueNode.builder("spawn.first-spawn-location", String.class) @@ -93,6 +97,7 @@ public class MVConfigNodes { .comment("Sets the world that Multiverse will use as the location for players that first join the server.") .comment("This only applies if first-spawn-override is set to true.") .defaultValue("") + .name("first-spawn-location") .build()); private static final MVCommentedNode PORTAL_HEADER = node(MVCommentedNode.builder("portal") @@ -104,6 +109,7 @@ public class MVConfigNodes { .comment("This config option defines whether or not Multiverse should interfere with's Bukkit's default portal search radius.") .comment("Setting it to false would mean you want to simply let Bukkit decides the search radius itself.") .defaultValue(false) + .name("use-custom-portal-search") .build()); public static final MVValueNode CUSTOM_PORTAL_SEARCH_RADIUS = node(MVValueNode.builder("portal.custom-portal-search-radius", Integer.class) @@ -111,6 +117,7 @@ public class MVConfigNodes { .comment("This config option defines the search radius Multiverse should use when searching for a portal.") .comment("This only applies if use-custom-portal-search is set to true.") .defaultValue(128) + .name("custom-portal-search-radius") .build()); private static final MVCommentedNode MESSAGING_HEADER = node(MVCommentedNode.builder("messaging") @@ -122,6 +129,7 @@ public class MVConfigNodes { .comment("This config option defines whether or not Multiverse should prefix the chat with the world name.") .comment("This only applies if use-custom-portal-search is set to true.") .defaultValue(false) + .name("enable-chat-prefix") .build()); public static final MVValueNode CHAT_PREFIX_FORMAT = node(MVValueNode.builder("messaging.chat-prefix-format", String.class) @@ -129,6 +137,7 @@ public class MVConfigNodes { .comment("This config option defines the format Multiverse should use when prefixing the chat with the world name.") .comment("This only applies if enable-chat-prefix is set to true.") .defaultValue("[%world%]%chat%") + .name("chat-prefix-format") .build()); private static final MVCommentedNode MISC_HEADER = node(MVCommentedNode.builder("misc") @@ -145,18 +154,21 @@ public class MVConfigNodes { .comment(" 2 = finer") .comment(" 3 = finest") .defaultValue(0) + .name("global-debug") .build()); public static final MVValueNode SILENT_START = node(MVValueNode.builder("misc.silent-start", Boolean.class) .comment("") .comment("If true, the startup console messages will no longer show.") .defaultValue(false) + .name("silent-start") .build()); public static final MVValueNode SHOW_DONATION_MESSAGE = node(MVValueNode.builder("misc.show-donation-message", Boolean.class) .comment("") .comment("If you don't want to donate, you can set this to false and Multiverse will stop nagging you.") .defaultValue(true) + .name("show-donation-message") .build()); public static final MVValueNode VERSION = node(MVValueNode.builder("version", Double.class) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java index fe9898df..10cef4d4 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java @@ -177,8 +177,9 @@ public class MVSettings { * @param node The node to set the value of. * @param value The value to set. */ - public void set(@NotNull ValueNode node, Object value) { + public boolean set(@NotNull ValueNode node, Object value) { config.set(node.getPath(), value); + return true; } /** @@ -187,12 +188,10 @@ public class MVSettings { * @param name The name of the node to set the value of. * @param value The value to set. */ - public void set(@NotNull String name, Object value) { - nodes.findNode(name).ifPresent(node -> { - if (node instanceof ValueNode valueNode) { - set(valueNode, value); - } - }); + public boolean set(@NotNull String name, Object value) { + return nodes.findNode(name) + .map(node -> node instanceof ValueNode valueNode && set(valueNode, value)) + .orElse(false); } /** From 433160f1878452099b62fe7bf550b903a3fdfc78 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 24 Mar 2023 14:12:26 +0800 Subject: [PATCH 11/37] fix: Java 11 compatibility with instanceof pattern --- .../MultiverseCore/utils/settings/MVSettings.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java index 10cef4d4..0c6a7e58 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java @@ -107,8 +107,8 @@ public class MVSettings { if (node.getComments().length > 0) { tempConfig.addComment(node.getPath(), node.getComments()); } - if (node instanceof ValueNode valueNode) { - tempConfig.set(node.getPath(), get(valueNode)); + if (node instanceof ValueNode) { + tempConfig.set(node.getPath(), get((ValueNode) node)); } } @@ -144,7 +144,7 @@ public class MVSettings { */ public Object get(@NotNull String name) { return nodes.findNode(name) - .map(node -> (node instanceof ValueNode valueNode) ? get(valueNode) : null) + .map(node -> (node instanceof ValueNode) ? get((ValueNode) node) : null) .orElse(null); } @@ -190,7 +190,7 @@ public class MVSettings { */ public boolean set(@NotNull String name, Object value) { return nodes.findNode(name) - .map(node -> node instanceof ValueNode valueNode && set(valueNode, value)) + .map(node -> node instanceof ValueNode && set((ValueNode) node, value)) .orElse(false); } From 136bd3cbe290690fb1cfa2cc84549f9011e1d7fc Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 24 Mar 2023 14:16:03 +0800 Subject: [PATCH 12/37] fix: Clean up config migration logging --- .../utils/settings/migration/ConfigMigrator.java | 3 +-- .../utils/settings/migration/MoveMigratorAction.java | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java index c8ca6d98..3f0c9419 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java @@ -23,9 +23,8 @@ public class ConfigMigrator { public void migrate(MVSettings settings) { double versionNumber = settings.get(versionNode); for (VersionMigrator versionMigrator : versionMigrators) { - Logging.info("Checking if config needs to be migrated from version " + versionNumber + " to " + versionMigrator.getVersion()); if (versionNumber < versionMigrator.getVersion()) { - Logging.info("Migrating config from version " + versionNumber + " to " + versionMigrator.getVersion()); + Logging.config("Migrating config from version %s to %s...", versionNumber, versionMigrator.getVersion()); versionMigrator.migrate(settings); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java index a8c8e39b..10f2ed43 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java @@ -20,12 +20,11 @@ public class MoveMigratorAction implements MigratorAction { @Override public void migrate(MVSettings settings) { - Logging.info(String.valueOf(settings.getConfig().get(fromPath))); Optional.ofNullable(settings.getConfig().get(fromPath)) .ifPresent(value -> { - Logging.info("Moving " + fromPath + " to " + toPath); settings.getConfig().set(toPath, value); settings.getConfig().set(fromPath, null); + Logging.config("Moved path %s to %s", fromPath, toPath); }); } } From 2edf95533268186ebbc55b4b9f31ca9493ab93aa Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 24 Mar 2023 14:39:58 +0800 Subject: [PATCH 13/37] refactor: Rename config methods to align with new structure --- .../MultiverseCore/MultiverseCore.java | 5 +- .../MultiverseCore/api/MVConfig.java | 51 +++++++++++++++---- .../configuration/DefaultMVConfig.java | 26 +++++++--- .../listeners/MVChatListener.java | 2 +- .../listeners/MVEntityListener.java | 2 +- .../listeners/MVPlayerListener.java | 2 +- .../MultiverseCore/world/SimpleMVWorld.java | 2 +- .../world/SimpleMVWorldManager.java | 2 +- .../MultiverseCore/TestWorldProperties.java | 8 +-- 9 files changed, 71 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index 3469b17b..2b9484ba 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -67,7 +67,6 @@ import com.onarandombox.MultiverseCore.utils.metrics.MetricsConfigurator; import com.onarandombox.MultiverseCore.world.SimpleMVWorldManager; import com.onarandombox.MultiverseCore.world.WorldProperties; import me.main__.util.SerializationConfig.SerializationConfig; -import org.bukkit.ChatColor; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -148,10 +147,10 @@ public class MultiverseCore extends JavaPlugin implements MVCore { this.worldManager.loadWorlds(true); // Now set the firstspawnworld (after the worlds are loaded): - this.worldManager.setFirstSpawnWorld(getMVConfig().getFirstSpawnWorld()); + this.worldManager.setFirstSpawnWorld(getMVConfig().getFirstSpawnLocation()); MVWorld firstSpawnWorld = this.worldManager.getFirstSpawnWorld(); if (firstSpawnWorld != null) { - getMVConfig().setFirstSpawnWorld(firstSpawnWorld.getName()); + getMVConfig().setFirstSpawnLocation(firstSpawnWorld.getName()); } //Setup economy here so vault is loaded diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java index c3692d2c..2e946cf4 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java @@ -1,16 +1,37 @@ package com.onarandombox.MultiverseCore.api; public interface MVConfig { + + /** + * Loads the config from disk. + * @return True if the config was loaded successfully. + */ boolean load(); + /** + * Saves the config to disk. + */ void save(); + /** + * Gets a property from the config. + * + * @param name The name of the property. + * @return The value of the property. + */ Object getProperty(String name); + /** + * Sets a property in the config. + * + * @param name The name of the property. + * @param value The value of the property. + * @return True if the property was set successfully. + */ boolean setProperty(String name, Object value); /** - * Sets enforceAccess. + * Sets world access permissions should be enforced. * @param enforceAccess The new value. */ void setEnforceAccess(boolean enforceAccess); @@ -21,19 +42,31 @@ public interface MVConfig { */ boolean getEnforceAccess(); + /** + * Sets whether the game mode should be enforced. + * @param enforceGameMode The new value. + */ + void setEnforceGameMode(boolean enforceGameMode); + + /** + * Gets enforceGameMode value. + * @return True if game mode should be enforced. + */ + boolean getEnforceGameMode(); + /** * Sets whether or not the automatic purge of entities is enabled. * * @param autopurge True if automatic purge should be enabled. */ - void setAutoPurgeEnabled(boolean autopurge); + void setAutoPurgeEntities(boolean autopurge); /** * Gets whether or not the automatic purge of entities is enabled. * * @return True if automatic purge is enabled. */ - boolean isAutoPurgeEnabled(); + boolean isAutoPurgeEntities(); /** * Sets teleportIntercept. @@ -63,13 +96,13 @@ public interface MVConfig { * Sets firstSpawnWorld. * @param firstSpawnWorld The new value. */ - void setFirstSpawnWorld(String firstSpawnWorld); + void setFirstSpawnLocation(String firstSpawnWorld); /** * Gets firstSpawnWorld. * @return firstSpawnWorld. */ - String getFirstSpawnWorld(); + String getFirstSpawnLocation(); /** * Sets whether or not to let Bukkit determine portal search radius on its own or if Multiverse should give input. @@ -90,26 +123,26 @@ public interface MVConfig { * * @param searchRadius The portal search radius. */ - void setPortalSearchRadius(int searchRadius); + void setCustomPortalSearchRadius(int searchRadius); /** * Gets the radius at which vanilla style portals will be searched for to connect to worlds together. * * @return The portal search radius. */ - int getPortalSearchRadius(); + int getCustomPortalSearchRadius(); /** * Sets prefixChat. * @param prefixChat The new value. */ - void setPrefixChat(boolean prefixChat); + void setEnablePrefixChat(boolean prefixChat); /** * Gets prefixChat. * @return prefixChat. */ - boolean getPrefixChat(); + boolean isEnablePrefixChat(); /** * Sets prefixChatFormat. diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java index 79933337..fa154b62 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java @@ -112,12 +112,22 @@ public class DefaultMVConfig implements MVConfig { } @Override - public void setAutoPurgeEnabled(boolean autopurge) { + public void setEnforceGameMode(boolean enforceGameMode) { + settings.set(MVConfigNodes.ENFORCE_GAMEMODE, enforceGameMode); + } + + @Override + public boolean getEnforceGameMode() { + return settings.get(MVConfigNodes.ENFORCE_GAMEMODE); + } + + @Override + public void setAutoPurgeEntities(boolean autopurge) { settings.set(MVConfigNodes.AUTO_PURGE_ENTITIES, autopurge); } @Override - public boolean isAutoPurgeEnabled() { + public boolean isAutoPurgeEntities() { return settings.get(MVConfigNodes.AUTO_PURGE_ENTITIES); } @@ -142,12 +152,12 @@ public class DefaultMVConfig implements MVConfig { } @Override - public void setFirstSpawnWorld(String firstSpawnWorld) { + public void setFirstSpawnLocation(String firstSpawnWorld) { settings.set(MVConfigNodes.FIRST_SPAWN_LOCATION, firstSpawnWorld); } @Override - public String getFirstSpawnWorld() { + public String getFirstSpawnLocation() { return settings.get(MVConfigNodes.FIRST_SPAWN_LOCATION); } @@ -162,22 +172,22 @@ public class DefaultMVConfig implements MVConfig { } @Override - public void setPortalSearchRadius(int searchRadius) { + public void setCustomPortalSearchRadius(int searchRadius) { settings.set(MVConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS, searchRadius); } @Override - public int getPortalSearchRadius() { + public int getCustomPortalSearchRadius() { return settings.get(MVConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS); } @Override - public void setPrefixChat(boolean prefixChat) { + public void setEnablePrefixChat(boolean prefixChat) { settings.set(MVConfigNodes.ENABLE_CHAT_PREFIX, prefixChat); } @Override - public boolean getPrefixChat() { + public boolean isEnablePrefixChat() { return settings.get(MVConfigNodes.ENABLE_CHAT_PREFIX); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVChatListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVChatListener.java index 78df0ecc..83672d55 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVChatListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVChatListener.java @@ -34,7 +34,7 @@ public class MVChatListener implements Listener { } // Check whether the Server is set to prefix the chat with the World name. // If not we do nothing, if so we need to check if the World has an Alias. - if (plugin.getMVConfig().getPrefixChat()) { + if (plugin.getMVConfig().isEnablePrefixChat()) { String world = playerListener.getPlayerWorld().get(event.getPlayer().getName()); if (world == null) { world = event.getPlayer().getWorld().getName(); diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java index d9894c56..bdf922da 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java @@ -116,7 +116,7 @@ public class MVEntityListener implements Listener { return; } if (this.plugin.getMVConfig().isUsingCustomPortalSearch()) { - event.setSearchRadius(this.plugin.getMVConfig().getPortalSearchRadius()); + event.setSearchRadius(this.plugin.getMVConfig().getCustomPortalSearchRadius()); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java index 960eb517..d9d06470 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java @@ -293,7 +293,7 @@ public class MVPlayerListener implements Listener { + "' because enforceaccess is off."); } if (this.plugin.getMVConfig().isUsingCustomPortalSearch()) { - event.setSearchRadius(this.plugin.getMVConfig().getPortalSearchRadius()); + event.setSearchRadius(this.plugin.getMVConfig().getCustomPortalSearchRadius()); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java index 8df6ed58..20783801 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java @@ -281,7 +281,7 @@ public class SimpleMVWorld implements MVWorld { } world.setSpawnFlags(allowMonsters, allowAnimals); } - if (plugin.getMVConfig().isAutoPurgeEnabled()) { + if (plugin.getMVConfig().isAutoPurgeEntities()) { plugin.getMVWorldManager().getTheWorldPurger().purgeWorld(SimpleMVWorld.this); } return super.validateChange(property, newValue, oldValue, object); diff --git a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java index 188d44d5..be12dfa8 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java @@ -481,7 +481,7 @@ public class SimpleMVWorldManager implements MVWorldManager { return false; } SimpleMVWorld world = new SimpleMVWorld(plugin, cbworld, mvworld); - if (plugin.getMVConfig().isAutoPurgeEnabled()) { + if (plugin.getMVConfig().isAutoPurgeEntities()) { this.worldPurger.purgeWorld(world); } this.worlds.put(worldName, world); diff --git a/src/old-test/java/com/onarandombox/MultiverseCore/TestWorldProperties.java b/src/old-test/java/com/onarandombox/MultiverseCore/TestWorldProperties.java index 4c7c701f..9211d680 100644 --- a/src/old-test/java/com/onarandombox/MultiverseCore/TestWorldProperties.java +++ b/src/old-test/java/com/onarandombox/MultiverseCore/TestWorldProperties.java @@ -177,10 +177,10 @@ public class TestWorldProperties { assertFalse(thunderChangeOnEvent.isCancelled()); // call player chat event - core.getMVConfig().setPrefixChat(true); + core.getMVConfig().setEnablePrefixChat(true); core.getChatListener().playerChat(playerChatEvent); verify(playerChatEvent).setFormat("[" + mvWorld.getColoredWorldString() + "]" + "format"); - core.getMVConfig().setPrefixChat(false); + core.getMVConfig().setEnablePrefixChat(false); core.getChatListener().playerChat(playerChatEvent); verify(playerChatEvent, times(1)).setFormat(anyString()); // only ONE TIME (not the 2nd time!) @@ -271,7 +271,7 @@ public class TestWorldProperties { assertTrue(thunderChangeOnEvent.isCancelled()); // call player chat event - core.getMVConfig().setPrefixChat(true); + core.getMVConfig().setEnablePrefixChat(true); core.getChatListener().playerChat(playerChatEvent); // never because it's hidden! verify(playerChatEvent, never()).setFormat( @@ -279,7 +279,7 @@ public class TestWorldProperties { mvWorld.setHidden(false); core.getChatListener().playerChat(playerChatEvent); verify(playerChatEvent).setFormat("[" + mvWorld.getColoredWorldString() + "]" + "format"); - core.getMVConfig().setPrefixChat(false); + core.getMVConfig().setEnablePrefixChat(false); core.getChatListener().playerChat(playerChatEvent); verify(playerChatEvent, times(1)).setFormat(anyString()); // only ONE TIME (not the 2nd time!) mvWorld.setHidden(true); // reset hidden-state From d126b3d31a0ba21b1a84d29f0b88f9f70436c467 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 24 Mar 2023 15:47:12 +0800 Subject: [PATCH 14/37] feat: Add config for papi hook --- .../MultiverseCore/MultiverseCore.java | 3 ++- .../onarandombox/MultiverseCore/api/MVConfig.java | 14 ++++++++++++++ .../configuration/DefaultMVConfig.java | 10 ++++++++++ .../configuration/MVConfigNodes.java | 8 ++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index 2b9484ba..2b4591fe 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -258,8 +258,9 @@ public class MultiverseCore extends JavaPlugin implements MVCore { } private void setupPlaceholderAPI() { - if(getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) { + if(config.isRegisterPapiHook() && getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) { new MultiverseCorePlaceholders(this).register(); + Logging.config("Registered PlaceholderAPI hook."); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java index 2e946cf4..de27948b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java @@ -1,5 +1,7 @@ package com.onarandombox.MultiverseCore.api; +import com.onarandombox.MultiverseCore.placeholders.MultiverseCorePlaceholders; + public interface MVConfig { /** @@ -156,6 +158,18 @@ public interface MVConfig { */ String getPrefixChatFormat(); + /** + * Sets whether to register the {@link MultiverseCorePlaceholders} class with PlaceholderAPI plugin. + * @param registerPapiHook The new value. + */ + void setRegisterPapiHook(boolean registerPapiHook); + + /** + * Gets whether to register the {@link MultiverseCorePlaceholders} class with PlaceholderAPI plugin. + * @return registerPapiHook. + */ + boolean isRegisterPapiHook(); + /** * Sets globalDebug. * @param globalDebug The new value. diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java index fa154b62..5376cf4c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java @@ -201,6 +201,16 @@ public class DefaultMVConfig implements MVConfig { return settings.get(MVConfigNodes.CHAT_PREFIX_FORMAT); } + @Override + public void setRegisterPapiHook(boolean registerPapiHook) { + settings.set(MVConfigNodes.REGISTER_PAPI_HOOK, registerPapiHook); + } + + @Override + public boolean isRegisterPapiHook() { + return settings.get(MVConfigNodes.REGISTER_PAPI_HOOK); + } + @Override public void setGlobalDebug(int globalDebug) { settings.set(MVConfigNodes.GLOBAL_DEBUG, globalDebug); diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java index 5e3b97c9..e92956d6 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java @@ -140,6 +140,14 @@ public class MVConfigNodes { .name("chat-prefix-format") .build()); + public static final MVValueNode REGISTER_PAPI_HOOK = node(MVValueNode.builder("messaging.register-papi-hook", Boolean.class) + .comment("") + .comment("This config option defines whether or not Multiverse should register the PlaceholderAPI hook.") + .comment("This only applies if PlaceholderAPI is installed.") + .defaultValue(true) + .name("register-papi-hook") + .build()); + private static final MVCommentedNode MISC_HEADER = node(MVCommentedNode.builder("misc") .comment("") .comment("") From cb5877b20693de52283f6bed2f25ff1abcc6a06c Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:09:10 +0800 Subject: [PATCH 15/37] docs: Add docstrings to methods implemented --- .../utils/settings/MVSettings.java | 51 ++++++++++++++++++- .../settings/migration/ConfigMigrator.java | 34 ++++++++++++- .../migration/InvertBoolMigratorAction.java | 15 +++++- .../settings/migration/MigratorAction.java | 9 ++++ .../migration/MoveMigratorAction.java | 16 +++++- .../settings/migration/VersionMigrator.java | 39 ++++++++++++++ .../utils/settings/node/MVCommentedNode.java | 9 ++++ .../utils/settings/node/MVValueNode.java | 15 +++++- .../utils/settings/node/NamedValueNode.java | 8 ++- .../utils/settings/node/NodeGroup.java | 22 +++++--- 10 files changed, 205 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java index 0c6a7e58..936e292e 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java @@ -3,7 +3,6 @@ package com.onarandombox.MultiverseCore.utils.settings; import java.io.File; import java.io.IOException; import java.nio.file.Path; -import java.util.Collection; import java.util.logging.Logger; import com.onarandombox.MultiverseCore.utils.settings.migration.ConfigMigrator; @@ -16,6 +15,9 @@ import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +/** + * A class that makes use of CommentedConfiguration to provide a simple way to load and save with node objects. + */ public class MVSettings { public static Builder builder(String configPath) { @@ -122,6 +124,11 @@ public class MVSettings { config.save(); } + /** + * Checks if the configuration is loaded. + * + * @return True if the configuration is loaded, false otherwise. + */ public boolean isLoaded() { return config != null; } @@ -224,6 +231,9 @@ public class MVSettings { return config; } + /** + * Gets the path of the configuration file. + */ public static class Builder { private final Path configPath; @@ -232,33 +242,72 @@ public class MVSettings { private ConfigMigrator migrator; + /** + * Creates a new builder. + * + * @param configPath The path of the configuration file. + */ public Builder(String configPath) { this.configPath = Path.of(configPath); } + /** + * Creates a new builder. + * + * @param configPath The path of the configuration file. + */ public Builder(Path configPath) { this.configPath = configPath; } + /** + * Sets the logger to use. + * + * @param plugin The plugin to get the logger from. + * @return The builder. + */ public Builder logger(@NotNull Plugin plugin) { return logger(plugin.getLogger()); } + /** + * Sets the logger to use. + * + * @param logger The logger to use. + * @return The builder. + */ public Builder logger(@Nullable Logger logger) { this.logger = logger; return this; } + /** + * Sets the nodes to use. + * + * @param nodes The nodes to use. + * @return The builder. + */ public Builder nodes(@Nullable NodeGroup nodes) { this.nodes = nodes; return this; } + /** + * Sets the migrator to use. + * + * @param migrator The migrator to use. + * @return The builder. + */ public Builder migrator(@Nullable ConfigMigrator migrator) { this.migrator = migrator; return this; } + /** + * Builds the settings. + * + * @return The built settings. + */ public MVSettings build() { return new MVSettings(configPath, logger, nodes, migrator); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java index 3f0c9419..cbe88aa5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java @@ -7,7 +7,18 @@ import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.utils.settings.MVSettings; import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; +/** + * Helper class for migrating configs to the latest config version. + */ public class ConfigMigrator { + + /** + * Creates a new builder for a ConfigMigrator. + * + * @param versionNode The node that stores the version number of the config. + * Default value should be the current latest version number. + * @return The builder instance. + */ public static Builder builder(TypedValueNode versionNode) { return new Builder(versionNode); } @@ -15,11 +26,16 @@ public class ConfigMigrator { private final TypedValueNode versionNode; private final List versionMigrators; - public ConfigMigrator(TypedValueNode versionNode, List versionMigrators) { + protected ConfigMigrator(TypedValueNode versionNode, List versionMigrators) { this.versionNode = versionNode; this.versionMigrators = versionMigrators; } + /** + * Migrates the config to the latest version if necessary. + * + * @param settings The target settings instance to migrate. + */ public void migrate(MVSettings settings) { double versionNumber = settings.get(versionNode); for (VersionMigrator versionMigrator : versionMigrators) { @@ -28,10 +44,13 @@ public class ConfigMigrator { versionMigrator.migrate(settings); } } - + // Set the version number to the latest version number settings.setDefault(versionNode); } + /** + * A builder for a ConfigMigrator. + */ public static class Builder { private final TypedValueNode versionNode; private final List versionMigrators; @@ -47,11 +66,22 @@ public class ConfigMigrator { this.versionMigrators = new ArrayList<>(); } + /** + * Adds a version migrator to the list of migrators. + * + * @param versionMigrator The migrator to add. + * @return The builder instance. + */ public Builder addVersionMigrator(VersionMigrator versionMigrator) { versionMigrators.add(versionMigrator); return this; } + /** + * Builds the ConfigMigrator. + * + * @return The built ConfigMigrator. + */ public ConfigMigrator build() { return new ConfigMigrator(versionNode, versionMigrators); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/InvertBoolMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/InvertBoolMigratorAction.java index ccd76d99..ad8546bf 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/InvertBoolMigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/InvertBoolMigratorAction.java @@ -2,17 +2,30 @@ package com.onarandombox.MultiverseCore.utils.settings.migration; import com.onarandombox.MultiverseCore.utils.settings.MVSettings; +/** + * Single migrator action that inverts a boolean value for a given path. + */ public class InvertBoolMigratorAction implements MigratorAction { + + /** + * Creates a new migrator action that inverts a boolean value for a given path. + * + * @param path The path to invert value of. + * @return The new migrator action. + */ public static InvertBoolMigratorAction of(String path) { return new InvertBoolMigratorAction(path); } private final String path; - public InvertBoolMigratorAction(String path) { + protected InvertBoolMigratorAction(String path) { this.path = path; } + /** + * {@InheritDoc} + */ @Override public void migrate(MVSettings settings) { settings.getConfig().set(path, !settings.getConfig().getBoolean(path)); diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigratorAction.java index 84b560c7..d1e180f7 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigratorAction.java @@ -2,6 +2,15 @@ package com.onarandombox.MultiverseCore.utils.settings.migration; import com.onarandombox.MultiverseCore.utils.settings.MVSettings; +/** + * A migrator action is a single action that is performed when migrating a config. + */ public interface MigratorAction { + + /** + * Performs the migration action. + * + * @param settings The target settings instance to migrate. + */ void migrate(MVSettings settings); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java index 10f2ed43..8ac99a14 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java @@ -5,7 +5,18 @@ import java.util.Optional; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.utils.settings.MVSettings; +/** + * Single migrator action that moves a value from one path to another. + */ public class MoveMigratorAction implements MigratorAction { + + /** + * Creates a new migrator action that moves a value from one path to another. + * + * @param fromPath The path to move value from. + * @param toPath The path to move value to. + * @return The new migrator action. + */ public static MoveMigratorAction of(String fromPath, String toPath) { return new MoveMigratorAction(fromPath, toPath); } @@ -13,11 +24,14 @@ public class MoveMigratorAction implements MigratorAction { private final String fromPath; private final String toPath; - public MoveMigratorAction(String fromPath, String toPath) { + protected MoveMigratorAction(String fromPath, String toPath) { this.fromPath = fromPath; this.toPath = toPath; } + /** + * {@InheritDoc} + */ @Override public void migrate(MVSettings settings) { Optional.ofNullable(settings.getConfig().get(fromPath)) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/VersionMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/VersionMigrator.java index 3796a3d0..4b3de046 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/VersionMigrator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/VersionMigrator.java @@ -5,7 +5,17 @@ import java.util.List; import com.onarandombox.MultiverseCore.utils.settings.MVSettings; +/** + * A version migrator is a collection of migrator actions that are performed when migrating a config to a specific version. + */ public class VersionMigrator { + + /** + * Creates a new builder for a VersionMigrator. + * + * @param version The version number of the config that this migrator migrates to. + * @return The builder instance. + */ public static Builder builder(double version) { return new Builder(version); } @@ -18,27 +28,56 @@ public class VersionMigrator { this.actions = actions; } + /** + * Performs all the migrator actions. + * + * @param settings The target settings instance to migrate. + */ public void migrate(MVSettings settings) { actions.forEach(action -> action.migrate(settings)); } + /** + * Gets the version number of the config that this migrator migrates to. + * + * @return The version number. + */ public double getVersion() { return version; } + /** + * A builder for a VersionMigrator. + */ public static class Builder { private final double version; private final List actions = new ArrayList<>(); + /** + * Creates a new builder for a VersionMigrator. + * + * @param version The version number of the config that this migrator migrates to. + */ public Builder(double version) { this.version = version; } + /** + * Adds a migrator action to the list of actions. + * + * @param action The action to add. + * @return The builder instance. + */ public Builder addAction(MigratorAction action) { actions.add(action); return this; } + /** + * Builds the VersionMigrator. + * + * @return The built VersionMigrator. + */ public VersionMigrator build() { return new VersionMigrator(version, actions); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java index 8c06d15d..124d3efa 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java @@ -6,6 +6,9 @@ import java.util.List; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; import org.jetbrains.annotations.NotNull; +/** + * Implementation of {@link CommentedNode} that allows for comments to be added to the node. + */ public class MVCommentedNode implements CommentedNode { public static Builder builder(String path) { @@ -20,11 +23,17 @@ public class MVCommentedNode implements CommentedNode { this.comments = comments; } + /** + * {@InheritDoc} + */ @Override public @NotNull String getPath() { return path; } + /** + * {@InheritDoc} + */ @Override public @NotNull String[] getComments() { return comments; diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java index b97e0c84..7ac1ce8b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java @@ -3,6 +3,10 @@ package com.onarandombox.MultiverseCore.utils.settings.node; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +/** + * Implementation of {@link NamedValueNode}. + * @param The type of the value. + */ public class MVValueNode extends MVCommentedNode implements NamedValueNode { public static Builder builder(String path, Class type) { @@ -20,18 +24,27 @@ public class MVValueNode extends MVCommentedNode implements NamedValueNode this.name = name; } + /** + * {@InheritDoc} + */ @Override public @NotNull Class getType() { return type; } + /** + * {@InheritDoc} + */ @Override public @Nullable T getDefaultValue() { return defaultValue; } + /** + * {@InheritDoc} + */ @Override - public String getName() { + public @Nullable String getName() { return name; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java index 712f3418..f6c7abe1 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java @@ -1,7 +1,13 @@ package com.onarandombox.MultiverseCore.utils.settings.node; import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; +import org.jetbrains.annotations.Nullable; public interface NamedValueNode extends TypedValueNode { - String getName(); + /** + * Gets the name of this node. Used for identifying the node from user input. + * + * @return The name of this node. + */ + @Nullable String getName(); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodeGroup.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodeGroup.java index 187f5d71..0cc1373f 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodeGroup.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodeGroup.java @@ -6,12 +6,13 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Optional; -import java.util.Set; -import com.dumptruckman.minecraft.util.Logging; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; import org.jetbrains.annotations.NotNull; +/** + * A collection of {@link CommentedNode}s, with mappings to nodes by name. + */ public class NodeGroup implements Collection { private final Collection nodes; private final Map nodesMap; @@ -39,10 +40,21 @@ public class NodeGroup implements Collection { } } + /** + * Gets the names of all nodes in this group. + * + * @return The names of all nodes in this group. + */ public Collection getNames() { return nodesMap.keySet(); } + /** + * Gets the node with the given name. + * + * @param name The name of the node to get. + * @return The node with the given name, or {@link Optional#empty()} if no node with the given name exists. + */ public Optional findNode(String name) { return Optional.ofNullable(nodesMap.get(name)); } @@ -68,15 +80,13 @@ public class NodeGroup implements Collection { return nodes.iterator(); } - @NotNull @Override - public Object[] toArray() { + public Object @NotNull [] toArray() { return nodes.toArray(); } - @NotNull @Override - public T[] toArray(@NotNull T[] ts) { + public T @NotNull [] toArray(@NotNull T[] ts) { return nodes.toArray(ts); } From c8ae27894e7a88a053a18c6fbd41cf6ce7d3b4be Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:20:03 +0800 Subject: [PATCH 16/37] refactor: Change name to optional and fix inheritDoc typo --- .../utils/settings/node/MVValueNode.java | 18 ++++++++++-------- .../utils/settings/node/NamedValueNode.java | 4 +++- .../utils/settings/node/NodeGroup.java | 10 +++++----- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java index 7ac1ce8b..e7fcea3f 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java @@ -1,5 +1,7 @@ package com.onarandombox.MultiverseCore.utils.settings.node; +import java.util.Optional; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -25,7 +27,7 @@ public class MVValueNode extends MVCommentedNode implements NamedValueNode } /** - * {@InheritDoc} + * {@inheritDoc} */ @Override public @NotNull Class getType() { @@ -33,7 +35,7 @@ public class MVValueNode extends MVCommentedNode implements NamedValueNode } /** - * {@InheritDoc} + * {@inheritDoc} */ @Override public @Nullable T getDefaultValue() { @@ -41,11 +43,11 @@ public class MVValueNode extends MVCommentedNode implements NamedValueNode } /** - * {@InheritDoc} + * {@inheritDoc} */ @Override - public @Nullable String getName() { - return name; + public Optional getName() { + return Optional.ofNullable(name); } public static class Builder> extends MVCommentedNode.Builder { @@ -54,18 +56,18 @@ public class MVValueNode extends MVCommentedNode implements NamedValueNode protected T defaultValue; private String name; - public Builder(String path, Class type) { + public Builder(@NotNull String path, @NotNull Class type) { super(path); this.type = type; this.name = path; } - public B defaultValue(T defaultValue) { + public B defaultValue(@NotNull T defaultValue) { this.defaultValue = defaultValue; return (B) this; } - public B name(String name) { + public B name(@Nullable String name) { this.name = name; return (B) this; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java index f6c7abe1..080186bc 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java @@ -1,5 +1,7 @@ package com.onarandombox.MultiverseCore.utils.settings.node; +import java.util.Optional; + import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; import org.jetbrains.annotations.Nullable; @@ -9,5 +11,5 @@ public interface NamedValueNode extends TypedValueNode { * * @return The name of this node. */ - @Nullable String getName(); + Optional getName(); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodeGroup.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodeGroup.java index 0cc1373f..1f33cfe0 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodeGroup.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodeGroup.java @@ -29,14 +29,14 @@ public class NodeGroup implements Collection { } private void addNodeIndex(CommentedNode node) { - if (node instanceof NamedValueNode namedValueNode && namedValueNode.getName() != null) { - nodesMap.put(namedValueNode.getName(), node); + if (node instanceof NamedValueNode) { + ((NamedValueNode) node).getName().ifPresent(name -> nodesMap.put(name, node)); } } private void removeNodeIndex(CommentedNode node) { - if (node instanceof NamedValueNode namedValueNode) { - nodesMap.remove(namedValueNode.getName()); + if (node instanceof NamedValueNode) { + ((NamedValueNode) node).getName().ifPresent(nodesMap::remove); } } @@ -86,7 +86,7 @@ public class NodeGroup implements Collection { } @Override - public T @NotNull [] toArray(@NotNull T[] ts) { + public T @NotNull [] toArray(T @NotNull [] ts) { return nodes.toArray(ts); } From ba2be243629d2e1f4f568bf6891ada61eceba1ad Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:20:56 +0800 Subject: [PATCH 17/37] docs: Fix inheritDoc more typo --- .../utils/settings/migration/InvertBoolMigratorAction.java | 2 +- .../utils/settings/migration/MoveMigratorAction.java | 2 +- .../MultiverseCore/utils/settings/node/MVCommentedNode.java | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/InvertBoolMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/InvertBoolMigratorAction.java index ad8546bf..6b301b37 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/InvertBoolMigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/InvertBoolMigratorAction.java @@ -24,7 +24,7 @@ public class InvertBoolMigratorAction implements MigratorAction { } /** - * {@InheritDoc} + * {@inheritDoc} */ @Override public void migrate(MVSettings settings) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java index 8ac99a14..dffdaa6a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java @@ -30,7 +30,7 @@ public class MoveMigratorAction implements MigratorAction { } /** - * {@InheritDoc} + * {@inheritDoc} */ @Override public void migrate(MVSettings settings) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java index 124d3efa..af4ccb19 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java @@ -24,7 +24,7 @@ public class MVCommentedNode implements CommentedNode { } /** - * {@InheritDoc} + * {@inheritDoc} */ @Override public @NotNull String getPath() { @@ -32,7 +32,7 @@ public class MVCommentedNode implements CommentedNode { } /** - * {@InheritDoc} + * {@inheritDoc} */ @Override public @NotNull String[] getComments() { From 274a7ed82bb0915f55b7f012aa5ba8dfe912c01c Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:26:38 +0800 Subject: [PATCH 18/37] docs: Add more docstrings to methods implemented --- .../utils/settings/node/MVCommentedNode.java | 29 ++++++++++++++- .../utils/settings/node/MVValueNode.java | 37 ++++++++++++++++++- .../utils/settings/node/NamedValueNode.java | 6 ++- 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java index af4ccb19..0a6b0582 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java @@ -11,7 +11,13 @@ import org.jetbrains.annotations.NotNull; */ public class MVCommentedNode implements CommentedNode { - public static Builder builder(String path) { + /** + * Creates a new builder for a {@link MVCommentedNode}. + * + * @param path The path of the node. + * @return The new builder. + */ + public static Builder builder(String path) { return new Builder<>(path); } @@ -39,15 +45,31 @@ public class MVCommentedNode implements CommentedNode { return comments; } + /** + * Builder for {@link MVCommentedNode}. + * + * @param The type of the builder. + */ public static class Builder { protected final String path; protected final List comments; + /** + * Creates a new builder for a {@link MVCommentedNode}. + * + * @param path The path of the node. + */ protected Builder(String path) { this.path = path; this.comments = new ArrayList<>(); } + /** + * Adds a comment line to the node. + * + * @param comment The comment to add. + * @return This builder. + */ public B comment(@NotNull String comment) { if (!comment.isEmpty() && !comment.trim().startsWith("#")) { // Automatically add a comment prefix if the comment doesn't start with one. @@ -57,6 +79,11 @@ public class MVCommentedNode implements CommentedNode { return (B) this; } + /** + * Builds the node. + * + * @return The built node. + */ public MVCommentedNode build() { return new MVCommentedNode(path, comments.toArray(new String[0])); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java index e7fcea3f..3783d496 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java @@ -11,6 +11,14 @@ import org.jetbrains.annotations.Nullable; */ public class MVValueNode extends MVCommentedNode implements NamedValueNode { + /** + * Creates a new builder for a {@link MVValueNode}. + * + * @param path The path of the node. + * @param type The type of the value. + * @return The new builder. + * @param The type of the value. + */ public static Builder builder(String path, Class type) { return new Builder<>(path, type); } @@ -50,28 +58,55 @@ public class MVValueNode extends MVCommentedNode implements NamedValueNode return Optional.ofNullable(name); } + /** + * Builder for {@link MVValueNode}. + * + * @param The type of the value. + * @param The type of the builder. + */ public static class Builder> extends MVCommentedNode.Builder { protected final Class type; protected T defaultValue; private String name; - public Builder(@NotNull String path, @NotNull Class type) { + /** + * Creates a new builder. + * + * @param path The path of the node. + * @param type The type of the value. + */ + protected Builder(@NotNull String path, @NotNull Class type) { super(path); this.type = type; this.name = path; } + /** + * Sets the default value for this node. + * + * @param defaultValue The default value. + * @return This builder. + */ public B defaultValue(@NotNull T defaultValue) { this.defaultValue = defaultValue; return (B) this; } + /** + * Sets the name of this node. Used for identifying the node from user input. + * + * @param name The name of this node. + * @return This builder. + */ public B name(@Nullable String name) { this.name = name; return (B) this; } + /** + * {@inheritDoc} + */ @Override public MVValueNode build() { return new MVValueNode<>(path, comments.toArray(new String[0]), type, defaultValue, name); diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java index 080186bc..0c8891b7 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java @@ -3,8 +3,12 @@ package com.onarandombox.MultiverseCore.utils.settings.node; import java.util.Optional; import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; -import org.jetbrains.annotations.Nullable; +/** + * A {@link TypedValueNode} that has a name. + * + * @param The type of the node's value. + */ public interface NamedValueNode extends TypedValueNode { /** * Gets the name of this node. Used for identifying the node from user input. From 3ccdfd7cf590a637d604d4eb1bd394e40395b326 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:32:10 +0800 Subject: [PATCH 19/37] refactor: Move to configuration package and rename MVSettings -> ConfigHandle --- .../ConfigHandle.java} | 14 ++-- .../configuration/DefaultMVConfig.java | 77 +++++++++---------- .../configuration/MVConfigNodes.java | 6 +- .../migration/ConfigMigrator.java | 6 +- .../migration/InvertBoolMigratorAction.java | 6 +- .../migration/MigratorAction.java | 6 +- .../migration/MoveMigratorAction.java | 6 +- .../migration/VersionMigrator.java | 6 +- .../node/MVCommentedNode.java | 2 +- .../node/MVValueNode.java | 2 +- .../node/NamedValueNode.java | 2 +- .../node/NodeGroup.java | 2 +- 12 files changed, 67 insertions(+), 68 deletions(-) rename src/main/java/com/onarandombox/MultiverseCore/{utils/settings/MVSettings.java => configuration/ConfigHandle.java} (94%) rename src/main/java/com/onarandombox/MultiverseCore/{utils/settings => configuration}/migration/ConfigMigrator.java (94%) rename src/main/java/com/onarandombox/MultiverseCore/{utils/settings => configuration}/migration/InvertBoolMigratorAction.java (79%) rename src/main/java/com/onarandombox/MultiverseCore/{utils/settings => configuration}/migration/MigratorAction.java (59%) rename src/main/java/com/onarandombox/MultiverseCore/{utils/settings => configuration}/migration/MoveMigratorAction.java (86%) rename src/main/java/com/onarandombox/MultiverseCore/{utils/settings => configuration}/migration/VersionMigrator.java (92%) rename src/main/java/com/onarandombox/MultiverseCore/{utils/settings => configuration}/node/MVCommentedNode.java (97%) rename src/main/java/com/onarandombox/MultiverseCore/{utils/settings => configuration}/node/MVValueNode.java (97%) rename src/main/java/com/onarandombox/MultiverseCore/{utils/settings => configuration}/node/NamedValueNode.java (88%) rename src/main/java/com/onarandombox/MultiverseCore/{utils/settings => configuration}/node/NodeGroup.java (98%) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java similarity index 94% rename from src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java rename to src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java index 936e292e..75151ac9 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/MVSettings.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java @@ -1,12 +1,12 @@ -package com.onarandombox.MultiverseCore.utils.settings; +package com.onarandombox.MultiverseCore.configuration; import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.util.logging.Logger; -import com.onarandombox.MultiverseCore.utils.settings.migration.ConfigMigrator; -import com.onarandombox.MultiverseCore.utils.settings.node.NodeGroup; +import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator; +import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; import io.github.townyadvanced.commentedconfiguration.CommentedConfiguration; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; @@ -18,7 +18,7 @@ import org.jetbrains.annotations.Nullable; /** * A class that makes use of CommentedConfiguration to provide a simple way to load and save with node objects. */ -public class MVSettings { +public class ConfigHandle { public static Builder builder(String configPath) { return new Builder(configPath); @@ -44,7 +44,7 @@ public class MVSettings { * @param nodes All the node path and values for the configuration. * @param migrator The migrator to use for migrating the configuration. */ - protected MVSettings(@NotNull Path configPath, @Nullable Logger logger, @NotNull NodeGroup nodes, ConfigMigrator migrator) { + protected ConfigHandle(@NotNull Path configPath, @Nullable Logger logger, @NotNull NodeGroup nodes, ConfigMigrator migrator) { this.configPath = configPath; this.nodes = nodes; this.logger = logger; @@ -308,8 +308,8 @@ public class MVSettings { * * @return The built settings. */ - public MVSettings build() { - return new MVSettings(configPath, logger, nodes, migrator); + public ConfigHandle build() { + return new ConfigHandle(configPath, logger, nodes, migrator); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java index 5376cf4c..0fda3753 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java @@ -7,11 +7,10 @@ import java.nio.file.Path; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVConfig; -import com.onarandombox.MultiverseCore.utils.settings.MVSettings; -import com.onarandombox.MultiverseCore.utils.settings.migration.ConfigMigrator; -import com.onarandombox.MultiverseCore.utils.settings.migration.InvertBoolMigratorAction; -import com.onarandombox.MultiverseCore.utils.settings.migration.MoveMigratorAction; -import com.onarandombox.MultiverseCore.utils.settings.migration.VersionMigrator; +import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator; +import com.onarandombox.MultiverseCore.configuration.migration.InvertBoolMigratorAction; +import com.onarandombox.MultiverseCore.configuration.migration.MoveMigratorAction; +import com.onarandombox.MultiverseCore.configuration.migration.VersionMigrator; public class DefaultMVConfig implements MVConfig { public static final String CONFIG_FILENAME = "config.yml"; @@ -31,14 +30,14 @@ public class DefaultMVConfig implements MVConfig { } private final Path configPath; - private final MVSettings settings; + private final ConfigHandle configHandle; public DefaultMVConfig(MultiverseCore core) { configPath = Path.of(core.getDataFolder().getPath(), CONFIG_FILENAME); migrateFromOldConfigFile(); - settings = MVSettings.builder(configPath) + configHandle = ConfigHandle.builder(configPath) .logger(Logging.getLogger()) .nodes(MVConfigNodes.getNodes()) .migrator(ConfigMigrator.builder(MVConfigNodes.VERSION) @@ -83,161 +82,161 @@ public class DefaultMVConfig implements MVConfig { @Override public boolean load() { - return settings.load(); + return configHandle.load(); } @Override public void save() { - settings.save(); + configHandle.save(); } @Override public Object getProperty(String name) { - return settings.get(name); + return configHandle.get(name); } @Override public boolean setProperty(String name, Object value) { - return settings.set(name, value); + return configHandle.set(name, value); } @Override public void setEnforceAccess(boolean enforceAccess) { - settings.set(MVConfigNodes.ENFORCE_ACCESS, enforceAccess); + configHandle.set(MVConfigNodes.ENFORCE_ACCESS, enforceAccess); } @Override public boolean getEnforceAccess() { - return settings.get(MVConfigNodes.ENFORCE_ACCESS); + return configHandle.get(MVConfigNodes.ENFORCE_ACCESS); } @Override public void setEnforceGameMode(boolean enforceGameMode) { - settings.set(MVConfigNodes.ENFORCE_GAMEMODE, enforceGameMode); + configHandle.set(MVConfigNodes.ENFORCE_GAMEMODE, enforceGameMode); } @Override public boolean getEnforceGameMode() { - return settings.get(MVConfigNodes.ENFORCE_GAMEMODE); + return configHandle.get(MVConfigNodes.ENFORCE_GAMEMODE); } @Override public void setAutoPurgeEntities(boolean autopurge) { - settings.set(MVConfigNodes.AUTO_PURGE_ENTITIES, autopurge); + configHandle.set(MVConfigNodes.AUTO_PURGE_ENTITIES, autopurge); } @Override public boolean isAutoPurgeEntities() { - return settings.get(MVConfigNodes.AUTO_PURGE_ENTITIES); + return configHandle.get(MVConfigNodes.AUTO_PURGE_ENTITIES); } @Override public void setTeleportIntercept(boolean teleportIntercept) { - settings.set(MVConfigNodes.TELEPORT_INTERCEPT, teleportIntercept); + configHandle.set(MVConfigNodes.TELEPORT_INTERCEPT, teleportIntercept); } @Override public boolean getTeleportIntercept() { - return settings.get(MVConfigNodes.TELEPORT_INTERCEPT); + return configHandle.get(MVConfigNodes.TELEPORT_INTERCEPT); } @Override public void setFirstSpawnOverride(boolean firstSpawnOverride) { - settings.set(MVConfigNodes.FIRST_SPAWN_OVERRIDE, firstSpawnOverride); + configHandle.set(MVConfigNodes.FIRST_SPAWN_OVERRIDE, firstSpawnOverride); } @Override public boolean getFirstSpawnOverride() { - return settings.get(MVConfigNodes.FIRST_SPAWN_OVERRIDE); + return configHandle.get(MVConfigNodes.FIRST_SPAWN_OVERRIDE); } @Override public void setFirstSpawnLocation(String firstSpawnWorld) { - settings.set(MVConfigNodes.FIRST_SPAWN_LOCATION, firstSpawnWorld); + configHandle.set(MVConfigNodes.FIRST_SPAWN_LOCATION, firstSpawnWorld); } @Override public String getFirstSpawnLocation() { - return settings.get(MVConfigNodes.FIRST_SPAWN_LOCATION); + return configHandle.get(MVConfigNodes.FIRST_SPAWN_LOCATION); } @Override public void setUseCustomPortalSearch(boolean useDefaultPortalSearch) { - settings.set(MVConfigNodes.USE_CUSTOM_PORTAL_SEARCH, useDefaultPortalSearch); + configHandle.set(MVConfigNodes.USE_CUSTOM_PORTAL_SEARCH, useDefaultPortalSearch); } @Override public boolean isUsingCustomPortalSearch() { - return settings.get(MVConfigNodes.USE_CUSTOM_PORTAL_SEARCH); + return configHandle.get(MVConfigNodes.USE_CUSTOM_PORTAL_SEARCH); } @Override public void setCustomPortalSearchRadius(int searchRadius) { - settings.set(MVConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS, searchRadius); + configHandle.set(MVConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS, searchRadius); } @Override public int getCustomPortalSearchRadius() { - return settings.get(MVConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS); + return configHandle.get(MVConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS); } @Override public void setEnablePrefixChat(boolean prefixChat) { - settings.set(MVConfigNodes.ENABLE_CHAT_PREFIX, prefixChat); + configHandle.set(MVConfigNodes.ENABLE_CHAT_PREFIX, prefixChat); } @Override public boolean isEnablePrefixChat() { - return settings.get(MVConfigNodes.ENABLE_CHAT_PREFIX); + return configHandle.get(MVConfigNodes.ENABLE_CHAT_PREFIX); } @Override public void setPrefixChatFormat(String prefixChatFormat) { - settings.set(MVConfigNodes.CHAT_PREFIX_FORMAT, prefixChatFormat); + configHandle.set(MVConfigNodes.CHAT_PREFIX_FORMAT, prefixChatFormat); } @Override public String getPrefixChatFormat() { - return settings.get(MVConfigNodes.CHAT_PREFIX_FORMAT); + return configHandle.get(MVConfigNodes.CHAT_PREFIX_FORMAT); } @Override public void setRegisterPapiHook(boolean registerPapiHook) { - settings.set(MVConfigNodes.REGISTER_PAPI_HOOK, registerPapiHook); + configHandle.set(MVConfigNodes.REGISTER_PAPI_HOOK, registerPapiHook); } @Override public boolean isRegisterPapiHook() { - return settings.get(MVConfigNodes.REGISTER_PAPI_HOOK); + return configHandle.get(MVConfigNodes.REGISTER_PAPI_HOOK); } @Override public void setGlobalDebug(int globalDebug) { - settings.set(MVConfigNodes.GLOBAL_DEBUG, globalDebug); + configHandle.set(MVConfigNodes.GLOBAL_DEBUG, globalDebug); } @Override public int getGlobalDebug() { - return settings.get(MVConfigNodes.GLOBAL_DEBUG); + return configHandle.get(MVConfigNodes.GLOBAL_DEBUG); } @Override public void setSilentStart(boolean silentStart) { - settings.set(MVConfigNodes.SILENT_START, silentStart); + configHandle.set(MVConfigNodes.SILENT_START, silentStart); } @Override public boolean getSilentStart() { - return settings.get(MVConfigNodes.SILENT_START); + return configHandle.get(MVConfigNodes.SILENT_START); } @Override public void setShowDonateMessage(boolean showDonateMessage) { - settings.set(MVConfigNodes.SHOW_DONATION_MESSAGE, showDonateMessage); + configHandle.set(MVConfigNodes.SHOW_DONATION_MESSAGE, showDonateMessage); } @Override public boolean isShowingDonateMessage() { - return settings.get(MVConfigNodes.SHOW_DONATION_MESSAGE); + return configHandle.get(MVConfigNodes.SHOW_DONATION_MESSAGE); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java index e92956d6..b5149fdf 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java @@ -1,8 +1,8 @@ package com.onarandombox.MultiverseCore.configuration; -import com.onarandombox.MultiverseCore.utils.settings.node.MVCommentedNode; -import com.onarandombox.MultiverseCore.utils.settings.node.MVValueNode; -import com.onarandombox.MultiverseCore.utils.settings.node.NodeGroup; +import com.onarandombox.MultiverseCore.configuration.node.MVCommentedNode; +import com.onarandombox.MultiverseCore.configuration.node.MVValueNode; +import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; public class MVConfigNodes { diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/ConfigMigrator.java similarity index 94% rename from src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java rename to src/main/java/com/onarandombox/MultiverseCore/configuration/migration/ConfigMigrator.java index cbe88aa5..7e842c12 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/ConfigMigrator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/ConfigMigrator.java @@ -1,10 +1,10 @@ -package com.onarandombox.MultiverseCore.utils.settings.migration; +package com.onarandombox.MultiverseCore.configuration.migration; import java.util.ArrayList; import java.util.List; import com.dumptruckman.minecraft.util.Logging; -import com.onarandombox.MultiverseCore.utils.settings.MVSettings; +import com.onarandombox.MultiverseCore.configuration.ConfigHandle; import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; /** @@ -36,7 +36,7 @@ public class ConfigMigrator { * * @param settings The target settings instance to migrate. */ - public void migrate(MVSettings settings) { + public void migrate(ConfigHandle settings) { double versionNumber = settings.get(versionNode); for (VersionMigrator versionMigrator : versionMigrators) { if (versionNumber < versionMigrator.getVersion()) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/InvertBoolMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/InvertBoolMigratorAction.java similarity index 79% rename from src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/InvertBoolMigratorAction.java rename to src/main/java/com/onarandombox/MultiverseCore/configuration/migration/InvertBoolMigratorAction.java index 6b301b37..c9022688 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/InvertBoolMigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/InvertBoolMigratorAction.java @@ -1,6 +1,6 @@ -package com.onarandombox.MultiverseCore.utils.settings.migration; +package com.onarandombox.MultiverseCore.configuration.migration; -import com.onarandombox.MultiverseCore.utils.settings.MVSettings; +import com.onarandombox.MultiverseCore.configuration.ConfigHandle; /** * Single migrator action that inverts a boolean value for a given path. @@ -27,7 +27,7 @@ public class InvertBoolMigratorAction implements MigratorAction { * {@inheritDoc} */ @Override - public void migrate(MVSettings settings) { + public void migrate(ConfigHandle settings) { settings.getConfig().set(path, !settings.getConfig().getBoolean(path)); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/MigratorAction.java similarity index 59% rename from src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigratorAction.java rename to src/main/java/com/onarandombox/MultiverseCore/configuration/migration/MigratorAction.java index d1e180f7..1f3fdbe8 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/MigratorAction.java @@ -1,6 +1,6 @@ -package com.onarandombox.MultiverseCore.utils.settings.migration; +package com.onarandombox.MultiverseCore.configuration.migration; -import com.onarandombox.MultiverseCore.utils.settings.MVSettings; +import com.onarandombox.MultiverseCore.configuration.ConfigHandle; /** * A migrator action is a single action that is performed when migrating a config. @@ -12,5 +12,5 @@ public interface MigratorAction { * * @param settings The target settings instance to migrate. */ - void migrate(MVSettings settings); + void migrate(ConfigHandle settings); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/MoveMigratorAction.java similarity index 86% rename from src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java rename to src/main/java/com/onarandombox/MultiverseCore/configuration/migration/MoveMigratorAction.java index dffdaa6a..662d5685 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/MoveMigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/MoveMigratorAction.java @@ -1,9 +1,9 @@ -package com.onarandombox.MultiverseCore.utils.settings.migration; +package com.onarandombox.MultiverseCore.configuration.migration; import java.util.Optional; import com.dumptruckman.minecraft.util.Logging; -import com.onarandombox.MultiverseCore.utils.settings.MVSettings; +import com.onarandombox.MultiverseCore.configuration.ConfigHandle; /** * Single migrator action that moves a value from one path to another. @@ -33,7 +33,7 @@ public class MoveMigratorAction implements MigratorAction { * {@inheritDoc} */ @Override - public void migrate(MVSettings settings) { + public void migrate(ConfigHandle settings) { Optional.ofNullable(settings.getConfig().get(fromPath)) .ifPresent(value -> { settings.getConfig().set(toPath, value); diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/VersionMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/VersionMigrator.java similarity index 92% rename from src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/VersionMigrator.java rename to src/main/java/com/onarandombox/MultiverseCore/configuration/migration/VersionMigrator.java index 4b3de046..e0aacbfd 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/migration/VersionMigrator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/VersionMigrator.java @@ -1,9 +1,9 @@ -package com.onarandombox.MultiverseCore.utils.settings.migration; +package com.onarandombox.MultiverseCore.configuration.migration; import java.util.ArrayList; import java.util.List; -import com.onarandombox.MultiverseCore.utils.settings.MVSettings; +import com.onarandombox.MultiverseCore.configuration.ConfigHandle; /** * A version migrator is a collection of migrator actions that are performed when migrating a config to a specific version. @@ -33,7 +33,7 @@ public class VersionMigrator { * * @param settings The target settings instance to migrate. */ - public void migrate(MVSettings settings) { + public void migrate(ConfigHandle settings) { actions.forEach(action -> action.migrate(settings)); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVCommentedNode.java similarity index 97% rename from src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java rename to src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVCommentedNode.java index 0a6b0582..eaf2637c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVCommentedNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVCommentedNode.java @@ -1,4 +1,4 @@ -package com.onarandombox.MultiverseCore.utils.settings.node; +package com.onarandombox.MultiverseCore.configuration.node; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVValueNode.java similarity index 97% rename from src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java rename to src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVValueNode.java index 3783d496..adf9e94d 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/MVValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVValueNode.java @@ -1,4 +1,4 @@ -package com.onarandombox.MultiverseCore.utils.settings.node; +package com.onarandombox.MultiverseCore.configuration.node; import java.util.Optional; diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NamedValueNode.java similarity index 88% rename from src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java rename to src/main/java/com/onarandombox/MultiverseCore/configuration/node/NamedValueNode.java index 0c8891b7..69f80b1d 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NamedValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NamedValueNode.java @@ -1,4 +1,4 @@ -package com.onarandombox.MultiverseCore.utils.settings.node; +package com.onarandombox.MultiverseCore.configuration.node; import java.util.Optional; diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodeGroup.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NodeGroup.java similarity index 98% rename from src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodeGroup.java rename to src/main/java/com/onarandombox/MultiverseCore/configuration/node/NodeGroup.java index 1f33cfe0..919c4889 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/settings/node/NodeGroup.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NodeGroup.java @@ -1,4 +1,4 @@ -package com.onarandombox.MultiverseCore.utils.settings.node; +package com.onarandombox.MultiverseCore.configuration.node; import java.util.ArrayList; import java.util.Collection; From 358404b407ce14c02e9ae9473e417359f93c7e34 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Fri, 24 Mar 2023 23:42:49 +0800 Subject: [PATCH 20/37] feat: Add a runnable when value is set. --- .../configuration/ConfigHandle.java | 59 +++++++++++++++---- .../configuration/MVConfigNodes.java | 3 + ...ValueNode.java => EnchancedValueNode.java} | 4 +- .../configuration/node/MVValueNode.java | 24 ++++++-- .../configuration/node/NodeGroup.java | 8 +-- 5 files changed, 76 insertions(+), 22 deletions(-) rename src/main/java/com/onarandombox/MultiverseCore/configuration/node/{NamedValueNode.java => EnchancedValueNode.java} (79%) diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java index 75151ac9..3293e4af 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java @@ -6,6 +6,7 @@ import java.nio.file.Path; import java.util.logging.Logger; import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator; +import com.onarandombox.MultiverseCore.configuration.node.EnchancedValueNode; import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; import io.github.townyadvanced.commentedconfiguration.CommentedConfiguration; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; @@ -20,10 +21,22 @@ import org.jetbrains.annotations.Nullable; */ public class ConfigHandle { + /** + * A builder class for creating a ConfigHandle. + * + * @param configPath The path to the configuration file in string. + * @return A new Builder instance. + */ public static Builder builder(String configPath) { return new Builder(configPath); } + /** + * A builder class for creating a ConfigHandle. + * + * @param configPath The path to the configuration file. + * @return A new Builder instance. + */ public static Builder builder(Path configPath) { return new Builder(configPath); } @@ -178,17 +191,6 @@ public class ConfigHandle { return config.getObject(node.getPath(), node.getType(), node.getDefaultValue()); } - /** - * Sets the value of a node, if the validator is not null, it will be tested first. - * - * @param node The node to set the value of. - * @param value The value to set. - */ - public boolean set(@NotNull ValueNode node, Object value) { - config.set(node.getPath(), value); - return true; - } - /** * Set the value of the node by name. * @@ -201,6 +203,20 @@ public class ConfigHandle { .orElse(false); } + /** + * Sets the value of a node, if the validator is not null, it will be tested first. + * + * @param node The node to set the value of. + * @param value The value to set. + */ + public boolean set(@NotNull ValueNode node, Object value) { + if (node instanceof TypedValueNode) { + return set(node, value); + } + config.set(node.getPath(), value); + return true; + } + /** * Sets the value of a node, if the validator is not null, it will be tested first. * @@ -208,8 +224,27 @@ public class ConfigHandle { * @param value The value to set. * @param The type of the node value. */ - public void set(@NotNull TypedValueNode node, T value) { + public boolean set(@NotNull TypedValueNode node, T value) { + if (node instanceof EnchancedValueNode) { + return set((EnchancedValueNode) node, value); + } config.set(node.getPath(), value); + return true; + } + + /** + * Sets the value of a node, if the validator is not null, it will be tested first. + * + * @param node The node to set the value of. + * @param value The value to set. + * @return True if the value was set, false otherwise. + * @param The type of the node value. + */ + public boolean set(@NotNull EnchancedValueNode node, T value) { + T oldValue = get(node); + config.set(node.getPath(), value); + node.onSetValue(oldValue, get(node)); + return true; } /** diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java index b5149fdf..8b64f5e2 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java @@ -1,5 +1,6 @@ package com.onarandombox.MultiverseCore.configuration; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.configuration.node.MVCommentedNode; import com.onarandombox.MultiverseCore.configuration.node.MVValueNode; import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; @@ -163,6 +164,7 @@ public class MVConfigNodes { .comment(" 3 = finest") .defaultValue(0) .name("global-debug") + .onSetValue((oldValue, newValue) -> Logging.setDebugLevel(newValue)) .build()); public static final MVValueNode SILENT_START = node(MVValueNode.builder("misc.silent-start", Boolean.class) @@ -170,6 +172,7 @@ public class MVConfigNodes { .comment("If true, the startup console messages will no longer show.") .defaultValue(false) .name("silent-start") + .onSetValue((oldValue, newValue) -> Logging.setShowingConfig(!newValue)) .build()); public static final MVValueNode SHOW_DONATION_MESSAGE = node(MVValueNode.builder("misc.show-donation-message", Boolean.class) diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NamedValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/EnchancedValueNode.java similarity index 79% rename from src/main/java/com/onarandombox/MultiverseCore/configuration/node/NamedValueNode.java rename to src/main/java/com/onarandombox/MultiverseCore/configuration/node/EnchancedValueNode.java index 69f80b1d..1f3f5420 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NamedValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/EnchancedValueNode.java @@ -9,11 +9,13 @@ import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; * * @param The type of the node's value. */ -public interface NamedValueNode extends TypedValueNode { +public interface EnchancedValueNode extends TypedValueNode { /** * Gets the name of this node. Used for identifying the node from user input. * * @return The name of this node. */ Optional getName(); + + void onSetValue(T oldValue, T newValue); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVValueNode.java index adf9e94d..d5aa1acd 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVValueNode.java @@ -1,15 +1,16 @@ package com.onarandombox.MultiverseCore.configuration.node; import java.util.Optional; +import java.util.function.BiConsumer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** - * Implementation of {@link NamedValueNode}. + * Implementation of {@link EnchancedValueNode}. * @param The type of the value. */ -public class MVValueNode extends MVCommentedNode implements NamedValueNode { +public class MVValueNode extends MVCommentedNode implements EnchancedValueNode { /** * Creates a new builder for a {@link MVValueNode}. @@ -26,12 +27,14 @@ public class MVValueNode extends MVCommentedNode implements NamedValueNode protected final Class type; protected final T defaultValue; protected final String name; + protected final BiConsumer onSetValue; - protected MVValueNode(String path, String[] comments, Class type, T defaultValue, String name) { + protected MVValueNode(String path, String[] comments, Class type, T defaultValue, String name, BiConsumer onSetValue) { super(path, comments); this.type = type; this.defaultValue = defaultValue; this.name = name; + this.onSetValue = onSetValue; } /** @@ -58,6 +61,11 @@ public class MVValueNode extends MVCommentedNode implements NamedValueNode return Optional.ofNullable(name); } + @Override + public void onSetValue(T oldValue, T newValue) { + onSetValue.accept(oldValue, newValue); + } + /** * Builder for {@link MVValueNode}. * @@ -68,7 +76,8 @@ public class MVValueNode extends MVCommentedNode implements NamedValueNode protected final Class type; protected T defaultValue; - private String name; + protected String name; + protected BiConsumer onSetValue; /** * Creates a new builder. @@ -104,12 +113,17 @@ public class MVValueNode extends MVCommentedNode implements NamedValueNode return (B) this; } + public B onSetValue(@Nullable BiConsumer onSetValue) { + this.onSetValue = onSetValue; + return (B) this; + } + /** * {@inheritDoc} */ @Override public MVValueNode build() { - return new MVValueNode<>(path, comments.toArray(new String[0]), type, defaultValue, name); + return new MVValueNode<>(path, comments.toArray(new String[0]), type, defaultValue, name, onSetValue); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NodeGroup.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NodeGroup.java index 919c4889..be7ead94 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NodeGroup.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NodeGroup.java @@ -29,14 +29,14 @@ public class NodeGroup implements Collection { } private void addNodeIndex(CommentedNode node) { - if (node instanceof NamedValueNode) { - ((NamedValueNode) node).getName().ifPresent(name -> nodesMap.put(name, node)); + if (node instanceof EnchancedValueNode) { + ((EnchancedValueNode) node).getName().ifPresent(name -> nodesMap.put(name, node)); } } private void removeNodeIndex(CommentedNode node) { - if (node instanceof NamedValueNode) { - ((NamedValueNode) node).getName().ifPresent(nodesMap::remove); + if (node instanceof EnchancedValueNode) { + ((EnchancedValueNode) node).getName().ifPresent(nodesMap::remove); } } From cd260b0f5ee280589fc09398af0ff37ddf86c3d1 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Sun, 26 Mar 2023 00:31:36 +0800 Subject: [PATCH 21/37] feat: Fully implement working migration and validation --- .../configuration/ConfigHandle.java | 47 +++++++++++-------- .../configuration/DefaultMVConfig.java | 12 +++++ .../configuration/MVConfigNodes.java | 2 + .../migration/BooleanMigratorAction.java | 28 +++++++++++ .../migration/ConfigMigrator.java | 2 +- .../migration/IntegerMigratorAction.java | 28 +++++++++++ .../migration/InvertBoolMigratorAction.java | 5 +- ...dValueNode.java => EnhancedValueNode.java} | 12 ++++- .../configuration/node/MVValueNode.java | 41 ++++++++++++++-- .../configuration/node/NodeGroup.java | 8 ++-- 10 files changed, 152 insertions(+), 33 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/configuration/migration/BooleanMigratorAction.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/configuration/migration/IntegerMigratorAction.java rename src/main/java/com/onarandombox/MultiverseCore/configuration/node/{EnchancedValueNode.java => EnhancedValueNode.java} (55%) diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java index 3293e4af..05888ac3 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java @@ -5,8 +5,9 @@ import java.io.IOException; import java.nio.file.Path; import java.util.logging.Logger; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator; -import com.onarandombox.MultiverseCore.configuration.node.EnchancedValueNode; +import com.onarandombox.MultiverseCore.configuration.node.EnhancedValueNode; import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; import io.github.townyadvanced.commentedconfiguration.CommentedConfiguration; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; @@ -78,7 +79,7 @@ public class ConfigHandle { return false; } migrateConfig(); - addDefaultNodes(); + parseAllNodes(); return true; } @@ -112,22 +113,26 @@ public class ConfigHandle { /** * Adds default node values to the configuration if they are not already present. */ - protected void addDefaultNodes() { - if (nodes.isEmpty()) { - return; - } - - CommentedConfiguration tempConfig = new CommentedConfiguration(configPath, logger); + protected void parseAllNodes() { + CommentedConfiguration oldConfig = config; + this.config = new CommentedConfiguration(configPath, logger); for (CommentedNode node : nodes) { + Logging.config("Parsing node: %s", node.getPath()); if (node.getComments().length > 0) { - tempConfig.addComment(node.getPath(), node.getComments()); + config.addComment(node.getPath(), node.getComments()); } - if (node instanceof ValueNode) { - tempConfig.set(node.getPath(), get((ValueNode) node)); + if (node instanceof TypedValueNode typedNode) { + if (!set(typedNode, oldConfig.getObject(node.getPath(), typedNode.getType(), typedNode.getDefaultValue()))) { + Logging.warning("Invalid value for node: %s, resetting to default...", node.getPath()); + setDefault(typedNode); + } + } else if (node instanceof ValueNode valueNode) { + if (!set(valueNode, oldConfig.get(node.getPath(), valueNode.getDefaultValue()))) { + Logging.warning("Invalid value for node: %s, resetting to default...", node.getPath()); + setDefault(valueNode); + } } } - - this.config = tempConfig; } /** @@ -210,8 +215,8 @@ public class ConfigHandle { * @param value The value to set. */ public boolean set(@NotNull ValueNode node, Object value) { - if (node instanceof TypedValueNode) { - return set(node, value); + if (node instanceof TypedValueNode typedValueNode) { + return set(typedValueNode, value); } config.set(node.getPath(), value); return true; @@ -225,8 +230,8 @@ public class ConfigHandle { * @param The type of the node value. */ public boolean set(@NotNull TypedValueNode node, T value) { - if (node instanceof EnchancedValueNode) { - return set((EnchancedValueNode) node, value); + if (node instanceof EnhancedValueNode enhancedValueNode) { + return set(enhancedValueNode, value); } config.set(node.getPath(), value); return true; @@ -240,7 +245,10 @@ public class ConfigHandle { * @return True if the value was set, false otherwise. * @param The type of the node value. */ - public boolean set(@NotNull EnchancedValueNode node, T value) { + public boolean set(@NotNull EnhancedValueNode node, T value) { + if (!node.isValid(value)) { + return false; + } T oldValue = get(node); config.set(node.getPath(), value); node.onSetValue(oldValue, get(node)); @@ -251,9 +259,8 @@ public class ConfigHandle { * Sets the default value of a node. * * @param node The node to set the default value of. - * @param The type of the node value. */ - public void setDefault(@NotNull TypedValueNode node) { + public void setDefault(@NotNull ValueNode node) { config.set(node.getPath(), node.getDefaultValue()); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java index 0fda3753..11e336a8 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java @@ -7,7 +7,9 @@ import java.nio.file.Path; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVConfig; +import com.onarandombox.MultiverseCore.configuration.migration.BooleanMigratorAction; import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator; +import com.onarandombox.MultiverseCore.configuration.migration.IntegerMigratorAction; import com.onarandombox.MultiverseCore.configuration.migration.InvertBoolMigratorAction; import com.onarandombox.MultiverseCore.configuration.migration.MoveMigratorAction; import com.onarandombox.MultiverseCore.configuration.migration.VersionMigrator; @@ -43,19 +45,29 @@ public class DefaultMVConfig implements MVConfig { .migrator(ConfigMigrator.builder(MVConfigNodes.VERSION) .addVersionMigrator(VersionMigrator.builder(5.0) .addAction(MoveMigratorAction.of("multiverse-configuration.enforceaccess", "world.enforce-access")) + .addAction(BooleanMigratorAction.of("world.enforce-access")) .addAction(MoveMigratorAction.of("multiverse-configuration.prefixchat", "messaging.enable-chat-prefix")) + .addAction(BooleanMigratorAction.of("messaging.enable-chat-prefix")) .addAction(MoveMigratorAction.of("multiverse-configuration.prefixchatformat", "messaging.chat-prefix-format")) .addAction(MoveMigratorAction.of("multiverse-configuration.teleportintercept", "world.teleport-intercept")) + .addAction(BooleanMigratorAction.of("world.teleport-intercept")) .addAction(MoveMigratorAction.of("multiverse-configuration.firstspawnoverride", "spawn.first-spawn-override")) + .addAction(BooleanMigratorAction.of("spawn.first-spawn-override")) //.addAction(MoveMigratorAction.of("multiverse-configuration.displaypermerrors", "")) .addAction(MoveMigratorAction.of("multiverse-configuration.globaldebug", "misc.global-debug")) + .addAction(IntegerMigratorAction.of("misc.global-debug")) .addAction(MoveMigratorAction.of("multiverse-configuration.silentstart", "misc.silent-start")) + .addAction(BooleanMigratorAction.of("misc.silent-start")) .addAction(MoveMigratorAction.of("multiverse-configuration.firstspawnworld", "spawn.first-spawn-location")) .addAction(MoveMigratorAction.of("multiverse-configuration.defaultportalsearch", "portal.use-custom-portal-search")) + .addAction(BooleanMigratorAction.of("portal.use-custom-portal-search")) .addAction(InvertBoolMigratorAction.of("portal.use-custom-portal-search")) .addAction(MoveMigratorAction.of("multiverse-configuration.portalsearchradius", "portal.custom-portal-search-radius")) + .addAction(IntegerMigratorAction.of("portal.custom-portal-search-radius")) .addAction(MoveMigratorAction.of("multiverse-configuration.autopurge", "world.auto-purge-entities")) + .addAction(BooleanMigratorAction.of("world.auto-purge-entities")) .addAction(MoveMigratorAction.of("multiverse-configuration.idonotwanttodonate", "misc.show-donation-message")) + .addAction(BooleanMigratorAction.of("misc.show-donation-message")) .addAction(InvertBoolMigratorAction.of("misc.show-donation-message")) .build()) .build()) diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java index 8b64f5e2..d4588f7e 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java @@ -119,6 +119,7 @@ public class MVConfigNodes { .comment("This only applies if use-custom-portal-search is set to true.") .defaultValue(128) .name("custom-portal-search-radius") + .validator(value -> value >= 0) .build()); private static final MVCommentedNode MESSAGING_HEADER = node(MVCommentedNode.builder("messaging") @@ -164,6 +165,7 @@ public class MVConfigNodes { .comment(" 3 = finest") .defaultValue(0) .name("global-debug") + .validator(value -> value >= 0 && value <= 3) .onSetValue((oldValue, newValue) -> Logging.setDebugLevel(newValue)) .build()); diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/BooleanMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/BooleanMigratorAction.java new file mode 100644 index 00000000..b4546215 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/BooleanMigratorAction.java @@ -0,0 +1,28 @@ +package com.onarandombox.MultiverseCore.configuration.migration; + +import co.aikar.commands.ACFUtil; +import com.dumptruckman.minecraft.util.Logging; +import com.onarandombox.MultiverseCore.configuration.ConfigHandle; + +/** + * Single migrator action that converts a string value to a boolean. + */ +public class BooleanMigratorAction implements MigratorAction { + + public static BooleanMigratorAction of(String path) { + return new BooleanMigratorAction(path); + } + + private final String path; + + protected BooleanMigratorAction(String path) { + this.path = path; + } + + @Override + public void migrate(ConfigHandle settings) { + settings.getConfig().set(path, ACFUtil.isTruthy(settings.getConfig().getString(path, ""))); + Logging.info("Converted %s to boolean %s", path, settings.getConfig().getBoolean(path)); + } +} + diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/ConfigMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/ConfigMigrator.java index 7e842c12..c0d5860a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/ConfigMigrator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/ConfigMigrator.java @@ -40,7 +40,7 @@ public class ConfigMigrator { double versionNumber = settings.get(versionNode); for (VersionMigrator versionMigrator : versionMigrators) { if (versionNumber < versionMigrator.getVersion()) { - Logging.config("Migrating config from version %s to %s...", versionNumber, versionMigrator.getVersion()); + Logging.info("Migrating config from version %s to %s...", versionNumber, versionMigrator.getVersion()); versionMigrator.migrate(settings); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/IntegerMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/IntegerMigratorAction.java new file mode 100644 index 00000000..964187c1 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/IntegerMigratorAction.java @@ -0,0 +1,28 @@ +package com.onarandombox.MultiverseCore.configuration.migration; + +import co.aikar.commands.ACFUtil; +import com.dumptruckman.minecraft.util.Logging; +import com.onarandombox.MultiverseCore.configuration.ConfigHandle; +import org.bukkit.util.NumberConversions; + +/** + * Single migrator action that converts a string value to an integer. + */ +public class IntegerMigratorAction implements MigratorAction { + + public static IntegerMigratorAction of(String path) { + return new IntegerMigratorAction(path); + } + + private final String path; + + public IntegerMigratorAction(String path) { + this.path = path; + } + + @Override + public void migrate(ConfigHandle settings) { + settings.getConfig().set(path, ACFUtil.parseInt(settings.getConfig().getString(path))); + Logging.info("Converted %s to integer %s", path, settings.getConfig().getInt(path)); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/InvertBoolMigratorAction.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/InvertBoolMigratorAction.java index c9022688..e5709242 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/InvertBoolMigratorAction.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/InvertBoolMigratorAction.java @@ -1,5 +1,6 @@ package com.onarandombox.MultiverseCore.configuration.migration; +import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.configuration.ConfigHandle; /** @@ -28,6 +29,8 @@ public class InvertBoolMigratorAction implements MigratorAction { */ @Override public void migrate(ConfigHandle settings) { - settings.getConfig().set(path, !settings.getConfig().getBoolean(path)); + boolean boolValue = !settings.getConfig().getBoolean(path); + settings.getConfig().set(path, boolValue); + Logging.info("Inverted %s to boolean %s", path, boolValue); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/EnchancedValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/EnhancedValueNode.java similarity index 55% rename from src/main/java/com/onarandombox/MultiverseCore/configuration/node/EnchancedValueNode.java rename to src/main/java/com/onarandombox/MultiverseCore/configuration/node/EnhancedValueNode.java index 1f3f5420..c29ecf17 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/EnchancedValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/EnhancedValueNode.java @@ -5,11 +5,11 @@ import java.util.Optional; import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; /** - * A {@link TypedValueNode} that has a name. + * A {@link TypedValueNode} that has a name, validation, and action to be performed when the value is set. * * @param The type of the node's value. */ -public interface EnchancedValueNode extends TypedValueNode { +public interface EnhancedValueNode extends TypedValueNode { /** * Gets the name of this node. Used for identifying the node from user input. * @@ -17,5 +17,13 @@ public interface EnchancedValueNode extends TypedValueNode { */ Optional getName(); + boolean isValid(T value); + + /** + * Called when the value of this node is set. + * + * @param oldValue The old value. + * @param newValue The new value. + */ void onSetValue(T oldValue, T newValue); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVValueNode.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVValueNode.java index d5aa1acd..c8b9d9df 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVValueNode.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/MVValueNode.java @@ -2,15 +2,16 @@ package com.onarandombox.MultiverseCore.configuration.node; import java.util.Optional; import java.util.function.BiConsumer; +import java.util.function.Function; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** - * Implementation of {@link EnchancedValueNode}. + * Implementation of {@link EnhancedValueNode}. * @param The type of the value. */ -public class MVValueNode extends MVCommentedNode implements EnchancedValueNode { +public class MVValueNode extends MVCommentedNode implements EnhancedValueNode { /** * Creates a new builder for a {@link MVValueNode}. @@ -27,13 +28,15 @@ public class MVValueNode extends MVCommentedNode implements EnchancedValueNod protected final Class type; protected final T defaultValue; protected final String name; + protected final Function validator; protected final BiConsumer onSetValue; - protected MVValueNode(String path, String[] comments, Class type, T defaultValue, String name, BiConsumer onSetValue) { + protected MVValueNode(String path, String[] comments, Class type, T defaultValue, String name, Function validator, BiConsumer onSetValue) { super(path, comments); this.type = type; this.defaultValue = defaultValue; this.name = name; + this.validator = validator; this.onSetValue = onSetValue; } @@ -61,9 +64,25 @@ public class MVValueNode extends MVCommentedNode implements EnchancedValueNod return Optional.ofNullable(name); } + /** + * {@inheritDoc} + */ + @Override + public boolean isValid(T value) { + if (validator != null) { + return validator.apply(value); + } + return true; + } + + /** + * {@inheritDoc} + */ @Override public void onSetValue(T oldValue, T newValue) { - onSetValue.accept(oldValue, newValue); + if (onSetValue != null) { + onSetValue.accept(oldValue, newValue); + } } /** @@ -77,6 +96,7 @@ public class MVValueNode extends MVCommentedNode implements EnchancedValueNod protected final Class type; protected T defaultValue; protected String name; + protected Function validator; protected BiConsumer onSetValue; /** @@ -113,6 +133,17 @@ public class MVValueNode extends MVCommentedNode implements EnchancedValueNod return (B) this; } + public B validator(@Nullable Function validator) { + this.validator = validator; + return (B) this; + } + + /** + * Sets the action to be performed when the value is set. + * + * @param onSetValue The action to be performed. + * @return This builder. + */ public B onSetValue(@Nullable BiConsumer onSetValue) { this.onSetValue = onSetValue; return (B) this; @@ -123,7 +154,7 @@ public class MVValueNode extends MVCommentedNode implements EnchancedValueNod */ @Override public MVValueNode build() { - return new MVValueNode<>(path, comments.toArray(new String[0]), type, defaultValue, name, onSetValue); + return new MVValueNode<>(path, comments.toArray(new String[0]), type, defaultValue, name, validator, onSetValue); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NodeGroup.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NodeGroup.java index be7ead94..810d5c2b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NodeGroup.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/node/NodeGroup.java @@ -29,14 +29,14 @@ public class NodeGroup implements Collection { } private void addNodeIndex(CommentedNode node) { - if (node instanceof EnchancedValueNode) { - ((EnchancedValueNode) node).getName().ifPresent(name -> nodesMap.put(name, node)); + if (node instanceof EnhancedValueNode) { + ((EnhancedValueNode) node).getName().ifPresent(name -> nodesMap.put(name, node)); } } private void removeNodeIndex(CommentedNode node) { - if (node instanceof EnchancedValueNode) { - ((EnchancedValueNode) node).getName().ifPresent(nodesMap::remove); + if (node instanceof EnhancedValueNode) { + ((EnhancedValueNode) node).getName().ifPresent(nodesMap::remove); } } From 7f9c45d8e14792f33bf38df07e1c586ac2bf84b4 Mon Sep 17 00:00:00 2001 From: Jeremy Wood Date: Sun, 26 Mar 2023 15:58:50 -0400 Subject: [PATCH 22/37] Use DI for new config. --- .../MultiverseCore/MultiverseCore.java | 12 ---- .../commands/ConfigCommand.java | 31 +++++--- .../commands/ReloadCommand.java | 10 +-- .../config/MVCoreConfigProvider.java | 70 +++---------------- .../destination/DestinationsProvider.java | 2 - .../MultiverseCore/world/SimpleMVWorld.java | 10 ++- .../world/SimpleMVWorldManager.java | 10 ++- .../multiverse/core/inject/InjectionTest.kt | 2 +- 8 files changed, 51 insertions(+), 96 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index ae02e772..adfa66b9 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -21,11 +21,9 @@ import com.onarandombox.MultiverseCore.api.Destination; import com.onarandombox.MultiverseCore.api.MVCore; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; -import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand; import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; -import com.onarandombox.MultiverseCore.configuration.DefaultMVConfig; import com.onarandombox.MultiverseCore.destination.DestinationsProvider; import com.onarandombox.MultiverseCore.economy.MVEconomist; import com.onarandombox.MultiverseCore.inject.InjectableListener; @@ -347,16 +345,6 @@ public class MultiverseCore extends JavaPlugin implements MVCore { @Override public void loadConfigs() { getConfigProvider().loadConfigs(); - // TODO move to config provider -// config = DefaultMVConfig.init(this); -// -// this.worldManager.loadWorldConfig(new File(getDataFolder(), "worlds.yml")); -// -// int level = Logging.getDebugLevel(); -// Logging.setDebugLevel(getMVConfig().getGlobalDebug()); -// if (level != Logging.getDebugLevel()) { -// getServer().getPluginManager().callEvent(new MVDebugModeEvent(level)); -// } } /** diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java index 1d9246a7..1944f121 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java @@ -9,18 +9,29 @@ import co.aikar.commands.annotation.Optional; import co.aikar.commands.annotation.Single; import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Syntax; -import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVConfig; +import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; +import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand; import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue; +import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import jakarta.inject.Inject; import org.jetbrains.annotations.NotNull; +import org.jvnet.hk2.annotations.Service; +@Service @CommandAlias("mv") -public class ConfigCommand extends MultiverseCoreCommand { - private final MVConfig config; +public class ConfigCommand extends MultiverseCommand { - public ConfigCommand(@NotNull MultiverseCore plugin) { - super(plugin); - this.config = plugin.getMVConfig(); + private final MVCoreConfigProvider configProvider; + + @Inject + public ConfigCommand(@NotNull MVCommandManager commandManager, @NotNull MVCoreConfigProvider configProvider) { + super(commandManager); + this.configProvider = configProvider; + } + + private MVConfig getConfig() { + return configProvider.getConfig(); } @Subcommand("config") @@ -48,20 +59,20 @@ public class ConfigCommand extends MultiverseCoreCommand { } private void showConfigValue(BukkitCommandIssuer issuer, String name) { - Object currentValue = config.getProperty(name); + Object currentValue = getConfig().getProperty(name); if (currentValue == null) { issuer.sendMessage("No such config option: " + name); return; } - issuer.sendMessage(name + "is currently set to " + config.getProperty(name)); + issuer.sendMessage(name + "is currently set to " + getConfig().getProperty(name)); } private void updateConfigValue(BukkitCommandIssuer issuer, String name, Object value) { - if (!config.setProperty(name, value)) { + if (!getConfig().setProperty(name, value)) { issuer.sendMessage("Unable to set " + name + " to " + value); return; } - config.save(); + getConfig().save(); issuer.sendMessage("Successfully set " + name + " to " + value); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java index 203491da..57e95392 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java @@ -8,11 +8,11 @@ import co.aikar.commands.annotation.CommandAlias; import co.aikar.commands.annotation.CommandPermission; import co.aikar.commands.annotation.Description; import co.aikar.commands.annotation.Subcommand; -import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.anchor.AnchorManager; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand; +import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; import com.onarandombox.MultiverseCore.event.MVConfigReloadEvent; import com.onarandombox.MultiverseCore.utils.MVCorei18n; import jakarta.inject.Inject; @@ -24,7 +24,7 @@ import org.jvnet.hk2.annotations.Service; @CommandAlias("mv") public class ReloadCommand extends MultiverseCommand { - private final MultiverseCore plugin; + private final MVCoreConfigProvider configProvider; private final AnchorManager anchorManager; private final MVWorldManager worldManager; private final PluginManager pluginManager; @@ -32,13 +32,13 @@ public class ReloadCommand extends MultiverseCommand { @Inject public ReloadCommand( @NotNull MVCommandManager commandManager, - @NotNull MultiverseCore plugin, + @NotNull MVCoreConfigProvider configProvider, @NotNull AnchorManager anchorManager, @NotNull MVWorldManager worldManager, @NotNull PluginManager pluginManager ) { super(commandManager); - this.plugin = plugin; + this.configProvider = configProvider; this.anchorManager = anchorManager; this.worldManager = worldManager; this.pluginManager = pluginManager; @@ -49,7 +49,7 @@ public class ReloadCommand extends MultiverseCommand { @Description("{@@mv-core.reload.description}") public void onReloadCommand(@NotNull BukkitCommandIssuer issuer) { issuer.sendInfo(MVCorei18n.RELOAD_RELOADING); - this.plugin.loadConfigs(); + this.configProvider.loadConfigs(); this.anchorManager.loadAnchors(); this.worldManager.loadWorlds(true); diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java index 11d78775..7e650024 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java @@ -2,39 +2,27 @@ package com.onarandombox.MultiverseCore.config; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; -import com.onarandombox.MultiverseCore.MultiverseCoreConfiguration; import com.onarandombox.MultiverseCore.api.MVConfig; import com.onarandombox.MultiverseCore.api.MVWorldManager; +import com.onarandombox.MultiverseCore.configuration.DefaultMVConfig; import com.onarandombox.MultiverseCore.event.MVDebugModeEvent; -import io.vavr.control.Option; import io.vavr.control.Try; import jakarta.inject.Inject; import jakarta.inject.Provider; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.jetbrains.annotations.NotNull; import org.jvnet.hk2.annotations.Service; -import java.io.BufferedReader; import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; @Service public final class MVCoreConfigProvider { - private static final String CONFIG_FILE = "config.yml"; - private static final String DEFAULTS_CONFIG_FILE = "/defaults/config.yml"; private static final String WORLDS_CONFIG_FILE = "worlds.yml"; - private static final String CHARACTER_ENCODING = "UTF-8"; - private static final String CONFIG_KEY = "multiverse-configuration"; - private FileConfiguration multiverseConfig; - private volatile MultiverseCoreConfiguration config; + private volatile DefaultMVConfig config; - private final Plugin plugin; + private final MultiverseCore plugin; private final PluginManager pluginManager; private final Provider worldManagerProvider; // TODO remove this dependency @@ -60,60 +48,26 @@ public final class MVCoreConfigProvider { } /** - * Gets the Core configuration instance. + * Gets the Core configuration instance. If the config has not been loaded yet, it will first be loaded. * * @return The config - * @throws IllegalStateException If the config is not loaded */ @NotNull - public MVConfig getConfig() throws IllegalStateException { + public MVConfig getConfig() { if (config == null) { - throw new IllegalStateException("Config is not loaded"); + loadConfigs(); } return config; } public void loadConfigs() { - multiverseConfig = loadConfigWithDefaults(); - setConfigOptions(multiverseConfig); - config = getOrCreateConfigObject(multiverseConfig); + config = DefaultMVConfig.init(plugin); loadWorldConfigs(); setDebugLevelFromConfig(config); } - @NotNull - private FileConfiguration loadConfigWithDefaults() { - var config = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), CONFIG_FILE)); - - Option.of(plugin.getClass().getResourceAsStream(DEFAULTS_CONFIG_FILE)) - .toTry() - .mapTry(defaultsStream -> YamlConfiguration.loadConfiguration( - new BufferedReader(new InputStreamReader(defaultsStream, CHARACTER_ENCODING)))) - .andThen(config::setDefaults) - .onFailure(e -> { - Logging.severe("Couldn't load default config with UTF-8 encoding. Details follow:"); - e.printStackTrace(); - Logging.severe("Default configs NOT loaded."); - }); - - return config; - } - - private void setConfigOptions(@NotNull FileConfiguration config) { - config.options().copyDefaults(false); - config.options().copyHeader(true); - } - - @NotNull - private MultiverseCoreConfiguration getOrCreateConfigObject(@NotNull FileConfiguration config) { - return Try.of(() -> (MultiverseCoreConfiguration) config.get(CONFIG_KEY)) - .toOption() // Ignore exceptions - .map(c -> c == null ? new MultiverseCoreConfiguration() : c) - .getOrElse(new MultiverseCoreConfiguration()); - } - private void loadWorldConfigs() { worldManagerProvider.get().loadWorldConfig(new File(plugin.getDataFolder(), WORLDS_CONFIG_FILE)); } @@ -128,13 +82,9 @@ public final class MVCoreConfigProvider { @NotNull public Try saveConfig() { - try { - this.multiverseConfig.set(CONFIG_KEY, config); - this.multiverseConfig.save(new File(plugin.getDataFolder(), CONFIG_FILE)); - return Try.success(null); - } catch (IOException e) { - return Try.failure(new MultiverseConfigurationException( - "Could not save Multiverse config.yml config. Please check your file permissions.", e)); + if (config != null) { + return Try.run(() -> config.save()); } + return Try.failure(new IllegalStateException("Config not loaded yet")); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationsProvider.java b/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationsProvider.java index a891247f..608bc389 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationsProvider.java +++ b/src/main/java/com/onarandombox/MultiverseCore/destination/DestinationsProvider.java @@ -7,13 +7,11 @@ import java.util.stream.Collectors; import co.aikar.commands.BukkitCommandIssuer; import co.aikar.commands.CommandIssuer; -import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.Destination; import com.onarandombox.MultiverseCore.api.DestinationInstance; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.api.Teleporter; import jakarta.inject.Inject; -import org.bukkit.Server; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.permissions.Permission; diff --git a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java index cb0eb563..b1d3815c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java @@ -13,13 +13,13 @@ import java.util.Map; import java.util.UUID; import com.dumptruckman.minecraft.util.Logging; -import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.BlockSafety; import com.onarandombox.MultiverseCore.api.LocationManipulation; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.api.WorldPurger; +import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; import com.onarandombox.MultiverseCore.exceptions.PropertyDoesNotExistException; import com.onarandombox.MultiverseCore.listeners.MVPlayerListener; import com.onarandombox.MultiverseCore.world.configuration.AllowedPortalType; @@ -55,6 +55,7 @@ public class SimpleMVWorld implements MVWorld { private static final int SPAWN_LOCATION_SEARCH_RADIUS = 16; private final MVWorldManager worldManager; + private final MVCoreConfigProvider configProvider; private final WorldPurger worldPurger; private final MVPlayerListener playerListener; private final BlockSafety blockSafety; @@ -67,6 +68,7 @@ public class SimpleMVWorld implements MVWorld { public SimpleMVWorld( MVWorldManager worldManager, + MVCoreConfigProvider configProvider, WorldPurger worldPurger, MVPlayerListener playerListener, BlockSafety blockSafety, @@ -76,7 +78,7 @@ public class SimpleMVWorld implements MVWorld { World world, WorldProperties properties ) { - this(worldManager, worldPurger, playerListener, blockSafety, safeTTeleporter, + this(worldManager, configProvider, worldPurger, playerListener, blockSafety, safeTTeleporter, locationManipulation, server, world, properties, true); } @@ -85,6 +87,7 @@ public class SimpleMVWorld implements MVWorld { */ public SimpleMVWorld( MVWorldManager worldManager, + MVCoreConfigProvider configProvider, WorldPurger worldPurger, MVPlayerListener playerListener, BlockSafety blockSafety, @@ -96,6 +99,7 @@ public class SimpleMVWorld implements MVWorld { boolean fixSpawn ) { this.worldManager = worldManager; + this.configProvider = configProvider; this.worldPurger = worldPurger; this.playerListener = playerListener; this.blockSafety = blockSafety; @@ -321,7 +325,7 @@ public class SimpleMVWorld implements MVWorld { } world.setSpawnFlags(allowMonsters, allowAnimals); } - if (MultiverseCoreConfiguration.getInstance().isAutoPurgeEntities()) { + if (configProvider.getConfig().isAutoPurgeEntities()) { worldPurger.purgeWorld(SimpleMVWorld.this); } return super.validateChange(property, newValue, oldValue, object); diff --git a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java index 74b4c1c3..c1f87901 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java @@ -32,6 +32,7 @@ import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.api.WorldPurger; +import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; import com.onarandombox.MultiverseCore.event.MVWorldDeleteEvent; import com.onarandombox.MultiverseCore.listeners.MVPlayerListener; import com.onarandombox.MultiverseCore.utils.UnsafeCallWrapper; @@ -61,6 +62,7 @@ import org.jvnet.hk2.annotations.Service; @Service public class SimpleMVWorldManager implements MVWorldManager { private final MultiverseCore plugin; + private final MVCoreConfigProvider configProvider; private final MVPlayerListener playerListener; private final BlockSafety blockSafety; private final SafeTTeleporter safeTTeleporter; @@ -77,6 +79,7 @@ public class SimpleMVWorldManager implements MVWorldManager { @Inject public SimpleMVWorldManager( MultiverseCore plugin, + MVCoreConfigProvider configProvider, MVPlayerListener playerListener, BlockSafety blockSafety, SafeTTeleporter safeTTeleporter, @@ -86,6 +89,7 @@ public class SimpleMVWorldManager implements MVWorldManager { Server server ) { this.plugin = plugin; + this.configProvider = configProvider; this.playerListener = playerListener; this.blockSafety = blockSafety; this.safeTTeleporter = safeTTeleporter; @@ -511,9 +515,9 @@ public class SimpleMVWorldManager implements MVWorldManager { nullWorld(worldName); return false; } - SimpleMVWorld world = new SimpleMVWorld(this, worldPurger, playerListener, blockSafety, safeTTeleporter, - locationManipulation, server, cbworld, mvworld); - if (MultiverseCoreConfiguration.getInstance().isAutoPurgeEntities()) { + SimpleMVWorld world = new SimpleMVWorld(this, configProvider, worldPurger, playerListener, blockSafety, + safeTTeleporter, locationManipulation, server, cbworld, mvworld); + if (configProvider.getConfig().isAutoPurgeEntities()) { this.worldPurger.purgeWorld(world); } this.worlds.put(worldName, world); diff --git a/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt b/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt index 6b9a8d79..84d69970 100644 --- a/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt +++ b/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt @@ -120,7 +120,7 @@ class InjectionTest : TestWithMockBukkit() { fun `Commands are available as services`() { val commands = multiverseCore.getAllServices(MultiverseCommand::class.java) // TODO come up with a better way to test this like via actually testing the effect of calling each command - assertEquals(16, commands.size) + assertEquals(17, commands.size) } @Test From f05092076fc8c342b0a5900a8c7b23968cb76cfd Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Mon, 27 Mar 2023 10:39:29 +0800 Subject: [PATCH 23/37] fix: Don't need to check version before removing serialisation value --- .../MultiverseCore/configuration/DefaultMVConfig.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java index 11e336a8..ab27232a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java @@ -82,9 +82,7 @@ public class DefaultMVConfig implements MVConfig { return; } // Remove the old config section if it is still in the old ConfigurationSerializable. - if (content.contains("version: 2.5")) { - content = content.replace("==: com.onarandombox.MultiverseCore.MultiverseCoreConfiguration", ""); - } + content = content.replace("==: com.onarandombox.MultiverseCore.MultiverseCoreConfiguration", ""); try { Files.writeString(configPath, content); } catch (IOException e) { From db9879af8dad24ba195933f732a28587b647194a Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Mon, 27 Mar 2023 10:48:46 +0800 Subject: [PATCH 24/37] refactor: Move DefaultMVConfig and MVConfigNodes to config package --- .../MultiverseCore/commandtools/MVCommandCompletions.java | 2 +- .../MultiverseCore/commandtools/MVCommandContexts.java | 4 +--- .../{configuration => config}/DefaultMVConfig.java | 3 ++- .../{configuration => config}/MVConfigNodes.java | 2 +- .../MultiverseCore/config/MVCoreConfigProvider.java | 1 - 5 files changed, 5 insertions(+), 7 deletions(-) rename src/main/java/com/onarandombox/MultiverseCore/{configuration => config}/DefaultMVConfig.java (98%) rename src/main/java/com/onarandombox/MultiverseCore/{configuration => config}/MVConfigNodes.java (99%) diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java index 904779fe..7fe45be9 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java @@ -18,7 +18,7 @@ import co.aikar.commands.RootCommand; import com.google.common.collect.Sets; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; -import com.onarandombox.MultiverseCore.configuration.MVConfigNodes; +import com.onarandombox.MultiverseCore.config.MVConfigNodes; import com.onarandombox.MultiverseCore.destination.DestinationsProvider; import com.onarandombox.MultiverseCore.destination.ParsedDestination; import jakarta.inject.Inject; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java index b1c44190..a2400b13 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java @@ -10,12 +10,11 @@ import co.aikar.commands.InvalidCommandArgument; import co.aikar.commands.PaperCommandContexts; import co.aikar.commands.contexts.ContextResolver; import com.google.common.base.Strings; -import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.commandtools.context.GameRuleValue; import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue; -import com.onarandombox.MultiverseCore.configuration.MVConfigNodes; +import com.onarandombox.MultiverseCore.config.MVConfigNodes; import com.onarandombox.MultiverseCore.destination.DestinationsProvider; import com.onarandombox.MultiverseCore.destination.ParsedDestination; import com.onarandombox.MultiverseCore.display.filters.ContentFilter; @@ -24,7 +23,6 @@ import com.onarandombox.MultiverseCore.display.filters.RegexContentFilter; import com.onarandombox.MultiverseCore.utils.PlayerFinder; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode; -import io.github.townyadvanced.commentedconfiguration.setting.ValueNode; import jakarta.inject.Inject; import org.bukkit.GameRule; import org.bukkit.entity.Player; diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/config/DefaultMVConfig.java similarity index 98% rename from src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java rename to src/main/java/com/onarandombox/MultiverseCore/config/DefaultMVConfig.java index ab27232a..9dc93306 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultMVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/DefaultMVConfig.java @@ -1,4 +1,4 @@ -package com.onarandombox.MultiverseCore.configuration; +package com.onarandombox.MultiverseCore.config; import java.io.IOException; import java.nio.file.Files; @@ -7,6 +7,7 @@ import java.nio.file.Path; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVConfig; +import com.onarandombox.MultiverseCore.configuration.ConfigHandle; import com.onarandombox.MultiverseCore.configuration.migration.BooleanMigratorAction; import com.onarandombox.MultiverseCore.configuration.migration.ConfigMigrator; import com.onarandombox.MultiverseCore.configuration.migration.IntegerMigratorAction; diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVConfigNodes.java similarity index 99% rename from src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java rename to src/main/java/com/onarandombox/MultiverseCore/config/MVConfigNodes.java index d4588f7e..5c7532b1 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVConfigNodes.java @@ -1,4 +1,4 @@ -package com.onarandombox.MultiverseCore.configuration; +package com.onarandombox.MultiverseCore.config; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.configuration.node.MVCommentedNode; diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java index 7e650024..6d593bc5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java @@ -4,7 +4,6 @@ import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVConfig; import com.onarandombox.MultiverseCore.api.MVWorldManager; -import com.onarandombox.MultiverseCore.configuration.DefaultMVConfig; import com.onarandombox.MultiverseCore.event.MVDebugModeEvent; import io.vavr.control.Try; import jakarta.inject.Inject; From 7af22c451e30151c1450fe8dc2042793022c724d Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Mon, 27 Mar 2023 10:49:07 +0800 Subject: [PATCH 25/37] chore Remove unused MultiverseCoreConfiguration.java --- .../onarandombox/MultiverseCore/MultiverseCoreConfiguration.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCoreConfiguration.java deleted file mode 100644 index e69de29b..00000000 From ce9d53c6e9a0d27c17a1a302bf79d58e27726767 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Mon, 27 Mar 2023 11:14:58 +0800 Subject: [PATCH 26/37] refactor: Rename to MVCoreConfig --- .../commandtools/MVCommandCompletions.java | 4 +- .../commandtools/MVCommandContexts.java | 4 +- ...DefaultMVConfig.java => MVCoreConfig.java} | 68 +++++++++---------- ...onfigNodes.java => MVCoreConfigNodes.java} | 4 +- .../config/MVCoreConfigProvider.java | 4 +- 5 files changed, 42 insertions(+), 42 deletions(-) rename src/main/java/com/onarandombox/MultiverseCore/config/{DefaultMVConfig.java => MVCoreConfig.java} (75%) rename src/main/java/com/onarandombox/MultiverseCore/config/{MVConfigNodes.java => MVCoreConfigNodes.java} (99%) diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java index 7fe45be9..ab1985c3 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java @@ -18,7 +18,7 @@ import co.aikar.commands.RootCommand; import com.google.common.collect.Sets; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; -import com.onarandombox.MultiverseCore.config.MVConfigNodes; +import com.onarandombox.MultiverseCore.config.MVCoreConfigNodes; import com.onarandombox.MultiverseCore.destination.DestinationsProvider; import com.onarandombox.MultiverseCore.destination.ParsedDestination; import jakarta.inject.Inject; @@ -48,7 +48,7 @@ public class MVCommandCompletions extends PaperCommandCompletions { registerAsyncCompletion("destinations", this::suggestDestinations); registerAsyncCompletion("flags", this::suggestFlags); registerStaticCompletion("gamerules", this::suggestGamerules); - registerStaticCompletion("mvconfigs", MVConfigNodes.getNodes().getNames()); + registerStaticCompletion("mvconfigs", MVCoreConfigNodes.getNodes().getNames()); registerAsyncCompletion("mvworlds", this::suggestMVWorlds); setDefaultCompletion("destinations", ParsedDestination.class); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java index a2400b13..394c4288 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java @@ -14,7 +14,7 @@ import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.commandtools.context.GameRuleValue; import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue; -import com.onarandombox.MultiverseCore.config.MVConfigNodes; +import com.onarandombox.MultiverseCore.config.MVCoreConfigNodes; import com.onarandombox.MultiverseCore.destination.DestinationsProvider; import com.onarandombox.MultiverseCore.destination.ParsedDestination; import com.onarandombox.MultiverseCore.display.filters.ContentFilter; @@ -120,7 +120,7 @@ public class MVCommandContexts extends PaperCommandContexts { if (Strings.isNullOrEmpty(configName)) { throw new InvalidCommandArgument("No config name specified."); } - Optional node = MVConfigNodes.getNodes().findNode(configName); + Optional node = MVCoreConfigNodes.getNodes().findNode(configName); if (node.isEmpty()) { throw new InvalidCommandArgument("The config " + configName + " is not valid."); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/DefaultMVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java similarity index 75% rename from src/main/java/com/onarandombox/MultiverseCore/config/DefaultMVConfig.java rename to src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java index 9dc93306..5dcf8dcc 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/config/DefaultMVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java @@ -15,7 +15,7 @@ import com.onarandombox.MultiverseCore.configuration.migration.InvertBoolMigrato import com.onarandombox.MultiverseCore.configuration.migration.MoveMigratorAction; import com.onarandombox.MultiverseCore.configuration.migration.VersionMigrator; -public class DefaultMVConfig implements MVConfig { +public class MVCoreConfig implements MVConfig { public static final String CONFIG_FILENAME = "config.yml"; public static final double CONFIG_VERSION = 5.0; @@ -25,8 +25,8 @@ public class DefaultMVConfig implements MVConfig { * @param core The MultiverseCore instance. * @return The new DefaultMVConfig instance. */ - public static DefaultMVConfig init(MultiverseCore core) { - var config = new DefaultMVConfig(core); + public static MVCoreConfig init(MultiverseCore core) { + var config = new MVCoreConfig(core); config.load(); config.save(); return config; @@ -35,15 +35,15 @@ public class DefaultMVConfig implements MVConfig { private final Path configPath; private final ConfigHandle configHandle; - public DefaultMVConfig(MultiverseCore core) { + public MVCoreConfig(MultiverseCore core) { configPath = Path.of(core.getDataFolder().getPath(), CONFIG_FILENAME); migrateFromOldConfigFile(); configHandle = ConfigHandle.builder(configPath) .logger(Logging.getLogger()) - .nodes(MVConfigNodes.getNodes()) - .migrator(ConfigMigrator.builder(MVConfigNodes.VERSION) + .nodes(MVCoreConfigNodes.getNodes()) + .migrator(ConfigMigrator.builder(MVCoreConfigNodes.VERSION) .addVersionMigrator(VersionMigrator.builder(5.0) .addAction(MoveMigratorAction.of("multiverse-configuration.enforceaccess", "world.enforce-access")) .addAction(BooleanMigratorAction.of("world.enforce-access")) @@ -113,141 +113,141 @@ public class DefaultMVConfig implements MVConfig { @Override public void setEnforceAccess(boolean enforceAccess) { - configHandle.set(MVConfigNodes.ENFORCE_ACCESS, enforceAccess); + configHandle.set(MVCoreConfigNodes.ENFORCE_ACCESS, enforceAccess); } @Override public boolean getEnforceAccess() { - return configHandle.get(MVConfigNodes.ENFORCE_ACCESS); + return configHandle.get(MVCoreConfigNodes.ENFORCE_ACCESS); } @Override public void setEnforceGameMode(boolean enforceGameMode) { - configHandle.set(MVConfigNodes.ENFORCE_GAMEMODE, enforceGameMode); + configHandle.set(MVCoreConfigNodes.ENFORCE_GAMEMODE, enforceGameMode); } @Override public boolean getEnforceGameMode() { - return configHandle.get(MVConfigNodes.ENFORCE_GAMEMODE); + return configHandle.get(MVCoreConfigNodes.ENFORCE_GAMEMODE); } @Override public void setAutoPurgeEntities(boolean autopurge) { - configHandle.set(MVConfigNodes.AUTO_PURGE_ENTITIES, autopurge); + configHandle.set(MVCoreConfigNodes.AUTO_PURGE_ENTITIES, autopurge); } @Override public boolean isAutoPurgeEntities() { - return configHandle.get(MVConfigNodes.AUTO_PURGE_ENTITIES); + return configHandle.get(MVCoreConfigNodes.AUTO_PURGE_ENTITIES); } @Override public void setTeleportIntercept(boolean teleportIntercept) { - configHandle.set(MVConfigNodes.TELEPORT_INTERCEPT, teleportIntercept); + configHandle.set(MVCoreConfigNodes.TELEPORT_INTERCEPT, teleportIntercept); } @Override public boolean getTeleportIntercept() { - return configHandle.get(MVConfigNodes.TELEPORT_INTERCEPT); + return configHandle.get(MVCoreConfigNodes.TELEPORT_INTERCEPT); } @Override public void setFirstSpawnOverride(boolean firstSpawnOverride) { - configHandle.set(MVConfigNodes.FIRST_SPAWN_OVERRIDE, firstSpawnOverride); + configHandle.set(MVCoreConfigNodes.FIRST_SPAWN_OVERRIDE, firstSpawnOverride); } @Override public boolean getFirstSpawnOverride() { - return configHandle.get(MVConfigNodes.FIRST_SPAWN_OVERRIDE); + return configHandle.get(MVCoreConfigNodes.FIRST_SPAWN_OVERRIDE); } @Override public void setFirstSpawnLocation(String firstSpawnWorld) { - configHandle.set(MVConfigNodes.FIRST_SPAWN_LOCATION, firstSpawnWorld); + configHandle.set(MVCoreConfigNodes.FIRST_SPAWN_LOCATION, firstSpawnWorld); } @Override public String getFirstSpawnLocation() { - return configHandle.get(MVConfigNodes.FIRST_SPAWN_LOCATION); + return configHandle.get(MVCoreConfigNodes.FIRST_SPAWN_LOCATION); } @Override public void setUseCustomPortalSearch(boolean useDefaultPortalSearch) { - configHandle.set(MVConfigNodes.USE_CUSTOM_PORTAL_SEARCH, useDefaultPortalSearch); + configHandle.set(MVCoreConfigNodes.USE_CUSTOM_PORTAL_SEARCH, useDefaultPortalSearch); } @Override public boolean isUsingCustomPortalSearch() { - return configHandle.get(MVConfigNodes.USE_CUSTOM_PORTAL_SEARCH); + return configHandle.get(MVCoreConfigNodes.USE_CUSTOM_PORTAL_SEARCH); } @Override public void setCustomPortalSearchRadius(int searchRadius) { - configHandle.set(MVConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS, searchRadius); + configHandle.set(MVCoreConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS, searchRadius); } @Override public int getCustomPortalSearchRadius() { - return configHandle.get(MVConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS); + return configHandle.get(MVCoreConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS); } @Override public void setEnablePrefixChat(boolean prefixChat) { - configHandle.set(MVConfigNodes.ENABLE_CHAT_PREFIX, prefixChat); + configHandle.set(MVCoreConfigNodes.ENABLE_CHAT_PREFIX, prefixChat); } @Override public boolean isEnablePrefixChat() { - return configHandle.get(MVConfigNodes.ENABLE_CHAT_PREFIX); + return configHandle.get(MVCoreConfigNodes.ENABLE_CHAT_PREFIX); } @Override public void setPrefixChatFormat(String prefixChatFormat) { - configHandle.set(MVConfigNodes.CHAT_PREFIX_FORMAT, prefixChatFormat); + configHandle.set(MVCoreConfigNodes.CHAT_PREFIX_FORMAT, prefixChatFormat); } @Override public String getPrefixChatFormat() { - return configHandle.get(MVConfigNodes.CHAT_PREFIX_FORMAT); + return configHandle.get(MVCoreConfigNodes.CHAT_PREFIX_FORMAT); } @Override public void setRegisterPapiHook(boolean registerPapiHook) { - configHandle.set(MVConfigNodes.REGISTER_PAPI_HOOK, registerPapiHook); + configHandle.set(MVCoreConfigNodes.REGISTER_PAPI_HOOK, registerPapiHook); } @Override public boolean isRegisterPapiHook() { - return configHandle.get(MVConfigNodes.REGISTER_PAPI_HOOK); + return configHandle.get(MVCoreConfigNodes.REGISTER_PAPI_HOOK); } @Override public void setGlobalDebug(int globalDebug) { - configHandle.set(MVConfigNodes.GLOBAL_DEBUG, globalDebug); + configHandle.set(MVCoreConfigNodes.GLOBAL_DEBUG, globalDebug); } @Override public int getGlobalDebug() { - return configHandle.get(MVConfigNodes.GLOBAL_DEBUG); + return configHandle.get(MVCoreConfigNodes.GLOBAL_DEBUG); } @Override public void setSilentStart(boolean silentStart) { - configHandle.set(MVConfigNodes.SILENT_START, silentStart); + configHandle.set(MVCoreConfigNodes.SILENT_START, silentStart); } @Override public boolean getSilentStart() { - return configHandle.get(MVConfigNodes.SILENT_START); + return configHandle.get(MVCoreConfigNodes.SILENT_START); } @Override public void setShowDonateMessage(boolean showDonateMessage) { - configHandle.set(MVConfigNodes.SHOW_DONATION_MESSAGE, showDonateMessage); + configHandle.set(MVCoreConfigNodes.SHOW_DONATION_MESSAGE, showDonateMessage); } @Override public boolean isShowingDonateMessage() { - return configHandle.get(MVConfigNodes.SHOW_DONATION_MESSAGE); + return configHandle.get(MVCoreConfigNodes.SHOW_DONATION_MESSAGE); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MVConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java similarity index 99% rename from src/main/java/com/onarandombox/MultiverseCore/config/MVConfigNodes.java rename to src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java index 5c7532b1..0833a655 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MVConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java @@ -6,7 +6,7 @@ import com.onarandombox.MultiverseCore.configuration.node.MVValueNode; import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; -public class MVConfigNodes { +public class MVCoreConfigNodes { private static final NodeGroup nodes = new NodeGroup(); public static NodeGroup getNodes() { @@ -189,7 +189,7 @@ public class MVConfigNodes { .comment("") .comment("This just signifies the version number so we can see what version of config you have.") .comment("NEVER TOUCH THIS VALUE") - .defaultValue(DefaultMVConfig.CONFIG_VERSION) + .defaultValue(MVCoreConfig.CONFIG_VERSION) .name(null) .build()); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java index 6d593bc5..15bd1acd 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java @@ -19,7 +19,7 @@ public final class MVCoreConfigProvider { private static final String WORLDS_CONFIG_FILE = "worlds.yml"; - private volatile DefaultMVConfig config; + private volatile MVCoreConfig config; private final MultiverseCore plugin; private final PluginManager pluginManager; @@ -60,7 +60,7 @@ public final class MVCoreConfigProvider { } public void loadConfigs() { - config = DefaultMVConfig.init(plugin); + config = MVCoreConfig.init(plugin); loadWorldConfigs(); From a8c8ef7a6b5cb50f4ca84c91a8f110db6dd2e854 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Mon, 27 Mar 2023 11:39:11 +0800 Subject: [PATCH 27/37] refactor: Move MVCoreConfigNodes to a non-static context --- .../MultiverseCore/api/MVConfig.java | 10 +++ .../commandtools/MVCommandCompletions.java | 9 ++- .../commandtools/MVCommandContexts.java | 9 ++- .../MultiverseCore/config/MVCoreConfig.java | 74 ++++++++++--------- .../config/MVCoreConfigNodes.java | 59 ++++++++------- 5 files changed, 95 insertions(+), 66 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java index de27948b..393273f3 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java @@ -1,5 +1,8 @@ package com.onarandombox.MultiverseCore.api; +import java.util.Collection; + +import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; import com.onarandombox.MultiverseCore.placeholders.MultiverseCorePlaceholders; public interface MVConfig { @@ -15,6 +18,13 @@ public interface MVConfig { */ void save(); + /** + * Gets the nodes for the config. + * + * @return The nodes for the config. + */ + NodeGroup getNodes(); + /** * Gets a property from the config. * diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java index ab1985c3..70a256bd 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java @@ -18,7 +18,7 @@ import co.aikar.commands.RootCommand; import com.google.common.collect.Sets; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; -import com.onarandombox.MultiverseCore.config.MVCoreConfigNodes; +import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; import com.onarandombox.MultiverseCore.destination.DestinationsProvider; import com.onarandombox.MultiverseCore.destination.ParsedDestination; import jakarta.inject.Inject; @@ -37,8 +37,9 @@ public class MVCommandCompletions extends PaperCommandCompletions { public MVCommandCompletions( @NotNull MVCommandManager mvCommandManager, @NotNull MVWorldManager worldManager, - @NotNull DestinationsProvider destinationsProvider - ) { + @NotNull DestinationsProvider destinationsProvider, + @NotNull MVCoreConfigProvider configProvider + ) { super(mvCommandManager); this.commandManager = mvCommandManager; this.worldManager = worldManager; @@ -48,7 +49,7 @@ public class MVCommandCompletions extends PaperCommandCompletions { registerAsyncCompletion("destinations", this::suggestDestinations); registerAsyncCompletion("flags", this::suggestFlags); registerStaticCompletion("gamerules", this::suggestGamerules); - registerStaticCompletion("mvconfigs", MVCoreConfigNodes.getNodes().getNames()); + registerStaticCompletion("mvconfigs", configProvider.getConfig().getNodes().getNames()); registerAsyncCompletion("mvworlds", this::suggestMVWorlds); setDefaultCompletion("destinations", ParsedDestination.class); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java index 394c4288..e403867c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java @@ -14,7 +14,7 @@ import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.commandtools.context.GameRuleValue; import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue; -import com.onarandombox.MultiverseCore.config.MVCoreConfigNodes; +import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; import com.onarandombox.MultiverseCore.destination.DestinationsProvider; import com.onarandombox.MultiverseCore.destination.ParsedDestination; import com.onarandombox.MultiverseCore.display.filters.ContentFilter; @@ -33,16 +33,19 @@ public class MVCommandContexts extends PaperCommandContexts { private final DestinationsProvider destinationsProvider; private final MVWorldManager worldManager; + private final MVCoreConfigProvider configProvider; @Inject public MVCommandContexts( MVCommandManager mvCommandManager, DestinationsProvider destinationsProvider, - MVWorldManager worldManager + MVWorldManager worldManager, + MVCoreConfigProvider configProvider ) { super(mvCommandManager); this.destinationsProvider = destinationsProvider; this.worldManager = worldManager; + this.configProvider = configProvider; registerIssuerOnlyContext(BukkitCommandIssuer.class, BukkitCommandExecutionContext::getIssuer); registerOptionalContext(ContentFilter.class, this::parseContentFilter); @@ -120,7 +123,7 @@ public class MVCommandContexts extends PaperCommandContexts { if (Strings.isNullOrEmpty(configName)) { throw new InvalidCommandArgument("No config name specified."); } - Optional node = MVCoreConfigNodes.getNodes().findNode(configName); + Optional node = configProvider.getConfig().getNodes().findNode(configName); if (node.isEmpty()) { throw new InvalidCommandArgument("The config " + configName + " is not valid."); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java index 5dcf8dcc..82ed3921 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java @@ -14,6 +14,8 @@ import com.onarandombox.MultiverseCore.configuration.migration.IntegerMigratorAc import com.onarandombox.MultiverseCore.configuration.migration.InvertBoolMigratorAction; import com.onarandombox.MultiverseCore.configuration.migration.MoveMigratorAction; import com.onarandombox.MultiverseCore.configuration.migration.VersionMigrator; +import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; +import org.bukkit.plugin.PluginManager; public class MVCoreConfig implements MVConfig { public static final String CONFIG_FILENAME = "config.yml"; @@ -33,17 +35,16 @@ public class MVCoreConfig implements MVConfig { } private final Path configPath; + private final MVCoreConfigNodes configNodes; private final ConfigHandle configHandle; public MVCoreConfig(MultiverseCore core) { configPath = Path.of(core.getDataFolder().getPath(), CONFIG_FILENAME); - - migrateFromOldConfigFile(); - + configNodes = new MVCoreConfigNodes(core.getService(PluginManager.class)); configHandle = ConfigHandle.builder(configPath) .logger(Logging.getLogger()) - .nodes(MVCoreConfigNodes.getNodes()) - .migrator(ConfigMigrator.builder(MVCoreConfigNodes.VERSION) + .nodes(configNodes.getNodes()) + .migrator(ConfigMigrator.builder(configNodes.VERSION) .addVersionMigrator(VersionMigrator.builder(5.0) .addAction(MoveMigratorAction.of("multiverse-configuration.enforceaccess", "world.enforce-access")) .addAction(BooleanMigratorAction.of("world.enforce-access")) @@ -73,6 +74,8 @@ public class MVCoreConfig implements MVConfig { .build()) .build()) .build(); + + migrateFromOldConfigFile(); } private void migrateFromOldConfigFile() { @@ -101,6 +104,11 @@ public class MVCoreConfig implements MVConfig { configHandle.save(); } + @Override + public NodeGroup getNodes() { + return configNodes.getNodes(); + } + @Override public Object getProperty(String name) { return configHandle.get(name); @@ -113,141 +121,141 @@ public class MVCoreConfig implements MVConfig { @Override public void setEnforceAccess(boolean enforceAccess) { - configHandle.set(MVCoreConfigNodes.ENFORCE_ACCESS, enforceAccess); + configHandle.set(configNodes.ENFORCE_ACCESS, enforceAccess); } @Override public boolean getEnforceAccess() { - return configHandle.get(MVCoreConfigNodes.ENFORCE_ACCESS); + return configHandle.get(configNodes.ENFORCE_ACCESS); } @Override public void setEnforceGameMode(boolean enforceGameMode) { - configHandle.set(MVCoreConfigNodes.ENFORCE_GAMEMODE, enforceGameMode); + configHandle.set(configNodes.ENFORCE_GAMEMODE, enforceGameMode); } @Override public boolean getEnforceGameMode() { - return configHandle.get(MVCoreConfigNodes.ENFORCE_GAMEMODE); + return configHandle.get(configNodes.ENFORCE_GAMEMODE); } @Override public void setAutoPurgeEntities(boolean autopurge) { - configHandle.set(MVCoreConfigNodes.AUTO_PURGE_ENTITIES, autopurge); + configHandle.set(configNodes.AUTO_PURGE_ENTITIES, autopurge); } @Override public boolean isAutoPurgeEntities() { - return configHandle.get(MVCoreConfigNodes.AUTO_PURGE_ENTITIES); + return configHandle.get(configNodes.AUTO_PURGE_ENTITIES); } @Override public void setTeleportIntercept(boolean teleportIntercept) { - configHandle.set(MVCoreConfigNodes.TELEPORT_INTERCEPT, teleportIntercept); + configHandle.set(configNodes.TELEPORT_INTERCEPT, teleportIntercept); } @Override public boolean getTeleportIntercept() { - return configHandle.get(MVCoreConfigNodes.TELEPORT_INTERCEPT); + return configHandle.get(configNodes.TELEPORT_INTERCEPT); } @Override public void setFirstSpawnOverride(boolean firstSpawnOverride) { - configHandle.set(MVCoreConfigNodes.FIRST_SPAWN_OVERRIDE, firstSpawnOverride); + configHandle.set(configNodes.FIRST_SPAWN_OVERRIDE, firstSpawnOverride); } @Override public boolean getFirstSpawnOverride() { - return configHandle.get(MVCoreConfigNodes.FIRST_SPAWN_OVERRIDE); + return configHandle.get(configNodes.FIRST_SPAWN_OVERRIDE); } @Override public void setFirstSpawnLocation(String firstSpawnWorld) { - configHandle.set(MVCoreConfigNodes.FIRST_SPAWN_LOCATION, firstSpawnWorld); + configHandle.set(configNodes.FIRST_SPAWN_LOCATION, firstSpawnWorld); } @Override public String getFirstSpawnLocation() { - return configHandle.get(MVCoreConfigNodes.FIRST_SPAWN_LOCATION); + return configHandle.get(configNodes.FIRST_SPAWN_LOCATION); } @Override public void setUseCustomPortalSearch(boolean useDefaultPortalSearch) { - configHandle.set(MVCoreConfigNodes.USE_CUSTOM_PORTAL_SEARCH, useDefaultPortalSearch); + configHandle.set(configNodes.USE_CUSTOM_PORTAL_SEARCH, useDefaultPortalSearch); } @Override public boolean isUsingCustomPortalSearch() { - return configHandle.get(MVCoreConfigNodes.USE_CUSTOM_PORTAL_SEARCH); + return configHandle.get(configNodes.USE_CUSTOM_PORTAL_SEARCH); } @Override public void setCustomPortalSearchRadius(int searchRadius) { - configHandle.set(MVCoreConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS, searchRadius); + configHandle.set(configNodes.CUSTOM_PORTAL_SEARCH_RADIUS, searchRadius); } @Override public int getCustomPortalSearchRadius() { - return configHandle.get(MVCoreConfigNodes.CUSTOM_PORTAL_SEARCH_RADIUS); + return configHandle.get(configNodes.CUSTOM_PORTAL_SEARCH_RADIUS); } @Override public void setEnablePrefixChat(boolean prefixChat) { - configHandle.set(MVCoreConfigNodes.ENABLE_CHAT_PREFIX, prefixChat); + configHandle.set(configNodes.ENABLE_CHAT_PREFIX, prefixChat); } @Override public boolean isEnablePrefixChat() { - return configHandle.get(MVCoreConfigNodes.ENABLE_CHAT_PREFIX); + return configHandle.get(configNodes.ENABLE_CHAT_PREFIX); } @Override public void setPrefixChatFormat(String prefixChatFormat) { - configHandle.set(MVCoreConfigNodes.CHAT_PREFIX_FORMAT, prefixChatFormat); + configHandle.set(configNodes.CHAT_PREFIX_FORMAT, prefixChatFormat); } @Override public String getPrefixChatFormat() { - return configHandle.get(MVCoreConfigNodes.CHAT_PREFIX_FORMAT); + return configHandle.get(configNodes.CHAT_PREFIX_FORMAT); } @Override public void setRegisterPapiHook(boolean registerPapiHook) { - configHandle.set(MVCoreConfigNodes.REGISTER_PAPI_HOOK, registerPapiHook); + configHandle.set(configNodes.REGISTER_PAPI_HOOK, registerPapiHook); } @Override public boolean isRegisterPapiHook() { - return configHandle.get(MVCoreConfigNodes.REGISTER_PAPI_HOOK); + return configHandle.get(configNodes.REGISTER_PAPI_HOOK); } @Override public void setGlobalDebug(int globalDebug) { - configHandle.set(MVCoreConfigNodes.GLOBAL_DEBUG, globalDebug); + configHandle.set(configNodes.GLOBAL_DEBUG, globalDebug); } @Override public int getGlobalDebug() { - return configHandle.get(MVCoreConfigNodes.GLOBAL_DEBUG); + return configHandle.get(configNodes.GLOBAL_DEBUG); } @Override public void setSilentStart(boolean silentStart) { - configHandle.set(MVCoreConfigNodes.SILENT_START, silentStart); + configHandle.set(configNodes.SILENT_START, silentStart); } @Override public boolean getSilentStart() { - return configHandle.get(MVCoreConfigNodes.SILENT_START); + return configHandle.get(configNodes.SILENT_START); } @Override public void setShowDonateMessage(boolean showDonateMessage) { - configHandle.set(MVCoreConfigNodes.SHOW_DONATION_MESSAGE, showDonateMessage); + configHandle.set(configNodes.SHOW_DONATION_MESSAGE, showDonateMessage); } @Override public boolean isShowingDonateMessage() { - return configHandle.get(MVCoreConfigNodes.SHOW_DONATION_MESSAGE); + return configHandle.get(configNodes.SHOW_DONATION_MESSAGE); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java index 0833a655..65cf48c3 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java @@ -5,20 +5,27 @@ import com.onarandombox.MultiverseCore.configuration.node.MVCommentedNode; import com.onarandombox.MultiverseCore.configuration.node.MVValueNode; import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; +import org.bukkit.plugin.PluginManager; -public class MVCoreConfigNodes { - private static final NodeGroup nodes = new NodeGroup(); +class MVCoreConfigNodes { - public static NodeGroup getNodes() { + private final NodeGroup nodes = new NodeGroup(); + private final PluginManager pluginManager; + + MVCoreConfigNodes(PluginManager pluginManager) { + this.pluginManager = pluginManager; + } + + public NodeGroup getNodes() { return nodes; } - private static N node(N node) { + private N node(N node) { nodes.add(node); return node; } - private static final MVCommentedNode HEADER = node(MVCommentedNode.builder("world") // TODO hacky way to get the header to the top of the file + private final MVCommentedNode HEADER = node(MVCommentedNode.builder("world") // TODO hacky way to get the header to the top of the file .comment("####################################################################################################") .comment("# #") .comment("# █▀▄▀█ █░█ █░░ ▀█▀ █ █░█ █▀▀ █▀█ █▀ █▀▀   █▀▀ █▀█ █▀█ █▀▀ #") @@ -42,12 +49,12 @@ public class MVCoreConfigNodes { .comment("") .build()); -// private static final MVCommentedNode WORLD_HEADER = node(MVCommentedNode.builder("world") +// private final MVCommentedNode WORLD_HEADER = node(MVCommentedNode.builder("world") // .comment("") // .comment("") // .build()); - public static final MVValueNode ENFORCE_ACCESS = node(MVValueNode.builder("world.enforce-access", Boolean.class) + public final MVValueNode ENFORCE_ACCESS = node(MVValueNode.builder("world.enforce-access", Boolean.class) .comment("This setting will prevent players from entering worlds they don't have access to.") .comment("If this is set to false, players will be able to enter any world they want.") .comment("If this is set to true, players will only be able to enter worlds they have") @@ -56,7 +63,7 @@ public class MVCoreConfigNodes { .name("enforce-access") .build()); - public static final MVValueNode ENFORCE_GAMEMODE = node(MVValueNode.builder("world.enforce-gamemode", Boolean.class) + public final MVValueNode ENFORCE_GAMEMODE = node(MVValueNode.builder("world.enforce-gamemode", Boolean.class) .comment("") .comment("Sets whether Multiverse will should enforce gamemode on world change.") .comment("If enabled, players will be forced into the gamemode of the world they are entering, unless they have") @@ -65,14 +72,14 @@ public class MVCoreConfigNodes { .name("enforce-gamemode") .build()); - public static final MVValueNode AUTO_PURGE_ENTITIES = node(MVValueNode.builder("world.auto-purge-entities", Boolean.class) + public final MVValueNode AUTO_PURGE_ENTITIES = node(MVValueNode.builder("world.auto-purge-entities", Boolean.class) .comment("") .comment("Sets whether Multiverse will purge mobs and entities with be automatically.") .defaultValue(false) .name("auto-purge-entities") .build()); - public static final MVValueNode TELEPORT_INTERCEPT = node(MVValueNode.builder("world.teleport-intercept", Boolean.class) + public final MVValueNode TELEPORT_INTERCEPT = node(MVValueNode.builder("world.teleport-intercept", Boolean.class) .comment("") .comment("If this is set to true, Multiverse will enforce access permissions for all teleportation,") .comment("including teleportation from other plugins.") @@ -80,12 +87,12 @@ public class MVCoreConfigNodes { .name("teleport-intercept") .build()); - private static final MVCommentedNode SPAWN_HEADER = node(MVCommentedNode.builder("spawn") + private final MVCommentedNode SPAWN_HEADER = node(MVCommentedNode.builder("spawn") .comment("") .comment("") .build()); - public static final MVValueNode FIRST_SPAWN_OVERRIDE = node(MVValueNode.builder("spawn.first-spawn-override", Boolean.class) + public final MVValueNode FIRST_SPAWN_OVERRIDE = node(MVValueNode.builder("spawn.first-spawn-override", Boolean.class) .comment("Sets whether Multiverse will override the first spawn location of a world.") .comment("If enabled, Multiverse will set the first spawn location of a world to the spawn location of the world.") .comment("If disabled, it will default to server.properties settings.") @@ -93,7 +100,7 @@ public class MVCoreConfigNodes { .name("first-spawn-override") .build()); - public static final MVValueNode FIRST_SPAWN_LOCATION = node(MVValueNode.builder("spawn.first-spawn-location", String.class) + public final MVValueNode FIRST_SPAWN_LOCATION = node(MVValueNode.builder("spawn.first-spawn-location", String.class) .comment("") .comment("Sets the world that Multiverse will use as the location for players that first join the server.") .comment("This only applies if first-spawn-override is set to true.") @@ -101,19 +108,19 @@ public class MVCoreConfigNodes { .name("first-spawn-location") .build()); - private static final MVCommentedNode PORTAL_HEADER = node(MVCommentedNode.builder("portal") + private final MVCommentedNode PORTAL_HEADER = node(MVCommentedNode.builder("portal") .comment("") .comment("") .build()); - public static final MVValueNode USE_CUSTOM_PORTAL_SEARCH = node(MVValueNode.builder("portal.use-custom-portal-search", Boolean.class) + public final MVValueNode USE_CUSTOM_PORTAL_SEARCH = node(MVValueNode.builder("portal.use-custom-portal-search", Boolean.class) .comment("This config option defines whether or not Multiverse should interfere with's Bukkit's default portal search radius.") .comment("Setting it to false would mean you want to simply let Bukkit decides the search radius itself.") .defaultValue(false) .name("use-custom-portal-search") .build()); - public static final MVValueNode CUSTOM_PORTAL_SEARCH_RADIUS = node(MVValueNode.builder("portal.custom-portal-search-radius", Integer.class) + public final MVValueNode CUSTOM_PORTAL_SEARCH_RADIUS = node(MVValueNode.builder("portal.custom-portal-search-radius", Integer.class) .comment("") .comment("This config option defines the search radius Multiverse should use when searching for a portal.") .comment("This only applies if use-custom-portal-search is set to true.") @@ -122,19 +129,19 @@ public class MVCoreConfigNodes { .validator(value -> value >= 0) .build()); - private static final MVCommentedNode MESSAGING_HEADER = node(MVCommentedNode.builder("messaging") + private final MVCommentedNode MESSAGING_HEADER = node(MVCommentedNode.builder("messaging") .comment("") .comment("") .build()); - public static final MVValueNode ENABLE_CHAT_PREFIX = node(MVValueNode.builder("messaging.enable-chat-prefix", Boolean.class) + public final MVValueNode ENABLE_CHAT_PREFIX = node(MVValueNode.builder("messaging.enable-chat-prefix", Boolean.class) .comment("This config option defines whether or not Multiverse should prefix the chat with the world name.") .comment("This only applies if use-custom-portal-search is set to true.") .defaultValue(false) .name("enable-chat-prefix") .build()); - public static final MVValueNode CHAT_PREFIX_FORMAT = node(MVValueNode.builder("messaging.chat-prefix-format", String.class) + public final MVValueNode CHAT_PREFIX_FORMAT = node(MVValueNode.builder("messaging.chat-prefix-format", String.class) .comment("") .comment("This config option defines the format Multiverse should use when prefixing the chat with the world name.") .comment("This only applies if enable-chat-prefix is set to true.") @@ -142,7 +149,7 @@ public class MVCoreConfigNodes { .name("chat-prefix-format") .build()); - public static final MVValueNode REGISTER_PAPI_HOOK = node(MVValueNode.builder("messaging.register-papi-hook", Boolean.class) + public final MVValueNode REGISTER_PAPI_HOOK = node(MVValueNode.builder("messaging.register-papi-hook", Boolean.class) .comment("") .comment("This config option defines whether or not Multiverse should register the PlaceholderAPI hook.") .comment("This only applies if PlaceholderAPI is installed.") @@ -150,12 +157,12 @@ public class MVCoreConfigNodes { .name("register-papi-hook") .build()); - private static final MVCommentedNode MISC_HEADER = node(MVCommentedNode.builder("misc") + private final MVCommentedNode MISC_HEADER = node(MVCommentedNode.builder("misc") .comment("") .comment("") .build()); - public static final MVValueNode GLOBAL_DEBUG = node(MVValueNode.builder("misc.global-debug", Integer.class) + public final MVValueNode GLOBAL_DEBUG = node(MVValueNode.builder("misc.global-debug", Integer.class) .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.") @@ -166,10 +173,10 @@ public class MVCoreConfigNodes { .defaultValue(0) .name("global-debug") .validator(value -> value >= 0 && value <= 3) - .onSetValue((oldValue, newValue) -> Logging.setDebugLevel(newValue)) + .onSetValue((oldValue, newValue) -> Logging.setDebugLevel(newValue)) //TODO Debug event .build()); - public static final MVValueNode SILENT_START = node(MVValueNode.builder("misc.silent-start", Boolean.class) + public final MVValueNode SILENT_START = node(MVValueNode.builder("misc.silent-start", Boolean.class) .comment("") .comment("If true, the startup console messages will no longer show.") .defaultValue(false) @@ -177,14 +184,14 @@ public class MVCoreConfigNodes { .onSetValue((oldValue, newValue) -> Logging.setShowingConfig(!newValue)) .build()); - public static final MVValueNode SHOW_DONATION_MESSAGE = node(MVValueNode.builder("misc.show-donation-message", Boolean.class) + public final MVValueNode SHOW_DONATION_MESSAGE = node(MVValueNode.builder("misc.show-donation-message", Boolean.class) .comment("") .comment("If you don't want to donate, you can set this to false and Multiverse will stop nagging you.") .defaultValue(true) .name("show-donation-message") .build()); - public static final MVValueNode VERSION = node(MVValueNode.builder("version", Double.class) + public final MVValueNode VERSION = node(MVValueNode.builder("version", Double.class) .comment("") .comment("") .comment("This just signifies the version number so we can see what version of config you have.") From 9f1ddcc81eb0f75d2fbd663e7143dab28ee6a512 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Mon, 27 Mar 2023 12:25:13 +0800 Subject: [PATCH 28/37] refactor: Remove use of MVCoreConfigProvider --- .../MultiverseCore/MultiverseCore.java | 50 +++-------- .../MultiverseCore/anchor/AnchorManager.java | 12 +-- .../MultiverseCore/api/MVConfig.java | 12 ++- .../MultiverseCore/api/MVCore.java | 13 --- .../MultiverseCore/api/MVWorldManager.java | 5 +- .../commands/ConfigCommand.java | 20 ++--- .../MultiverseCore/commands/DebugCommand.java | 14 +-- .../commands/ReloadCommand.java | 15 ++-- .../commandtools/MVCommandCompletions.java | 6 +- .../commandtools/MVCommandContexts.java | 10 +-- .../MultiverseCore/config/MVCoreConfig.java | 39 ++++---- .../config/MVCoreConfigNodes.java | 13 ++- .../config/MVCoreConfigProvider.java | 89 ------------------- .../listeners/MVChatListener.java | 12 +-- .../listeners/MVEntityListener.java | 12 +-- .../listeners/MVPlayerListener.java | 20 ++--- .../MultiverseCore/utils/MVPermissions.java | 10 +-- .../MultiverseCore/utils/PermissionTools.java | 12 +-- .../utils/UnsafeCallWrapper.java | 10 +-- .../MultiverseCore/world/SimpleMVWorld.java | 14 +-- .../world/SimpleMVWorldManager.java | 19 ++-- .../multiverse/core/inject/InjectionTest.kt | 13 +-- 22 files changed, 152 insertions(+), 268 deletions(-) delete mode 100644 src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index adfa66b9..e7274072 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -23,7 +23,7 @@ import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand; -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import com.onarandombox.MultiverseCore.config.MVCoreConfig; import com.onarandombox.MultiverseCore.destination.DestinationsProvider; import com.onarandombox.MultiverseCore.economy.MVEconomist; import com.onarandombox.MultiverseCore.inject.InjectableListener; @@ -55,7 +55,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore { private ServiceLocator serviceLocator; @Inject - private MVCoreConfigProvider configProvider; + private MVCoreConfig config; @Inject private Provider worldManagerProvider; @Inject @@ -100,8 +100,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore { initializeDependencyInjection(); // Load our configs first as we need them for everything else. - this.loadConfigs(); - if (!getConfigProvider().isConfigLoaded()) { + if (config == null || !config.isLoaded()) { Logging.severe("Your configs were not loaded."); Logging.severe("Please check your configs and restart the server."); this.getServer().getPluginManager().disablePlugin(this); @@ -112,15 +111,16 @@ public class MultiverseCore extends JavaPlugin implements MVCore { var worldManager = worldManagerProvider.get(); + worldManager.loadWorldsConfig(); worldManager.getDefaultWorldGenerators(); worldManager.loadDefaultWorlds(); worldManager.loadWorlds(true); // Now set the firstspawnworld (after the worlds are loaded): - worldManager.setFirstSpawnWorld(getConfigProvider().getConfig().getFirstSpawnLocation()); + worldManager.setFirstSpawnWorld(config.getFirstSpawnLocation()); MVWorld firstSpawnWorld = worldManager.getFirstSpawnWorld(); if (firstSpawnWorld != null) { - getConfigProvider().getConfig().setFirstSpawnLocation(firstSpawnWorld.getName()); + config.setFirstSpawnLocation(firstSpawnWorld.getName()); } //Setup economy here so vault is loaded @@ -134,7 +134,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore { this.registerDestinations(); this.setupMetrics(); this.loadPlaceholderAPIIntegration(); - this.saveMVConfig(); + this.saveAllConfigs(); this.logEnableMessage(); } @@ -168,7 +168,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore { } private boolean shouldShowConfig() { - return !getConfigProvider().getConfig().getSilentStart(); + return !config.getSilentStart(); } private void loadEconomist() { @@ -251,24 +251,20 @@ public class MultiverseCore extends JavaPlugin implements MVCore { private void logEnableMessage() { Logging.config("Version %s (API v%s) Enabled - By %s", this.getDescription().getVersion(), PROTOCOL, getAuthors()); - if (getConfigProvider().getConfig().isShowingDonateMessage()) { + if (config.isShowingDonateMessage()) { Logging.config("Help dumptruckman keep this project alive. Become a patron! https://www.patreon.com/dumptruckman"); Logging.config("One time donations are also appreciated: https://www.paypal.me/dumptruckman"); } } private void loadPlaceholderAPIIntegration() { - if (getConfigProvider().getConfig().isRegisterPapiHook() + if (config.isRegisterPapiHook() && getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) { Try.run(() -> serviceLocator.createAndInitialize(MultiverseCorePlaceholders.class)) .onFailure(e -> Logging.severe("Failed to load PlaceholderAPI integration.", e)); } } - private MVCoreConfigProvider getConfigProvider() { - return configProvider; - } - /** * {@inheritDoc} */ @@ -339,34 +335,14 @@ public class MultiverseCore extends JavaPlugin implements MVCore { this.pluginCount -= 1; } - /** - * {@inheritDoc} - */ - @Override - public void loadConfigs() { - getConfigProvider().loadConfigs(); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean saveMVConfig() { - return getConfigProvider().saveConfig() - .map(v -> true) - .recover(e -> { - Logging.severe(e.getMessage(), e); - return false; - }) - .get(); - } - /** * {@inheritDoc} */ @Override public boolean saveAllConfigs() { - return this.saveMVConfig() && worldManagerProvider.get().saveWorldsConfig(); + return config.save() + && worldManagerProvider.get().saveWorldsConfig() + && anchorManagerProvider.get().saveAnchors(); } //TODO: REMOVE THIS STATIC CRAP - START diff --git a/src/main/java/com/onarandombox/MultiverseCore/anchor/AnchorManager.java b/src/main/java/com/onarandombox/MultiverseCore/anchor/AnchorManager.java index 18cfa0d5..3d256f18 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/anchor/AnchorManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/anchor/AnchorManager.java @@ -10,7 +10,7 @@ package com.onarandombox.MultiverseCore.anchor; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.LocationManipulation; -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import com.onarandombox.MultiverseCore.config.MVCoreConfig; import jakarta.inject.Inject; import org.bukkit.Location; import org.bukkit.configuration.ConfigurationSection; @@ -38,17 +38,17 @@ public class AnchorManager { private final Plugin plugin; private final LocationManipulation locationManipulation; - private final MVCoreConfigProvider configProvider; + private final MVCoreConfig config; @Inject public AnchorManager( MultiverseCore plugin, LocationManipulation locationManipulation, - MVCoreConfigProvider configProvider + MVCoreConfig config ) { this.plugin = plugin; this.locationManipulation = locationManipulation; - this.configProvider = configProvider; + this.config = config; this.anchors = new HashMap(); } @@ -160,8 +160,8 @@ public class AnchorManager { // Add to the list if we're not enforcing access // OR // We are enforcing access and the user has the permission. - if (!this.configProvider.getConfig().getEnforceAccess() || - (this.configProvider.getConfig().getEnforceAccess() && p.hasPermission(worldPerm))) { + if (!config.getEnforceAccess() || + (config.getEnforceAccess() && p.hasPermission(worldPerm))) { myAnchors.add(anchor); } else { Logging.finer(String.format("Not adding anchor %s to the list, user %s doesn't have the %s " + diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java index 393273f3..090fc296 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVConfig.java @@ -1,10 +1,10 @@ package com.onarandombox.MultiverseCore.api; -import java.util.Collection; - import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; import com.onarandombox.MultiverseCore.placeholders.MultiverseCorePlaceholders; +import org.jvnet.hk2.annotations.Contract; +@Contract public interface MVConfig { /** @@ -13,10 +13,16 @@ public interface MVConfig { */ boolean load(); + /** + * Whether the config has been loaded. + * @return True if the config has been loaded. + */ + boolean isLoaded(); + /** * Saves the config to disk. */ - void save(); + boolean save(); /** * Gets the nodes for the config. diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVCore.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVCore.java index acb9d980..22c24a3f 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVCore.java @@ -14,19 +14,6 @@ package com.onarandombox.MultiverseCore.api; */ public interface MVCore extends MVPlugin { - /** - * Reloads the Multiverse Configuration files: - * worlds.yml and config.yml. - */ - void loadConfigs(); - - /** - * Saves the Multiverse-Config. - * - * @return Whether the Multiverse-Config was successfully saved - */ - boolean saveMVConfig(); - /** * Saves all configs. * diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java index 3640fb86..c5b21f79 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java @@ -7,11 +7,9 @@ package com.onarandombox.MultiverseCore.api; -import java.io.File; import java.util.Collection; import java.util.List; -import com.onarandombox.MultiverseCore.world.SimpleWorldPurger; import org.bukkit.World; import org.bukkit.World.Environment; import org.bukkit.WorldType; @@ -246,10 +244,9 @@ public interface MVWorldManager { /** * Load the config from a file. * - * @param file The file to load. * @return A loaded configuration. */ - FileConfiguration loadWorldConfig(File file); + FileConfiguration loadWorldsConfig(); /** * Saves the world config to disk. diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java index 1944f121..484a3228 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java @@ -13,7 +13,7 @@ import com.onarandombox.MultiverseCore.api.MVConfig; import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand; import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue; -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import com.onarandombox.MultiverseCore.config.MVCoreConfig; import jakarta.inject.Inject; import org.jetbrains.annotations.NotNull; import org.jvnet.hk2.annotations.Service; @@ -22,16 +22,12 @@ import org.jvnet.hk2.annotations.Service; @CommandAlias("mv") public class ConfigCommand extends MultiverseCommand { - private final MVCoreConfigProvider configProvider; + private final MVCoreConfig config; @Inject - public ConfigCommand(@NotNull MVCommandManager commandManager, @NotNull MVCoreConfigProvider configProvider) { + public ConfigCommand(@NotNull MVCommandManager commandManager, @NotNull MVCoreConfig config) { super(commandManager); - this.configProvider = configProvider; - } - - private MVConfig getConfig() { - return configProvider.getConfig(); + this.config = config; } @Subcommand("config") @@ -59,20 +55,20 @@ public class ConfigCommand extends MultiverseCommand { } private void showConfigValue(BukkitCommandIssuer issuer, String name) { - Object currentValue = getConfig().getProperty(name); + Object currentValue = config.getProperty(name); if (currentValue == null) { issuer.sendMessage("No such config option: " + name); return; } - issuer.sendMessage(name + "is currently set to " + getConfig().getProperty(name)); + issuer.sendMessage(name + "is currently set to " + config.getProperty(name)); } private void updateConfigValue(BukkitCommandIssuer issuer, String name, Object value) { - if (!getConfig().setProperty(name, value)) { + if (!config.setProperty(name, value)) { issuer.sendMessage("Unable to set " + name + " to " + value); return; } - getConfig().save(); + config.save(); issuer.sendMessage("Successfully set " + name + " to " + value); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java index cabe4cc1..0632dff2 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java @@ -11,7 +11,7 @@ import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand; -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import com.onarandombox.MultiverseCore.config.MVCoreConfig; import com.onarandombox.MultiverseCore.utils.MVCorei18n; import jakarta.inject.Inject; import org.jetbrains.annotations.NotNull; @@ -21,17 +21,17 @@ import org.jvnet.hk2.annotations.Service; @CommandAlias("mv") public class DebugCommand extends MultiverseCommand { - private final MVCoreConfigProvider configProvider; + private final MVCoreConfig config; private final MultiverseCore plugin; @Inject public DebugCommand( @NotNull MVCommandManager commandManager, - @NotNull MVCoreConfigProvider configProvider, + @NotNull MVCoreConfig config, @NotNull MultiverseCore plugin ) { super(commandManager); - this.configProvider = configProvider; + this.config = config; this.plugin = plugin; } @@ -53,13 +53,13 @@ public class DebugCommand extends MultiverseCommand { @Description("{@@mv-core.debug.change.level.description}") int level) { - this.configProvider.getConfig().setGlobalDebug(level); - this.plugin.saveAllConfigs(); + config.setGlobalDebug(level); + config.save(); this.displayDebugMode(issuer); } private void displayDebugMode(BukkitCommandIssuer issuer) { - final int debugLevel = this.configProvider.getConfig().getGlobalDebug(); + final int debugLevel = config.getGlobalDebug(); if (debugLevel == 0) { issuer.sendInfo(MVCorei18n.DEBUG_INFO_OFF); return; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java index 57e95392..e75421f8 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ReloadCommand.java @@ -8,11 +8,13 @@ import co.aikar.commands.annotation.CommandAlias; import co.aikar.commands.annotation.CommandPermission; import co.aikar.commands.annotation.Description; import co.aikar.commands.annotation.Subcommand; +import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.anchor.AnchorManager; +import com.onarandombox.MultiverseCore.api.MVCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand; -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import com.onarandombox.MultiverseCore.config.MVCoreConfig; import com.onarandombox.MultiverseCore.event.MVConfigReloadEvent; import com.onarandombox.MultiverseCore.utils.MVCorei18n; import jakarta.inject.Inject; @@ -24,7 +26,7 @@ import org.jvnet.hk2.annotations.Service; @CommandAlias("mv") public class ReloadCommand extends MultiverseCommand { - private final MVCoreConfigProvider configProvider; + private final MVCoreConfig config; private final AnchorManager anchorManager; private final MVWorldManager worldManager; private final PluginManager pluginManager; @@ -32,13 +34,13 @@ public class ReloadCommand extends MultiverseCommand { @Inject public ReloadCommand( @NotNull MVCommandManager commandManager, - @NotNull MVCoreConfigProvider configProvider, + @NotNull MVCoreConfig config, @NotNull AnchorManager anchorManager, @NotNull MVWorldManager worldManager, @NotNull PluginManager pluginManager ) { super(commandManager); - this.configProvider = configProvider; + this.config = config; this.anchorManager = anchorManager; this.worldManager = worldManager; this.pluginManager = pluginManager; @@ -49,9 +51,10 @@ public class ReloadCommand extends MultiverseCommand { @Description("{@@mv-core.reload.description}") public void onReloadCommand(@NotNull BukkitCommandIssuer issuer) { issuer.sendInfo(MVCorei18n.RELOAD_RELOADING); - this.configProvider.loadConfigs(); - this.anchorManager.loadAnchors(); + this.config.load(); + this.worldManager.loadWorldsConfig(); this.worldManager.loadWorlds(true); + this.anchorManager.loadAnchors(); List configsLoaded = new ArrayList<>(); configsLoaded.add("Multiverse-Core - config.yml"); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java index 70a256bd..75d8d1dd 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandCompletions.java @@ -18,7 +18,7 @@ import co.aikar.commands.RootCommand; import com.google.common.collect.Sets; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import com.onarandombox.MultiverseCore.config.MVCoreConfig; import com.onarandombox.MultiverseCore.destination.DestinationsProvider; import com.onarandombox.MultiverseCore.destination.ParsedDestination; import jakarta.inject.Inject; @@ -38,7 +38,7 @@ public class MVCommandCompletions extends PaperCommandCompletions { @NotNull MVCommandManager mvCommandManager, @NotNull MVWorldManager worldManager, @NotNull DestinationsProvider destinationsProvider, - @NotNull MVCoreConfigProvider configProvider + @NotNull MVCoreConfig config ) { super(mvCommandManager); this.commandManager = mvCommandManager; @@ -49,7 +49,7 @@ public class MVCommandCompletions extends PaperCommandCompletions { registerAsyncCompletion("destinations", this::suggestDestinations); registerAsyncCompletion("flags", this::suggestFlags); registerStaticCompletion("gamerules", this::suggestGamerules); - registerStaticCompletion("mvconfigs", configProvider.getConfig().getNodes().getNames()); + registerStaticCompletion("mvconfigs", config.getNodes().getNames()); registerAsyncCompletion("mvworlds", this::suggestMVWorlds); setDefaultCompletion("destinations", ParsedDestination.class); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java index e403867c..3b1ea6b9 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandtools/MVCommandContexts.java @@ -14,7 +14,7 @@ import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.commandtools.context.GameRuleValue; import com.onarandombox.MultiverseCore.commandtools.context.MVConfigValue; -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import com.onarandombox.MultiverseCore.config.MVCoreConfig; import com.onarandombox.MultiverseCore.destination.DestinationsProvider; import com.onarandombox.MultiverseCore.destination.ParsedDestination; import com.onarandombox.MultiverseCore.display.filters.ContentFilter; @@ -33,19 +33,19 @@ public class MVCommandContexts extends PaperCommandContexts { private final DestinationsProvider destinationsProvider; private final MVWorldManager worldManager; - private final MVCoreConfigProvider configProvider; + private final MVCoreConfig config; @Inject public MVCommandContexts( MVCommandManager mvCommandManager, DestinationsProvider destinationsProvider, MVWorldManager worldManager, - MVCoreConfigProvider configProvider + MVCoreConfig config ) { super(mvCommandManager); this.destinationsProvider = destinationsProvider; this.worldManager = worldManager; - this.configProvider = configProvider; + this.config = config; registerIssuerOnlyContext(BukkitCommandIssuer.class, BukkitCommandExecutionContext::getIssuer); registerOptionalContext(ContentFilter.class, this::parseContentFilter); @@ -123,7 +123,7 @@ public class MVCommandContexts extends PaperCommandContexts { if (Strings.isNullOrEmpty(configName)) { throw new InvalidCommandArgument("No config name specified."); } - Optional node = configProvider.getConfig().getNodes().findNode(configName); + Optional node = config.getNodes().findNode(configName); if (node.isEmpty()) { throw new InvalidCommandArgument("The config " + configName + " is not valid."); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java index 82ed3921..cb568320 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java @@ -15,33 +15,28 @@ import com.onarandombox.MultiverseCore.configuration.migration.InvertBoolMigrato import com.onarandombox.MultiverseCore.configuration.migration.MoveMigratorAction; import com.onarandombox.MultiverseCore.configuration.migration.VersionMigrator; import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; +import jakarta.inject.Inject; import org.bukkit.plugin.PluginManager; +import org.jetbrains.annotations.NotNull; +import org.jvnet.hk2.annotations.Service; +@Service public class MVCoreConfig implements MVConfig { public static final String CONFIG_FILENAME = "config.yml"; public static final double CONFIG_VERSION = 5.0; - /** - * Creates a new DefaultMVConfig instance and loads the configuration automatically. - * - * @param core The MultiverseCore instance. - * @return The new DefaultMVConfig instance. - */ - public static MVCoreConfig init(MultiverseCore core) { - var config = new MVCoreConfig(core); - config.load(); - config.save(); - return config; - } - private final Path configPath; private final MVCoreConfigNodes configNodes; private final ConfigHandle configHandle; - public MVCoreConfig(MultiverseCore core) { - configPath = Path.of(core.getDataFolder().getPath(), CONFIG_FILENAME); - configNodes = new MVCoreConfigNodes(core.getService(PluginManager.class)); - configHandle = ConfigHandle.builder(configPath) + @Inject + MVCoreConfig( + @NotNull MultiverseCore core, + @NotNull PluginManager pluginManager + ) { + this.configPath = Path.of(core.getDataFolder().getPath(), CONFIG_FILENAME); + this.configNodes = new MVCoreConfigNodes(pluginManager); + this.configHandle = ConfigHandle.builder(configPath) .logger(Logging.getLogger()) .nodes(configNodes.getNodes()) .migrator(ConfigMigrator.builder(configNodes.VERSION) @@ -76,6 +71,8 @@ public class MVCoreConfig implements MVConfig { .build(); migrateFromOldConfigFile(); + load(); + save(); } private void migrateFromOldConfigFile() { @@ -100,8 +97,14 @@ public class MVCoreConfig implements MVConfig { } @Override - public void save() { + public boolean isLoaded() { + return configHandle.isLoaded(); + } + + @Override + public boolean save() { configHandle.save(); + return true; } @Override diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java index 65cf48c3..ae027b4e 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigNodes.java @@ -4,13 +4,16 @@ import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.configuration.node.MVCommentedNode; import com.onarandombox.MultiverseCore.configuration.node.MVValueNode; import com.onarandombox.MultiverseCore.configuration.node.NodeGroup; +import com.onarandombox.MultiverseCore.event.MVDebugModeEvent; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; +import jakarta.inject.Inject; import org.bukkit.plugin.PluginManager; +import org.jvnet.hk2.annotations.Service; class MVCoreConfigNodes { private final NodeGroup nodes = new NodeGroup(); - private final PluginManager pluginManager; + private PluginManager pluginManager; MVCoreConfigNodes(PluginManager pluginManager) { this.pluginManager = pluginManager; @@ -173,7 +176,13 @@ class MVCoreConfigNodes { .defaultValue(0) .name("global-debug") .validator(value -> value >= 0 && value <= 3) - .onSetValue((oldValue, newValue) -> Logging.setDebugLevel(newValue)) //TODO Debug event + .onSetValue((oldValue, newValue) -> { + int level = Logging.getDebugLevel(); + Logging.setDebugLevel(newValue); + if (level != Logging.getDebugLevel()) { + pluginManager.callEvent(new MVDebugModeEvent(level)); + } + }) .build()); public final MVValueNode SILENT_START = node(MVValueNode.builder("misc.silent-start", Boolean.class) diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java deleted file mode 100644 index 15bd1acd..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfigProvider.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.onarandombox.MultiverseCore.config; - -import com.dumptruckman.minecraft.util.Logging; -import com.onarandombox.MultiverseCore.MultiverseCore; -import com.onarandombox.MultiverseCore.api.MVConfig; -import com.onarandombox.MultiverseCore.api.MVWorldManager; -import com.onarandombox.MultiverseCore.event.MVDebugModeEvent; -import io.vavr.control.Try; -import jakarta.inject.Inject; -import jakarta.inject.Provider; -import org.bukkit.plugin.PluginManager; -import org.jetbrains.annotations.NotNull; -import org.jvnet.hk2.annotations.Service; - -import java.io.File; - -@Service -public final class MVCoreConfigProvider { - - private static final String WORLDS_CONFIG_FILE = "worlds.yml"; - - private volatile MVCoreConfig config; - - private final MultiverseCore plugin; - private final PluginManager pluginManager; - - private final Provider worldManagerProvider; // TODO remove this dependency - - @Inject - MVCoreConfigProvider( - MultiverseCore plugin, - PluginManager pluginManager, - Provider worldManagerProvider - ) { - this.plugin = plugin; - this.pluginManager = pluginManager; - this.worldManagerProvider = worldManagerProvider; - } - - /** - * Checks if the config is loaded. - * - * @return True if the config is loaded, false otherwise - */ - public boolean isConfigLoaded() { - return config != null; - } - - /** - * Gets the Core configuration instance. If the config has not been loaded yet, it will first be loaded. - * - * @return The config - */ - @NotNull - public MVConfig getConfig() { - if (config == null) { - loadConfigs(); - } - return config; - } - - public void loadConfigs() { - config = MVCoreConfig.init(plugin); - - loadWorldConfigs(); - - setDebugLevelFromConfig(config); - } - - private void loadWorldConfigs() { - worldManagerProvider.get().loadWorldConfig(new File(plugin.getDataFolder(), WORLDS_CONFIG_FILE)); - } - - private void setDebugLevelFromConfig(@NotNull MVConfig config) { - int level = Logging.getDebugLevel(); - Logging.setDebugLevel(config.getGlobalDebug()); - if (level != Logging.getDebugLevel()) { - pluginManager.callEvent(new MVDebugModeEvent(level)); - } - } - - @NotNull - public Try saveConfig() { - if (config != null) { - return Try.run(() -> config.save()); - } - return Try.failure(new IllegalStateException("Config not loaded yet")); - } -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVChatListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVChatListener.java index 84652850..209556ac 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVChatListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVChatListener.java @@ -3,7 +3,7 @@ package com.onarandombox.MultiverseCore.listeners; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MVWorld; -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import com.onarandombox.MultiverseCore.config.MVCoreConfig; import com.onarandombox.MultiverseCore.inject.InjectableListener; import jakarta.inject.Inject; import org.bukkit.ChatColor; @@ -16,17 +16,17 @@ import org.jvnet.hk2.annotations.Service; */ @Service public class MVChatListener implements InjectableListener { - private final MVCoreConfigProvider configProvider; + private final MVCoreConfig config; private final MVWorldManager worldManager; private final MVPlayerListener playerListener; @Inject public MVChatListener( - MVCoreConfigProvider configProvider, + MVCoreConfig config, MVWorldManager worldManager, MVPlayerListener playerListener ) { - this.configProvider = configProvider; + this.config = config; this.worldManager = worldManager; this.playerListener = playerListener; } @@ -42,7 +42,7 @@ public class MVChatListener implements InjectableListener { } // Check whether the Server is set to prefix the chat with the World name. // If not we do nothing, if so we need to check if the World has an Alias. - if (configProvider.getConfig().isEnablePrefixChat()) { + if (config.isEnablePrefixChat()) { String world = playerListener.getPlayerWorld().get(event.getPlayer().getName()); if (world == null) { world = event.getPlayer().getWorld().getName(); @@ -60,7 +60,7 @@ public class MVChatListener implements InjectableListener { prefix = mvworld.getColoredWorldString(); String chat = event.getFormat(); - String prefixChatFormat = configProvider.getConfig().getPrefixChatFormat(); + String prefixChatFormat = config.getPrefixChatFormat(); prefixChatFormat = prefixChatFormat.replace("%world%", prefix).replace("%chat%", chat); prefixChatFormat = ChatColor.translateAlternateColorCodes('&', prefixChatFormat); diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java index 07124eb1..236452e1 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVEntityListener.java @@ -11,7 +11,7 @@ import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.WorldPurger; -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import com.onarandombox.MultiverseCore.config.MVCoreConfig; import com.onarandombox.MultiverseCore.inject.InjectableListener; import jakarta.inject.Inject; import org.bukkit.World; @@ -33,17 +33,17 @@ import org.jvnet.hk2.annotations.Service; */ @Service public class MVEntityListener implements InjectableListener { - private final MVCoreConfigProvider configProvider; + private final MVCoreConfig config; private final MVWorldManager worldManager; private final WorldPurger worldPurger; @Inject public MVEntityListener( - @NotNull MVCoreConfigProvider configProvider, + @NotNull MVCoreConfig config, @NotNull MVWorldManager worldManager, @NotNull WorldPurger worldPurger ) { - this.configProvider = configProvider; + this.config = config; this.worldManager = worldManager; this.worldPurger = worldPurger; } @@ -128,8 +128,8 @@ public class MVEntityListener implements InjectableListener { if (event.isCancelled() || event.getTo() == null) { return; } - if (!this.configProvider.getConfig().isUsingCustomPortalSearch()) { - event.setSearchRadius(this.configProvider.getConfig().getCustomPortalSearchRadius()); + if (!config.isUsingCustomPortalSearch()) { + event.setSearchRadius(config.getCustomPortalSearchRadius()); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java index 841c84ec..80d26546 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java @@ -15,7 +15,7 @@ import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import com.onarandombox.MultiverseCore.config.MVCoreConfig; import com.onarandombox.MultiverseCore.event.MVRespawnEvent; import com.onarandombox.MultiverseCore.inject.InjectableListener; import com.onarandombox.MultiverseCore.utils.MVPermissions; @@ -45,7 +45,7 @@ import org.jvnet.hk2.annotations.Service; @Service public class MVPlayerListener implements InjectableListener { private final Plugin plugin; - private final MVCoreConfigProvider configProvider; + private final MVCoreConfig config; private final Provider worldManagerProvider; private final PermissionTools pt; private final Provider mvPermsProvider; @@ -57,7 +57,7 @@ public class MVPlayerListener implements InjectableListener { @Inject public MVPlayerListener( MultiverseCore plugin, - MVCoreConfigProvider configProvider, + MVCoreConfig config, Provider worldManagerProvider, PermissionTools permissionTools, Provider mvPermsProvider, @@ -65,7 +65,7 @@ public class MVPlayerListener implements InjectableListener { Server server ) { this.plugin = plugin; - this.configProvider = configProvider; + this.config = config; this.worldManagerProvider = worldManagerProvider; this.pt = permissionTools; this.mvPermsProvider = mvPermsProvider; @@ -142,7 +142,7 @@ public class MVPlayerListener implements InjectableListener { Player p = event.getPlayer(); if (!p.hasPlayedBefore()) { Logging.finer("Player joined for the FIRST time!"); - if (configProvider.getConfig().getFirstSpawnOverride()) { + if (config.getFirstSpawnOverride()) { Logging.fine("Moving NEW player to(firstspawnoverride): " + getWorldManager().getFirstSpawnWorld().getSpawnLocation()); this.sendPlayerToDefaultWorld(p); @@ -150,7 +150,7 @@ public class MVPlayerListener implements InjectableListener { return; } else { Logging.finer("Player joined AGAIN!"); - if (this.configProvider.getConfig().getEnforceAccess() // check this only if we're enforcing access! + if (config.getEnforceAccess() // check this only if we're enforcing access! && !this.getMVPerms().hasPermission(p, "multiverse.access." + p.getWorld().getName(), false)) { p.sendMessage("[MV] - Sorry you can't be in this world anymore!"); this.sendPlayerToDefaultWorld(p); @@ -222,7 +222,7 @@ public class MVPlayerListener implements InjectableListener { } // Check if player is allowed to enter the world if we're enforcing permissions - if (configProvider.getConfig().getEnforceAccess()) { + if (config.getEnforceAccess()) { event.setCancelled(!pt.playerCanGoFromTo(fromWorld, toWorld, teleporter, teleportee)); if (event.isCancelled() && teleporter != null) { Logging.fine("Player '" + teleportee.getName() @@ -314,7 +314,7 @@ public class MVPlayerListener implements InjectableListener { + "' because they don't have the FUNDS required to enter."); return; } - if (configProvider.getConfig().getEnforceAccess()) { + if (config.getEnforceAccess()) { event.setCancelled(!pt.playerCanGoFromTo(fromWorld, toWorld, event.getPlayer(), event.getPlayer())); if (event.isCancelled()) { Logging.fine("Player '" + event.getPlayer().getName() @@ -326,8 +326,8 @@ public class MVPlayerListener implements InjectableListener { + "' was allowed to go to '" + event.getTo().getWorld().getName() + "' because enforceaccess is off."); } - if (!this.configProvider.getConfig().isUsingCustomPortalSearch()) { - event.setSearchRadius(this.configProvider.getConfig().getCustomPortalSearchRadius()); + if (!config.isUsingCustomPortalSearch()) { + event.setSearchRadius(config.getCustomPortalSearchRadius()); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java b/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java index fbcd0502..3189c470 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java @@ -13,7 +13,7 @@ import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.api.MVDestination; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MVWorld; -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import com.onarandombox.MultiverseCore.config.MVCoreConfig; import jakarta.inject.Inject; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -31,17 +31,17 @@ import org.jvnet.hk2.annotations.Service; public class MVPermissions { private final PluginManager pluginManager; - private final MVCoreConfigProvider configProvider; + private final MVCoreConfig config; private final MVWorldManager worldMgr; @Inject public MVPermissions( PluginManager pluginManager, - MVCoreConfigProvider configProvider, + MVCoreConfig config, MVWorldManager worldManager ) { this.pluginManager = pluginManager; - this.configProvider = configProvider; + this.config = config; this.worldMgr = worldManager; } @@ -108,7 +108,7 @@ public class MVPermissions { */ public boolean canEnterWorld(Player p, MVWorld w) { // If we're not enforcing access, anyone can enter. - if (!configProvider.getConfig().getEnforceAccess()) { + if (!config.getEnforceAccess()) { Logging.finest("EnforceAccess is OFF. Player was allowed in " + w.getAlias()); return true; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java b/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java index 14c5c32d..3bacf8ba 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java @@ -9,7 +9,7 @@ package com.onarandombox.MultiverseCore.utils; import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.api.MVWorld; -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import com.onarandombox.MultiverseCore.config.MVCoreConfig; import com.onarandombox.MultiverseCore.economy.MVEconomist; import jakarta.inject.Inject; import jakarta.inject.Provider; @@ -27,19 +27,19 @@ import org.jvnet.hk2.annotations.Service; @Service public class PermissionTools { - private final MVCoreConfigProvider configProvider; + private final MVCoreConfig config; private final PluginManager pluginManager; private final Provider mvPermsProvider; private final MVEconomist economist; @Inject public PermissionTools( - MVCoreConfigProvider configProvider, + MVCoreConfig config, PluginManager pluginManager, Provider mvPermsProvider, MVEconomist economist) { - this.configProvider = configProvider; + this.config = config; this.pluginManager = pluginManager; this.mvPermsProvider = mvPermsProvider; this.economist = economist; @@ -123,7 +123,7 @@ public class PermissionTools { */ public boolean playerHasMoneyToEnter(MVWorld fromWorld, MVWorld toWorld, CommandSender teleporter, Player teleportee, boolean pay) { Player teleporterPlayer; - if (configProvider.getConfig().getTeleportIntercept()) { + if (config.getTeleportIntercept()) { if (teleporter instanceof ConsoleCommandSender) { return true; } @@ -219,7 +219,7 @@ public class PermissionTools { Logging.finest("Checking '" + teleporter + "' can send '" + teleportee + "' somewhere"); Player teleporterPlayer; - if (configProvider.getConfig().getTeleportIntercept()) { + if (config.getTeleportIntercept()) { // The console can send anyone anywhere if (teleporter instanceof ConsoleCommandSender) { return true; diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/UnsafeCallWrapper.java b/src/main/java/com/onarandombox/MultiverseCore/utils/UnsafeCallWrapper.java index f37b6697..b459e304 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/UnsafeCallWrapper.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/UnsafeCallWrapper.java @@ -1,7 +1,7 @@ package com.onarandombox.MultiverseCore.utils; import com.dumptruckman.minecraft.util.Logging; -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import com.onarandombox.MultiverseCore.config.MVCoreConfig; import jakarta.inject.Inject; import org.jvnet.hk2.annotations.Service; @@ -12,11 +12,11 @@ import java.util.concurrent.Callable; */ @Service public class UnsafeCallWrapper { - private final MVCoreConfigProvider configProvider; + private final MVCoreConfig config; @Inject - public UnsafeCallWrapper(MVCoreConfigProvider configProvider) { - this.configProvider = configProvider; + public UnsafeCallWrapper(MVCoreConfig configProvider) { + this.config = configProvider; } /** @@ -40,7 +40,7 @@ public class UnsafeCallWrapper { actualFormatArgs[formatArgs.length] = t; Logging.warning(action, actualFormatArgs); Logging.warning("This is a bug in %s, NOT a bug in Multiverse!", plugin); - if (configProvider.getConfig().getGlobalDebug() >= 1) + if (config.getGlobalDebug() >= 1) t.printStackTrace(); return null; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java index b1d3815c..cea0f803 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorld.java @@ -19,7 +19,7 @@ import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.api.WorldPurger; -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import com.onarandombox.MultiverseCore.config.MVCoreConfig; import com.onarandombox.MultiverseCore.exceptions.PropertyDoesNotExistException; import com.onarandombox.MultiverseCore.listeners.MVPlayerListener; import com.onarandombox.MultiverseCore.world.configuration.AllowedPortalType; @@ -55,7 +55,7 @@ public class SimpleMVWorld implements MVWorld { private static final int SPAWN_LOCATION_SEARCH_RADIUS = 16; private final MVWorldManager worldManager; - private final MVCoreConfigProvider configProvider; + private final MVCoreConfig config; private final WorldPurger worldPurger; private final MVPlayerListener playerListener; private final BlockSafety blockSafety; @@ -68,7 +68,7 @@ public class SimpleMVWorld implements MVWorld { public SimpleMVWorld( MVWorldManager worldManager, - MVCoreConfigProvider configProvider, + MVCoreConfig config, WorldPurger worldPurger, MVPlayerListener playerListener, BlockSafety blockSafety, @@ -78,7 +78,7 @@ public class SimpleMVWorld implements MVWorld { World world, WorldProperties properties ) { - this(worldManager, configProvider, worldPurger, playerListener, blockSafety, safeTTeleporter, + this(worldManager, config, worldPurger, playerListener, blockSafety, safeTTeleporter, locationManipulation, server, world, properties, true); } @@ -87,7 +87,7 @@ public class SimpleMVWorld implements MVWorld { */ public SimpleMVWorld( MVWorldManager worldManager, - MVCoreConfigProvider configProvider, + MVCoreConfig config, WorldPurger worldPurger, MVPlayerListener playerListener, BlockSafety blockSafety, @@ -99,7 +99,7 @@ public class SimpleMVWorld implements MVWorld { boolean fixSpawn ) { this.worldManager = worldManager; - this.configProvider = configProvider; + this.config = config; this.worldPurger = worldPurger; this.playerListener = playerListener; this.blockSafety = blockSafety; @@ -325,7 +325,7 @@ public class SimpleMVWorld implements MVWorld { } world.setSpawnFlags(allowMonsters, allowAnimals); } - if (configProvider.getConfig().isAutoPurgeEntities()) { + if (config.isAutoPurgeEntities()) { worldPurger.purgeWorld(SimpleMVWorld.this); } return super.validateChange(property, newValue, oldValue, object); diff --git a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java index c1f87901..d37a59b2 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/world/SimpleMVWorldManager.java @@ -32,7 +32,7 @@ import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MVWorld; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.api.WorldPurger; -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider; +import com.onarandombox.MultiverseCore.config.MVCoreConfig; import com.onarandombox.MultiverseCore.event.MVWorldDeleteEvent; import com.onarandombox.MultiverseCore.listeners.MVPlayerListener; import com.onarandombox.MultiverseCore.utils.UnsafeCallWrapper; @@ -61,8 +61,10 @@ import org.jvnet.hk2.annotations.Service; */ @Service public class SimpleMVWorldManager implements MVWorldManager { + public static final String WORLD_CONFIG_FILE = "worlds.yml"; + private final MultiverseCore plugin; - private final MVCoreConfigProvider configProvider; + private final MVCoreConfig config; private final MVPlayerListener playerListener; private final BlockSafety blockSafety; private final SafeTTeleporter safeTTeleporter; @@ -79,7 +81,7 @@ public class SimpleMVWorldManager implements MVWorldManager { @Inject public SimpleMVWorldManager( MultiverseCore plugin, - MVCoreConfigProvider configProvider, + MVCoreConfig config, MVPlayerListener playerListener, BlockSafety blockSafety, SafeTTeleporter safeTTeleporter, @@ -89,7 +91,7 @@ public class SimpleMVWorldManager implements MVWorldManager { Server server ) { this.plugin = plugin; - this.configProvider = configProvider; + this.config = config; this.playerListener = playerListener; this.blockSafety = blockSafety; this.safeTTeleporter = safeTTeleporter; @@ -515,9 +517,9 @@ public class SimpleMVWorldManager implements MVWorldManager { nullWorld(worldName); return false; } - SimpleMVWorld world = new SimpleMVWorld(this, configProvider, worldPurger, playerListener, blockSafety, + SimpleMVWorld world = new SimpleMVWorld(this, config, worldPurger, playerListener, blockSafety, safeTTeleporter, locationManipulation, server, cbworld, mvworld); - if (configProvider.getConfig().isAutoPurgeEntities()) { + if (config.isAutoPurgeEntities()) { this.worldPurger.purgeWorld(world); } this.worlds.put(worldName, world); @@ -817,11 +819,12 @@ public class SimpleMVWorldManager implements MVWorldManager { * {@inheritDoc} */ @Override - public FileConfiguration loadWorldConfig(File file) { + public FileConfiguration loadWorldsConfig() { + File file = new File(this.plugin.getDataFolder(), WORLD_CONFIG_FILE); this.configWorlds = YamlConfiguration.loadConfiguration(file); this.ensureConfigIsPrepared(); try { - this.configWorlds.save(new File(this.plugin.getDataFolder(), "worlds.yml")); + this.configWorlds.save(file); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); diff --git a/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt b/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt index 84d69970..e5603af0 100644 --- a/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt +++ b/src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt @@ -9,7 +9,7 @@ import com.onarandombox.MultiverseCore.api.MVWorldManager import com.onarandombox.MultiverseCore.api.SafeTTeleporter import com.onarandombox.MultiverseCore.commandtools.MVCommandManager import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand -import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider +import com.onarandombox.MultiverseCore.config.MVCoreConfig import com.onarandombox.MultiverseCore.economy.MVEconomist import com.onarandombox.MultiverseCore.listeners.MVChatListener import com.onarandombox.MultiverseCore.listeners.MVEntityListener @@ -112,8 +112,8 @@ class InjectionTest : TestWithMockBukkit() { } @Test - fun `MVCoreConfigProvider is available as a service`() { - assertNotNull(multiverseCore.getService(MVCoreConfigProvider::class.java)) + fun `MVCoreConfig is available as a service`() { + assertNotNull(multiverseCore.getService(MVCoreConfig::class.java)) } @Test @@ -130,13 +130,6 @@ class InjectionTest : TestWithMockBukkit() { assertEquals(6, destinations.size) } - @Test - fun `MVConfig is not available as a service`() { - // We need one test case for asking for non-services to make sure we don't accidentally make them available - // and that the getService method doesn't throw an exception - assertNull(multiverseCore.getService(MVConfig::class.java)) - } - @Test fun `MetricsConfigurator is not available as a service`() { // Also making sure this is not loaded automatically since it's supposed to be disabled during tests From 5a0957d894989d53c611803b59bad3276a5aafd4 Mon Sep 17 00:00:00 2001 From: Jeremy Wood Date: Mon, 27 Mar 2023 00:51:55 -0400 Subject: [PATCH 29/37] Remove unused MultiverseConfigurationException. --- .../config/MultiverseConfigurationException.java | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 src/main/java/com/onarandombox/MultiverseCore/config/MultiverseConfigurationException.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MultiverseConfigurationException.java b/src/main/java/com/onarandombox/MultiverseCore/config/MultiverseConfigurationException.java deleted file mode 100644 index 5c2ffc95..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MultiverseConfigurationException.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.onarandombox.MultiverseCore.config; - -import java.io.IOException; - -public class MultiverseConfigurationException extends IOException { - public MultiverseConfigurationException(String message, Throwable cause) { - super(message, cause); - } -} From 7b39d78bfd2b09ff929ba9de7698a040ae9a9565 Mon Sep 17 00:00:00 2001 From: Jeremy Wood Date: Mon, 27 Mar 2023 00:53:36 -0400 Subject: [PATCH 30/37] Remove MultiverseCore dependency from DebugCommand. --- .../MultiverseCore/commands/DebugCommand.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java index 0632dff2..b4500432 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/DebugCommand.java @@ -8,7 +8,6 @@ import co.aikar.commands.annotation.Description; import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Syntax; import com.dumptruckman.minecraft.util.Logging; -import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.commandtools.MVCommandManager; import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand; import com.onarandombox.MultiverseCore.config.MVCoreConfig; @@ -22,17 +21,11 @@ import org.jvnet.hk2.annotations.Service; public class DebugCommand extends MultiverseCommand { private final MVCoreConfig config; - private final MultiverseCore plugin; @Inject - public DebugCommand( - @NotNull MVCommandManager commandManager, - @NotNull MVCoreConfig config, - @NotNull MultiverseCore plugin - ) { + public DebugCommand(@NotNull MVCommandManager commandManager, @NotNull MVCoreConfig config) { super(commandManager); this.config = config; - this.plugin = plugin; } @Subcommand("debug") From 61cd5938449c33f9638f600a84a0e986e908945f Mon Sep 17 00:00:00 2001 From: Jeremy Wood Date: Mon, 27 Mar 2023 01:03:21 -0400 Subject: [PATCH 31/37] Fix a couple nits. --- .../com/onarandombox/MultiverseCore/config/MVCoreConfig.java | 5 +---- .../MultiverseCore/configuration/ConfigHandle.java | 3 +++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java index cb568320..72924a30 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java @@ -30,10 +30,7 @@ public class MVCoreConfig implements MVConfig { private final ConfigHandle configHandle; @Inject - MVCoreConfig( - @NotNull MultiverseCore core, - @NotNull PluginManager pluginManager - ) { + MVCoreConfig(@NotNull MultiverseCore core, @NotNull PluginManager pluginManager) { this.configPath = Path.of(core.getDataFolder().getPath(), CONFIG_FILENAME); this.configNodes = new MVCoreConfigNodes(pluginManager); this.configHandle = ConfigHandle.builder(configPath) diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java index 05888ac3..5f3155f6 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java @@ -42,8 +42,11 @@ public class ConfigHandle { return new Builder(configPath); } + @NotNull protected final Path configPath; + @Nullable protected final Logger logger; + @NotNull protected final NodeGroup nodes; protected final ConfigMigrator migrator; From 98fc0178aa1df3e90bda80e1337a884b3bcf43d1 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Mon, 27 Mar 2023 14:09:36 +0800 Subject: [PATCH 32/37] fix: Migrate from old config on every load --- .../com/onarandombox/MultiverseCore/config/MVCoreConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java index 72924a30..27613587 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java +++ b/src/main/java/com/onarandombox/MultiverseCore/config/MVCoreConfig.java @@ -67,7 +67,6 @@ public class MVCoreConfig implements MVConfig { .build()) .build(); - migrateFromOldConfigFile(); load(); save(); } @@ -90,6 +89,7 @@ public class MVCoreConfig implements MVConfig { @Override public boolean load() { + migrateFromOldConfigFile(); return configHandle.load(); } From 624d5ad117916fd835e12f1ad842b464bc70a690 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Mon, 27 Mar 2023 14:10:22 +0800 Subject: [PATCH 33/37] fix: Get version without default fallback --- .../MultiverseCore/configuration/migration/ConfigMigrator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/ConfigMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/ConfigMigrator.java index c0d5860a..6d7faac3 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/ConfigMigrator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/migration/ConfigMigrator.java @@ -37,7 +37,7 @@ public class ConfigMigrator { * @param settings The target settings instance to migrate. */ public void migrate(ConfigHandle settings) { - double versionNumber = settings.get(versionNode); + double versionNumber = settings.getConfig().getDouble(versionNode.getPath()); for (VersionMigrator versionMigrator : versionMigrators) { if (versionNumber < versionMigrator.getVersion()) { Logging.info("Migrating config from version %s to %s...", versionNumber, versionMigrator.getVersion()); From a5ae29c91aaa0b0c3e6a7a788c33e1197743bf01 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Mon, 27 Mar 2023 14:10:49 +0800 Subject: [PATCH 34/37] test: Implement test for config --- .../multiverse/core/TestWithMockBukkit.kt | 4 +- .../multiverse/core/config/ConfigTest.kt | 90 +++++++++++++++++ src/test/resources/default_config.yml | 96 +++++++++++++++++++ src/test/resources/old_config.yml | 16 ++++ 4 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt create mode 100644 src/test/resources/default_config.yml create mode 100644 src/test/resources/old_config.yml diff --git a/src/test/java/org/mvplugins/multiverse/core/TestWithMockBukkit.kt b/src/test/java/org/mvplugins/multiverse/core/TestWithMockBukkit.kt index 6751b909..b31d9cf5 100644 --- a/src/test/java/org/mvplugins/multiverse/core/TestWithMockBukkit.kt +++ b/src/test/java/org/mvplugins/multiverse/core/TestWithMockBukkit.kt @@ -16,7 +16,7 @@ abstract class TestWithMockBukkit { protected lateinit var multiverseCore: MultiverseCore @BeforeTest - fun setUp() { + open fun setUp() { TestingMode.enable() server = MockBukkit.mock() multiverseCore = MockBukkit.load(MultiverseCore::class.java) @@ -26,4 +26,6 @@ abstract class TestWithMockBukkit { fun tearDown() { MockBukkit.unmock() } + + protected fun getResourceAsText(path: String): String? = object {}.javaClass.getResource(path)?.readText() } diff --git a/src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt b/src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt new file mode 100644 index 00000000..f17ccd41 --- /dev/null +++ b/src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt @@ -0,0 +1,90 @@ +package org.mvplugins.multiverse.core.config + +import com.onarandombox.MultiverseCore.config.MVCoreConfig +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.mvplugins.multiverse.core.TestWithMockBukkit +import java.io.File +import java.nio.file.Path +import kotlin.io.path.absolutePathString +import kotlin.test.* + +class ConfigTest : TestWithMockBukkit() { + + private lateinit var config : MVCoreConfig + + @BeforeTest + override fun setUp() { + super.setUp() + config = multiverseCore.getService(MVCoreConfig::class.java).takeIf { it != null } ?: run { + throw IllegalStateException("MVCoreConfig is not available as a service") } + } + + @BeforeEach + fun `Config load`() { + val defaultConfig = getResourceAsText("/default_config.yml") + assertNotNull(defaultConfig) + File(Path.of(multiverseCore.dataFolder.absolutePath, "config.yml").absolutePathString()).writeText(defaultConfig) + + assertTrue(config.load()) + assertTrue(config.save()) + } + + @Test + fun `Config is loaded`() { + assertTrue(config.isLoaded) + } + + @Test + fun `Config migration`() { + val oldConfig = getResourceAsText("/old_config.yml") + assertNotNull(oldConfig) + File(Path.of(multiverseCore.dataFolder.absolutePath, "config.yml").absolutePathString()).writeText(oldConfig) + assertTrue(config.load()) + assertTrue(config.save()) + + assertEquals(true, config.enforceAccess) + assertEquals(false, config.isEnablePrefixChat) + assertEquals("[%world%]>>%chat%", config.prefixChatFormat) + assertEquals(false, config.teleportIntercept) + assertEquals(true, config.firstSpawnOverride) + assertEquals(2, config.globalDebug) + assertEquals(false, config.silentStart) + assertEquals("world", config.firstSpawnLocation) + assertEquals(false, config.isUsingCustomPortalSearch) + assertEquals(128, config.customPortalSearchRadius) + assertEquals(true, config.isAutoPurgeEntities) + assertEquals(false, config.isShowingDonateMessage) + } + + @Test + fun `Get valid property`() { + assertEquals(false, config.getProperty("enforce-access")) + assertEquals("world", config.getProperty("first-spawn-location")) + assertEquals(3, config.globalDebug) + } + + @Test + fun `Get invalid property`() { + assertNull(config.getProperty("invalid-property")) + assertNull(config.getProperty("version")) + } + + @Test + fun `Set valid property`() { + assertTrue(config.setProperty("enforce-access", true)) + assertEquals(true, config.getProperty("enforce-access")) + + assertTrue(config.setProperty("first-spawn-location", "world2")) + assertEquals("world2", config.getProperty("first-spawn-location")) + + assertTrue(config.setProperty("global-debug", 1)) + assertEquals(1, config.getProperty("global-debug")) + } + + @Test + fun `Set invalid property`() { + assertFalse(config.setProperty("invalid-property", false)) + assertFalse(config.setProperty("version", 1.1)) + } +} diff --git a/src/test/resources/default_config.yml b/src/test/resources/default_config.yml new file mode 100644 index 00000000..c1be2db5 --- /dev/null +++ b/src/test/resources/default_config.yml @@ -0,0 +1,96 @@ +#################################################################################################### +# # +# █▀▄▀█ █░█ █░░ ▀█▀ █ █░█ █▀▀ █▀█ █▀ █▀▀   █▀▀ █▀█ █▀█ █▀▀ # +# █░▀░█ █▄█ █▄▄ ░█░ █ ▀▄▀ ██▄ █▀▄ ▄█ ██▄   █▄▄ █▄█ █▀▄ ██▄ # +# # +# # +# WIKI: https://github.com/Multiverse/Multiverse-Core/wiki # +# DISCORD: https://discord.gg/NZtfKky # +# BUG REPORTS: https://github.com/Multiverse/Multiverse-Core/issues # +# # +# # +# Each option in this file is documented and explained here: # +# ==> https://github.com/Multiverse/Multiverse-Core/wiki/config.yml # +# # +# # +# New options are added to this file automatically. If you manually made changes # +# to this file while your server is running, please run `/mv reload` command. # +# # +#################################################################################################### + + +world: + # This setting will prevent players from entering worlds they don't have access to. + # If this is set to false, players will be able to enter any world they want. + # If this is set to true, players will only be able to enter worlds they have + # the `mv.access.` permission. + enforce-access: false + + # Sets whether Multiverse will should enforce gamemode on world change. + # If enabled, players will be forced into the gamemode of the world they are entering, unless they have + # the `mv.bypass.gamemode.` permission. + enforce-gamemode: true + + # Sets whether Multiverse will purge mobs and entities with be automatically. + auto-purge-entities: true + + # If this is set to true, Multiverse will enforce access permissions for all teleportation, + # including teleportation from other plugins. + teleport-intercept: true + + +spawn: + # Sets whether Multiverse will override the first spawn location of a world. + # If enabled, Multiverse will set the first spawn location of a world to the spawn location of the world. + # If disabled, it will default to server.properties settings. + first-spawn-override: true + + # Sets the world that Multiverse will use as the location for players that first join the server. + # This only applies if first-spawn-override is set to true. + first-spawn-location: world + + +portal: + # This config option defines whether or not Multiverse should interfere with's Bukkit's default portal search radius. + # Setting it to false would mean you want to simply let Bukkit decides the search radius itself. + use-custom-portal-search: false + + # This config option defines the search radius Multiverse should use when searching for a portal. + # This only applies if use-custom-portal-search is set to true. + custom-portal-search-radius: 128 + + +messaging: + # This config option defines whether or not Multiverse should prefix the chat with the world name. + # This only applies if use-custom-portal-search is set to true. + enable-chat-prefix: false + + # This config option defines the format Multiverse should use when prefixing the chat with the world name. + # This only applies if enable-chat-prefix is set to true. + chat-prefix-format: '[%world%]%chat%' + + # This config option defines whether or not Multiverse should register the PlaceholderAPI hook. + # This only applies if PlaceholderAPI is installed. + register-papi-hook: true + + +misc: + # This is our debug flag to help identify issues with Multiverse. + # If you are having issues with Multiverse, please set this to 3 and then post your log to pastebin.com + # Otherwise, there's no need to touch this. If not instructed by a wiki page or developer. + # 0 = Off, No debug messages + # 1 = fine + # 2 = finer + # 3 = finest + global-debug: 3 + + # If true, the startup console messages will no longer show. + silent-start: false + + # If you don't want to donate, you can set this to false and Multiverse will stop nagging you. + show-donation-message: true + + +# This just signifies the version number so we can see what version of config you have. +# NEVER TOUCH THIS VALUE +version: 5.0 \ No newline at end of file diff --git a/src/test/resources/old_config.yml b/src/test/resources/old_config.yml new file mode 100644 index 00000000..9fa97cc7 --- /dev/null +++ b/src/test/resources/old_config.yml @@ -0,0 +1,16 @@ +multiverse-configuration: + ==: com.onarandombox.MultiverseCore.MultiverseCoreConfiguration + enforceaccess: 'true' + prefixchat: 'false' + prefixchatformat: '[%world%]>>%chat%' + teleportintercept: 'false' + firstspawnoverride: 'true' + displaypermerrors: 'false' + globaldebug: '2' + silentstart: 'false' + version: '2.9' + firstspawnworld: world + defaultportalsearch: 'true' + portalsearchradius: '128' + autopurge: 'true' + idonotwanttodonate: 'true' \ No newline at end of file From 8f9df0294da512eb8e835670efa7458b336d323b Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Mon, 27 Mar 2023 18:29:34 +0800 Subject: [PATCH 35/37] chore: Improve logging in ConfigHandle --- .../onarandombox/MultiverseCore/configuration/ConfigHandle.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java index 5f3155f6..4aabb2d7 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/ConfigHandle.java @@ -100,6 +100,7 @@ public class ConfigHandle { if (!configFile.createNewFile()) { return false; } + Logging.info("Created new config file: %s", configFile.getName()); } catch (IOException e) { return false; } @@ -120,7 +121,6 @@ public class ConfigHandle { CommentedConfiguration oldConfig = config; this.config = new CommentedConfiguration(configPath, logger); for (CommentedNode node : nodes) { - Logging.config("Parsing node: %s", node.getPath()); if (node.getComments().length > 0) { config.addComment(node.getPath(), node.getComments()); } From 55390952c937447e20af0524899b14b3d8ff9680 Mon Sep 17 00:00:00 2001 From: Jeremy Wood Date: Mon, 27 Mar 2023 08:39:18 -0400 Subject: [PATCH 36/37] Add additional test set up more idiomatically. --- .../multiverse/core/TestWithMockBukkit.kt | 4 ++-- .../multiverse/core/config/ConfigTest.kt | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/test/java/org/mvplugins/multiverse/core/TestWithMockBukkit.kt b/src/test/java/org/mvplugins/multiverse/core/TestWithMockBukkit.kt index b31d9cf5..c5fd0a00 100644 --- a/src/test/java/org/mvplugins/multiverse/core/TestWithMockBukkit.kt +++ b/src/test/java/org/mvplugins/multiverse/core/TestWithMockBukkit.kt @@ -16,14 +16,14 @@ abstract class TestWithMockBukkit { protected lateinit var multiverseCore: MultiverseCore @BeforeTest - open fun setUp() { + fun setUpMockBukkit() { TestingMode.enable() server = MockBukkit.mock() multiverseCore = MockBukkit.load(MultiverseCore::class.java) } @AfterTest - fun tearDown() { + fun tearDownMockBukkit() { MockBukkit.unmock() } diff --git a/src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt b/src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt index f17ccd41..ff81817d 100644 --- a/src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt +++ b/src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt @@ -1,27 +1,27 @@ package org.mvplugins.multiverse.core.config import com.onarandombox.MultiverseCore.config.MVCoreConfig -import org.junit.jupiter.api.BeforeEach -import org.junit.jupiter.api.Test import org.mvplugins.multiverse.core.TestWithMockBukkit import java.io.File import java.nio.file.Path import kotlin.io.path.absolutePathString -import kotlin.test.* +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue class ConfigTest : TestWithMockBukkit() { private lateinit var config : MVCoreConfig @BeforeTest - override fun setUp() { - super.setUp() + fun setUp() { config = multiverseCore.getService(MVCoreConfig::class.java).takeIf { it != null } ?: run { throw IllegalStateException("MVCoreConfig is not available as a service") } - } - @BeforeEach - fun `Config load`() { val defaultConfig = getResourceAsText("/default_config.yml") assertNotNull(defaultConfig) File(Path.of(multiverseCore.dataFolder.absolutePath, "config.yml").absolutePathString()).writeText(defaultConfig) From 9bbf42f993b4b3ef93d3dc8da382f13b3e8d10df Mon Sep 17 00:00:00 2001 From: Jeremy Wood Date: Mon, 27 Mar 2023 09:51:27 -0400 Subject: [PATCH 37/37] Rename test methods. --- .../multiverse/core/config/ConfigTest.kt | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt b/src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt index ff81817d..d106b727 100644 --- a/src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt +++ b/src/test/java/org/mvplugins/multiverse/core/config/ConfigTest.kt @@ -36,7 +36,7 @@ class ConfigTest : TestWithMockBukkit() { } @Test - fun `Config migration`() { + fun `Old config is migrated`() { val oldConfig = getResourceAsText("/old_config.yml") assertNotNull(oldConfig) File(Path.of(multiverseCore.dataFolder.absolutePath, "config.yml").absolutePathString()).writeText(oldConfig) @@ -58,20 +58,25 @@ class ConfigTest : TestWithMockBukkit() { } @Test - fun `Get valid property`() { + fun `Getting existing config property with getProperty returns expected value`() { assertEquals(false, config.getProperty("enforce-access")) assertEquals("world", config.getProperty("first-spawn-location")) - assertEquals(3, config.globalDebug) } @Test - fun `Get invalid property`() { + fun `Getting non-existing config property with getProperty returns null`() { assertNull(config.getProperty("invalid-property")) assertNull(config.getProperty("version")) } @Test - fun `Set valid property`() { + fun `Getting existing config property by getter returns expected value`() { + assertEquals(false, config.enforceAccess) + assertEquals("world", config.firstSpawnLocation) + } + + @Test + fun `Updating an existing config property with setProperty reflects the changes in getProperty`() { assertTrue(config.setProperty("enforce-access", true)) assertEquals(true, config.getProperty("enforce-access")) @@ -83,7 +88,7 @@ class ConfigTest : TestWithMockBukkit() { } @Test - fun `Set invalid property`() { + fun `Updating a non-existing property with setProperty returns false`() { assertFalse(config.setProperty("invalid-property", false)) assertFalse(config.setProperty("version", 1.1)) }