Skip to content

Commit

Permalink
Invocation Source API
Browse files Browse the repository at this point in the history
Allows proxies to detect if a command is executed from an unsigned/signed/api source.

This is useful because it allows commands executed from the player manually or by clicking on a chat message to be controlled.
  • Loading branch information
Owen1212055 committed Dec 3, 2024
1 parent be5f0ac commit 99f963a
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public final class CommandExecuteEvent implements ResultedEvent<CommandResult> {
private final CommandSource commandSource;
private final String command;
private CommandResult result;
private InvocationSource invocationSource;

/**
* Constructs a CommandExecuteEvent.
Expand All @@ -34,9 +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);
}

/**
* 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
*/
public CommandExecuteEvent(CommandSource commandSource, String command, InvocationSource invocationSource) {
this.commandSource = Preconditions.checkNotNull(commandSource, "commandSource");
this.command = Preconditions.checkNotNull(command, "command");
this.result = CommandResult.allowed();
this.invocationSource = invocationSource;
}

/**
Expand All @@ -61,6 +74,16 @@ public String getCommand() {
return command;
}

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

@Override
public CommandResult getResult() {
return result;
Expand All @@ -80,6 +103,34 @@ public String toString() {
+ '}';
}

/**
* Represents the source of a command invocation.
*/
public enum InvocationSource {
/**
* Indicates that the command was executed from an signed source,
* such as a player's direct input (e.g., typing in chat).
*/
SIGNED,
/**
* 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}.
*
* <p>Sent by clients on 1.20.5+
*/
UNSIGNED,
/**
* Indicates that the command was invoked programmatically via an API call.
*/
API,
/**
* Indicates that the command was executed from an unknown source.
*
* <p>This is sent on command execution for pre 1.19.3 clients.
*/
UNKNOWN
}

/**
* Represents the result of the {@link CommandExecuteEvent}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ public void unregister(CommandMeta meta) {
* @return the {@link CompletableFuture} of the event
*/
public CompletableFuture<CommandExecuteEvent> callCommandEvent(final CommandSource source,
final String cmdLine) {
final String cmdLine, final CommandExecuteEvent.InvocationSource unsignedSource) {
Preconditions.checkNotNull(source, "source");
Preconditions.checkNotNull(cmdLine, "cmdLine");
return eventManager.fire(new CommandExecuteEvent(source, cmdLine));
return eventManager.fire(new CommandExecuteEvent(source, cmdLine, unsignedSource));
}

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

return callCommandEvent(source, cmdLine).thenComposeAsync(event -> {
return callCommandEvent(source, cmdLine, CommandExecuteEvent.InvocationSource.API).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,8 +56,9 @@ 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) {
CompletableFuture<CommandExecuteEvent> eventFuture = server.getCommandManager().callCommandEvent(player, message);
String message, Instant timestamp, @Nullable LastSeenMessages lastSeenMessages, CommandExecuteEvent.InvocationSource invocationSource) {
CompletableFuture<CommandExecuteEvent> eventFuture = server.getCommandManager().callCommandEvent(player, message,
invocationSource);
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);
}, packet.getCommand(), packet.getTimestamp(), null, CommandExecuteEvent.InvocationSource.UNKNOWN);
}
}
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);
}, command, Instant.now(), null, CommandExecuteEvent.InvocationSource.UNKNOWN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,6 @@ public void handlePlayerCommandInternal(SessionPlayerCommandPacket packet) {
}
return forwardCommand(fixedPacket, commandToRun);
});
}, packet.command, packet.timeStamp, packet.lastSeenMessages);
}, packet.command, packet.timeStamp, packet.lastSeenMessages, packet.unsignedSource() ? CommandExecuteEvent.InvocationSource.UNSIGNED : CommandExecuteEvent.InvocationSource.SIGNED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public boolean isSigned() {
return !argumentSignatures.isEmpty();
}

public boolean unsignedSource() {
return false;
}

@Override
public boolean handle(MinecraftSessionHandler handler) {
return handler.handle(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public boolean isSigned() {
return false;
}

@Override
public boolean unsignedSource() {
return true;
}

@Override
public String toString() {
return "UnsignedPlayerCommandPacket{" +
Expand Down

0 comments on commit 99f963a

Please sign in to comment.