Experimental support for disabling the core

This commit is contained in:
Chipmunk 2023-04-19 23:26:48 -04:00
parent 7d3cafa578
commit 35f247ef29
5 changed files with 71 additions and 17 deletions

View file

@ -14,19 +14,29 @@ import land.chipmunk.chipmunkbot.plugins.*;
import java.util.List; import java.util.List;
public class ChipmunkBot extends Client { public class ChipmunkBot extends Client {
@Getter public final ChatPlugin chat = new ChatPlugin(this); @Getter public final ChatPlugin chat;
@Getter public final TabCompletePlugin tabComplete = new TabCompletePlugin(this); @Getter public final TabCompletePlugin tabComplete;
@Getter public final QueryPlugin query = new QueryPlugin(this); @Getter public final QueryPlugin query;
@Getter public final PlayerListPlugin playerList = new PlayerListPlugin(this); @Getter public final PlayerListPlugin playerList;
@Getter public final CommandManager commandManager = new CommandManager(this); @Getter public final CommandManager commandManager;
@Getter public final ChatCommandHandler chatCommandHandler; @Getter public final ChatCommandHandler chatCommandHandler;
@Getter public final PositionManager position = new PositionManager(this); @Getter public final PositionManager position;
@Getter public final CommandCore core = new CommandCore(this); @Getter public final CommandCore core;
@Getter public final SelfCarePlugin selfCare = new SelfCarePlugin(this); @Getter public final SelfCarePlugin selfCare;
@Getter public final SongPlayer songPlayer = new SongPlayer(this); @Getter public final SongPlayer songPlayer;
public ChipmunkBot (Options options, List<Client> allClients) { public ChipmunkBot (Options options, List<Client> allClients) {
super(options.host, options.port, new MinecraftProtocol(options.username), null, options.reconnectDelay, 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);
} }
} }

View file

@ -9,7 +9,13 @@ public class Options {
public Commands commands = new Commands(); public Commands commands = new Commands();
public Core core = new Core();
public class Commands { public class Commands {
public String prefix = "default."; public String prefix = "default.";
} }
public class Core {
public boolean enabled = true;
}
} }

View file

@ -21,9 +21,9 @@ public class ChatCommandHandler extends ChatPlugin.Listener {
private ChipmunkBot client; private ChipmunkBot client;
@Getter @Setter private String prefix; @Getter @Setter private String prefix;
public ChatCommandHandler (ChipmunkBot client, Options.Commands options) { public ChatCommandHandler (ChipmunkBot client, Options options) {
this.client = client; this.client = client;
this.prefix = options.prefix; this.prefix = options.commands.prefix;
client.chat().addListener((ChatPlugin.Listener) this); client.chat().addListener((ChatPlugin.Listener) this);
} }

View file

@ -1,6 +1,7 @@
package land.chipmunk.chipmunkbot.plugins; package land.chipmunk.chipmunkbot.plugins;
import land.chipmunk.chipmunkbot.ChipmunkBot; import land.chipmunk.chipmunkbot.ChipmunkBot;
import land.chipmunk.chipmunkbot.Options;
import land.chipmunk.chipmunkbot.plugins.PositionManager; import land.chipmunk.chipmunkbot.plugins.PositionManager;
import land.chipmunk.chipmunkbot.data.BlockArea; import land.chipmunk.chipmunkbot.data.BlockArea;
import org.cloudburstmc.math.vector.Vector3i; import org.cloudburstmc.math.vector.Vector3i;
@ -34,6 +35,7 @@ import java.util.concurrent.CompletableFuture;
public class CommandCore extends SessionAdapter { public class CommandCore extends SessionAdapter {
private ChipmunkBot client; private ChipmunkBot client;
@Getter @Setter private boolean enabled = false;
@Getter @Setter private boolean ready = false; @Getter @Setter private boolean ready = false;
@Getter @Setter private Vector3i origin; @Getter @Setter private Vector3i origin;
// TODO: Make it configurable // TODO: Make it configurable
@ -43,8 +45,9 @@ public class CommandCore extends SessionAdapter {
@Getter private List<Listener> listeners = new ArrayList<>(); @Getter private List<Listener> listeners = new ArrayList<>();
public CommandCore (ChipmunkBot client) { public CommandCore (ChipmunkBot client, Options options) {
this.client = client; this.client = client;
this.enabled = options.core.enabled;
client.addListener((SessionListener) this); client.addListener((SessionListener) this);
} }
@ -56,9 +59,18 @@ public class CommandCore extends SessionAdapter {
public void packetReceived (Session session, ClientboundPlayerPositionPacket packet) { public void packetReceived (Session session, ClientboundPlayerPositionPacket packet) {
if (!ready) { if (!ready) {
ready = true; 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(); for (Listener listener : listeners) listener.ready();
} }
if (!enabled) return;
final Vector3i oldOrigin = origin; final Vector3i oldOrigin = origin;
origin = Vector3i.from( origin = Vector3i.from(
((int) packet.getX() / 16) * 16, ((int) packet.getX() / 16) * 16,
@ -68,10 +80,6 @@ public class CommandCore extends SessionAdapter {
if (currentBlockRelative == null) currentBlockRelative = Vector3i.from(relativeArea.start()); if (currentBlockRelative == null) currentBlockRelative = Vector3i.from(relativeArea.start());
if (!origin.equals(oldOrigin)) refill(); 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 () { 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)); 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 () { public void incrementCurrentBlock () {
final Vector3i start = relativeArea.start(); final Vector3i start = relativeArea.start();
final Vector3i end = relativeArea.end(); final Vector3i end = relativeArea.end();
@ -142,6 +155,13 @@ public class CommandCore extends SessionAdapter {
} }
public void run (String command) { 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 Session session = client.session();
final Vector3i currentBlock = currentBlockAbsolute(); final Vector3i currentBlock = currentBlockAbsolute();
@ -153,6 +173,14 @@ public class CommandCore extends SessionAdapter {
} }
public CompletableFuture<CompoundTag> runTracked (String command) { public CompletableFuture<CompoundTag> 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 Session session = client.session();
final Vector3i currentBlock = currentBlockAbsolute(); final Vector3i currentBlock = currentBlockAbsolute();
@ -179,6 +207,12 @@ public class CommandCore extends SessionAdapter {
return future; return future;
} }
private CompletableFuture<CompoundTag> emptyCompoundTagFuture () {
CompletableFuture<CompoundTag> future = new CompletableFuture<CompoundTag>();
future.complete(new CompoundTag(""));
return future;
}
@Override @Override
public void disconnected (DisconnectedEvent event) { public void disconnected (DisconnectedEvent event) {
origin = null; origin = null;

View file

@ -8,6 +8,10 @@
"commands": { "commands": {
"prefix": "default." "prefix": "default."
},
"core": {
"enabled": true
} }
} }
] ]