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.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,55 +42,24 @@ public class SessionCommandHandler implements CommandHandler<SessionPlayerComman
|
||||
return SessionPlayerCommandPacket.class;
|
||||
}
|
||||
|
||||
@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."));
|
||||
}
|
||||
// We seemingly can't actually do this if signed args exist, if not, we can probs keep stuff happy
|
||||
@Nullable
|
||||
private MinecraftPacket consumeCommand(SessionPlayerCommandPacket packet) {
|
||||
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);
|
||||
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 runCommand(this.server, this.player, commandToRun, hasRun -> {
|
||||
if (!hasRun) {
|
||||
if (packet.isSigned() && commandToRun.equals(packet.command)) {
|
||||
@Nullable
|
||||
private MinecraftPacket forwardCommand(SessionPlayerCommandPacket packet, String newCommand) {
|
||||
if (packet.isSigned() && newCommand.equals(packet.command)) {
|
||||
return packet;
|
||||
} else {
|
||||
}
|
||||
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. "
|
||||
@ -103,14 +74,36 @@ public class SessionCommandHandler implements CommandHandler<SessionPlayerComman
|
||||
.builder()
|
||||
.setTimestamp(packet.timeStamp)
|
||||
.asPlayer(this.player)
|
||||
.message("/" + commandToRun)
|
||||
.message("/" + newCommand)
|
||||
.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 new ChatAcknowledgementPacket(packet.lastSeenMessages.getOffset());
|
||||
return CompletableFuture.completedFuture(consumeCommand(packet));
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren