Skip to content

Commit

Permalink
Update api significantly to improve api coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen1212055 committed Dec 3, 2024
1 parent 99f963a commit 6b55d95
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public final class CommandExecuteEvent implements ResultedEvent<CommandResult> {
private final CommandSource commandSource;
private final String command;
private CommandResult result;
private InvocationSource invocationSource;
private InvocationInfo invocationSource;

/**
* Constructs a CommandExecuteEvent.
Expand All @@ -35,21 +35,21 @@ public final class CommandExecuteEvent implements ResultedEvent<CommandResult> {
* @param command the command being executed without first slash
*/
public CommandExecuteEvent(CommandSource commandSource, String command) {
this(commandSource, command, InvocationSource.API);
this(commandSource, command, new InvocationInfo(SignedState.UNSUPPORTED, Source.API));
}

/**
* Constructs a CommandExecuteEvent.
*
* @param commandSource the source executing the command
* @param command the command being executed without first slash
* @param invocationSource the invocation source of this command
* @param invocationInfo the invocation info of this command
*/
public CommandExecuteEvent(CommandSource commandSource, String command, InvocationSource invocationSource) {
public CommandExecuteEvent(CommandSource commandSource, String command, InvocationInfo invocationInfo) {
this.commandSource = Preconditions.checkNotNull(commandSource, "commandSource");
this.command = Preconditions.checkNotNull(command, "command");
this.result = CommandResult.allowed();
this.invocationSource = invocationSource;
this.invocationSource = invocationInfo;
}

/**
Expand All @@ -75,12 +75,12 @@ public String getCommand() {
}

/**
* Returns the source of the command invocation, indicating how the command was executed.
* Returns the info of the command invocation.
*
* @since 3.4.0
* @return invocation source
*/
@NonNull
public InvocationSource getInvocationSource() {
public InvocationInfo getInvocationInfo() {
return this.invocationSource;
}

Expand All @@ -104,31 +104,73 @@ public String toString() {
}

/**
* Represents the source of a command invocation.
* Represents information about a command invocation, including its signed state and source.
*
* @since 3.4.0
*/
public record InvocationInfo(SignedState signedState, Source source) {
}

/**
* Represents the signed state of a command invocation.
*
* @since 3.4.0
*/
public enum InvocationSource {
public enum SignedState {
/**
* Indicates that the command was executed from an signed source,
* such as a player's direct input (e.g., typing in chat).
* Indicates that the command was executed from a signed source with signed message arguments,
* such as a player's direct input (e.g., typing a command in chat with signed arguments).
*
* <p><b>Note:</b> Cancelling the {@link CommandExecuteEvent} in this state will result in the player being kicked.</p>
*
* @since 3.4.0
*/
SIGNED_WITH_ARGS,

/**
* Indicates that the command was executed from a signed source without signed message arguments,
* such as a player's direct input (e.g., typing a command in chat without any signed arguments).
*
* @since 3.4.0
*/
SIGNED,
SIGNED_WITHOUT_ARGS,
/**
* Indicates that the command was executed from an unsigned source,
* such as clicking a component with a {@link net.kyori.adventure.text.event.ClickEvent.Action#RUN_COMMAND}.
* such as clicking on a component with a {@link net.kyori.adventure.text.event.ClickEvent.Action#RUN_COMMAND}.
*
* <p>Sent by clients on 1.20.5+
* <p>Clients running version 1.20.5 or later will send this state.</p>
*
* @since 3.4.0
*/
UNSIGNED,
/**
* Indicates that the command was invoked programmatically via an API call.
* Indicates that the command invocation does not support signing.
*
* <p>This state is sent by clients running versions prior to 1.19.3.</p>
*
* @since 3.4.0
*/
UNSUPPORTED
}

/**
* Represents the source of a command invocation.
*
* @since 3.4.0
*/
public enum Source {
/**
* Indicates that the command was invoked by a player.
*
* @since 3.4.0
*/
API,
PLAYER,
/**
* Indicates that the command was executed from an unknown source.
* Indicates that the command was invoked programmatically through an API call.
*
* <p>This is sent on command execution for pre 1.19.3 clients.
* @since 3.4.0
*/
UNKNOWN
API
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,14 @@ public void unregister(CommandMeta meta) {
*
* @param source the source to execute the command for
* @param cmdLine the command to execute
* @param invocationInfo the invocation info
* @return the {@link CompletableFuture} of the event
*/
public CompletableFuture<CommandExecuteEvent> callCommandEvent(final CommandSource source,
final String cmdLine, final CommandExecuteEvent.InvocationSource unsignedSource) {
final String cmdLine, final CommandExecuteEvent.InvocationInfo invocationInfo) {
Preconditions.checkNotNull(source, "source");
Preconditions.checkNotNull(cmdLine, "cmdLine");
return eventManager.fire(new CommandExecuteEvent(source, cmdLine, unsignedSource));
return eventManager.fire(new CommandExecuteEvent(source, cmdLine, invocationInfo));
}

private boolean executeImmediately0(final CommandSource source, final ParseResults<CommandSource> parsed) {
Expand Down Expand Up @@ -266,7 +267,12 @@ public CompletableFuture<Boolean> executeAsync(final CommandSource source, final
Preconditions.checkNotNull(source, "source");
Preconditions.checkNotNull(cmdLine, "cmdLine");

return callCommandEvent(source, cmdLine, CommandExecuteEvent.InvocationSource.API).thenComposeAsync(event -> {
CommandExecuteEvent.InvocationInfo invocationInfo = new CommandExecuteEvent.InvocationInfo(
CommandExecuteEvent.SignedState.UNSUPPORTED,
CommandExecuteEvent.Source.API
);

return callCommandEvent(source, cmdLine, invocationInfo).thenComposeAsync(event -> {
CommandExecuteEvent.CommandResult commandResult = event.getResult();
if (commandResult.isForwardToServer() || !commandResult.isAllowed()) {
return CompletableFuture.completedFuture(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ default CompletableFuture<MinecraftPacket> runCommand(VelocityServer server,

default void queueCommandResult(VelocityServer server, ConnectedPlayer player,
BiFunction<CommandExecuteEvent, LastSeenMessages, CompletableFuture<MinecraftPacket>> futurePacketCreator,
String message, Instant timestamp, @Nullable LastSeenMessages lastSeenMessages, CommandExecuteEvent.InvocationSource invocationSource) {
String message, Instant timestamp, @Nullable LastSeenMessages lastSeenMessages,
CommandExecuteEvent.InvocationInfo invocationInfo) {
CompletableFuture<CommandExecuteEvent> eventFuture = server.getCommandManager().callCommandEvent(player, message,
invocationSource);
invocationInfo);
player.getChatQueue().queuePacket(
newLastSeenMessages -> eventFuture
.thenComposeAsync(event -> futurePacketCreator.apply(event, newLastSeenMessages))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ public void handlePlayerCommandInternal(KeyedPlayerCommandPacket packet) {
}
return null;
});
}, packet.getCommand(), packet.getTimestamp(), null, CommandExecuteEvent.InvocationSource.UNKNOWN);
}, packet.getCommand(), packet.getTimestamp(), null, new CommandExecuteEvent.InvocationInfo(CommandExecuteEvent.SignedState.UNSUPPORTED, CommandExecuteEvent.Source.PLAYER));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ public void handlePlayerCommandInternal(LegacyChatPacket packet) {
}
return null;
});
}, command, Instant.now(), null, CommandExecuteEvent.InvocationSource.UNKNOWN);
}, command, Instant.now(), null, new CommandExecuteEvent.InvocationInfo(CommandExecuteEvent.SignedState.UNSUPPORTED, CommandExecuteEvent.Source.PLAYER));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public void handlePlayerCommandInternal(SessionPlayerCommandPacket packet) {
}
return forwardCommand(fixedPacket, commandToRun);
});
}, packet.command, packet.timeStamp, packet.lastSeenMessages, packet.unsignedSource() ? CommandExecuteEvent.InvocationSource.UNSIGNED : CommandExecuteEvent.InvocationSource.SIGNED);
}, packet.command, packet.timeStamp, packet.lastSeenMessages,
new CommandExecuteEvent.InvocationInfo(packet.getEventSignedState(), CommandExecuteEvent.Source.PLAYER));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.velocitypowered.proxy.protocol.packet.chat.session;

