refactor: merge cloop task into CommandLoop

This commit is contained in:
ChomeNS
2025-06-08 09:07:57 +07:00
parent 233b386af9
commit 3d6f1193e6
2 changed files with 21 additions and 23 deletions

View File

@@ -1,10 +1,12 @@
package me.chayapak1.chomens_bot.data.cloop;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.ScheduledFuture;
public record CommandLoop(
String command,
long interval,
ChronoUnit unit
ChronoUnit unit,
ScheduledFuture<?> task
) { }

View File

@@ -1,21 +1,19 @@
package me.chayapak1.chomens_bot.plugins;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
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;
import java.util.concurrent.TimeUnit;
public class CloopPlugin {
private final Bot bot;
private final List<ScheduledFuture<?>> loopTasks = new ArrayList<>();
public final List<CommandLoop> loops = new ArrayList<>();
public final List<CommandLoop> loops = new ObjectArrayList<>();
public CloopPlugin (final Bot bot) {
this.bot = bot;
@@ -27,34 +25,32 @@ public class CloopPlugin {
final long convertedInterval = converted.getLeft();
final TimeUnit timeUnit = converted.getRight();
loops.add(new CommandLoop(command, interval, unit));
loopTasks.add(
bot.executor.scheduleAtFixedRate(
() -> bot.core.run(command),
0,
convertedInterval,
timeUnit
loops.add(
new CommandLoop(
command,
interval,
unit,
bot.executor.scheduleAtFixedRate(
() -> bot.core.run(command),
0,
convertedInterval,
timeUnit
)
)
);
}
public CommandLoop remove (final int index) {
final ScheduledFuture<?> loopTask = loopTasks.remove(index);
if (loopTask != null) {
loopTask.cancel(false);
}
return loops.remove(index);
final CommandLoop removed = loops.remove(index);
removed.task().cancel(false);
return removed;
}
public void clear () {
for (final ScheduledFuture<?> loopTask : loopTasks) {
loopTask.cancel(false);
for (final CommandLoop loop : loops) {
loop.task().cancel(false);
}
loopTasks.clear();
loops.clear();
}
}