feat: re-add ChronoUnit cloop unit that should actually work this time
This commit is contained in:
@@ -1 +1 @@
|
||||
2760
|
||||
2762
|
||||
@@ -10,16 +10,16 @@ import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.JoinConfiguration;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class CloopCommand extends Command {
|
||||
public CloopCommand () {
|
||||
super(
|
||||
"cloop",
|
||||
"Loops commands",
|
||||
new String[] { "add <interval> <TimeUnit> <command>", "remove <index>", "clear", "list" },
|
||||
new String[] { "add <interval> <ChronoUnit> <command>", "remove <index>", "clear", "list" },
|
||||
new String[] { "commandloop" },
|
||||
TrustLevel.TRUSTED
|
||||
);
|
||||
@@ -36,11 +36,15 @@ public class CloopCommand extends Command {
|
||||
long interval = context.getLong(true);
|
||||
if (interval < 1) interval = 1;
|
||||
|
||||
final TimeUnit unit = context.getEnum(TimeUnit.class);
|
||||
final ChronoUnit unit = context.getEnum(ChronoUnit.class);
|
||||
|
||||
final String command = context.getString(true, true);
|
||||
|
||||
bot.cloop.add(unit, interval, command);
|
||||
try {
|
||||
bot.cloop.add(unit, interval, command);
|
||||
} catch (final ArithmeticException e) {
|
||||
throw new CommandException(Component.text(e.toString()));
|
||||
}
|
||||
|
||||
return Component.translatable(
|
||||
"Added %s with interval %s %s to the cloops",
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package me.chayapak1.chomens_bot.data.cloop;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
public record CommandLoop(
|
||||
String command,
|
||||
long interval,
|
||||
TimeUnit unit
|
||||
ChronoUnit unit
|
||||
) { }
|
||||
|
||||
|
||||
@@ -2,7 +2,10 @@ package me.chayapak1.chomens_bot.plugins;
|
||||
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import me.chayapak1.chomens_bot.data.cloop.CommandLoop;
|
||||
import me.chayapak1.chomens_bot.util.TimeUnitUtilities;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
@@ -18,11 +21,21 @@ public class CloopPlugin {
|
||||
this.bot = bot;
|
||||
}
|
||||
|
||||
public void add (final TimeUnit unit, final long interval, final String command) {
|
||||
final Runnable loopTask = () -> bot.core.run(command);
|
||||
public void add (final ChronoUnit unit, final long interval, final String command) {
|
||||
final Pair<Long, TimeUnit> converted = TimeUnitUtilities.fromChronoUnit(interval, unit);
|
||||
|
||||
final long convertedInterval = converted.getLeft();
|
||||
final TimeUnit timeUnit = converted.getRight();
|
||||
|
||||
loops.add(new CommandLoop(command, interval, unit));
|
||||
loopTasks.add(bot.executor.scheduleAtFixedRate(loopTask, 0, interval, unit));
|
||||
loopTasks.add(
|
||||
bot.executor.scheduleAtFixedRate(
|
||||
() -> bot.core.run(command),
|
||||
0,
|
||||
convertedInterval,
|
||||
timeUnit
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public CommandLoop remove (final int index) {
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package me.chayapak1.chomens_bot.util;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class TimeUnitUtilities {
|
||||
// some weird function used in cloop
|
||||
public static Pair<Long, TimeUnit> fromChronoUnit (final long interval, final ChronoUnit chronoUnit) {
|
||||
final Duration duration = chronoUnit.getDuration();
|
||||
|
||||
if (duration.getSeconds() >= 1) return Pair.of(interval * duration.toSeconds(), TimeUnit.SECONDS);
|
||||
else return Pair.of(interval * duration.toNanos(), TimeUnit.NANOSECONDS);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user