refactor: merge cloop task into CommandLoop
This commit is contained in:
@@ -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
|
||||
) { }
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user