Skip to content

Commit 1372626

Browse files
authored
Merge pull request #3263 from Multiverse/feat/config-alias-name
Add support for alias name in config node
2 parents 518d648 + c09dacb commit 1372626

File tree

4 files changed

+64
-8
lines changed

4 files changed

+64
-8
lines changed

src/main/java/org/mvplugins/multiverse/core/config/node/ConfigNode.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import io.vavr.control.Option;
1010
import io.vavr.control.Try;
11+
import org.apache.logging.log4j.util.Strings;
1112
import org.bukkit.command.CommandSender;
1213
import org.jetbrains.annotations.ApiStatus;
1314
import org.jetbrains.annotations.NotNull;
@@ -45,6 +46,7 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
4546

4647
protected final @Nullable String name;
4748
protected final @NotNull Class<T> type;
49+
protected final @NotNull String[] aliases;
4850
protected @Nullable Supplier<T> defaultValue;
4951
protected @Nullable NodeSuggester suggester;
5052
protected @Nullable NodeStringParser<T> stringParser;
@@ -57,6 +59,7 @@ protected ConfigNode(
5759
@NotNull String[] comments,
5860
@Nullable String name,
5961
@NotNull Class<T> type,
62+
@NotNull String[] aliases,
6063
@Nullable Supplier<T> defaultValue,
6164
@Nullable NodeSuggester suggester,
6265
@Nullable NodeStringParser<T> stringParser,
@@ -66,6 +69,7 @@ protected ConfigNode(
6669
super(path, comments);
6770
this.name = name;
6871
this.type = type;
72+
this.aliases = aliases;
6973
this.defaultValue = defaultValue;
7074
this.suggester = (suggester != null)
7175
? suggester
@@ -96,6 +100,14 @@ protected ConfigNode(
96100
return type;
97101
}
98102

103+
/**
104+
* {@inheritDoc}
105+
*/
106+
@Override
107+
public @NotNull String[] getAliases() {
108+
return aliases;
109+
}
110+
99111
/**
100112
* {@inheritDoc}
101113
*/
@@ -178,6 +190,7 @@ public static class Builder<T, B extends ConfigNode.Builder<T, B>> extends Confi
178190

179191
protected @Nullable String name;
180192
protected @NotNull final Class<T> type;
193+
protected @NotNull String[] aliases = Strings.EMPTY_ARRAY;
181194
protected @Nullable Supplier<T> defaultValue;
182195
protected @Nullable NodeSuggester suggester;
183196
protected @Nullable NodeStringParser<T> stringParser;
@@ -257,6 +270,20 @@ protected Builder(@NotNull String path, @NotNull Class<T> type) {
257270
return name(null);
258271
}
259272

273+
/**
274+
* Sets the aliases for this node. Aliases are alternative identifiers for referencing the node.
275+
*
276+
* @param aliases The aliases to set for this node.
277+
* @return This builder.
278+
*
279+
* @since 5.1
280+
*/
281+
@ApiStatus.AvailableSince("5.1")
282+
public @NotNull B aliases(@NotNull String... aliases) {
283+
this.aliases = aliases;
284+
return self();
285+
}
286+
260287
/**
261288
* Sets the suggester for this node.
262289
*
@@ -332,7 +359,7 @@ protected Builder(@NotNull String path, @NotNull Class<T> type) {
332359
@Override
333360
public @NotNull ConfigNode<T> build() {
334361
return new ConfigNode<>(path, comments.toArray(new String[0]),
335-
name, type, defaultValue, suggester, stringParser, serializer, validator, onSetValue);
362+
name, type, aliases, defaultValue, suggester, stringParser, serializer, validator, onSetValue);
336363
}
337364
}
338365
}

src/main/java/org/mvplugins/multiverse/core/config/node/ListConfigNode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ protected ListConfigNode(
6060
@NotNull String[] comments,
6161
@Nullable String name,
6262
@NotNull Class<List<I>> type,
63+
@NotNull String[] aliases,
6364
@Nullable Supplier<List<I>> defaultValueSupplier,
6465
@Nullable NodeSuggester suggester,
6566
@Nullable NodeStringParser<List<I>> stringParser,
@@ -72,7 +73,7 @@ protected ListConfigNode(
7273
@Nullable NodeSerializer<I> itemSerializer,
7374
@Nullable Function<I, Try<Void>> itemValidator,
7475
@Nullable BiConsumer<I, I> onSetItemValue) {
75-
super(path, comments, name, type, defaultValueSupplier, suggester, stringParser, serializer,
76+
super(path, comments, name, type, aliases, defaultValueSupplier, suggester, stringParser, serializer,
7677
validator, onSetValue);
7778
this.itemType = itemType;
7879
this.itemSuggester = itemSuggester != null
@@ -348,6 +349,7 @@ protected Builder(@NotNull String path, @NotNull Class<I> itemType) {
348349
comments.toArray(new String[0]),
349350
name,
350351
type,
352+
aliases,
351353
defaultValue,
352354
suggester,
353355
stringParser,

src/main/java/org/mvplugins/multiverse/core/config/node/NodeGroup.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.mvplugins.multiverse.core.config.node;
22

33
import java.util.ArrayList;
4+
import java.util.Arrays;
45
import java.util.Collection;
56
import java.util.HashMap;
67
import java.util.Iterator;
8+
import java.util.List;
79
import java.util.Map;
810

911
import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode;
@@ -16,13 +18,15 @@
1618
*/
1719
public class NodeGroup implements Collection<Node> {
1820
private final Collection<Node> nodes;
21+
private final List<String> nodeNames;
1922
private final Map<String, Node> nodesMap;
2023

2124
/**
2225
* Creates a new empty node group.
2326
*/
2427
public NodeGroup() {
2528
this.nodes = new ArrayList<>();
29+
this.nodeNames = new ArrayList<>();
2630
this.nodesMap = new HashMap<>();
2731
}
2832

@@ -34,18 +38,27 @@ public NodeGroup() {
3438
public NodeGroup(@NotNull Collection<Node> nodes) {
3539
this.nodes = nodes;
3640
this.nodesMap = new HashMap<>(nodes.size());
41+
this.nodeNames = new ArrayList<>(nodes.size());
3742
nodes.forEach(this::addNodeIndex);
3843
}
3944

4045
private void addNodeIndex(@NotNull Node node) {
41-
if (node instanceof ValueNode) {
42-
((ValueNode<?>) node).getName().peek(name -> nodesMap.put(name, node));
46+
if (node instanceof ValueNode<?> valueNode) {
47+
valueNode.getName().peek(name -> {
48+
nodeNames.add(name);
49+
nodesMap.put(name, node);
50+
Arrays.stream(valueNode.getAliases()).forEach(alias -> nodesMap.put(alias, node));
51+
});
4352
}
4453
}
4554

4655
private void removeNodeIndex(@NotNull Node node) {
47-
if (node instanceof ValueNode) {
48-
((ValueNode<?>) node).getName().peek(nodesMap::remove);
56+
if (node instanceof ValueNode<?> valueNode) {
57+
valueNode.getName().peek(name -> {
58+
nodeNames.remove(name);
59+
nodesMap.remove(name);
60+
Arrays.stream(valueNode.getAliases()).forEach(alias -> nodesMap.remove(alias, node));
61+
});
4962
}
5063
}
5164

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

6174
/**

src/main/java/org/mvplugins/multiverse/core/config/node/ValueNode.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import io.vavr.control.Option;
66
import io.vavr.control.Try;
7+
import org.apache.logging.log4j.util.Strings;
78
import org.bukkit.command.CommandSender;
89
import org.jetbrains.annotations.ApiStatus;
910
import org.jetbrains.annotations.NotNull;
@@ -14,7 +15,7 @@
1415
public interface ValueNode<T> extends Node {
1516

1617
/**
17-
* Gets the name of this node. Used for identifying the node from user input.
18+
* Gets the name of this node. Used for identifying the node from user input. This must be unique within a node group.
1819
*
1920
* @return An {@link Option} containing the name of this node, or {@link Option.None} if the node has no name.
2021
*/
@@ -27,6 +28,19 @@ public interface ValueNode<T> extends Node {
2728
*/
2829
@NotNull Class<T> getType();
2930

31+
/**
32+
* Gets the aliases of this node. Serves as shorter or legacy alternatives the {@link #getName()} and must be
33+
* unique within a node group.
34+
*
35+
* @return The aliases of this node.
36+
*
37+
* @since 5.1
38+
*/
39+
@ApiStatus.AvailableSince("5.1")
40+
default @NotNull String[] getAliases() {
41+
return Strings.EMPTY_ARRAY;
42+
}
43+
3044
/**
3145
* Gets the default value with type {@link T} of the node.
3246
*

0 commit comments

Comments
 (0)