import com.google.common.collect.Lists;
import com.velocitypowered.api.event.command.CommandExecuteEvent;
import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
import com.velocitypowered.proxy.protocol.MinecraftPacket;
Expand Down Expand Up @@ -68,8 +69,8 @@ public boolean isSigned() {
return !argumentSignatures.isEmpty();
}

public boolean unsignedSource() {
return false;
public CommandExecuteEvent.SignedState getEventSignedState() {
return !this.argumentSignatures.isEmpty() ? CommandExecuteEvent.SignedState.SIGNED_WITH_ARGS : CommandExecuteEvent.SignedState.SIGNED_WITHOUT_ARGS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.velocitypowered.proxy.protocol.packet.chat.session;

import com.velocitypowered.api.event.command.CommandExecuteEvent;
import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.proxy.protocol.ProtocolUtils;
import com.velocitypowered.proxy.protocol.packet.chat.LastSeenMessages;
Expand Down Expand Up @@ -45,8 +46,8 @@ public boolean isSigned() {
}

@Override
public boolean unsignedSource() {
return true;
public CommandExecuteEvent.SignedState getEventSignedState() {
return CommandExecuteEvent.SignedState.UNSIGNED;
}

@Override
Expand Down

0 comments on commit 6b55d95

Please sign in to comment.