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 161abf63b..8a6d307aa 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 @@ -13,6 +13,7 @@ import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.packet.*; 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; @@ -97,18 +98,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { return; } } else { - 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()); + EventUtil.callPlayerChatEvent(server, player, msg, chat); } return; } 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 cd4c55572..e2f5a1e29 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,6 +29,7 @@ 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; @@ -420,7 +421,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"); - connectedServer.getMinecraftConnection().write(Chat.createServerbound(input)); + EventUtil.callPlayerChatEvent(server, this, input, 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 new file mode 100644 index 000000000..d16008824 --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/util/EventUtil.java @@ -0,0 +1,25 @@ +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())){ + player.getConnectedServer().getMinecraftConnection().write(original); + } else if (pme.getResult().isAllowed() && pme.getResult().getMessage().isPresent()){ + player.getConnectedServer().getMinecraftConnection().write(Chat.createServerbound(pme.getResult().getMessage().get())); + } + }, player.getConnectedServer().getMinecraftConnection().getChannel().eventLoop()); + } + +}