diff --git a/src/main/java/com/mojang/brigadier/CommandDispatcher.java b/src/main/java/com/mojang/brigadier/CommandDispatcher.java index 46d6119..cfec02e 100644 --- a/src/main/java/com/mojang/brigadier/CommandDispatcher.java +++ b/src/main/java/com/mojang/brigadier/CommandDispatcher.java @@ -39,9 +39,13 @@ public class CommandDispatcher { root.addChild(command.build()); } - public int execute(String command, S source) throws CommandException { - CommandContext context = parse(command, source).build(); - return context.getCommand().run(context); + public int execute(String input, S source) throws CommandException { + CommandContext context = parse(input, source).build(); + Command command = context.getCommand(); + if (command == null) { + throw ERROR_UNKNOWN_COMMAND.create(); + } + return command.run(context); } public CommandContextBuilder parse(String command, S source) throws CommandException { diff --git a/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java b/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java index 07914a8..b704a06 100644 --- a/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java +++ b/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java @@ -117,6 +117,19 @@ public class CommandDispatcherTest { } } + @Test + public void testExecuteEmptyCommand() throws Exception { + subject.register(literal("")); + + try { + subject.execute("", source); + fail(); + } catch (CommandException ex) { + assertThat(ex.getType(), is(CommandDispatcher.ERROR_UNKNOWN_COMMAND)); + assertThat(ex.getData(), is(Collections.emptyMap())); + } + } + @Test public void testExecuteUnknownSubcommand() throws Exception { subject.register(literal("foo").executes(command));