forked from ChomeNS/chomens-bot-java
fixes (and mabe improvements)
This commit is contained in:
parent
0a7b727c50
commit
a1e02ce21d
3 changed files with 72 additions and 38 deletions
|
@ -31,7 +31,8 @@ import java.util.regex.Pattern;
|
|||
|
||||
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 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;
|
||||
|
||||
|
@ -224,7 +225,7 @@ public class ChatPlugin extends Bot.Listener {
|
|||
}
|
||||
|
||||
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;
|
||||
|
||||
|
@ -287,21 +288,34 @@ public class ChatPlugin extends Bot.Listener {
|
|||
while (splitMatcher.find()) {
|
||||
final String eachMessage = splitMatcher.group(1);
|
||||
|
||||
if (
|
||||
eachMessage.trim().isEmpty() ||
|
||||
IllegalCharactersUtilities.containsIllegalCharacters(eachMessage)
|
||||
) continue;
|
||||
final Matcher eachMessageMatcher = CHAT_SPLIT_PATTERN.matcher(eachMessage);
|
||||
|
||||
if (!isFirst) {
|
||||
final Matcher colorCodeMatcher = COLOR_CODE_PATTERN.matcher(message);
|
||||
while (colorCodeMatcher.find()) lastColor = colorCodeMatcher.group();
|
||||
while (eachMessageMatcher.find()) {
|
||||
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) {
|
||||
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();
|
||||
}
|
||||
|
||||
queue.add(
|
||||
lastColor + strippedMessage // the regex has 254 (comes from 256 - 2 (color code length)) so we can do this here
|
||||
);
|
||||
|
||||
isFirst = false;
|
||||
}
|
||||
|
||||
queue.add(
|
||||
lastColor + eachMessage // the regex has 254 (comes from 256 - 2 (color code length)) so we can do this here
|
||||
);
|
||||
|
||||
isFirst = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -166,25 +166,37 @@ public class ComponentUtilities {
|
|||
return new PartiallyStringified("", null);
|
||||
}
|
||||
|
||||
public static String getStyle (Style style) {
|
||||
if (style == null) return null;
|
||||
public static String getStyle (Style textStyle, boolean motd) {
|
||||
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.State state = decorationEntry.getValue();
|
||||
|
||||
if (state == TextDecoration.State.NOT_SET || state == TextDecoration.State.FALSE) continue;
|
||||
|
||||
if (decoration == TextDecoration.BOLD) ansiStyle.append(ansiMap.get("l"));
|
||||
else if (decoration == TextDecoration.ITALIC) ansiStyle.append(ansiMap.get("o"));
|
||||
else if (decoration == TextDecoration.OBFUSCATED) ansiStyle.append(ansiMap.get("k"));
|
||||
else if (decoration == TextDecoration.UNDERLINED) ansiStyle.append(ansiMap.get("n"));
|
||||
else if (decoration == TextDecoration.STRIKETHROUGH) ansiStyle.append(ansiMap.get("m"));
|
||||
if (!motd) {
|
||||
switch (decoration) {
|
||||
case BOLD -> style.append(ansiMap.get("l"));
|
||||
case ITALIC -> style.append(ansiMap.get("o"));
|
||||
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) {
|
||||
|
@ -237,7 +249,7 @@ public class ComponentUtilities {
|
|||
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) {
|
||||
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();
|
||||
// seems very mabe mabe
|
||||
|
@ -252,7 +264,7 @@ public class ComponentUtilities {
|
|||
}
|
||||
|
||||
// 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);
|
||||
|
@ -265,7 +277,7 @@ public class ComponentUtilities {
|
|||
Matcher matcher = ARG_PATTERN.matcher(format);
|
||||
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);
|
||||
String color;
|
||||
if (_color == null) color = "";
|
||||
|
@ -298,16 +310,16 @@ public class ComponentUtilities {
|
|||
}
|
||||
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) {
|
||||
final String style = getStyle(message.style());
|
||||
final String style = getStyle(message.style(), motd);
|
||||
final String _color = getColor(message.color(), motd, ansi);
|
||||
String color;
|
||||
if (_color == null) 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) {
|
||||
|
|
|
@ -1,14 +1,22 @@
|
|||
package land.chipmunk.chayapak.chomens_bot.util;
|
||||
|
||||
// Original code made by _ChipMC_ IIRC and I ported it to Java
|
||||
public class IllegalCharactersUtilities {
|
||||
public static boolean isAllowedCharacter (char character) {
|
||||
return character != '§' && character != '\u007f';
|
||||
}
|
||||
public static String stripIllegalCharacters(String string) {
|
||||
final StringBuilder replaced = new StringBuilder();
|
||||
|
||||
public static boolean containsIllegalCharacters (String string) {
|
||||
for (int i = 0; i < string.length(); i++) if (!isAllowedCharacter(string.charAt(i))) return true;
|
||||
for (char character : string.toCharArray()) {
|
||||
if (
|
||||
character == '§' ||
|
||||
character == '\u007f' ||
|
||||
|
||||
return false;
|
||||
// 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);
|
||||
}
|
||||
|
||||
return replaced.toString();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue