fix major command handler issue + improve chat (mabe)

This commit is contained in:
Chayapak 2023-07-01 16:19:30 +07:00
parent 732be0f24f
commit 0d84a96ae1
2 changed files with 30 additions and 8 deletions

View file

@ -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<ChatParser> 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;
}
}

View file

@ -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;
}