From 3c7bbc9d2d3f2db201ea8f0a2825eb0573938cba Mon Sep 17 00:00:00 2001 From: themode Date: Mon, 8 Feb 2021 03:42:35 +0100 Subject: [PATCH] Added initial command return support --- .../server/command/CommandManager.java | 30 +++++++++++++----- .../minestom/server/command/ServerSender.java | 31 +++++++++++++++++++ .../server/command/builder/Arguments.java | 11 +++++++ .../command/builder/CommandDispatcher.java | 20 +++++++----- 4 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 src/main/java/net/minestom/server/command/ServerSender.java diff --git a/src/main/java/net/minestom/server/command/CommandManager.java b/src/main/java/net/minestom/server/command/CommandManager.java index 68105b5b7..232872125 100644 --- a/src/main/java/net/minestom/server/command/CommandManager.java +++ b/src/main/java/net/minestom/server/command/CommandManager.java @@ -27,6 +27,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jglrxavpok.hephaistos.nbt.NBTCompound; import java.io.BufferedReader; import java.io.IOException; @@ -45,6 +46,7 @@ public final class CommandManager { private volatile boolean running = true; + private final ServerSender serverSender = new ServerSender(); private final ConsoleSender consoleSender = new ConsoleSender(); private final CommandDispatcher dispatcher = new CommandDispatcher(); @@ -154,7 +156,8 @@ public final class CommandManager { * @param command the raw command string (without the command prefix) * @return true if the command hadn't been cancelled and has been successful */ - public boolean execute(@NotNull CommandSender sender, @NotNull String command) { + @Nullable + public NBTCompound execute(@NotNull CommandSender sender, @NotNull String command) { // Command event if (sender instanceof Player) { @@ -164,7 +167,7 @@ public final class CommandManager { player.callEvent(PlayerCommandEvent.class, playerCommandEvent); if (playerCommandEvent.isCancelled()) - return false; + return null; command = playerCommandEvent.getCommand(); } @@ -173,9 +176,9 @@ public final class CommandManager { { // Check for rich-command - final boolean result = this.dispatcher.execute(sender, command); - if (result) { - return true; + final NBTCompound commandData = this.dispatcher.execute(sender, command); + if (commandData != null) { + return commandData; } else { // Check for legacy-command final String[] splitCommand = command.split(StringUtils.SPACE); @@ -185,17 +188,28 @@ public final class CommandManager { if (unknownCommandCallback != null) { this.unknownCommandCallback.apply(sender, command); } - return false; + return null; } // Execute the legacy-command final String[] args = command.substring(command.indexOf(StringUtils.SPACE) + 1).split(StringUtils.SPACE); - - return commandProcessor.process(sender, commandName, args); + commandProcessor.process(sender, commandName, args); + return null; } } } + /** + * Executes the command using a {@link ServerSender} to do not + * print the command messages, and rely instead on the command return data. + * + * @see #execute(CommandSender, String) + */ + @Nullable + public NBTCompound executeServerCommand(@NotNull String command) { + return execute(serverSender, command); + } + /** * Gets the callback executed once an unknown command is run. * diff --git a/src/main/java/net/minestom/server/command/ServerSender.java b/src/main/java/net/minestom/server/command/ServerSender.java new file mode 100644 index 000000000..d8da8877b --- /dev/null +++ b/src/main/java/net/minestom/server/command/ServerSender.java @@ -0,0 +1,31 @@ +package net.minestom.server.command; + +import net.minestom.server.command.builder.Arguments; +import net.minestom.server.permission.Permission; +import org.jetbrains.annotations.NotNull; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +/** + * Sender used in {@link CommandManager#executeServerCommand(String)}. + *

+ * Be aware that {@link #sendMessage(String)} is empty on purpose because the purpose + * of this sender is to process the data of {@link Arguments#getCommandReturn()}. + */ +public class ServerSender implements CommandSender { + + private final Set permissions = Collections.unmodifiableSet(new HashSet<>()); + + @Override + public void sendMessage(@NotNull String message) { + // Empty on purpose + } + + @NotNull + @Override + public Set getAllPermissions() { + return permissions; + } +} diff --git a/src/main/java/net/minestom/server/command/builder/Arguments.java b/src/main/java/net/minestom/server/command/builder/Arguments.java index dbb61a394..9e057c504 100644 --- a/src/main/java/net/minestom/server/command/builder/Arguments.java +++ b/src/main/java/net/minestom/server/command/builder/Arguments.java @@ -35,6 +35,8 @@ public final class Arguments { private Map args = new HashMap<>(); + private NBTCompound commandReturn; + @NotNull public T get(@NotNull Argument argument) { return (T) getObject(argument.getId()); @@ -250,6 +252,15 @@ public final class Arguments { }); } + @Nullable + public NBTCompound getCommandReturn() { + return commandReturn; + } + + public void setCommandReturn(@Nullable NBTCompound commandReturn) { + this.commandReturn = commandReturn; + } + protected void setArg(@NotNull String id, Object value) { this.args.put(id, value); } diff --git a/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java b/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java index 90fdaa85e..fe8129e3a 100644 --- a/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java +++ b/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java @@ -8,6 +8,7 @@ import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jglrxavpok.hephaistos.nbt.NBTCompound; import java.util.*; import java.util.regex.Pattern; @@ -79,18 +80,19 @@ public class CommandDispatcher { } /** - * Check if the command exists, and execute it. + * Checks if the command exists, and execute it. * * @param source the command source * @param commandString the command with the argument(s) - * @return true if the command executed successfully, false if the command doesn't exist + * @return the command data, null if none */ - public boolean execute(@NotNull CommandSender source, @NotNull String commandString) { + @Nullable + public NBTCompound execute(@NotNull CommandSender source, @NotNull String commandString) { CommandResult result = parse(commandString); if (result != null) { - result.execute(source, commandString); + return result.execute(source, commandString); } - return result != null; + return null; } @NotNull @@ -377,8 +379,10 @@ public class CommandDispatcher { * * @param source the command source * @param commandString the command string + * @return the command data, null if none */ - public void execute(@NotNull CommandSender source, @NotNull String commandString) { + @Nullable + public NBTCompound execute(@NotNull CommandSender source, @NotNull String commandString) { // Global listener command.globalListener(source, arguments, commandString); // Command condition check @@ -386,7 +390,7 @@ public class CommandDispatcher { if (condition != null) { final boolean result = condition.canUse(source, commandString); if (!result) - return; + return null; } // Condition is respected if (executor != null) { @@ -408,6 +412,8 @@ public class CommandDispatcher { // Execute the faulty argument callback callback.apply(source, argumentSyntaxException); } + + return arguments.getCommandReturn(); } }