fixes (and mabe improvements)

This commit is contained in:
Chayapak 2023-07-31 17:45:27 +07:00
parent 0a7b727c50
commit a1e02ce21d
3 changed files with 72 additions and 38 deletions

View file

@ -31,7 +31,8 @@ import java.util.regex.Pattern;
public class ChatPlugin extends Bot.Listener { public class ChatPlugin extends Bot.Listener {
public static final Pattern CHAT_SPLIT_PATTERN = Pattern.compile("\\G\\s*([^\\r\\n]{1,254}(?=\\s|$)|[^\\r\\n]{254})"); // thanks HBot for the regex <3 public static final Pattern CHAT_SPLIT_PATTERN = Pattern.compile("\\G\\s*([^\\r\\n]{1,254}(?=\\s|$)|[^\\r\\n]{254})"); // thanks HBot for the regex <3
public static final Pattern COLOR_CODE_PATTERN = Pattern.compile("(&[a-f0-9rlonmk])"); public static final Pattern COLOR_CODE_PATTERN = Pattern.compile("(&[a-f0-9rlonmk])", Pattern.MULTILINE);
public static final Pattern COLOR_CODE_END_PATTERN = Pattern.compile("^.*&[a-f0-9rlonmk]$", Pattern.MULTILINE);
private final Bot bot; private final Bot bot;
@ -224,7 +225,7 @@ public class ChatPlugin extends Bot.Listener {
} }
private void sendChatTick () { private void sendChatTick () {
if (queue.size() > 50) queue.clear(); // detects spam, like spamming *echo for example if (queue.size() > 100) queue.clear(); // detects spam, like spamming *echo for example
if (queue.isEmpty()) return; if (queue.isEmpty()) return;
@ -287,23 +288,36 @@ public class ChatPlugin extends Bot.Listener {
while (splitMatcher.find()) { while (splitMatcher.find()) {
final String eachMessage = splitMatcher.group(1); final String eachMessage = splitMatcher.group(1);
if ( final Matcher eachMessageMatcher = CHAT_SPLIT_PATTERN.matcher(eachMessage);
eachMessage.trim().isEmpty() ||
IllegalCharactersUtilities.containsIllegalCharacters(eachMessage) while (eachMessageMatcher.find()) {
) continue; String strippedMessage = IllegalCharactersUtilities.stripIllegalCharacters(eachMessageMatcher.group(1));
if (strippedMessage.trim().isEmpty()) continue;
final Matcher colorCodeEndMatcher = COLOR_CODE_END_PATTERN.matcher(strippedMessage);
if (colorCodeEndMatcher.find()) strippedMessage = strippedMessage.substring(0, strippedMessage.length() - 2);
if (!isFirst) { if (!isFirst) {
final Matcher colorCodeMatcher = COLOR_CODE_PATTERN.matcher(message); final Matcher colorCodeEndMatcher2 = COLOR_CODE_END_PATTERN.matcher(message);
Matcher colorCodeMatcher;
if (!colorCodeEndMatcher2.find()) colorCodeMatcher = COLOR_CODE_PATTERN.matcher(message);
else colorCodeMatcher = COLOR_CODE_PATTERN.matcher(message.substring(0, message.length() - 2));
while (colorCodeMatcher.find()) lastColor = colorCodeMatcher.group(); while (colorCodeMatcher.find()) lastColor = colorCodeMatcher.group();
} }
queue.add( queue.add(
lastColor + eachMessage // the regex has 254 (comes from 256 - 2 (color code length)) so we can do this here lastColor + strippedMessage // the regex has 254 (comes from 256 - 2 (color code length)) so we can do this here
); );
isFirst = false; isFirst = false;
} }
} }
}
public void tellraw (Component component, String targets) { public void tellraw (Component component, String targets) {
if (bot.options.useChat) { if (bot.options.useChat) {

View file

@ -166,25 +166,37 @@ public class ComponentUtilities {
return new PartiallyStringified("", null); return new PartiallyStringified("", null);
} }
public static String getStyle (Style style) { public static String getStyle (Style textStyle, boolean motd) {
if (style == null) return null; if (textStyle == null) return null;
StringBuilder ansiStyle = new StringBuilder(); StringBuilder style = new StringBuilder();
for (Map.Entry<TextDecoration, TextDecoration.State> decorationEntry : style.decorations().entrySet()) { for (Map.Entry<TextDecoration, TextDecoration.State> decorationEntry : textStyle.decorations().entrySet()) {
final TextDecoration decoration = decorationEntry.getKey(); final TextDecoration decoration = decorationEntry.getKey();
final TextDecoration.State state = decorationEntry.getValue(); final TextDecoration.State state = decorationEntry.getValue();
if (state == TextDecoration.State.NOT_SET || state == TextDecoration.State.FALSE) continue; if (state == TextDecoration.State.NOT_SET || state == TextDecoration.State.FALSE) continue;
if (decoration == TextDecoration.BOLD) ansiStyle.append(ansiMap.get("l")); if (!motd) {
else if (decoration == TextDecoration.ITALIC) ansiStyle.append(ansiMap.get("o")); switch (decoration) {
else if (decoration == TextDecoration.OBFUSCATED) ansiStyle.append(ansiMap.get("k")); case BOLD -> style.append(ansiMap.get("l"));
else if (decoration == TextDecoration.UNDERLINED) ansiStyle.append(ansiMap.get("n")); case ITALIC -> style.append(ansiMap.get("o"));
else if (decoration == TextDecoration.STRIKETHROUGH) ansiStyle.append(ansiMap.get("m")); case OBFUSCATED -> style.append(ansiMap.get("k"));
case UNDERLINED -> style.append(ansiMap.get("n"));
case STRIKETHROUGH -> style.append(ansiMap.get("m"));
}
} else {
switch (decoration) {
case BOLD -> style.append("§l");
case ITALIC -> style.append("§o");
case OBFUSCATED -> style.append("§k");
case UNDERLINED -> style.append("§n");
case STRIKETHROUGH -> style.append("§m");
}
}
} }
return ansiStyle.toString(); return style.toString();
} }
public static String getColor (TextColor color, boolean motd, boolean ansi) { public static String getColor (TextColor color, boolean motd, boolean ansi) {
@ -237,7 +249,7 @@ public class ComponentUtilities {
public static PartiallyStringified stringifyPartially (TextComponent message, boolean motd, boolean ansi, String lastColor) { public static PartiallyStringified stringifyPartially (TextComponent message, boolean motd, boolean ansi, String lastColor) {
if ((motd || ansi) && /* don't color big messages -> */ message.content().length() < 25_000) { if ((motd || ansi) && /* don't color big messages -> */ message.content().length() < 25_000) {
final String color = getColor(message.color(), motd, ansi); final String color = getColor(message.color(), motd, ansi);
final String style = getStyle(message.style()); final String style = getStyle(message.style(), motd);
String replacedContent = message.content(); String replacedContent = message.content();
// seems very mabe mabe // seems very mabe mabe
@ -252,7 +264,7 @@ public class ComponentUtilities {
} }
// messy af // messy af
return new PartiallyStringified((lastColor != null ? lastColor : "") + (style != null ? style : "") + (color != null ? color : "") + replacedContent + (ansi ? ansiMap.get("r") : ""), color); return new PartiallyStringified((lastColor != null ? lastColor : "") + (color != null ? color : "") + (style != null ? style : "") + replacedContent + (ansi ? ansiMap.get("r") : ""), color);
} }
return new PartiallyStringified(message.content(), null); return new PartiallyStringified(message.content(), null);
@ -265,7 +277,7 @@ public class ComponentUtilities {
Matcher matcher = ARG_PATTERN.matcher(format); Matcher matcher = ARG_PATTERN.matcher(format);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
final String style = getStyle(message.style()); final String style = getStyle(message.style(), motd);
final String _color = getColor(message.color(), motd, ansi); final String _color = getColor(message.color(), motd, ansi);
String color; String color;
if (_color == null) color = ""; if (_color == null) color = "";
@ -298,16 +310,16 @@ public class ComponentUtilities {
} }
matcher.appendTail(sb); matcher.appendTail(sb);
return new PartiallyStringified((lastColor != null ? lastColor : "") + (style != null && ansi ? style : "") + color + sb + (ansi ? ansiMap.get("r") : ""), _color); return new PartiallyStringified((lastColor != null ? lastColor : "") + color + (style != null && ansi ? style : "") + sb + (ansi ? ansiMap.get("r") : ""), _color);
} }
public static PartiallyStringified stringifyPartially (SelectorComponent message, boolean motd, boolean ansi, String lastColor) { public static PartiallyStringified stringifyPartially (SelectorComponent message, boolean motd, boolean ansi, String lastColor) {
final String style = getStyle(message.style()); final String style = getStyle(message.style(), motd);
final String _color = getColor(message.color(), motd, ansi); final String _color = getColor(message.color(), motd, ansi);
String color; String color;
if (_color == null) color = ""; if (_color == null) color = "";
else color = _color; else color = _color;
return new PartiallyStringified((lastColor != null ? lastColor : "") + (style != null && ansi ? style : "") + color + message.pattern(), _color); // * Client-side selector components are equivalent to text ones, and do NOT list entities. return new PartiallyStringified((lastColor != null ? lastColor : "") + color + (style != null && ansi ? style : "") + message.pattern(), _color); // * Client-side selector components are equivalent to text ones, and do NOT list entities.
} }
public static PartiallyStringified stringifyPartially (KeybindComponent message, boolean motd, boolean ansi, String lastColor) { public static PartiallyStringified stringifyPartially (KeybindComponent message, boolean motd, boolean ansi, String lastColor) {

View file

@ -1,14 +1,22 @@
package land.chipmunk.chayapak.chomens_bot.util; package land.chipmunk.chayapak.chomens_bot.util;
// Original code made by _ChipMC_ IIRC and I ported it to Java
public class IllegalCharactersUtilities { public class IllegalCharactersUtilities {
public static boolean isAllowedCharacter (char character) { public static String stripIllegalCharacters(String string) {
return character != '§' && character != '\u007f'; final StringBuilder replaced = new StringBuilder();
for (char character : string.toCharArray()) {
if (
character == '§' ||
character == '\u007f' ||
// check if character is a control code, also space is the first character after
// the control characters so this is why we can do `character < ' '`
character < ' '
) continue;
replaced.append(character);
} }
public static boolean containsIllegalCharacters (String string) { return replaced.toString();
for (int i = 0; i < string.length(); i++) if (!isAllowedCharacter(string.charAt(i))) return true;
return false;
} }
} }