From 27757485d1b29e5a36e294f8d91f81066ff95918 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+chomens@users.noreply.github.com> Date: Sun, 19 Oct 2025 15:00:39 +0700 Subject: [PATCH] feat: use deploy core automatically if core size is a single block --- .../chipmunkmod/commands/CoreCommand.java | 2 + .../commands/ReloadConfigCommand.java | 2 +- .../chipmunkmod/modules/CommandCore.java | 139 +++++++++++------- 3 files changed, 92 insertions(+), 51 deletions(-) diff --git a/src/main/java/land/chipmunk/chipmunkmod/commands/CoreCommand.java b/src/main/java/land/chipmunk/chipmunkmod/commands/CoreCommand.java index fa447a4..3b90d39 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/commands/CoreCommand.java +++ b/src/main/java/land/chipmunk/chipmunkmod/commands/CoreCommand.java @@ -64,6 +64,8 @@ public class CoreCommand { final CompletableFuture future = CommandCore.INSTANCE.runTracked(command); future.thenApply(tag -> { + if (tag == null) return null; + try { tag.get("LastOutput", TextCodecs.CODEC).ifPresent(source::sendFeedback); } catch (final Exception e) { diff --git a/src/main/java/land/chipmunk/chipmunkmod/commands/ReloadConfigCommand.java b/src/main/java/land/chipmunk/chipmunkmod/commands/ReloadConfigCommand.java index 2be414e..a33b822 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/commands/ReloadConfigCommand.java +++ b/src/main/java/land/chipmunk/chipmunkmod/commands/ReloadConfigCommand.java @@ -27,7 +27,7 @@ public class ReloadConfigCommand { try { ChipmunkMod.CONFIG = ChipmunkMod.loadConfig(); - CommandCore.INSTANCE.reloadRelativeArea(); + CommandCore.INSTANCE.reloadFromConfig(); CustomChat.INSTANCE.reloadFromConfig(); source.sendFeedback(Text.literal("Successfully reloaded the config")); diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/CommandCore.java b/src/main/java/land/chipmunk/chipmunkmod/modules/CommandCore.java index f4f006c..28eb5c6 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/modules/CommandCore.java +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/CommandCore.java @@ -25,10 +25,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.nbt.NbtCompound; import net.minecraft.network.ClientConnection; -import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket; -import net.minecraft.network.packet.c2s.play.PlayerInteractBlockC2SPacket; -import net.minecraft.network.packet.c2s.play.UpdateCommandBlockC2SPacket; -import net.minecraft.network.packet.c2s.play.UpdateSelectedSlotC2SPacket; +import net.minecraft.network.packet.c2s.play.*; import net.minecraft.registry.Registries; import net.minecraft.registry.tag.BlockTags; import net.minecraft.state.property.Property; @@ -40,14 +37,11 @@ import net.minecraft.util.math.*; import net.minecraft.world.chunk.Chunk; import net.minecraft.world.dimension.DimensionType; -import java.util.Collections; -import java.util.Map; -import java.util.Timer; -import java.util.TimerTask; +import java.util.*; import java.util.concurrent.CompletableFuture; public class CommandCore implements Listener { - public static CommandCore INSTANCE = new CommandCore(MinecraftClient.getInstance()); + public static final CommandCore INSTANCE = new CommandCore(MinecraftClient.getInstance()); private final MinecraftClient client; @@ -57,6 +51,7 @@ public class CommandCore implements Listener { public BlockBox withPos; public BlockPos block; public boolean runFillCommand = true; + public boolean isSingleBlock = false; public boolean alreadyFilled = false; private final Map pendingSetBlockCommands = Collections.synchronizedMap(new Object2ObjectArrayMap<>()); @@ -67,7 +62,7 @@ public class CommandCore implements Listener { public CommandCore (final MinecraftClient client) { this.client = client; - reloadRelativeArea(); + reloadFromConfig(); ListenerManager.addListener(this); } @@ -97,11 +92,12 @@ public class CommandCore implements Listener { return; } - reloadRelativeArea(); + reloadFromConfig(); } - public void reloadRelativeArea () { + public void reloadFromConfig () { noPos = ChipmunkMod.CONFIG.core.relativeArea; + isSingleBlock = noPos.getDimensions().equals(Vec3i.ZERO); } @Override @@ -144,6 +140,8 @@ public class CommandCore implements Listener { } public void check () { + if (isSingleBlock) return; + final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler(); if (networkHandler == null || withPos == null || !ready) return; @@ -225,7 +223,7 @@ public class CommandCore implements Listener { final ClientPlayerEntity player = client.player; if ( - !runFillCommand + !runFillCommand || isSingleBlock || player == null || !player.isInCreativeMode() || !player.hasPermissionLevel(2) @@ -254,7 +252,7 @@ public class CommandCore implements Listener { networkHandler.sendChatCommand(command); refillTriesUsingPlaceBlock = 0; } else { - runPlaceBlock(command); + runPlaceBlock(command, true, true); refillTriesUsingPlaceBlock++; } } @@ -284,8 +282,12 @@ public class CommandCore implements Listener { public void run (final String command) { if (command.length() > 32767) return; - final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler(); + if (isSingleBlock) { + runPlaceBlock(command, false, true); + return; + } + final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler(); if (networkHandler == null) return; final ClientConnection connection = networkHandler.getConnection(); @@ -335,13 +337,17 @@ public class CommandCore implements Listener { public CompletableFuture runTracked (final String command) { final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler(); - if (networkHandler == null) return new CompletableFuture<>(); + if (networkHandler == null) return CompletableFuture.completedFuture(null); final ClientConnection connection = networkHandler.getConnection(); - if (block == null) return new CompletableFuture<>(); + if (block == null) return CompletableFuture.completedFuture(null); - if (KaboomCheck.INSTANCE.isKaboom) { + final BlockPos oldBlock; + + if (isSingleBlock) { + oldBlock = runPlaceBlock(command, false, false); + } else if (KaboomCheck.INSTANCE.isKaboom) { connection.send( new UpdateCommandBlockC2SPacket( block, @@ -352,6 +358,8 @@ public class CommandCore implements Listener { true ) ); + + oldBlock = block; } else { connection.send( new UpdateCommandBlockC2SPacket( @@ -374,17 +382,24 @@ public class CommandCore implements Listener { true ) ); + + oldBlock = block; } + if (oldBlock == null) return CompletableFuture.completedFuture(null); + final CompletableFuture future = new CompletableFuture<>(); final Timer timer = new Timer(); - final BlockPos oldBlock = block; - final TimerTask queryTask = new TimerTask() { public void run () { - client.getNetworkHandler().getDataQueryHandler().queryBlockNbt(oldBlock, future::complete); + client.getNetworkHandler().getDataQueryHandler().queryBlockNbt(oldBlock, result -> { + future.complete(result); + if (isSingleBlock && client.interactionManager != null) { + client.interactionManager.breakBlock(oldBlock); + } + }); } }; @@ -395,21 +410,21 @@ public class CommandCore implements Listener { return future; } - public void runPlaceBlock (final String command) { + public BlockPos runPlaceBlock (final String command, final boolean fallbackToChat, final boolean replaceOldBlock) { final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler(); - if (networkHandler == null) return; + if (networkHandler == null) return null; final ClientConnection connection = networkHandler.getConnection(); final ClientPlayerEntity player = client.player; final ClientWorld world = client.world; - if (player == null || world == null || !player.isInCreativeMode()) return; + if (player == null || world == null || !player.isInCreativeMode()) return null; final DimensionType dimensionType = world.getDimension(); final ClientPlayerInteractionManager interactionManager = client.interactionManager; - if (interactionManager == null) return; + if (interactionManager == null) return null; // stolen from two five hundred million dollars @@ -420,8 +435,8 @@ public class CommandCore implements Listener { || pair.getLeft().getY() < dimensionType.minY() || pair.getLeft().getY() > dimensionType.height() + dimensionType.minY() ) { - networkHandler.sendChatCommand(command); - return; + if (fallbackToChat) networkHandler.sendChatCommand(command); + return null; } final BlockPos position = pair.getLeft(); @@ -455,8 +470,15 @@ public class CommandCore implements Listener { connection.send(new UpdateSelectedSlotC2SPacket(freeHotBarSlot)); } - interactionManager.breakBlock(position); try (final PendingUpdateManager pendingUpdateManager = ((ClientWorldAccessor) world).getPendingUpdateManager()) { + connection.send( + new PlayerActionC2SPacket( + PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, + position, + Direction.UP, + pendingUpdateManager.incrementSequence().getSequence() + ) + ); connection.send( new PlayerInteractBlockC2SPacket( Hand.MAIN_HAND, @@ -469,39 +491,56 @@ public class CommandCore implements Listener { ) ); } - interactionManager.interactBlock( - player, - Hand.MAIN_HAND, - new BlockHitResult( - new Vec3d(position), - Direction.UP, - position, - false - ) - ); if (oldSelectedSlot != freeHotBarSlot) { connection.send(new UpdateSelectedSlotC2SPacket(oldSelectedSlot)); } connection.send(new CreativeInventoryActionC2SPacket(slot, oldStack)); - final StringBuilder oldBlockString = new StringBuilder(Registries.BLOCK.getId(oldBlockState.getBlock()).toString()); - if (!oldBlockState.getProperties().isEmpty()) { - oldBlockString.append('['); + if (ChipmunkMod.CONFIG.core.logCommands) + ChipmunkMod.LOGGER.info("Executing place block core command: {}", command); - for (final Property property : oldBlockState.getProperties()) { - oldBlockString.append(property.getName()) - .append('=') - .append(Util.getValueAsString(property, property.getValues().getLast())) // water[level=15] instead of water[level=0,level=1,....,level=15] - .append(','); + if (!replaceOldBlock) return position; + + if (isSingleBlock) { + final Timer timer = new Timer(); + timer.schedule(new TimerTask() { + @Override + public void run () { + // we're gonna just only break the block for now + try (final PendingUpdateManager pendingUpdateManager = ((ClientWorldAccessor) world).getPendingUpdateManager()) { + connection.send( + new PlayerActionC2SPacket( + PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, + position, + Direction.UP, + pendingUpdateManager.incrementSequence().getSequence() + ) + ); + } + } + }, 50 * 2); + } else { + final StringBuilder oldBlockString = new StringBuilder(Registries.BLOCK.getId(oldBlockState.getBlock()).toString()); + if (!oldBlockState.getProperties().isEmpty()) { + oldBlockString.append('['); + + for (final Property property : oldBlockState.getProperties()) { + oldBlockString.append(property.getName()) + .append('=') + .append(Util.getValueAsString(property, property.getValues().getLast())) // water[level=15] instead of water[level=0,level=1,....,level=15] + .append(','); + } + + oldBlockString.deleteCharAt(oldBlockString.length() - 1); + + oldBlockString.append(']'); } - oldBlockString.deleteCharAt(oldBlockString.length() - 1); - - oldBlockString.append(']'); + pendingSetBlockCommands.put(position, oldBlockString.toString()); } - pendingSetBlockCommands.put(position, oldBlockString.toString()); + return position; } // also from two five hundred million dollars