diff --git a/api/src/main/java/com/velocitypowered/api/event/ResultedEvent.java b/api/src/main/java/com/velocitypowered/api/event/ResultedEvent.java index 6d584be9d..227bbfe27 100644 --- a/api/src/main/java/com/velocitypowered/api/event/ResultedEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/ResultedEvent.java @@ -111,45 +111,4 @@ public interface ResultedEvent { 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 getMessage() { - return Optional.ofNullable(message); - } - - public static ChatResult message(@NonNull String message) { - Preconditions.checkNotNull(message, "message"); - return new ChatResult(true, message); - } - } } diff --git a/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEvent.java b/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEvent.java index 47c630936..6df36fb98 100644 --- a/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEvent.java @@ -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 { +public class PlayerChatEvent implements ResultedEvent { private final Player player; private final String message; private ChatResult result; @@ -16,7 +19,7 @@ public class PlayerChatEvent implements ResultedEvent 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 '}'; } + /** + * 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 getMessage() { + return Optional.ofNullable(message); + } + + public static ChatResult message(@NonNull String message) { + Preconditions.checkNotNull(message, "message"); + return new ChatResult(true, message); + } + } + } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index 8a6d307aa..bd315ff1e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -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; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index e2f5a1e29..cd4c55572 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -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 { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/util/EventUtil.java b/proxy/src/main/java/com/velocitypowered/proxy/util/EventUtil.java index d16008824..9ceaed50b 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/util/EventUtil.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/util/EventUtil.java @@ -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()));