diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java index dc844a1..2e8d692 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java @@ -53,6 +53,7 @@ public class Bot { @Getter private final TrustedPlugin trusted; @Getter private final BruhifyPlugin bruhify; @Getter private final GrepLogPlugin grepLog; + @Getter private final CloopPlugin cloop; public Bot (String host, int port, String _username, boolean kaboom, String serverName, List allBots, Configuration config) { this.host = host; @@ -85,6 +86,7 @@ public class Bot { this.trusted = new TrustedPlugin(this); this.bruhify = new BruhifyPlugin(this); this.grepLog = new GrepLogPlugin(this); + this.cloop = new CloopPlugin(this); reconnect(); } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CloopCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CloopCommand.java new file mode 100644 index 0000000..94f140b --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CloopCommand.java @@ -0,0 +1,101 @@ +package land.chipmunk.chayapak.chomens_bot.commands; + +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.command.Command; +import land.chipmunk.chayapak.chomens_bot.command.CommandContext; +import land.chipmunk.chayapak.chomens_bot.data.CommandLoop; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.JoinConfiguration; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class CloopCommand implements Command { + public String name() { return "cloop"; } + + public String description() { + return "Loop commands"; + } + + public List usage() { + final List usages = new ArrayList<>(); + usages.add("add <{command}>"); + usages.add("remove "); + usages.add("clear"); + usages.add("list"); + + return usages; + } + + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add("commandloop"); + + return aliases; + } + + public int trustLevel() { + return 1; + } + + public Component execute(CommandContext context, String[] args, String[] fullArgs) { + final Bot bot = context.bot(); + + switch (args[0]) { + case "add" -> { + if (args.length < 3) return Component.text("Please specify interval and command").color(NamedTextColor.RED); + int interval; + try { + interval = Integer.parseInt(args[1]); + } catch (IllegalArgumentException ignored) { + return Component.text("Invalid index").color(NamedTextColor.RED); + } + bot.cloop().add(interval, String.join(" ", Arrays.copyOfRange(args, 2, args.length))); + } + case "remove" -> { + try { + final int index = Integer.parseInt(args[1]); + bot.cloop().remove(index); + } catch (IllegalArgumentException | NullPointerException ignored) { + return Component.text("Invalid index").color(NamedTextColor.RED); + } + } + case "clear" -> bot.cloop().clear(); + case "list" -> { + final List cloopsComponent = new ArrayList<>(); + + int index = 0; + for (CommandLoop command : bot.cloop().loops()) { + cloopsComponent.add( + Component.translatable( + "%s › %s (%s)", + Component.text(index).color(NamedTextColor.GREEN), + Component.text(command.command()).color(NamedTextColor.AQUA), + Component.text(command.interval()).color(NamedTextColor.GOLD) + ).color(NamedTextColor.DARK_GRAY) + ); + index++; + } + + final Component component = Component.empty() + .append(Component.text("Cloops ").color(NamedTextColor.GREEN)) + .append(Component.text("(").color(NamedTextColor.DARK_GRAY)) + .append(Component.text(bot.cloop().loops().size()).color(NamedTextColor.GRAY)) + .append(Component.text(")").color(NamedTextColor.DARK_GRAY)) + .append(Component.newline()) + .append( + Component.join(JoinConfiguration.newlines(), cloopsComponent) + ); + + context.sendOutput(component); + } + default -> { + return Component.text("Invalid argument").color(NamedTextColor.RED); + } + } + + return Component.text("success"); + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/data/CommandLoop.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/data/CommandLoop.java new file mode 100644 index 0000000..fecdae7 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/data/CommandLoop.java @@ -0,0 +1,16 @@ +package land.chipmunk.chayapak.chomens_bot.data; + +import lombok.Getter; + +public class CommandLoop { + @Getter + private String command; + @Getter + private int interval; + + public CommandLoop (String command, int interval) { + this.command = command; + this.interval = interval; + } +} + diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CloopPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CloopPlugin.java new file mode 100644 index 0000000..a2c1a97 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CloopPlugin.java @@ -0,0 +1,55 @@ +package land.chipmunk.chayapak.chomens_bot.plugins; + +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.data.CommandLoop; +import lombok.Getter; + +import java.util.ArrayList; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; + +public class CloopPlugin { + private final Bot bot; + + // too lazy to use executor + private final List loopTasks = new ArrayList<>(); + @Getter private final List loops = new ArrayList<>(); + + private final Timer timer; + + public CloopPlugin (Bot bot) { + this.bot = bot; + this.timer = new Timer(); + } + + public void add (int interval, String command) { + TimerTask loopTask = new TimerTask() { + public void run() { + bot.core().run(command); + } + }; + loopTasks.add(loopTask); + loops.add(new CommandLoop(command, interval)); // mabe,.,.. + // should i use 50 or 0? + timer.scheduleAtFixedRate(loopTask, 0, interval); + } + + public void remove (int index) { + TimerTask loopTask = loopTasks.remove(index); + if (loopTask != null) { + loopTask.cancel(); + } + + loops.remove(index); + } + + public void clear () { + for (TimerTask loopTask : loopTasks) { + loopTask.cancel(); + } + loopTasks.clear(); + + loops.clear(); + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java index b958e95..23ff3bf 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java @@ -46,6 +46,7 @@ public class CommandHandlerPlugin { registerCommand(new GrepLogCommand()); registerCommand(new SudoAllCommand()); registerCommand(new EndCommand()); + registerCommand(new CloopCommand()); } public void registerCommand (Command command) {