feat: cloop interval units (now we can loop at 1 chomeNS and crash the server :D)

feat: show possible values of the enum in the command context's `getEnum`
This commit is contained in:
ChomeNS
2025-03-16 12:53:27 +07:00
parent fb8dd58de1
commit c86e2714e6
5 changed files with 30 additions and 16 deletions

View File

@@ -1 +1 @@
1832
1846

View File

@@ -232,7 +232,15 @@ public class CommandContext {
try {
return Enum.valueOf(enumClass, string.toUpperCase());
} catch (IllegalArgumentException | NullPointerException e) {
throw new CommandException(Component.text("Invalid enum"));
final T[] values = enumClass.getEnumConstants();
throw new CommandException(
Component.translatable(
"Invalid %s. Possible values are: %s",
Component.text(enumClass.getSimpleName()),
Component.text(Arrays.toString(values))
)
);
}
}

View File

@@ -13,13 +13,14 @@ import net.kyori.adventure.text.format.NamedTextColor;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class CloopCommand extends Command {
public CloopCommand () {
super(
"cloop",
"Loop commands",
new String[] { "add <interval> <command>", "remove <index>", "clear", "list" },
"Loops commands",
new String[] { "add <interval> <TimeUnit> <command>", "remove <index>", "clear", "list" },
new String[] { "commandloop" },
TrustLevel.TRUSTED,
false
@@ -37,14 +38,17 @@ public class CloopCommand extends Command {
int interval = context.getInteger(true);
if (interval < 1) interval = 1;
final TimeUnit unit = context.getEnum(TimeUnit.class);
final String command = context.getString(true, true);
bot.cloop.add(interval, command);
bot.cloop.add(unit, interval, command);
return Component.translatable(
"Added %s with interval %s to the cloops",
"Added %s with interval %s %s to the cloops",
Component.text(command).color(ColorUtilities.getColorByString(bot.config.colorPalette.string)),
Component.text(interval).color(ColorUtilities.getColorByString(bot.config.colorPalette.number))
Component.text(interval).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)),
Component.text(unit.toString()).color(ColorUtilities.getColorByString(bot.config.colorPalette.string))
).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}
case "remove" -> {
@@ -78,10 +82,11 @@ public class CloopCommand extends Command {
for (CommandLoop command : bot.cloop.loops) {
cloopsComponent.add(
Component.translatable(
"%s %s (%s)",
"%s %s (%s %s)",
Component.text(index).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)),
Component.text(command.command()).color(ColorUtilities.getColorByString(bot.config.colorPalette.string)),
Component.text(command.interval()).color(ColorUtilities.getColorByString(bot.config.colorPalette.number))
Component.text(command.interval()).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)),
Component.text(command.unit().toString()).color(ColorUtilities.getColorByString(bot.config.colorPalette.string))
).color(NamedTextColor.DARK_GRAY)
);
index++;

View File

@@ -1,8 +1,10 @@
package me.chayapak1.chomens_bot.data.cloop;
import java.util.concurrent.TimeUnit;
public record CommandLoop (
String command,
int interval
int interval,
TimeUnit unit
) {}

View File

@@ -18,19 +18,18 @@ public class CloopPlugin {
this.bot = bot;
}
public void add (int interval, String command) {
public void add (TimeUnit unit, int interval, String command) {
Runnable loopTask = () -> bot.core.run(command);
loops.add(new CommandLoop(command, interval)); // mabe,.,..
// should i use 50 or 0?
loopTasks.add(bot.executor.scheduleAtFixedRate(loopTask, 0, interval, TimeUnit.MILLISECONDS));
loops.add(new CommandLoop(command, interval, unit));
loopTasks.add(bot.executor.scheduleAtFixedRate(loopTask, 0, interval, unit));
}
public CommandLoop remove (int index) {
ScheduledFuture<?> loopTask = loopTasks.remove(index);
if (loopTask != null) {
loopTask.cancel(true);
loopTask.cancel(false);
}
return loops.remove(index);
@@ -38,7 +37,7 @@ public class CloopPlugin {
public void clear () {
for (ScheduledFuture<?> loopTask : loopTasks) {
loopTask.cancel(true);
loopTask.cancel(false);
}
loopTasks.clear();