diff --git a/src/main/java/org/mvplugins/multiverse/core/config/node/ConfigNode.java b/src/main/java/org/mvplugins/multiverse/core/config/node/ConfigNode.java index e15b1a8b7..4459404de 100644 --- a/src/main/java/org/mvplugins/multiverse/core/config/node/ConfigNode.java +++ b/src/main/java/org/mvplugins/multiverse/core/config/node/ConfigNode.java @@ -8,6 +8,7 @@ import io.vavr.control.Option; import io.vavr.control.Try; +import org.apache.logging.log4j.util.Strings; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -45,6 +46,7 @@ public class ConfigNode extends ConfigHeaderNode implements ValueNode { protected final @Nullable String name; protected final @NotNull Class type; + protected final @NotNull String[] aliases; protected @Nullable Supplier defaultValue; protected @Nullable NodeSuggester suggester; protected @Nullable NodeStringParser stringParser; @@ -57,6 +59,7 @@ protected ConfigNode( @NotNull String[] comments, @Nullable String name, @NotNull Class type, + @NotNull String[] aliases, @Nullable Supplier defaultValue, @Nullable NodeSuggester suggester, @Nullable NodeStringParser stringParser, @@ -66,6 +69,7 @@ protected ConfigNode( super(path, comments); this.name = name; this.type = type; + this.aliases = aliases; this.defaultValue = defaultValue; this.suggester = (suggester != null) ? suggester @@ -96,6 +100,14 @@ protected ConfigNode( return type; } + /** + * {@inheritDoc} + */ + @Override + public @NotNull String[] getAliases() { + return aliases; + } + /** * {@inheritDoc} */ @@ -178,6 +190,7 @@ public static class Builder> extends Confi protected @Nullable String name; protected @NotNull final Class type; + protected @NotNull String[] aliases = Strings.EMPTY_ARRAY; protected @Nullable Supplier defaultValue; protected @Nullable NodeSuggester suggester; protected @Nullable NodeStringParser stringParser; @@ -257,6 +270,20 @@ protected Builder(@NotNull String path, @NotNull Class type) { return name(null); } + /** + * Sets the aliases for this node. Aliases are alternative identifiers for referencing the node. + * + * @param aliases The aliases to set for this node. + * @return This builder. + * + * @since 5.1 + */ + @ApiStatus.AvailableSince("5.1") + public @NotNull B aliases(@NotNull String... aliases) { + this.aliases = aliases; + return self(); + } + /** * Sets the suggester for this node. * @@ -332,7 +359,7 @@ protected Builder(@NotNull String path, @NotNull Class type) { @Override public @NotNull ConfigNode build() { return new ConfigNode<>(path, comments.toArray(new String[0]), - name, type, defaultValue, suggester, stringParser, serializer, validator, onSetValue); + name, type, aliases, defaultValue, suggester, stringParser, serializer, validator, onSetValue); } } } diff --git a/src/main/java/org/mvplugins/multiverse/core/config/node/ListConfigNode.java b/src/main/java/org/mvplugins/multiverse/core/config/node/ListConfigNode.java index f306fc41e..ce520c283 100644 --- a/src/main/java/org/mvplugins/multiverse/core/config/node/ListConfigNode.java +++ b/src/main/java/org/mvplugins/multiverse/core/config/node/ListConfigNode.java @@ -60,6 +60,7 @@ protected ListConfigNode( @NotNull String[] comments, @Nullable String name, @NotNull Class> type, + @NotNull String[] aliases, @Nullable Supplier> defaultValueSupplier, @Nullable NodeSuggester suggester, @Nullable NodeStringParser> stringParser, @@ -72,7 +73,7 @@ protected ListConfigNode( @Nullable NodeSerializer itemSerializer, @Nullable Function> itemValidator, @Nullable BiConsumer onSetItemValue) { - super(path, comments, name, type, defaultValueSupplier, suggester, stringParser, serializer, + super(path, comments, name, type, aliases, defaultValueSupplier, suggester, stringParser, serializer, validator, onSetValue); this.itemType = itemType; this.itemSuggester = itemSuggester != null @@ -348,6 +349,7 @@ protected Builder(@NotNull String path, @NotNull Class itemType) { comments.toArray(new String[0]), name, type, + aliases, defaultValue, suggester, stringParser, diff --git a/src/main/java/org/mvplugins/multiverse/core/config/node/NodeGroup.java b/src/main/java/org/mvplugins/multiverse/core/config/node/NodeGroup.java index 7b3117615..779bb012b 100644 --- a/src/main/java/org/mvplugins/multiverse/core/config/node/NodeGroup.java +++ b/src/main/java/org/mvplugins/multiverse/core/config/node/NodeGroup.java @@ -1,9 +1,11 @@ package org.mvplugins.multiverse.core.config.node; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode; @@ -16,6 +18,7 @@ */ public class NodeGroup implements Collection { private final Collection nodes; + private final List nodeNames; private final Map nodesMap; /** @@ -23,6 +26,7 @@ public class NodeGroup implements Collection { */ public NodeGroup() { this.nodes = new ArrayList<>(); + this.nodeNames = new ArrayList<>(); this.nodesMap = new HashMap<>(); } @@ -34,18 +38,27 @@ public NodeGroup() { public NodeGroup(@NotNull Collection nodes) { this.nodes = nodes; this.nodesMap = new HashMap<>(nodes.size()); + this.nodeNames = new ArrayList<>(nodes.size()); nodes.forEach(this::addNodeIndex); } private void addNodeIndex(@NotNull Node node) { - if (node instanceof ValueNode) { - ((ValueNode) node).getName().peek(name -> nodesMap.put(name, node)); + if (node instanceof ValueNode valueNode) { + valueNode.getName().peek(name -> { + nodeNames.add(name); + nodesMap.put(name, node); + Arrays.stream(valueNode.getAliases()).forEach(alias -> nodesMap.put(alias, node)); + }); } } private void removeNodeIndex(@NotNull Node node) { - if (node instanceof ValueNode) { - ((ValueNode) node).getName().peek(nodesMap::remove); + if (node instanceof ValueNode valueNode) { + valueNode.getName().peek(name -> { + nodeNames.remove(name); + nodesMap.remove(name); + Arrays.stream(valueNode.getAliases()).forEach(alias -> nodesMap.remove(alias, node)); + }); } } @@ -55,7 +68,7 @@ private void removeNodeIndex(@NotNull Node node) { * @return The names of all nodes in this group. */ public @NotNull Collection getNames() { - return nodesMap.keySet(); + return nodeNames; } /** diff --git a/src/main/java/org/mvplugins/multiverse/core/config/node/ValueNode.java b/src/main/java/org/mvplugins/multiverse/core/config/node/ValueNode.java index 418cee043..7f2245b31 100644 --- a/src/main/java/org/mvplugins/multiverse/core/config/node/ValueNode.java +++ b/src/main/java/org/mvplugins/multiverse/core/config/node/ValueNode.java @@ -4,6 +4,7 @@ import io.vavr.control.Option; import io.vavr.control.Try; +import org.apache.logging.log4j.util.Strings; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -14,7 +15,7 @@ public interface ValueNode extends Node { /** - * Gets the name of this node. Used for identifying the node from user input. + * Gets the name of this node. Used for identifying the node from user input. This must be unique within a node group. * * @return An {@link Option} containing the name of this node, or {@link Option.None} if the node has no name. */ @@ -27,6 +28,19 @@ public interface ValueNode extends Node { */ @NotNull Class getType(); + /** + * Gets the aliases of this node. Serves as shorter or legacy alternatives the {@link #getName()} and must be + * unique within a node group. + * + * @return The aliases of this node. + * + * @since 5.1 + */ + @ApiStatus.AvailableSince("5.1") + default @NotNull String[] getAliases() { + return Strings.EMPTY_ARRAY; + } + /** * Gets the default value with type {@link T} of the node. *