3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-12-26 00:00:55 +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.proxy.VelocityServer;
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.CommandHandler;
import java.util.concurrent.CompletableFuture;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
public class SessionCommandHandler implements CommandHandler<SessionPlayerCommandPacket> {
@ -40,6 +42,42 @@ public class SessionCommandHandler implements CommandHandler<SessionPlayerComman
return SessionPlayerCommandPacket.class;
}
@Nullable
private MinecraftPacket consumeCommand(SessionPlayerCommandPacket packet) {
if (packet.lastSeenMessages != null) {
return new ChatAcknowledgementPacket(packet.lastSeenMessages.getOffset());
}
return null;
}
@Nullable
private MinecraftPacket forwardCommand(SessionPlayerCommandPacket packet, String newCommand) {
if (packet.isSigned() && newCommand.equals(packet.command)) {
return packet;
}
return modifyCommand(packet, newCommand);
}
@Nullable
private MinecraftPacket modifyCommand(SessionPlayerCommandPacket packet, String newCommand) {
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 null;
}
return this.player.getChatBuilderFactory()
.builder()
.setTimestamp(packet.timeStamp)
.asPlayer(this.player)
.message("/" + newCommand)
.toServer();
}
@Override
public void handlePlayerCommandInternal(SessionPlayerCommandPacket packet) {
queueCommandResult(this.server, this.player, event -> {
@ -53,64 +91,19 @@ public class SessionCommandHandler implements CommandHandler<SessionPlayerComman
"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) {
return CompletableFuture.completedFuture(new ChatAcknowledgementPacket(packet.lastSeenMessages.getOffset()));
}
return CompletableFuture.completedFuture(null);
return CompletableFuture.completedFuture(consumeCommand(packet));
}
String commandToRun = result.getCommand().orElse(packet.command);
if (result.isForwardToServer()) {
if (packet.isSigned() && commandToRun.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 CompletableFuture.completedFuture(forwardCommand(packet, commandToRun));
}
return runCommand(this.server, this.player, commandToRun, hasRun -> {
if (!hasRun) {
if (packet.isSigned() && commandToRun.equals(packet.command)) {
return 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 null;
}
return this.player.getChatBuilderFactory()
.builder()
.setTimestamp(packet.timeStamp)
.asPlayer(this.player)
.message("/" + commandToRun)
.toServer();
}
if (hasRun) {
return consumeCommand(packet);
}
if (packet.lastSeenMessages != null) {
return new ChatAcknowledgementPacket(packet.lastSeenMessages.getOffset());
}
return null;
return forwardCommand(packet, commandToRun);
});
}, packet.command, packet.timeStamp);
}