Mirror von
https://github.com/PaperMC/Velocity.git
synchronisiert 2024-12-26 16:12:50 +01:00
Extract common command handler paths
Dieser Commit ist enthalten in:
Ursprung
be73131d5d
Commit
92c48507eb
@ -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,6 +42,42 @@ public class SessionCommandHandler implements CommandHandler<SessionPlayerComman
|
|||||||
return SessionPlayerCommandPacket.class;
|
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
|
@Override
|
||||||
public void handlePlayerCommandInternal(SessionPlayerCommandPacket packet) {
|
public void handlePlayerCommandInternal(SessionPlayerCommandPacket packet) {
|
||||||
queueCommandResult(this.server, this.player, event -> {
|
queueCommandResult(this.server, this.player, event -> {
|
||||||
@ -53,64 +91,19 @@ public class SessionCommandHandler implements CommandHandler<SessionPlayerComman
|
|||||||
"A proxy plugin caused an illegal protocol state. "
|
"A proxy plugin caused an illegal protocol state. "
|
||||||
+ "Contact your network administrator."));
|
+ "Contact your network administrator."));
|
||||||
}
|
}
|
||||||
// We seemingly can't actually do this if signed args exist, if not, we can probs keep stuff happy
|
return CompletableFuture.completedFuture(consumeCommand(packet));
|
||||||
if (packet.lastSeenMessages != null) {
|
|
||||||
return CompletableFuture.completedFuture(new ChatAcknowledgementPacket(packet.lastSeenMessages.getOffset()));
|
|
||||||
}
|
|
||||||
return CompletableFuture.completedFuture(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String commandToRun = result.getCommand().orElse(packet.command);
|
String commandToRun = result.getCommand().orElse(packet.command);
|
||||||
if (result.isForwardToServer()) {
|
if (result.isForwardToServer()) {
|
||||||
if (packet.isSigned() && commandToRun.equals(packet.command)) {
|
return CompletableFuture.completedFuture(forwardCommand(packet, commandToRun));
|
||||||
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 -> {
|
return runCommand(this.server, this.player, commandToRun, hasRun -> {
|
||||||
if (!hasRun) {
|
if (hasRun) {
|
||||||
if (packet.isSigned() && commandToRun.equals(packet.command)) {
|
return consumeCommand(packet);
|
||||||
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 (packet.lastSeenMessages != null) {
|
return forwardCommand(packet, commandToRun);
|
||||||
return new ChatAcknowledgementPacket(packet.lastSeenMessages.getOffset());
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
});
|
});
|
||||||
}, packet.command, packet.timeStamp);
|
}, packet.command, packet.timeStamp);
|
||||||
}
|
}
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren