Merge pull request #324 from rtm516/chat-limit-warning

Added warning message if chat message is longer than 256 characters.
This commit is contained in:
Redned 2020-04-15 13:10:15 -05:00 committed by GitHub
commit f8c3cf1aac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 3 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.nukkitx.protocol.bedrock.packet.CommandRequestPacket;
import org.geysermc.connector.utils.MessageUtils;
@Translator(packet = CommandRequestPacket.class)
public class BedrockCommandRequestTranslator extends PacketTranslator<CommandRequestPacket> {
@ -45,7 +46,13 @@ public class BedrockCommandRequestTranslator extends PacketTranslator<CommandReq
if (session.getConnector().getPlatformType() == PlatformType.STANDALONE && command.startsWith("geyser ") && commandMap.getCommands().containsKey(command.split(" ")[1])) {
commandMap.runCommand(session, command);
} else {
ClientChatPacket chatPacket = new ClientChatPacket(packet.getCommand());
String message = packet.getCommand().trim();
if (MessageUtils.isTooLong(message, session)) {
return;
}
ClientChatPacket chatPacket = new ClientChatPacket(message);
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.nukkitx.protocol.bedrock.packet.TextPacket;
import org.geysermc.connector.utils.MessageUtils;
@Translator(packet = TextPacket.class)
public class BedrockTextTranslator extends PacketTranslator<TextPacket> {
@ -38,12 +39,24 @@ public class BedrockTextTranslator extends PacketTranslator<TextPacket> {
@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 (MessageUtils.isTooLong(message, session)) {
return;
}
ClientChatPacket chatPacket = new ClientChatPacket(message);
session.getDownstream().getSession().send(chatPacket);
return;
}
ClientChatPacket chatPacket = new ClientChatPacket(packet.getMessage());
String message = packet.getMessage().trim();
if (MessageUtils.isTooLong(message, session)) {
return;
}
ClientChatPacket chatPacket = new ClientChatPacket(message);
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.JsonParser;
import com.google.gson.JsonPrimitive;
import org.geysermc.connector.network.session.GeyserSession;
import java.util.*;
import java.util.regex.Matcher;
@ -294,4 +295,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;
}
}