Mirror von
https://github.com/PaperMC/Velocity.git
synchronisiert 2024-12-24 15:20:35 +01:00
Move ChatResult to PlayerChatEvent and don't call event on spoofChatInput()
Dieser Commit ist enthalten in:
Ursprung
d71f863045
Commit
3acc00de5e
@ -111,45 +111,4 @@ public interface ResultedEvent<R extends ResultedEvent.Result> {
|
||||
return new ComponentResult(false, reason);
|
||||
}
|
||||
}
|
||||
|
||||
class ChatResult implements Result {
|
||||
private static final ChatResult ALLOWED = new ChatResult(true, null);
|
||||
private static final ChatResult DENIED = new ChatResult(false, null);
|
||||
|
||||
// The server can not accept formatted text from clients!
|
||||
private @Nullable String message;
|
||||
private final boolean allowed;
|
||||
|
||||
protected ChatResult(boolean allowed, @Nullable String message) {
|
||||
this.allowed = allowed;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAllowed() {
|
||||
return allowed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return allowed ? "allowed" : "denied";
|
||||
}
|
||||
|
||||
public static ChatResult allowed() {
|
||||
return ALLOWED;
|
||||
}
|
||||
|
||||
public static ChatResult denied() {
|
||||
return DENIED;
|
||||
}
|
||||
|
||||
public Optional<String> getMessage() {
|
||||
return Optional.ofNullable(message);
|
||||
}
|
||||
|
||||
public static ChatResult message(@NonNull String message) {
|
||||
Preconditions.checkNotNull(message, "message");
|
||||
return new ChatResult(true, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,11 +4,14 @@ import com.google.common.base.Preconditions;
|
||||
import com.velocitypowered.api.event.ResultedEvent;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* This event is fired once the player has been authenticated but before they connect to a server on the proxy.
|
||||
*/
|
||||
public class PlayerChatEvent implements ResultedEvent<ResultedEvent.ChatResult> {
|
||||
public class PlayerChatEvent implements ResultedEvent<PlayerChatEvent.ChatResult> {
|
||||
private final Player player;
|
||||
private final String message;
|
||||
private ChatResult result;
|
||||
@ -16,7 +19,7 @@ public class PlayerChatEvent implements ResultedEvent<ResultedEvent.ChatResult>
|
||||
public PlayerChatEvent(Player player, String message) {
|
||||
this.player = Preconditions.checkNotNull(player, "player");
|
||||
this.message = Preconditions.checkNotNull(message, "message");
|
||||
this.result = (ChatResult) ChatResult.allowed();
|
||||
this.result = ChatResult.allowed();
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
@ -46,5 +49,49 @@ public class PlayerChatEvent implements ResultedEvent<ResultedEvent.ChatResult>
|
||||
'}';
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the result of the {@link PlayerChatEvent}.
|
||||
*/
|
||||
public static class ChatResult implements ResultedEvent.Result {
|
||||
private static final ChatResult ALLOWED = new ChatResult(true, null);
|
||||
private static final ChatResult DENIED = new ChatResult(false, null);
|
||||
|
||||
// The server can not accept formatted text from clients!
|
||||
private @Nullable String message;
|
||||
private final boolean allowed;
|
||||
|
||||
protected ChatResult(boolean allowed, @Nullable String message) {
|
||||
this.allowed = allowed;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAllowed() {
|
||||
return allowed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return allowed ? "allowed" : "denied";
|
||||
}
|
||||
|
||||
public static ChatResult allowed() {
|
||||
return ALLOWED;
|
||||
}
|
||||
|
||||
public static ChatResult denied() {
|
||||
return DENIED;
|
||||
}
|
||||
|
||||
public Optional<String> getMessage() {
|
||||
return Optional.ofNullable(message);
|
||||
}
|
||||
|
||||
public static ChatResult message(@NonNull String message) {
|
||||
Preconditions.checkNotNull(message, "message");
|
||||
return new ChatResult(true, message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
package com.velocitypowered.proxy.connection.client;
|
||||
|
||||
import com.velocitypowered.api.event.ResultedEvent;
|
||||
import com.velocitypowered.api.event.connection.DisconnectEvent;
|
||||
import com.velocitypowered.api.event.connection.PluginMessageEvent;
|
||||
import com.velocitypowered.api.event.player.PlayerChatEvent;
|
||||
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
|
||||
import com.velocitypowered.proxy.VelocityServer;
|
||||
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
|
||||
@ -16,10 +14,8 @@ import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
|
||||
import com.velocitypowered.proxy.util.EventUtil;
|
||||
import com.velocitypowered.proxy.util.ThrowableUtils;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.kyori.text.Component;
|
||||
import net.kyori.text.TextComponent;
|
||||
import net.kyori.text.format.TextColor;
|
||||
import net.kyori.text.serializer.ComponentSerializers;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
|
@ -29,7 +29,6 @@ import com.velocitypowered.proxy.connection.util.ConnectionRequestResults;
|
||||
import com.velocitypowered.proxy.protocol.ProtocolConstants;
|
||||
import com.velocitypowered.proxy.protocol.packet.*;
|
||||
import com.velocitypowered.proxy.server.VelocityRegisteredServer;
|
||||
import com.velocitypowered.proxy.util.EventUtil;
|
||||
import com.velocitypowered.proxy.util.ThrowableUtils;
|
||||
import net.kyori.text.Component;
|
||||
import net.kyori.text.TextComponent;
|
||||
@ -421,7 +420,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
|
||||
@Override
|
||||
public void spoofChatInput(String input) {
|
||||
Preconditions.checkArgument(input.length() <= Chat.MAX_SERVERBOUND_MESSAGE_LENGTH, "input cannot be greater than " + Chat.MAX_SERVERBOUND_MESSAGE_LENGTH + " characters in length");
|
||||
EventUtil.callPlayerChatEvent(server, this, input, Chat.createServerbound(input));
|
||||
connectedServer.getMinecraftConnection().write(Chat.createServerbound(input));
|
||||
}
|
||||
|
||||
private class ConnectionRequestBuilderImpl implements ConnectionRequestBuilder {
|
||||
|
@ -1,20 +1,17 @@
|
||||
package com.velocitypowered.proxy.util;
|
||||
|
||||
import com.velocitypowered.api.event.ResultedEvent;
|
||||
import com.velocitypowered.api.event.player.PlayerChatEvent;
|
||||
import com.velocitypowered.proxy.VelocityServer;
|
||||
import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
|
||||
import com.velocitypowered.proxy.protocol.packet.Chat;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class EventUtil {
|
||||
|
||||
public static void callPlayerChatEvent(VelocityServer server, ConnectedPlayer player, String message, Chat original){
|
||||
PlayerChatEvent event = new PlayerChatEvent(player, message);
|
||||
server.getEventManager().fire(event)
|
||||
.thenAcceptAsync(pme -> {
|
||||
if (pme.getResult().equals(ResultedEvent.ChatResult.allowed())){
|
||||
if (pme.getResult().equals(PlayerChatEvent.ChatResult.allowed())){
|
||||
player.getConnectedServer().getMinecraftConnection().write(original);
|
||||
} else if (pme.getResult().isAllowed() && pme.getResult().getMessage().isPresent()){
|
||||
player.getConnectedServer().getMinecraftConnection().write(Chat.createServerbound(pme.getResult().getMessage().get()));
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren