diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java index 3ea39f2..8b3452f 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatPlugin.java @@ -28,8 +28,13 @@ import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import java.time.Instant; import java.util.*; import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +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])"); + private final Bot bot; private final List chatParsers; @@ -256,15 +261,31 @@ public class ChatPlugin extends Bot.Listener { public void clearQueue () { queue.clear(); } public void send (String message) { - final String[] splitted = message.split("(?<=\\G.{255})|\\n"); + final Matcher splitMatcher = CHAT_SPLIT_PATTERN.matcher(message); + + String lastColor = ""; + + boolean isFirst = true; + + // kinda broken but whatever + while (splitMatcher.find()) { + final String eachMessage = splitMatcher.group(1); - for (String subMessage : splitted) { if ( - subMessage.trim().equals("") || - IllegalCharactersUtilities.containsIllegalCharacters(subMessage) + eachMessage.trim().isEmpty() || + IllegalCharactersUtilities.containsIllegalCharacters(eachMessage) ) continue; - queue.add(subMessage); + if (!isFirst) { + final Matcher colorCodeMatcher = COLOR_CODE_PATTERN.matcher(message); + while (colorCodeMatcher.find()) lastColor = colorCodeMatcher.group(); + } + + queue.add( + lastColor + eachMessage // the regex has 254 (comes from 256 - 2 (color code length)) so we can do this here + ); + + isFirst = false; } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java index 8c60e3e..aba4bcd 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java @@ -103,13 +103,13 @@ public class CommandHandlerPlugin { // TODO: improve these minimum args and maximum args stuff, the current one really sucks.,., final int shortestUsageIndex = getShortestUsageIndex(command.usages()); final int longestUsageIndex = getLongestUsageIndex(command.usages()); - final String shortestUsage = command.usages()[shortestUsageIndex]; - final String longestUsage = command.usages()[longestUsageIndex]; + final String shortestUsage = shortestUsageIndex == 0 && command.usages().length == 0 ? "" : command.usages()[shortestUsageIndex]; + final String longestUsage = longestUsageIndex == 0 && command.usages().length == 0 ? "" : command.usages()[longestUsageIndex]; final int minimumArgs = getMinimumArgs(shortestUsage, inGame, command.trustLevel()); final int maximumArgs = getMaximumArgs(longestUsage, inGame, command.trustLevel()); if (fullArgs.length < minimumArgs) return Component.text("Excepted minimum of " + minimumArgs + " argument(s), got " + fullArgs.length).color(NamedTextColor.RED); - if (fullArgs.length > maximumArgs && !longestUsage.contains("{")) return Component.text("Too much arguments, expected " + maximumArgs + " max").color(NamedTextColor.RED); + if (fullArgs.length > maximumArgs && !longestUsage.contains("{")) return Component.text("Too many arguments, expected " + maximumArgs + " max").color(NamedTextColor.RED); if (trustLevel != TrustLevel.PUBLIC && splitInput.length < 2 && inGame) return Component.text("Please provide a hash").color(NamedTextColor.RED); @@ -220,6 +220,7 @@ public class CommandHandlerPlugin { minLength = args.length; } } + return shortestIndex; }