Add CommandExecuteEvent

This commit is contained in:
Leymooo
2020-04-27 00:04:12 +03:00
parent 957c0dd307
commit 6555e0e337
7 changed files with 226 additions and 19 deletions

View File

@@ -35,10 +35,21 @@ public interface CommandManager {
/**
* Attempts to execute a command from the specified {@code cmdLine}.
* CommandExecuteEvent will not called
*
* @param source the command's source
* @param cmdLine the command to run
* @return true if the command was found and executed, false if it was not
*/
boolean execute(CommandSource source, String cmdLine);
/**
* Attempts to execute a command from the specified {@code cmdLine}.
*
* @param source the command's source
* @param cmdLine the command to run
* @param callEvent will CommandExecuteEvent called or not
* @return true if the command was found and executed, false if it was not
*/
boolean execute(CommandSource source, String cmdLine, boolean callEvent);
}

View File

@@ -0,0 +1,143 @@
package com.velocitypowered.api.event.command;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* This event is fired when a player types in a chat message.
*/
public final class CommandExecuteEvent implements ResultedEvent<CommandResult> {
private final CommandSource commandSource;
private final String command;
private CommandResult result;
/**
* Constructs a PlayerChatEvent.
* @param commandSource the source executing the command
* @param command the command being executed without first slash
*/
public CommandExecuteEvent(CommandSource commandSource, String command) {
this.commandSource = Preconditions.checkNotNull(commandSource, "commandSource");
this.command = Preconditions.checkNotNull(command, "command");
this.result = CommandResult.allowed();
}
public CommandSource getCommandSource() {
return commandSource;
}
/**
* Gets the original command being executed without first slash.
* @return the original command being executed
*/
public String getCommand() {
return command;
}
@Override
public CommandResult getResult() {
return result;
}
@Override
public void setResult(CommandResult result) {
this.result = Preconditions.checkNotNull(result, "result");
}
@Override
public String toString() {
return "PlayerCommmandEvent{"
+ "commandSource=" + commandSource
+ ", command=" + command
+ ", result=" + result
+ '}';
}
/**
* Represents the result of the {@link CommandExecuteEvent}.
*/
public static final class CommandResult implements Result {
private static final CommandResult ALLOWED = new CommandResult(true, false,null);
private static final CommandResult DENIED = new CommandResult(false, false,null);
private static final CommandResult FORWARD_TO_SERVER = new CommandResult(false, true, null);
private @Nullable String command;
private final boolean status;
private final boolean forward;
private CommandResult(boolean status, boolean forward, @Nullable String command) {
this.status = status;
this.forward = forward;
this.command = command;
}
public Optional<String> getCommand() {
return Optional.ofNullable(command);
}
public boolean isForwardToServer() {
return forward;
}
@Override
public boolean isAllowed() {
return status;
}
@Override
public String toString() {
return status ? "allowed" : "denied";
}
/**
* Allows the command to be sent, without modification.
* @return the allowed result
*/
public static CommandResult allowed() {
return ALLOWED;
}
/**
* Prevents the command from being executed.
* @return the denied result
*/
public static CommandResult denied() {
return DENIED;
}
/**
* Prevents the command from being executed, but forward command to server.
* @return the forward result
*/
public static CommandResult forwardToServer() {
return FORWARD_TO_SERVER;
}
/**
* Prevents the command from being executed on proxy, but forward command to server.
* @param newCommand the command without first slash to use instead
* @return a result with a new command being forwarded to server
*/
public static CommandResult forwardToServer(@NonNull String newCommand) {
Preconditions.checkNotNull(newCommand, "newCommand");
return new CommandResult(false, true, newCommand);
}
/**
* Allows the command to be executed, but silently replaced old command with another.
* @param newCommand the command to use instead without first slash
* @return a result with a new command
*/
public static CommandResult command(@NonNull String newCommand) {
Preconditions.checkNotNull(newCommand, "newCommand");
return new CommandResult(true, false, newCommand);
}
}
}

View File

@@ -70,6 +70,10 @@ public final class PlayerChatEvent implements ResultedEvent<PlayerChatEvent.Chat
this.message = message;
}
public Optional<String> getMessage() {
return Optional.ofNullable(message);
}
@Override
public boolean isAllowed() {
return status;
@@ -96,10 +100,6 @@ public final class PlayerChatEvent implements ResultedEvent<PlayerChatEvent.Chat
return DENIED;
}
public Optional<String> getMessage() {
return Optional.ofNullable(message);
}
/**
* Allows the message to be sent, but silently replaced with another.
* @param message the message to use instead
@@ -110,6 +110,4 @@ public final class PlayerChatEvent implements ResultedEvent<PlayerChatEvent.Chat
return new ChatResult(true, message);
}
}
}