Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +46,7 @@

protected final @Nullable String name;
protected final @NotNull Class<T> type;
protected final @NotNull String[] aliases;

Check warning on line 49 in src/main/java/org/mvplugins/multiverse/core/config/node/ConfigNode.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/config/node/ConfigNode.java:49:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocVariableCheck)
protected @Nullable Supplier<T> defaultValue;
protected @Nullable NodeSuggester suggester;
protected @Nullable NodeStringParser<T> stringParser;
Expand All @@ -57,6 +59,7 @@
@NotNull String[] comments,
@Nullable String name,
@NotNull Class<T> type,
@NotNull String[] aliases,
@Nullable Supplier<T> defaultValue,
@Nullable NodeSuggester suggester,
@Nullable NodeStringParser<T> stringParser,
Expand All @@ -66,6 +69,7 @@
super(path, comments);
this.name = name;
this.type = type;
this.aliases = aliases;
this.defaultValue = defaultValue;
this.suggester = (suggester != null)
? suggester
Expand Down Expand Up @@ -96,6 +100,14 @@
return type;
}

/**
* {@inheritDoc}
*/
@Override
public @NotNull String[] getAliases() {
return aliases;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -178,6 +190,7 @@

protected @Nullable String name;
protected @NotNull final Class<T> type;
protected @NotNull String[] aliases = Strings.EMPTY_ARRAY;

Check warning on line 193 in src/main/java/org/mvplugins/multiverse/core/config/node/ConfigNode.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/config/node/ConfigNode.java:193:9: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocVariableCheck)
protected @Nullable Supplier<T> defaultValue;
protected @Nullable NodeSuggester suggester;
protected @Nullable NodeStringParser<T> stringParser;
Expand Down Expand Up @@ -257,6 +270,20 @@
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.
*
Expand Down Expand Up @@ -332,7 +359,7 @@
@Override
public @NotNull ConfigNode<T> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected ListConfigNode(
@NotNull String[] comments,
@Nullable String name,
@NotNull Class<List<I>> type,
@NotNull String[] aliases,
@Nullable Supplier<List<I>> defaultValueSupplier,
@Nullable NodeSuggester suggester,
@Nullable NodeStringParser<List<I>> stringParser,
Expand All @@ -72,7 +73,7 @@ protected ListConfigNode(
@Nullable NodeSerializer<I> itemSerializer,
@Nullable Function<I, Try<Void>> itemValidator,
@Nullable BiConsumer<I, I> 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
Expand Down Expand Up @@ -348,6 +349,7 @@ protected Builder(@NotNull String path, @NotNull Class<I> itemType) {
comments.toArray(new String[0]),
name,
type,
aliases,
defaultValue,
suggester,
stringParser,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,13 +18,15 @@
*/
public class NodeGroup implements Collection<Node> {
private final Collection<Node> nodes;
private final List<String> nodeNames;
private final Map<String, Node> nodesMap;

/**
* Creates a new empty node group.
*/
public NodeGroup() {
this.nodes = new ArrayList<>();
this.nodeNames = new ArrayList<>();
this.nodesMap = new HashMap<>();
}

Expand All @@ -34,18 +38,27 @@ public NodeGroup() {
public NodeGroup(@NotNull Collection<Node> 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));
});
}
}

Expand All @@ -55,7 +68,7 @@ private void removeNodeIndex(@NotNull Node node) {
* @return The names of all nodes in this group.
*/
public @NotNull Collection<String> getNames() {
return nodesMap.keySet();
return nodeNames;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,7 +15,7 @@
public interface ValueNode<T> 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.

Check warning on line 18 in src/main/java/org/mvplugins/multiverse/core/config/node/ValueNode.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 122). Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/config/node/ValueNode.java:18:0: warning: Line is longer than 120 characters (found 122). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
*
* @return An {@link Option} containing the name of this node, or {@link Option.None} if the node has no name.
*/
Expand All @@ -27,6 +28,19 @@
*/
@NotNull Class<T> 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.
*
Expand Down