diff --git a/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java b/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java index 47746b5..7193963 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java +++ b/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java @@ -14,19 +14,29 @@ import land.chipmunk.chipmunkbot.plugins.*; import java.util.List; public class ChipmunkBot extends Client { - @Getter public final ChatPlugin chat = new ChatPlugin(this); - @Getter public final TabCompletePlugin tabComplete = new TabCompletePlugin(this); - @Getter public final QueryPlugin query = new QueryPlugin(this); - @Getter public final PlayerListPlugin playerList = new PlayerListPlugin(this); - @Getter public final CommandManager commandManager = new CommandManager(this); + @Getter public final ChatPlugin chat; + @Getter public final TabCompletePlugin tabComplete; + @Getter public final QueryPlugin query; + @Getter public final PlayerListPlugin playerList; + @Getter public final CommandManager commandManager; @Getter public final ChatCommandHandler chatCommandHandler; - @Getter public final PositionManager position = new PositionManager(this); - @Getter public final CommandCore core = new CommandCore(this); - @Getter public final SelfCarePlugin selfCare = new SelfCarePlugin(this); - @Getter public final SongPlayer songPlayer = new SongPlayer(this); + @Getter public final PositionManager position; + @Getter public final CommandCore core; + @Getter public final SelfCarePlugin selfCare; + @Getter public final SongPlayer songPlayer; public ChipmunkBot (Options options, List allClients) { super(options.host, options.port, new MinecraftProtocol(options.username), null, options.reconnectDelay, allClients); - chatCommandHandler = new ChatCommandHandler(this, options.commands); + + this.chat = new ChatPlugin(this); + this.tabComplete = new TabCompletePlugin(this); + this.query = new QueryPlugin(this); + this.playerList = new PlayerListPlugin(this); + this.commandManager = new CommandManager(this); + this.chatCommandHandler = new ChatCommandHandler(this, options); + this.position = new PositionManager(this); + this.core = new CommandCore(this, options); + this.selfCare = new SelfCarePlugin(this); + this.songPlayer = new SongPlayer(this); } } diff --git a/src/main/java/land/chipmunk/chipmunkbot/Options.java b/src/main/java/land/chipmunk/chipmunkbot/Options.java index e4713a2..e356482 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/Options.java +++ b/src/main/java/land/chipmunk/chipmunkbot/Options.java @@ -9,7 +9,13 @@ public class Options { public Commands commands = new Commands(); + public Core core = new Core(); + public class Commands { public String prefix = "default."; } + + public class Core { + public boolean enabled = true; + } } diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatCommandHandler.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatCommandHandler.java index 2dd3469..f2057c6 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatCommandHandler.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatCommandHandler.java @@ -21,9 +21,9 @@ public class ChatCommandHandler extends ChatPlugin.Listener { private ChipmunkBot client; @Getter @Setter private String prefix; - public ChatCommandHandler (ChipmunkBot client, Options.Commands options) { + public ChatCommandHandler (ChipmunkBot client, Options options) { this.client = client; - this.prefix = options.prefix; + this.prefix = options.commands.prefix; client.chat().addListener((ChatPlugin.Listener) this); } diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandCore.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandCore.java index bd36690..a6e035e 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandCore.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandCore.java @@ -1,6 +1,7 @@ package land.chipmunk.chipmunkbot.plugins; import land.chipmunk.chipmunkbot.ChipmunkBot; +import land.chipmunk.chipmunkbot.Options; import land.chipmunk.chipmunkbot.plugins.PositionManager; import land.chipmunk.chipmunkbot.data.BlockArea; import org.cloudburstmc.math.vector.Vector3i; @@ -34,6 +35,7 @@ import java.util.concurrent.CompletableFuture; public class CommandCore extends SessionAdapter { private ChipmunkBot client; + @Getter @Setter private boolean enabled = false; @Getter @Setter private boolean ready = false; @Getter @Setter private Vector3i origin; // TODO: Make it configurable @@ -43,8 +45,9 @@ public class CommandCore extends SessionAdapter { @Getter private List listeners = new ArrayList<>(); - public CommandCore (ChipmunkBot client) { + public CommandCore (ChipmunkBot client, Options options) { this.client = client; + this.enabled = options.core.enabled; client.addListener((SessionListener) this); } @@ -56,9 +59,18 @@ public class CommandCore extends SessionAdapter { public void packetReceived (Session session, ClientboundPlayerPositionPacket packet) { if (!ready) { ready = true; + + if (enabled) { + final TimerTask refillTask = new TimerTask () { @Override public void run () { refill(); } }; + refillTimer = new Timer(); + refillTimer.schedule(refillTask, 60L * 1000L, 60L * 1000L); + } + for (Listener listener : listeners) listener.ready(); } + if (!enabled) return; + final Vector3i oldOrigin = origin; origin = Vector3i.from( ((int) packet.getX() / 16) * 16, @@ -68,10 +80,6 @@ public class CommandCore extends SessionAdapter { if (currentBlockRelative == null) currentBlockRelative = Vector3i.from(relativeArea.start()); if (!origin.equals(oldOrigin)) refill(); - - final TimerTask refillTask = new TimerTask () { @Override public void run () { refill(); } }; - refillTimer = new Timer(); - refillTimer.schedule(refillTask, 60L * 1000L, 60L * 1000L); } public void refill () { @@ -108,6 +116,11 @@ public class CommandCore extends SessionAdapter { session.send(new ServerboundUseItemOnPacket(temporaryBlockPosition, Direction.NORTH, Hand.OFF_HAND, 0.5f, 0.5f, 0.5f, false, 0)); } + public int maxCommandLength () { + if (!enabled) return 256; + return 32767; + } + public void incrementCurrentBlock () { final Vector3i start = relativeArea.start(); final Vector3i end = relativeArea.end(); @@ -142,6 +155,13 @@ public class CommandCore extends SessionAdapter { } public void run (String command) { + if (command.length() > maxCommandLength()) return; + + if (!enabled) { + client.chat().command(command); // fall back to chat + return; + } + final Session session = client.session(); final Vector3i currentBlock = currentBlockAbsolute(); @@ -153,6 +173,14 @@ public class CommandCore extends SessionAdapter { } public CompletableFuture runTracked (String command) { + if (command.length() > maxCommandLength()) return emptyCompoundTagFuture(); + + if (!enabled) { + client.chat().command(command); // fall back to chat + + return emptyCompoundTagFuture(); + } + final Session session = client.session(); final Vector3i currentBlock = currentBlockAbsolute(); @@ -179,6 +207,12 @@ public class CommandCore extends SessionAdapter { return future; } + private CompletableFuture emptyCompoundTagFuture () { + CompletableFuture future = new CompletableFuture(); + future.complete(new CompoundTag("")); + return future; + } + @Override public void disconnected (DisconnectedEvent event) { origin = null; diff --git a/src/main/resources/default_config.json b/src/main/resources/default_config.json index bbd34c9..a2a850f 100644 --- a/src/main/resources/default_config.json +++ b/src/main/resources/default_config.json @@ -8,6 +8,10 @@ "commands": { "prefix": "default." + }, + + "core": { + "enabled": true } } ]