WIP CommandExecutionOption, missing the ignoreMessage option

This commit is contained in:
themode 2021-03-07 19:42:02 +01:00
parent c20cf38399
commit 435e3ea363
4 changed files with 64 additions and 9 deletions

View File

@ -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;
}
}

View File

@ -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.

View File

@ -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;
}

View File

@ -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;