From 79a7258fd1df227d3cba040f0568bc6fb2b88883 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Mon, 13 Apr 2020 11:10:53 +0100 Subject: [PATCH 1/3] Added warning message if chat message is longer than 256 characters. --- .../BedrockCommandRequestTranslator.java | 9 ++++++++- .../bedrock/BedrockTextTranslator.java | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 3 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 28cbf4c41..47895666c 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 @@ -45,7 +45,14 @@ public class BedrockCommandRequestTranslator extends PacketTranslator 256) { + session.sendMessage("Your message is bigger than 256 characters (" + message.length() + ") so it has not been sent."); + 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 1c8416310..89766be3b 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 @@ -38,12 +38,26 @@ public class BedrockTextTranslator extends PacketTranslator { @Override public void translate(TextPacket packet, GeyserSession session) { if (packet.getMessage().charAt(0) == '.') { - ClientChatPacket chatPacket = new ClientChatPacket(packet.getMessage().replace(".", "/")); + 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; + } + + ClientChatPacket chatPacket = new ClientChatPacket(message); session.getDownstream().getSession().send(chatPacket); return; } - ClientChatPacket chatPacket = new ClientChatPacket(packet.getMessage()); + 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; + } + + ClientChatPacket chatPacket = new ClientChatPacket(message); session.getDownstream().getSession().send(chatPacket); } } From 55608a2a48dc35166b423e7f0094b7e9ea22d77b Mon Sep 17 00:00:00 2001 From: rtm516 Date: Mon, 13 Apr 2020 17:49:29 +0100 Subject: [PATCH 2/3] 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 47895666c..c8d8c3233 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 89766be3b..b8800faab 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 a28d6a7a1..5b5ce7dff 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; + } } From 34a8a6d55fc9fa2298a9d5fe3a0d917ee4043ba1 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Wed, 15 Apr 2020 10:51:56 +0100 Subject: [PATCH 3/3] Fixed single line if statements --- .../bedrock/BedrockCommandRequestTranslator.java | 4 +++- .../translators/bedrock/BedrockTextTranslator.java | 8 ++++++-- 2 files changed, 9 insertions(+), 3 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 c8d8c3233..e7d4c7486 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 @@ -48,7 +48,9 @@ public class BedrockCommandRequestTranslator extends PacketTranslator { if (packet.getMessage().charAt(0) == '.') { String message = packet.getMessage().replace(".", "/").trim(); - if (MessageUtils.isTooLong(message, session)) { return; } + if (MessageUtils.isTooLong(message, session)) { + return; + } ClientChatPacket chatPacket = new ClientChatPacket(message); session.getDownstream().getSession().send(chatPacket); @@ -50,7 +52,9 @@ public class BedrockTextTranslator extends PacketTranslator { String message = packet.getMessage().trim(); - if (MessageUtils.isTooLong(message, session)) { return; } + if (MessageUtils.isTooLong(message, session)) { + return; + } ClientChatPacket chatPacket = new ClientChatPacket(message); session.getDownstream().getSession().send(chatPacket);