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); }