From aa55137f369fe65d877e9a320eac88f6163e3974 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sat, 8 Apr 2023 10:52:05 +0700 Subject: [PATCH] make cb return output yes finally last bot to have this feature?? prob not lol --- .../commands/CommandBlockCommand.java | 23 +++++++++-- .../chomens_bot/plugins/CorePlugin.java | 41 ++++++++++++++++++- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CommandBlockCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CommandBlockCommand.java index 4a61b89..fd5f7ad 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CommandBlockCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/CommandBlockCommand.java @@ -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 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 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"); } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java index 52729b6..3d0b12a 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java @@ -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> 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 runTracked (String command) { + final Vector3i position = absoluteCorePosition(); + + run(command); + + final int transactionId = nextTransactionId++; + + // promises are renamed to future lmao + final CompletableFuture 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); }