make cb return output yes finally

last bot to have this feature?? prob not lol
This commit is contained in:
Chayapak 2023-04-08 10:52:05 +07:00
parent bec3908919
commit aa55137f36
2 changed files with 59 additions and 5 deletions

View file

@ -1,18 +1,23 @@
package land.chipmunk.chayapak.chomens_bot.commands;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.command.Command;
import land.chipmunk.chayapak.chomens_bot.command.CommandContext;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class CommandBlockCommand implements Command {
public String name() { return "cb"; }
public String description() {
return "Executes a command in the command core";
return "Executes a command in the command core and return it's output";
}
public List<String> usage() {
@ -35,10 +40,22 @@ public class CommandBlockCommand implements Command {
return 0;
}
public Component execute(CommandContext context, String[] args, String[] fullArgs) {
public Component execute(CommandContext context, String[] args, String[] fullArgs) throws ExecutionException, InterruptedException {
final Bot bot = context.bot();
bot.core().run(String.join(" ", args));
final CompletableFuture<CompoundTag> future = bot.core().runTracked(String.join(" ", args));
future.thenApply(tags -> {
if (!tags.contains("LastOutput") || !(tags.get("LastOutput") instanceof StringTag)) return tags;
final StringTag lastOutput = tags.get("LastOutput");
final Component output = GsonComponentSerializer.gson().deserialize(lastOutput.getValue());
context.sendOutput(output);
return tags;
});
return Component.text("success");
}

View file

@ -5,8 +5,10 @@ import com.github.steveice10.mc.protocol.data.game.entity.object.Direction;
import com.github.steveice10.mc.protocol.data.game.entity.player.Hand;
import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction;
import com.github.steveice10.mc.protocol.data.game.level.block.CommandBlockMode;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.ClientboundTagQueryPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSetCommandBlockPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSetCreativeModeSlotPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.level.ServerboundBlockEntityTagQuery;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.player.ServerboundPlayerActionPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.player.ServerboundUseItemOnPacket;
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
@ -16,11 +18,16 @@ import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import com.github.steveice10.packetlib.event.session.SessionAdapter;
import com.github.steveice10.packetlib.packet.Packet;
import com.nukkitx.math.vector.Vector3i;
import lombok.Getter;
import land.chipmunk.chayapak.chomens_bot.Bot;
import lombok.Getter;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
public class CorePlugin extends PositionPlugin.PositionListener {
@ -37,6 +44,9 @@ public class CorePlugin extends PositionPlugin.PositionListener {
public Vector3i relativeCorePosition = Vector3i.from(coreStart);
private int nextTransactionId = 0;
private final Map<Integer, CompletableFuture<CompoundTag>> transactions = new HashMap<>();
private final boolean kaboom;
public CorePlugin (Bot bot) {
@ -50,6 +60,11 @@ public class CorePlugin extends PositionPlugin.PositionListener {
public void disconnected (DisconnectedEvent event) {
ready = false;
}
@Override
public void packetReceived(Session session, Packet packet) {
if (packet instanceof ClientboundTagQueryPacket) CorePlugin.this.packetReceived((ClientboundTagQueryPacket) packet);
}
});
}
@ -76,6 +91,28 @@ public class CorePlugin extends PositionPlugin.PositionListener {
incrementBlock();
}
public CompletableFuture<CompoundTag> runTracked (String command) {
final Vector3i position = absoluteCorePosition();
run(command);
final int transactionId = nextTransactionId++;
// promises are renamed to future lmao
final CompletableFuture<CompoundTag> future = new CompletableFuture<>();
transactions.put(transactionId, future);
final Runnable afterTick = () -> bot.session().send(new ServerboundBlockEntityTagQuery(transactionId, position));
bot.executor().schedule(afterTick, 50, TimeUnit.MILLISECONDS);
return future;
}
public void packetReceived (ClientboundTagQueryPacket packet) {
transactions.get(packet.getTransactionId()).complete(packet.getNbt());
}
public Vector3i absoluteCorePosition () {
return relativeCorePosition.add(origin);
}