From 55608a2a48dc35166b423e7f0094b7e9ea22d77b Mon Sep 17 00:00:00 2001 From: rtm516 Date: Mon, 13 Apr 2020 17:49:29 +0100 Subject: [PATCH] Centeralised message length checking and added TODO for localization --- .../bedrock/BedrockCommandRequestTranslator.java | 6 ++---- .../translators/bedrock/BedrockTextTranslator.java | 11 +++-------- .../org/geysermc/connector/utils/MessageUtils.java | 11 +++++++++++ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockCommandRequestTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockCommandRequestTranslator.java index 47895666..c8d8c323 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockCommandRequestTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockCommandRequestTranslator.java @@ -34,6 +34,7 @@ import org.geysermc.connector.network.translators.Translator; import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket; import com.nukkitx.protocol.bedrock.packet.CommandRequestPacket; +import org.geysermc.connector.utils.MessageUtils; @Translator(packet = CommandRequestPacket.class) public class BedrockCommandRequestTranslator extends PacketTranslator { @@ -47,10 +48,7 @@ public class BedrockCommandRequestTranslator extends PacketTranslator 256) { - session.sendMessage("Your message is bigger than 256 characters (" + message.length() + ") so it has not been sent."); - return; - } + if (MessageUtils.isTooLong(message, session)) { return; } ClientChatPacket chatPacket = new ClientChatPacket(message); session.getDownstream().getSession().send(chatPacket); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockTextTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockTextTranslator.java index 89766be3..b8800faa 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockTextTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockTextTranslator.java @@ -31,6 +31,7 @@ import org.geysermc.connector.network.translators.Translator; import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket; import com.nukkitx.protocol.bedrock.packet.TextPacket; +import org.geysermc.connector.utils.MessageUtils; @Translator(packet = TextPacket.class) public class BedrockTextTranslator extends PacketTranslator { @@ -40,10 +41,7 @@ public class BedrockTextTranslator extends PacketTranslator { if (packet.getMessage().charAt(0) == '.') { String message = packet.getMessage().replace(".", "/").trim(); - if (message.length() > 256) { - session.sendMessage("Your message is bigger than 256 characters (" + message.length() + ") so it has not been sent."); - return; - } + if (MessageUtils.isTooLong(message, session)) { return; } ClientChatPacket chatPacket = new ClientChatPacket(message); session.getDownstream().getSession().send(chatPacket); @@ -52,10 +50,7 @@ public class BedrockTextTranslator extends PacketTranslator { String message = packet.getMessage().trim(); - if (message.length() > 256) { - session.sendMessage("Your message is bigger than 256 characters (" + message.length() + ") so it has not been sent."); - return; - } + if (MessageUtils.isTooLong(message, session)) { return; } ClientChatPacket chatPacket = new ClientChatPacket(message); session.getDownstream().getSession().send(chatPacket); diff --git a/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java b/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java index a28d6a7a..5b5ce7df 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java @@ -32,6 +32,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonPrimitive; +import org.geysermc.connector.network.session.GeyserSession; import java.util.*; import java.util.regex.Matcher; @@ -290,4 +291,14 @@ public class MessageUtils { } return ""; } + + public static boolean isTooLong(String message, GeyserSession session) { + if (message.length() > 256) { + // TODO: Add Geyser localization and translate this based on language + session.sendMessage("Your message is bigger than 256 characters (" + message.length() + ") so it has not been sent."); + return false; + } + + return true; + } }