From 3946b084a0af53cecf9cae86096c09f0ea5834d5 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Tue, 27 Jun 2017 15:34:42 +0200 Subject: [PATCH] Commands should return an int success count --- .../java/com/mojang/brigadier/Command.java | 5 ++++- .../mojang/brigadier/CommandDispatcher.java | 4 ++-- .../brigadier/builder/ArgumentBuilder.java | 2 +- .../brigadier/CommandDispatcherTest.java | 20 ++++++++++++------- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/mojang/brigadier/Command.java b/src/main/java/com/mojang/brigadier/Command.java index 6272359..92c1672 100644 --- a/src/main/java/com/mojang/brigadier/Command.java +++ b/src/main/java/com/mojang/brigadier/Command.java @@ -2,6 +2,9 @@ package com.mojang.brigadier; import com.mojang.brigadier.context.CommandContext; +@FunctionalInterface public interface Command { - void run(CommandContext context); + int SINGLE_SUCCESS = 1; + + int run(CommandContext context); } diff --git a/src/main/java/com/mojang/brigadier/CommandDispatcher.java b/src/main/java/com/mojang/brigadier/CommandDispatcher.java index 9d39604..0ba62ab 100644 --- a/src/main/java/com/mojang/brigadier/CommandDispatcher.java +++ b/src/main/java/com/mojang/brigadier/CommandDispatcher.java @@ -39,9 +39,9 @@ public class CommandDispatcher { root.addChild(command.build()); } - public void execute(String command, S source) throws CommandException { + public int execute(String command, S source) throws CommandException { CommandContext context = parseNodes(root, command, new CommandContextBuilder<>(source)); - context.getCommand().run(context); + return context.getCommand().run(context); } private CommandContext parseNodes(CommandNode node, String command, CommandContextBuilder contextBuilder) throws CommandException { diff --git a/src/main/java/com/mojang/brigadier/builder/ArgumentBuilder.java b/src/main/java/com/mojang/brigadier/builder/ArgumentBuilder.java index 5b20952..f71192f 100644 --- a/src/main/java/com/mojang/brigadier/builder/ArgumentBuilder.java +++ b/src/main/java/com/mojang/brigadier/builder/ArgumentBuilder.java @@ -7,7 +7,7 @@ import com.mojang.brigadier.tree.RootCommandNode; import java.util.Collection; import java.util.function.Predicate; -public abstract class ArgumentBuilder> { +public abstract class ArgumentBuilder> { private final RootCommandNode arguments = new RootCommandNode<>(); private Command command; private Predicate requirement = s -> true; diff --git a/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java b/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java index 6f3295d..07914a8 100644 --- a/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java +++ b/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java @@ -33,6 +33,7 @@ public class CommandDispatcherTest { @Before public void setUp() throws Exception { subject = new CommandDispatcher<>(); + when(command.run(any())).thenReturn(42); } @SuppressWarnings("unchecked") @@ -40,7 +41,7 @@ public class CommandDispatcherTest { public void testCreateAndExecuteCommand() throws Exception { subject.register(literal("foo").executes(command)); - subject.execute("foo", source); + assertThat(subject.execute("foo", source), is(42)); verify(command).run(any(CommandContext.class)); } @@ -50,8 +51,8 @@ public class CommandDispatcherTest { subject.register(literal("base").then(literal("foo")).executes(command)); subject.register(literal("base").then(literal("bar")).executes(command)); - subject.execute("base foo", source); - subject.execute("base bar", source); + assertThat(subject.execute("base foo", source), is(42)); + assertThat(subject.execute("base bar", source), is(42)); verify(command, times(2)).run(any(CommandContext.class)); } @@ -62,6 +63,10 @@ public class CommandDispatcherTest { Command two = mock(Command.class); Command three = mock(Command.class); + when(one.run(any())).thenReturn(111); + when(two.run(any())).thenReturn(222); + when(three.run(any())).thenReturn(333); + subject.register( literal("foo").then( argument("one", integer()).then( @@ -78,13 +83,13 @@ public class CommandDispatcherTest { ) ); - subject.execute("foo 1 one", source); + assertThat(subject.execute("foo 1 one", source), is(111)); verify(one).run(any(CommandContext.class)); - subject.execute("foo 2 two", source); + assertThat(subject.execute("foo 2 two", source), is(222)); verify(two).run(any(CommandContext.class)); - subject.execute("foo 3 three", source); + assertThat(subject.execute("foo 3 three", source), is(333)); verify(three).run(any(CommandContext.class)); } @@ -129,6 +134,7 @@ public class CommandDispatcherTest { @Test public void testExecuteSubcommand() throws Exception { Command subCommand = mock(Command.class); + when(subCommand.run(any())).thenReturn(100); subject.register(literal("foo").then( literal("a") @@ -138,7 +144,7 @@ public class CommandDispatcherTest { literal("c") ).executes(command)); - subject.execute("foo b", source); + assertThat(subject.execute("foo b", source), is(100)); verify(subCommand).run(any(CommandContext.class)); }