forked from ChomeNS/chomens-bot-java
finally add clooping
This commit is contained in:
parent
9146c38450
commit
a2f04d010d
5 changed files with 175 additions and 0 deletions
|
@ -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<Bot> 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();
|
||||
}
|
||||
|
|
|
@ -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<String> usage() {
|
||||
final List<String> usages = new ArrayList<>();
|
||||
usages.add("add <interval> <{command}>");
|
||||
usages.add("remove <index>");
|
||||
usages.add("clear");
|
||||
usages.add("list");
|
||||
|
||||
return usages;
|
||||
}
|
||||
|
||||
public List<String> alias() {
|
||||
final List<String> 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<Component> 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");
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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<TimerTask> loopTasks = new ArrayList<>();
|
||||
@Getter private final List<CommandLoop> 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();
|
||||
}
|
||||
}
|
|
@ -46,6 +46,7 @@ public class CommandHandlerPlugin {
|
|||
registerCommand(new GrepLogCommand());
|
||||
registerCommand(new SudoAllCommand());
|
||||
registerCommand(new EndCommand());
|
||||
registerCommand(new CloopCommand());
|
||||
}
|
||||
|
||||
public void registerCommand (Command command) {
|
||||
|
|
Loading…
Reference in a new issue