Keep nodes sorted alphabetically for niceness
This commit is contained in:
@@ -9,5 +9,13 @@ import java.util.Set;
|
||||
public interface ArgumentType<T> {
|
||||
<S> ParsedArgument<S, T> parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException;
|
||||
|
||||
<S> void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder);
|
||||
default <S> void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) {}
|
||||
|
||||
default String getUsageSuffix() {
|
||||
return null;
|
||||
}
|
||||
|
||||
default String getUsageText() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,10 +83,6 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
@@ -111,4 +107,14 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
|
||||
return "integer(" + minimum + ", " + maximum + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsageSuffix() {
|
||||
return suffix.length() == 0 ? null : suffix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsageText() {
|
||||
return "int";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,10 +96,6 @@ public class StringArgumentType implements ArgumentType<String> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "string()";
|
||||
|
||||
@@ -38,7 +38,15 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
|
||||
|
||||
@Override
|
||||
public String getUsageText() {
|
||||
return USAGE_ARGUMENT_OPEN + name + USAGE_ARGUMENT_CLOSE;
|
||||
String usage = name;
|
||||
if (type.getUsageText() != null) {
|
||||
usage += ": " + type.getUsageText();
|
||||
}
|
||||
usage = USAGE_ARGUMENT_OPEN + usage + USAGE_ARGUMENT_CLOSE;
|
||||
if (type.getUsageSuffix() != null) {
|
||||
usage += type.getUsageSuffix();
|
||||
}
|
||||
return usage;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -89,4 +97,9 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
|
||||
result = 31 * result + type.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSortedKey() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.mojang.brigadier.tree;
|
||||
|
||||
import com.google.common.collect.ComparisonChain;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.mojang.brigadier.Command;
|
||||
import com.mojang.brigadier.builder.ArgumentBuilder;
|
||||
@@ -7,12 +8,14 @@ import com.mojang.brigadier.context.CommandContextBuilder;
|
||||
import com.mojang.brigadier.exceptions.CommandException;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class CommandNode<S> {
|
||||
private final Map<Object, CommandNode<S>> children = Maps.newLinkedHashMap();
|
||||
public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
||||
private Map<Object, CommandNode<S>> children = Maps.newLinkedHashMap();
|
||||
private Command<S> command;
|
||||
private Predicate<S> requirement;
|
||||
|
||||
@@ -46,6 +49,8 @@ public abstract class CommandNode<S> {
|
||||
} else {
|
||||
children.put(node.getMergeKey(), node);
|
||||
}
|
||||
|
||||
children = children.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -79,4 +84,15 @@ public abstract class CommandNode<S> {
|
||||
public abstract void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder);
|
||||
|
||||
public abstract ArgumentBuilder<S, ?> createBuilder();
|
||||
|
||||
protected abstract String getSortedKey();
|
||||
|
||||
@Override
|
||||
public int compareTo(CommandNode<S> o) {
|
||||
return ComparisonChain
|
||||
.start()
|
||||
.compareTrueFirst(this instanceof LiteralCommandNode, o instanceof LiteralCommandNode)
|
||||
.compare(getSortedKey(), o.getSortedKey())
|
||||
.result();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,4 +81,9 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSortedKey() {
|
||||
return literal;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,4 +41,9 @@ public class RootCommandNode<S> extends CommandNode<S> {
|
||||
public ArgumentBuilder<S, ?> createBuilder() {
|
||||
throw new IllegalStateException("Cannot convert root into a builder");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getSortedKey() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user