From 435e3ea36368c3671bf22a0b931b1451836d584b Mon Sep 17 00:00:00 2001 From: themode Date: Sun, 7 Mar 2021 19:42:02 +0100 Subject: [PATCH] WIP CommandExecutionOption, missing the ignoreMessage option --- .../command/CommandExecutionOption.java | 39 +++++++++++++++++++ .../server/command/CommandManager.java | 22 ++++++++--- .../command/builder/CommandDispatcher.java | 6 ++- .../server/command/builder/ParsedCommand.java | 6 ++- 4 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 src/main/java/net/minestom/server/command/CommandExecutionOption.java diff --git a/src/main/java/net/minestom/server/command/CommandExecutionOption.java b/src/main/java/net/minestom/server/command/CommandExecutionOption.java new file mode 100644 index 000000000..24f153dd4 --- /dev/null +++ b/src/main/java/net/minestom/server/command/CommandExecutionOption.java @@ -0,0 +1,39 @@ +package net.minestom.server.command; + +public class CommandExecutionOption { + + private boolean ignoreEvent = false; + private boolean ignorePermission = false; + private boolean ignoreMessage = false; + + public CommandExecutionOption() { + + } + + public boolean isIgnoreEvent() { + return ignoreEvent; + } + + public CommandExecutionOption ignoreEvent(boolean ignoreEvent) { + this.ignoreEvent = ignoreEvent; + return this; + } + + public boolean isIgnorePermission() { + return ignorePermission; + } + + public CommandExecutionOption ignorePermission(boolean ignorePermission) { + this.ignorePermission = ignorePermission; + return this; + } + + public boolean isIgnoreMessage() { + return ignoreMessage; + } + + public CommandExecutionOption ignoreMessage(boolean ignoreMessage) { + this.ignoreMessage = ignoreMessage; + return this; + } +} diff --git a/src/main/java/net/minestom/server/command/CommandManager.java b/src/main/java/net/minestom/server/command/CommandManager.java index 819b649d1..bec6f8f28 100644 --- a/src/main/java/net/minestom/server/command/CommandManager.java +++ b/src/main/java/net/minestom/server/command/CommandManager.java @@ -143,15 +143,17 @@ public final class CommandManager { /** * Executes a command for a {@link ConsoleSender}. * - * @param sender the sender of the command - * @param command the raw command string (without the command prefix) + * @param sender the sender of the command + * @param command the raw command string (without the command prefix) + * @param executionOption options to disable certain parts of the execution * @return the execution result */ @NotNull - public CommandResult execute(@NotNull CommandSender sender, @NotNull String command) { + public CommandResult execute(@NotNull CommandSender sender, @NotNull String command, + @NotNull CommandExecutionOption executionOption) { // Command event - if (sender instanceof Player) { + if (sender instanceof Player && !executionOption.isIgnoreEvent()) { Player player = (Player) sender; PlayerCommandEvent playerCommandEvent = new PlayerCommandEvent(player, command); @@ -167,7 +169,7 @@ public final class CommandManager { { // Check for rich-command - final CommandResult result = this.dispatcher.execute(sender, command); + final CommandResult result = this.dispatcher.execute(sender, command, executionOption); if (result.getType() != CommandResult.Type.UNKNOWN) { return result; } else { @@ -190,6 +192,16 @@ public final class CommandManager { } } + /** + * Executes a command with the default options. + * + * @see #execute(CommandSender, String, CommandExecutionOption) + */ + @NotNull + public CommandResult execute(@NotNull CommandSender sender, @NotNull String command) { + return execute(sender, command, new CommandExecutionOption()); + } + /** * Executes the command using a {@link ServerSender} to do not * print the command messages, and rely instead on the command return data. 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 a06c43033..7dc6bc6a2 100644 --- a/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java +++ b/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java @@ -3,6 +3,7 @@ package net.minestom.server.command.builder; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import it.unimi.dsi.fastutil.ints.Int2ObjectRBTreeMap; +import net.minestom.server.command.CommandExecutionOption; import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.arguments.Argument; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; @@ -87,11 +88,12 @@ public class CommandDispatcher { * @return the command result */ @NotNull - public CommandResult execute(@NotNull CommandSender source, @NotNull String commandString) { + public CommandResult execute(@NotNull CommandSender source, @NotNull String commandString, + @NotNull CommandExecutionOption executionOption) { CommandResult commandResult = parse(commandString); ParsedCommand parsedCommand = commandResult.parsedCommand; if (parsedCommand != null) { - commandResult.commandData = parsedCommand.execute(source, commandString); + commandResult.commandData = parsedCommand.execute(source, commandString, executionOption); } return commandResult; } diff --git a/src/main/java/net/minestom/server/command/builder/ParsedCommand.java b/src/main/java/net/minestom/server/command/builder/ParsedCommand.java index fbf03d5fb..390e696c9 100644 --- a/src/main/java/net/minestom/server/command/builder/ParsedCommand.java +++ b/src/main/java/net/minestom/server/command/builder/ParsedCommand.java @@ -1,5 +1,6 @@ package net.minestom.server.command.builder; +import net.minestom.server.command.CommandExecutionOption; import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.condition.CommandCondition; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; @@ -35,12 +36,13 @@ public class ParsedCommand { * @return the command data, null if none */ @Nullable - public CommandData execute(@NotNull CommandSender source, @NotNull String commandString) { + public CommandData execute(@NotNull CommandSender source, @NotNull String commandString, + @NotNull CommandExecutionOption executionOption) { // Global listener command.globalListener(source, arguments, commandString); // Command condition check final CommandCondition condition = command.getCondition(); - if (condition != null) { + if (condition != null && !executionOption.isIgnorePermission()) { final boolean result = condition.canUse(source, commandString); if (!result) return null;