refactor: improve flags system and support --flag instead of only -flag
had to remove flags in TestCommand sadly
This commit is contained in:
@@ -1 +1 @@
|
||||
3066
|
||||
3069
|
||||
@@ -15,8 +15,11 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class CommandContext {
|
||||
public static final Component UNKNOWN_ARGUMENT_COMPONENT = Component.text("???").style(Style.style(TextDecoration.UNDERLINED));
|
||||
private static final Pattern FLAGS_PATTERN = Pattern.compile("^\\s*-([^\\s0-9]\\S*)"); // stolen from HBot
|
||||
public static final Component UNKNOWN_ARGUMENT_COMPONENT = Component
|
||||
.text("???")
|
||||
.style(Style.style(TextDecoration.UNDERLINED));
|
||||
private static final Pattern FLAGS_PATTERN = Pattern
|
||||
.compile("^\\s*(?:--|-)([^\\s0-9]\\S*)"); // modified from HBot
|
||||
|
||||
public final Bot bot;
|
||||
|
||||
@@ -179,22 +182,24 @@ public class CommandContext {
|
||||
return getString(false, true, true, "action");
|
||||
}
|
||||
|
||||
public List<String> getFlags () throws CommandException { return getFlags(false); }
|
||||
public List<String> getFlags (final String... allowedFlags) throws CommandException { return getFlags(false, allowedFlags); }
|
||||
|
||||
public List<String> getFlags (final boolean returnLowerCase) throws CommandException {
|
||||
public List<String> getFlags (final boolean returnLowerCase, final String... allowedFlags) throws CommandException {
|
||||
final List<String> flags = new ArrayList<>();
|
||||
|
||||
String flag = getFlag(returnLowerCase);
|
||||
String flag = getFlag(returnLowerCase, allowedFlags);
|
||||
|
||||
while (flag != null) {
|
||||
flags.add(flag);
|
||||
flag = getFlag(returnLowerCase);
|
||||
flag = getFlag(returnLowerCase, allowedFlags);
|
||||
}
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
private String getFlag (final boolean returnLowerCase) throws CommandException {
|
||||
private String getFlag (final boolean returnLowerCase, final String[] allowedFlagsArray) throws CommandException {
|
||||
final List<String> allowedFlags = Arrays.asList(allowedFlagsArray);
|
||||
|
||||
final String string = getString(false, false, returnLowerCase);
|
||||
|
||||
if (string.isBlank()) return null;
|
||||
@@ -202,11 +207,14 @@ public class CommandContext {
|
||||
final Matcher matcher = FLAGS_PATTERN.matcher(string);
|
||||
|
||||
if (matcher.find()) {
|
||||
return matcher.group(1);
|
||||
} else {
|
||||
argsPosition--; // getString incremented argsPosition
|
||||
return null;
|
||||
final String match = matcher.group(1);
|
||||
|
||||
if (allowedFlags.contains(match)) return match;
|
||||
}
|
||||
|
||||
argsPosition--; // getString incremented argsPosition
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Integer getInteger (final boolean required) throws CommandException {
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package me.chayapak1.chomens_bot.command;
|
||||
|
||||
public final class CommonFlags {
|
||||
public static final String IGNORE_CASE = "ignorecase";
|
||||
public static final String REGEX = "regex";
|
||||
}
|
||||
@@ -1,10 +1,7 @@
|
||||
package me.chayapak1.chomens_bot.commands;
|
||||
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import me.chayapak1.chomens_bot.command.Command;
|
||||
import me.chayapak1.chomens_bot.command.CommandContext;
|
||||
import me.chayapak1.chomens_bot.command.CommandException;
|
||||
import me.chayapak1.chomens_bot.command.TrustLevel;
|
||||
import me.chayapak1.chomens_bot.command.*;
|
||||
import me.chayapak1.chomens_bot.data.filter.FilteredPlayer;
|
||||
import me.chayapak1.chomens_bot.plugins.DatabasePlugin;
|
||||
import me.chayapak1.chomens_bot.plugins.PlayerFilterPlugin;
|
||||
@@ -40,10 +37,10 @@ public class FilterCommand extends Command {
|
||||
public Component execute (final CommandContext context) throws CommandException {
|
||||
final Bot bot = context.bot;
|
||||
|
||||
final List<String> flags = context.getFlags(true);
|
||||
final List<String> flags = context.getFlags(true, CommonFlags.IGNORE_CASE, CommonFlags.REGEX);
|
||||
|
||||
final boolean ignoreCase = flags.contains("ignorecase");
|
||||
final boolean regex = flags.contains("regex");
|
||||
final boolean ignoreCase = flags.contains(CommonFlags.IGNORE_CASE);
|
||||
final boolean regex = flags.contains(CommonFlags.REGEX);
|
||||
|
||||
final String action = context.getString(false, true, true);
|
||||
|
||||
|
||||
@@ -18,15 +18,19 @@ import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class FindAltsCommand extends Command {
|
||||
// we allow both, since the flag used to be `allserver`
|
||||
private static final String ALL_SERVER_FLAG = "allserver";
|
||||
private static final String ALL_SERVERS_FLAG = "allservers";
|
||||
|
||||
public FindAltsCommand () {
|
||||
super(
|
||||
"findalts",
|
||||
"Finds players with the same IP address",
|
||||
new String[] { "-allserver <player|ip>", "<player|ip>" },
|
||||
new String[] { "-allservers <player|ip>", "<player|ip>" },
|
||||
new String[] { "alts", "sameip" },
|
||||
TrustLevel.PUBLIC,
|
||||
false,
|
||||
new ChatPacketType[]{ ChatPacketType.DISGUISED }
|
||||
new ChatPacketType[] { ChatPacketType.DISGUISED }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -39,9 +43,9 @@ public class FindAltsCommand extends Command {
|
||||
|
||||
Main.database.checkOverloaded();
|
||||
|
||||
final List<String> flags = context.getFlags(true);
|
||||
final List<String> flags = context.getFlags(true, ALL_SERVER_FLAG, ALL_SERVERS_FLAG);
|
||||
|
||||
final boolean allServer = flags.contains("allserver");
|
||||
final boolean allServer = flags.contains(ALL_SERVER_FLAG) || flags.contains(ALL_SERVERS_FLAG);
|
||||
|
||||
final String player = context.getString(true, true);
|
||||
|
||||
|
||||
@@ -2,10 +2,7 @@ package me.chayapak1.chomens_bot.commands;
|
||||
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import me.chayapak1.chomens_bot.Main;
|
||||
import me.chayapak1.chomens_bot.command.Command;
|
||||
import me.chayapak1.chomens_bot.command.CommandContext;
|
||||
import me.chayapak1.chomens_bot.command.CommandException;
|
||||
import me.chayapak1.chomens_bot.command.TrustLevel;
|
||||
import me.chayapak1.chomens_bot.command.*;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
@@ -32,10 +29,10 @@ public class GrepLogCommand extends Command {
|
||||
throw new CommandException(Component.text("The bot's Discord integration has to be enabled to use this command."));
|
||||
}
|
||||
|
||||
final List<String> flags = context.getFlags(true);
|
||||
final List<String> flags = context.getFlags(true, CommonFlags.IGNORE_CASE, CommonFlags.REGEX);
|
||||
|
||||
final boolean ignoreCase = flags.contains("ignorecase");
|
||||
final boolean regex = flags.contains("regex");
|
||||
final boolean ignoreCase = flags.contains(CommonFlags.IGNORE_CASE);
|
||||
final boolean regex = flags.contains(CommonFlags.REGEX);
|
||||
|
||||
final String input = context.getString(true, true);
|
||||
|
||||
|
||||
@@ -21,11 +21,10 @@ public class TestCommand extends Command {
|
||||
@Override
|
||||
public Component execute (final CommandContext context) throws CommandException {
|
||||
return Component.translatable(
|
||||
"Hello, World! Username: %s, Sender UUID: %s, Prefix: %s, Flags: %s, Args: %s",
|
||||
"Hello, World! Username: %s, Sender UUID: %s, Prefix: %s, Args: %s",
|
||||
Component.text(context.sender.profile.getName()),
|
||||
Component.text(context.sender.profile.getIdAsString()),
|
||||
Component.text(context.prefix),
|
||||
Component.text(String.join(" ", context.getFlags())),
|
||||
Component.text(context.getString(true, false))
|
||||
).color(NamedTextColor.GREEN);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user