3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-16 21:10:30 +01:00

Extract common command handler paths

Dieser Commit ist enthalten in:
Gegy 2023-11-27 18:36:08 +01:00 committet von Riley Park
Ursprung be73131d5d
Commit 92c48507eb

Datei anzeigen

@ -20,10 +20,12 @@ package com.velocitypowered.proxy.protocol.packet.chat.session;
import com.velocitypowered.api.event.command.CommandExecuteEvent; import com.velocitypowered.api.event.command.CommandExecuteEvent;
import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import com.velocitypowered.proxy.protocol.MinecraftPacket;
import com.velocitypowered.proxy.protocol.packet.chat.ChatAcknowledgementPacket; import com.velocitypowered.proxy.protocol.packet.chat.ChatAcknowledgementPacket;
import com.velocitypowered.proxy.protocol.packet.chat.CommandHandler; import com.velocitypowered.proxy.protocol.packet.chat.CommandHandler;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
public class SessionCommandHandler implements CommandHandler<SessionPlayerCommandPacket> { public class SessionCommandHandler implements CommandHandler<SessionPlayerCommandPacket> {
@ -40,55 +42,24 @@ public class SessionCommandHandler implements CommandHandler<SessionPlayerComman
return SessionPlayerCommandPacket.class; return SessionPlayerCommandPacket.class;
} }
@Override @Nullable
public void handlePlayerCommandInternal(SessionPlayerCommandPacket packet) { private MinecraftPacket consumeCommand(SessionPlayerCommandPacket packet) {
queueCommandResult(this.server, this.player, event -> {
CommandExecuteEvent.CommandResult result = event.getResult();
if (result == CommandExecuteEvent.CommandResult.denied()) {
if (packet.isSigned()) {
logger.fatal("A plugin tried to deny a command with signable component(s). "
+ "This is not supported. "
+ "Disconnecting player " + player.getUsername() + ". Command packet: " + packet);
player.disconnect(Component.text(
"A proxy plugin caused an illegal protocol state. "
+ "Contact your network administrator."));
}
// We seemingly can't actually do this if signed args exist, if not, we can probs keep stuff happy
if (packet.lastSeenMessages != null) { if (packet.lastSeenMessages != null) {
return CompletableFuture.completedFuture(new ChatAcknowledgementPacket(packet.lastSeenMessages.getOffset())); return new ChatAcknowledgementPacket(packet.lastSeenMessages.getOffset());
} }
return CompletableFuture.completedFuture(null); return null;
} }
String commandToRun = result.getCommand().orElse(packet.command); @Nullable
if (result.isForwardToServer()) { private MinecraftPacket forwardCommand(SessionPlayerCommandPacket packet, String newCommand) {
if (packet.isSigned() && commandToRun.equals(packet.command)) { if (packet.isSigned() && newCommand.equals(packet.command)) {
return CompletableFuture.completedFuture(packet);
} else {
if (packet.isSigned()) {
logger.fatal("A plugin tried to change a command with signed component(s). "
+ "This is not supported. "
+ "Disconnecting player " + player.getUsername() + ". Command packet: " + packet);
player.disconnect(Component.text(
"A proxy plugin caused an illegal protocol state. "
+ "Contact your network administrator."));
return CompletableFuture.completedFuture(null);
}
return CompletableFuture.completedFuture(this.player.getChatBuilderFactory()
.builder()
.setTimestamp(packet.timeStamp)
.asPlayer(this.player)
.message("/" + commandToRun)
.toServer());
}
}
return runCommand(this.server, this.player, commandToRun, hasRun -> {
if (!hasRun) {
if (packet.isSigned() && commandToRun.equals(packet.command)) {
return packet; return packet;
} else { }
return modifyCommand(packet, newCommand);
}
@Nullable
private MinecraftPacket modifyCommand(SessionPlayerCommandPacket packet, String newCommand) {
if (packet.isSigned()) { if (packet.isSigned()) {
logger.fatal("A plugin tried to change a command with signed component(s). " logger.fatal("A plugin tried to change a command with signed component(s). "
+ "This is not supported. " + "This is not supported. "
@ -103,14 +74,36 @@ public class SessionCommandHandler implements CommandHandler<SessionPlayerComman
.builder() .builder()
.setTimestamp(packet.timeStamp) .setTimestamp(packet.timeStamp)
.asPlayer(this.player) .asPlayer(this.player)
.message("/" + commandToRun) .message("/" + newCommand)
.toServer(); .toServer();
} }
@Override
public void handlePlayerCommandInternal(SessionPlayerCommandPacket packet) {
queueCommandResult(this.server, this.player, event -> {
CommandExecuteEvent.CommandResult result = event.getResult();
if (result == CommandExecuteEvent.CommandResult.denied()) {
if (packet.isSigned()) {
logger.fatal("A plugin tried to deny a command with signable component(s). "
+ "This is not supported. "
+ "Disconnecting player " + player.getUsername() + ". Command packet: " + packet);
player.disconnect(Component.text(
"A proxy plugin caused an illegal protocol state. "
+ "Contact your network administrator."));
} }
if (packet.lastSeenMessages != null) { return CompletableFuture.completedFuture(consumeCommand(packet));
return new ChatAcknowledgementPacket(packet.lastSeenMessages.getOffset());
} }
return null;
String commandToRun = result.getCommand().orElse(packet.command);
if (result.isForwardToServer()) {
return CompletableFuture.completedFuture(forwardCommand(packet, commandToRun));
}
return runCommand(this.server, this.player, commandToRun, hasRun -> {
if (hasRun) {
return consumeCommand(packet);
}
return forwardCommand(packet, commandToRun);
}); });
}, packet.command, packet.timeStamp); }, packet.command, packet.timeStamp);
} }