Centeralised message length checking and added TODO for localization

This commit is contained in:
rtm516 2020-04-13 17:49:29 +01:00
parent 79a7258fd1
commit 55608a2a48
3 changed files with 16 additions and 12 deletions

View file

@ -34,6 +34,7 @@ import org.geysermc.connector.network.translators.Translator;
import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket; import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket;
import com.nukkitx.protocol.bedrock.packet.CommandRequestPacket; import com.nukkitx.protocol.bedrock.packet.CommandRequestPacket;
import org.geysermc.connector.utils.MessageUtils;
@Translator(packet = CommandRequestPacket.class) @Translator(packet = CommandRequestPacket.class)
public class BedrockCommandRequestTranslator extends PacketTranslator<CommandRequestPacket> { public class BedrockCommandRequestTranslator extends PacketTranslator<CommandRequestPacket> {
@ -47,10 +48,7 @@ public class BedrockCommandRequestTranslator extends PacketTranslator<CommandReq
} else { } else {
String message = packet.getCommand().trim(); String message = packet.getCommand().trim();
if (message.length() > 256) { if (MessageUtils.isTooLong(message, session)) { return; }
session.sendMessage("Your message is bigger than 256 characters (" + message.length() + ") so it has not been sent.");
return;
}
ClientChatPacket chatPacket = new ClientChatPacket(message); ClientChatPacket chatPacket = new ClientChatPacket(message);
session.getDownstream().getSession().send(chatPacket); session.getDownstream().getSession().send(chatPacket);

View file

@ -31,6 +31,7 @@ import org.geysermc.connector.network.translators.Translator;
import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket; import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket;
import com.nukkitx.protocol.bedrock.packet.TextPacket; import com.nukkitx.protocol.bedrock.packet.TextPacket;
import org.geysermc.connector.utils.MessageUtils;
@Translator(packet = TextPacket.class) @Translator(packet = TextPacket.class)
public class BedrockTextTranslator extends PacketTranslator<TextPacket> { public class BedrockTextTranslator extends PacketTranslator<TextPacket> {
@ -40,10 +41,7 @@ public class BedrockTextTranslator extends PacketTranslator<TextPacket> {
if (packet.getMessage().charAt(0) == '.') { if (packet.getMessage().charAt(0) == '.') {
String message = packet.getMessage().replace(".", "/").trim(); String message = packet.getMessage().replace(".", "/").trim();
if (message.length() > 256) { if (MessageUtils.isTooLong(message, session)) { return; }
session.sendMessage("Your message is bigger than 256 characters (" + message.length() + ") so it has not been sent.");
return;
}
ClientChatPacket chatPacket = new ClientChatPacket(message); ClientChatPacket chatPacket = new ClientChatPacket(message);
session.getDownstream().getSession().send(chatPacket); session.getDownstream().getSession().send(chatPacket);
@ -52,10 +50,7 @@ public class BedrockTextTranslator extends PacketTranslator<TextPacket> {
String message = packet.getMessage().trim(); String message = packet.getMessage().trim();
if (message.length() > 256) { if (MessageUtils.isTooLong(message, session)) { return; }
session.sendMessage("Your message is bigger than 256 characters (" + message.length() + ") so it has not been sent.");
return;
}
ClientChatPacket chatPacket = new ClientChatPacket(message); ClientChatPacket chatPacket = new ClientChatPacket(message);
session.getDownstream().getSession().send(chatPacket); session.getDownstream().getSession().send(chatPacket);

View file

@ -32,6 +32,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import org.geysermc.connector.network.session.GeyserSession;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -290,4 +291,14 @@ public class MessageUtils {
} }
return ""; 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;
}
} }