feat: use deploy core automatically if core size is a single block

This commit is contained in:
Chayapak Supasakul 2025-10-19 15:00:39 +07:00
parent fa9c2954b3
commit 27757485d1
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
3 changed files with 92 additions and 51 deletions

View file

@ -64,6 +64,8 @@ public class CoreCommand {
final CompletableFuture<NbtCompound> 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) {

View file

@ -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"));

View file

@ -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<BlockPos, String> 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<NbtCompound> 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<NbtCompound> 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,22 +491,36 @@ 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));
if (ChipmunkMod.CONFIG.core.logCommands)
ChipmunkMod.LOGGER.info("Executing place block core command: {}", command);
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('[');
@ -504,6 +540,9 @@ public class CommandCore implements Listener {
pendingSetBlockCommands.put(position, oldBlockString.toString());
}
return position;
}
// also from two five hundred million dollars
private Pair<BlockPos, BlockState> findBlockLocation () {
final ClientPlayerEntity player = client.player;