From 7598918d9353e48a19e4e455b88f960455f42776 Mon Sep 17 00:00:00 2001 From: Thomas Vanmellaerts Date: Wed, 26 Sep 2018 13:00:43 +0200 Subject: [PATCH] Add chat event --- .../api/event/ResultedEvent.java | 41 +++++++++++++++ .../api/event/player/PlayerChatEvent.java | 50 +++++++++++++++++++ .../client/ClientPlaySessionHandler.java | 17 ++++++- 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEvent.java 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 227bbfe27..6d584be9d 100644 --- a/api/src/main/java/com/velocitypowered/api/event/ResultedEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/ResultedEvent.java @@ -111,4 +111,45 @@ 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 new file mode 100644 index 000000000..47c630936 --- /dev/null +++ b/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEvent.java @@ -0,0 +1,50 @@ +package com.velocitypowered.api.event.player; + +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; + +/** + * 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 { + private final Player player; + private final String message; + private ChatResult result; + + public PlayerChatEvent(Player player, String message) { + this.player = Preconditions.checkNotNull(player, "player"); + this.message = Preconditions.checkNotNull(message, "message"); + this.result = (ChatResult) ChatResult.allowed(); + } + + public Player getPlayer() { + return player; + } + + public String getMessage() { + return message; + } + + @Override + public ChatResult getResult() { + return result; + } + + @Override + public void setResult(@NonNull ChatResult result) { + this.result = Preconditions.checkNotNull(result, "result"); + } + + @Override + public String toString() { + return "PlayerChatEvent{" + + "player=" + player + + ", message=" + message + + ", result=" + result + + '}'; + } + + +} 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 aa27bc1cf..161abf63b 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,7 +1,9 @@ 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; @@ -13,8 +15,10 @@ import com.velocitypowered.proxy.protocol.packet.*; import com.velocitypowered.proxy.protocol.util.PluginMessageUtil; 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; @@ -93,7 +97,18 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { return; } } else { - player.getConnectedServer().getMinecraftConnection().write(chat); + PlayerChatEvent event = new PlayerChatEvent(player, msg); + server.getEventManager().fire(event) + .thenAcceptAsync(pme -> { + if (pme.getResult().equals(ResultedEvent.ChatResult.allowed())){ + player.getConnectedServer().getMinecraftConnection().write(chat); + } else if (pme.getResult().isAllowed() && pme.getResult().getMessage().isPresent()){ + Chat modifiedChat = new Chat(); + modifiedChat.setType(Chat.CHAT); + modifiedChat.setMessage(pme.getResult().getMessage().get()); + player.getConnectedServer().getMinecraftConnection().write(modifiedChat); + } + }, player.getConnectedServer().getMinecraftConnection().getChannel().eventLoop()); } return; }