Allow turning nodes back into builders without children

This commit is contained in:
Nathan Adams 2017-06-28 15:09:19 +02:00
parent 716e8f3f16
commit 523e9d0236
11 changed files with 70 additions and 4 deletions

View file

@ -162,4 +162,8 @@ public class CommandDispatcher<S> {
return nodes.toArray(new String[nodes.size()]); return nodes.toArray(new String[nodes.size()]);
} }
public RootCommandNode<S> getRoot() {
return root;
}
} }

View file

@ -28,7 +28,7 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
return getThis(); return getThis();
} }
protected Command<S> getCommand() { public Command<S> getCommand() {
return command; return command;
} }

View file

@ -19,7 +19,7 @@ public class LiteralArgumentBuilder<S> extends ArgumentBuilder<S, LiteralArgumen
return this; return this;
} }
private String getLiteral() { public String getLiteral() {
return literal; return literal;
} }

View file

@ -22,11 +22,11 @@ public class RequiredArgumentBuilder<S, T> extends ArgumentBuilder<S, RequiredAr
return this; return this;
} }
private CommandArgumentType<T> getType() { public CommandArgumentType<T> getType() {
return type; return type;
} }
private String getName() { public String getName() {
return name; return name;
} }

View file

@ -2,6 +2,8 @@ package com.mojang.brigadier.tree;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.CommandArgumentType; import com.mojang.brigadier.arguments.CommandArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument; import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
@ -60,6 +62,16 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
type.listSuggestions(command, output); type.listSuggestions(command, output);
} }
@Override
public RequiredArgumentBuilder<S, T> createBuilder() {
final RequiredArgumentBuilder<S, T> builder = RequiredArgumentBuilder.argument(name, type);
builder.requires(getRequirement());
if (getCommand() != null) {
builder.executes(getCommand());
}
return builder;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View file

@ -2,6 +2,7 @@ package com.mojang.brigadier.tree;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
@ -65,6 +66,10 @@ public abstract class CommandNode<S> {
return 31 * children.hashCode() + (command != null ? command.hashCode() : 0); return 31 * children.hashCode() + (command != null ? command.hashCode() : 0);
} }
public Predicate<S> getRequirement() {
return requirement;
}
protected abstract Object getMergeKey(); protected abstract Object getMergeKey();
public abstract String getUsageText(); public abstract String getUsageText();
@ -72,4 +77,6 @@ public abstract class CommandNode<S> {
public abstract String parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException; public abstract String parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException;
public abstract void listSuggestions(String command, Set<String> output); public abstract void listSuggestions(String command, Set<String> output);
public abstract ArgumentBuilder<S, ?> createBuilder();
} }

View file

@ -2,6 +2,8 @@ package com.mojang.brigadier.tree;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType; import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
@ -70,4 +72,14 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
result = 31 * result + super.hashCode(); result = 31 * result + super.hashCode();
return result; return result;
} }
@Override
public LiteralArgumentBuilder<S> createBuilder() {
final LiteralArgumentBuilder<S> builder = LiteralArgumentBuilder.literal(this.literal);
builder.requires(getRequirement());
if (getCommand() != null) {
builder.executes(getCommand());
}
return builder;
}
} }

View file

@ -1,5 +1,6 @@
package com.mojang.brigadier.tree; package com.mojang.brigadier.tree;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
@ -35,4 +36,9 @@ public class RootCommandNode<S> extends CommandNode<S> {
if (!(o instanceof RootCommandNode)) return false; if (!(o instanceof RootCommandNode)) return false;
return super.equals(o); return super.equals(o);
} }
@Override
public ArgumentBuilder<S, ?> createBuilder() {
throw new IllegalStateException("Cannot convert root into a builder");
}
} }

View file

@ -5,6 +5,7 @@ import com.google.common.collect.Sets;
import com.google.common.testing.EqualsTester; import com.google.common.testing.EqualsTester;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
import org.junit.Before; import org.junit.Before;
@ -105,4 +106,13 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
) )
.testEquals(); .testEquals();
} }
@Test
public void testCreateBuilder() throws Exception {
final RequiredArgumentBuilder<Object, Integer> builder = node.createBuilder();
assertThat(builder.getName(), is(node.getName()));
assertThat(builder.getType(), is(node.getType()));
assertThat(builder.getRequirement(), is(node.getRequirement()));
assertThat(builder.getCommand(), is(node.getCommand()));
}
} }

View file

@ -4,6 +4,8 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.common.testing.EqualsTester; import com.google.common.testing.EqualsTester;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
import org.junit.Before; import org.junit.Before;
@ -117,4 +119,12 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
) )
.testEquals(); .testEquals();
} }
@Test
public void testCreateBuilder() throws Exception {
final LiteralArgumentBuilder<Object> builder = node.createBuilder();
assertThat(builder.getLiteral(), is(node.getLiteral()));
assertThat(builder.getRequirement(), is(node.getRequirement()));
assertThat(builder.getCommand(), is(node.getCommand()));
}
} }

View file

@ -48,6 +48,11 @@ public class RootCommandNodeTest extends AbstractCommandNodeTest {
assertThat(set, is(empty())); assertThat(set, is(empty()));
} }
@Test(expected = IllegalStateException.class)
public void testCreateBuilder() throws Exception {
node.createBuilder();
}
@Test @Test
public void testEquals() throws Exception { public void testEquals() throws Exception {
new EqualsTester() new EqualsTester()