Skip to content

Commit 80a6e9b

Browse files
committed
Fixup some code
1 parent e43d7c1 commit 80a6e9b

5 files changed

Lines changed: 33 additions & 92 deletions

File tree

v1_21_4/src/main/java/me/aleksilassila/litematica/printer/v1_21_4/config/PrinterConfig.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public static PrinterConfig getInstance() {
3636
public static final ConfigBoolean ROTATE_PLAYER = new ConfigBoolean("printerRotatePlayer", false, "Rotate the player to face the block to place.");
3737
public static final ConfigBoolean PRINTER_GRIM_ROTATION = new ConfigBoolean("printerGrimRotate", true, "Allows you to place blocks anywhere while walking or using baritone.");
3838
public static final ConfigBoolean STOP_ON_MOVEMENT = new ConfigBoolean("printerStopOnMovement", false, "Stop the printer if the player velocity is to high.");
39-
public static final ConfigBoolean CARPET_MODE = new ConfigBoolean("printerCarpetMode", true, "Carpet mode. Checks more points on blocks for placement viability. This might be needed for very strict raycast anti cheat checks. Does nothing when raycast is disabled.");
4039
public static final ConfigInteger INVENTORY_DELAY = new ConfigInteger("printerInventoryDelay", 10, 0, 100, "The delay between each inventory action. 0 = no delay.");
4140
public static final ConfigOptionList PRINTER_INVENTORY_MANAGEMENT_MODE = new ConfigOptionList("printerInventoryManagementMode", InventoryManagementModeEnum.LEAST_USED, "Inventory management mode. Rolling = cycle through the hotbar, Least Used = use the least used slot in the hotbar.");
4241
public static final ConfigBoolean RAYCAST = new ConfigBoolean("printerRaycast", false, "Raycast the block to place to check if it is obstructed by another block. Most anti cheat don't check for this.");
@@ -73,7 +72,6 @@ public ImmutableList<IConfigBase> getOptions() {
7372
list.add(ROTATE_PLAYER);
7473
list.add(PRINTER_GRIM_ROTATION);
7574
list.add(STOP_ON_MOVEMENT);
76-
list.add(CARPET_MODE);
7775
list.add(INVENTORY_DELAY);
7876
list.add(PRINTER_INVENTORY_MANAGEMENT_MODE);
7977
list.add(RAYCAST);

v1_21_4/src/main/java/me/aleksilassila/litematica/printer/v1_21_4/guides/placement/FacingBlockGuide.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import net.minecraft.block.enums.BlockHalf;
66
import net.minecraft.state.property.Properties;
77
import net.minecraft.util.math.Direction;
8-
import net.minecraft.util.math.Vec3d;
98

109
import java.util.Arrays;
1110
import java.util.Collections;
@@ -32,18 +31,6 @@ protected List<Direction> getPossibleSides() {
3231
return Arrays.stream(Direction.values()).toList();
3332
}
3433

35-
protected Vec3d[] getPossibleHitVecs() {
36-
Vec3d[] parentVecs = super.getPossibleHitVecs();
37-
Block block = state.targetState.getBlock();
38-
if (!(block instanceof StairsBlock)) {
39-
return parentVecs;
40-
}
41-
42-
Direction half = getRequiredHalf(state);
43-
44-
return Arrays.stream(parentVecs).filter(vec -> half == Direction.DOWN ? vec.y <= 0 : vec.y > 0).toArray(Vec3d[]::new);
45-
}
46-
4734
@Override
4835
public boolean skipOtherGuides() {
4936
return true;

v1_21_4/src/main/java/me/aleksilassila/litematica/printer/v1_21_4/guides/placement/GeneralPlacementGuide.java

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,6 @@
3434
* for player state depending on the block being placed.
3535
*/
3636
public class GeneralPlacementGuide extends PlacementGuide {
37-
protected static Vec3d[] hitVecsToTry = new Vec3d[]{
38-
new Vec3d(-0.25, -0.25, -0.25),
39-
new Vec3d(+0.25, -0.25, -0.25),
40-
new Vec3d(-0.25, +0.25, -0.25),
41-
new Vec3d(-0.25, -0.25, +0.25),
42-
new Vec3d(+0.25, +0.25, -0.25),
43-
new Vec3d(-0.25, +0.25, +0.25),
44-
new Vec3d(+0.25, -0.25, +0.25),
45-
new Vec3d(+0.25, +0.25, +0.25)
46-
};
47-
4837
protected static Vec3d[] extendedHitVecs = new Vec3d[]{
4938
new Vec3d(-0.25, -0.25, -0.25),
5039
new Vec3d(+0.25, -0.25, -0.25),
@@ -122,7 +111,7 @@ private Optional<Direction> getValidSide(SchematicBlockState state) {
122111
}
123112

124113
protected Vec3d[] getPossibleHitVecs() {
125-
return PrinterConfig.CARPET_MODE.getBooleanValue() ? extendedHitVecs : hitVecsToTry;
114+
return extendedHitVecs;
126115
}
127116

128117
private List<Direction> getValidSides(SchematicBlockState state) {
@@ -170,7 +159,7 @@ public boolean canExecute(ClientPlayerEntity player) {
170159
@Override
171160
public @Nullable PrinterPlacementContext getPlacementContext(ClientPlayerEntity player) {
172161
if (PrinterConfig.PRINTER_AIRPLACE.getBooleanValue()) {
173-
return getContextByRotation(player);
162+
return getAirplaceContext(player);
174163
} else {
175164
return getContextByStrictLook(player);
176165
}
@@ -281,16 +270,16 @@ public PrinterPlacementContext getContextByStrictLook(ClientPlayerEntity player)
281270
}
282271

283272
protected static Direction[] directionsToTry = new Direction[]{
273+
Direction.UP,
274+
Direction.DOWN,
284275
Direction.NORTH,
285276
Direction.SOUTH,
286277
Direction.EAST,
287-
Direction.WEST,
288-
Direction.UP,
289-
Direction.DOWN
278+
Direction.WEST
290279
};
291280

292281
@Nullable
293-
public PrinterPlacementContext getContextByRotation(ClientPlayerEntity player) {
282+
public PrinterPlacementContext getAirplaceContext(ClientPlayerEntity player) {
294283
if (contextCache != null && !LitematicaMixinMod.DEBUG) return contextCache;
295284

296285
ItemStack requiredItem = getRequiredItem(player).stream().findFirst().orElse(ItemStack.EMPTY);
@@ -305,27 +294,24 @@ public PrinterPlacementContext getContextByRotation(ClientPlayerEntity player) {
305294
// We'll brute force: sides (including horizontal) + hit positions inside the target block volume
306295
// to emulate realistic clicks that yield correct orientation for special blocks (hoppers, froglights, basalt, slabs).
307296

308-
// Candidate sides to emulate a supporting face. For airplace we pretend there is a block on that side.
309-
Direction[] candidateSides = new Direction[]{
310-
Direction.UP, Direction.DOWN,
311-
Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST
312-
};
313-
314-
// Hit offsets inside block relative to its min corner (0..1). Include lower y (<0.5) for bottom slabs, and center variations.
315-
double[] ySamples = new double[]{0.25, 0.5, 0.75};
316-
double[] xzSamples = new double[]{0.25, 0.5, 0.75};
317-
318297
// First quick attempt: original center (UP side) (fast path)
319298
{
320299
BlockHitResult hr = new BlockHitResult(Vec3d.ofCenter(state.blockPos), Direction.UP, state.blockPos, true);
321300
PrinterPlacementContext quick = new PrinterPlacementContext(player, hr, requiredItem, slot, null, false);
322301
BlockState res = getRequiredItemAsBlock(player).orElse(targetState.getBlock()).getPlacementState(quick);
323302
if (res != null && correctObserverPlacement(targetState, res) && statesEqual(res, targetState)) {
324-
contextCache = quick; quick.isAirPlace = true; return quick; }
303+
contextCache = quick;
304+
quick.isAirPlace = true;
305+
return quick;
306+
}
325307
}
326308

309+
// Hit offsets inside block relative to its min corner (0..1). Include lower y (<0.5) for bottom slabs, and center variations.
310+
double[] ySamples = new double[]{0.25, 0.5, 0.75};
311+
double[] xzSamples = new double[]{0.25, 0.5, 0.75};
312+
327313
for (Direction lookDirection : directionsToTry) {
328-
for (Direction side : candidateSides) {
314+
for (Direction side : getPossibleSides()) {
329315
// Neighbor position (imaginary supporting block). For DOWN side we offset below, etc.
330316
BlockPos neighborPos = state.blockPos.offset(side);
331317

@@ -338,22 +324,24 @@ public PrinterPlacementContext getContextByRotation(ClientPlayerEntity player) {
338324
Vec3d hitVec = Vec3d.of(state.blockPos).add(sample);
339325

340326
// Ensure the hitVec lies on the correct face for the side: project coordinate component to face plane center
341-
switch (side) {
342-
case UP: hitVec = new Vec3d(hitVec.x, state.blockPos.getY() + 1 - 1e-4, hitVec.z); break;
343-
case DOWN: hitVec = new Vec3d(hitVec.x, state.blockPos.getY() + 1e-4, hitVec.z); break;
344-
case NORTH: hitVec = new Vec3d(hitVec.x, hitVec.y, state.blockPos.getZ() + 1e-4); break;
345-
case SOUTH: hitVec = new Vec3d(hitVec.x, hitVec.y, state.blockPos.getZ() + 1 - 1e-4); break;
346-
case WEST: hitVec = new Vec3d(state.blockPos.getX() + 1e-4, hitVec.y, hitVec.z); break;
347-
case EAST: hitVec = new Vec3d(state.blockPos.getX() + 1 - 1e-4, hitVec.y, hitVec.z); break;
348-
}
327+
hitVec = switch (side) {
328+
case UP -> new Vec3d(hitVec.x, state.blockPos.getY() + 1 - 1e-4, hitVec.z);
329+
case DOWN -> new Vec3d(hitVec.x, state.blockPos.getY() + 1e-4, hitVec.z);
330+
case NORTH -> new Vec3d(hitVec.x, hitVec.y, state.blockPos.getZ() + 1e-4);
331+
case SOUTH -> new Vec3d(hitVec.x, hitVec.y, state.blockPos.getZ() + 1 - 1e-4);
332+
case WEST -> new Vec3d(state.blockPos.getX() + 1e-4, hitVec.y, hitVec.z);
333+
case EAST -> new Vec3d(state.blockPos.getX() + 1 - 1e-4, hitVec.y, hitVec.z);
334+
};
349335

350336
BlockHitResult hitResult = new BlockHitResult(hitVec, side.getOpposite(), neighborPos, false);
351337
PrinterPlacementContext context = new PrinterPlacementContext(player, hitResult, requiredItem, slot, lookDirection, false);
352338
BlockState result = getRequiredItemAsBlock(player)
353339
.orElse(targetState.getBlock())
354340
.getPlacementState(context);
355341
if (result != null && correctObserverPlacement(targetState, result) && statesEqual(result, targetState)) {
356-
contextCache = context; context.isAirPlace = true; return context;
342+
contextCache = context;
343+
context.isAirPlace = true;
344+
return context;
357345
}
358346
}
359347
}
Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
package me.aleksilassila.litematica.printer.v1_21_4.guides.placement;
22

33
import me.aleksilassila.litematica.printer.v1_21_4.SchematicBlockState;
4-
import me.aleksilassila.litematica.printer.v1_21_4.config.PrinterConfig;
54
import net.minecraft.block.Block;
6-
import net.minecraft.block.BlockState;
75
import net.minecraft.block.HorizontalFacingBlock;
86
import net.minecraft.block.WallMountedBlock;
9-
import net.minecraft.state.property.Properties;
107
import net.minecraft.client.network.ClientPlayerEntity;
118
import net.minecraft.util.math.Direction;
129

1310
import java.util.Collections;
1411
import java.util.List;
1512
import java.util.Optional;
1613

17-
public class TorchGuide extends GuesserGuide {
14+
public class TorchGuide extends PropertySpecificGuesserGuide {
1815
public TorchGuide(SchematicBlockState state) {
1916
super(state);
2017
}
@@ -34,19 +31,4 @@ protected List<Direction> getPossibleSides() {
3431
protected Optional<Block> getRequiredItemAsBlock(ClientPlayerEntity player) {
3532
return Optional.of(state.targetState.getBlock());
3633
}
37-
38-
@Override
39-
protected boolean statesEqual(BlockState state1, BlockState state2) {
40-
// Redstone torches use LIT which can be false if powered; ignore it to allow placement
41-
if (PrinterConfig.PRINTER_IGNORE_ROTATION.getBooleanValue()) {
42-
var rotationProps = PropertySpecificGuesserGuide.rotationProperties;
43-
// Merge rotation props with LIT
44-
net.minecraft.state.property.Property<?>[] merged = new net.minecraft.state.property.Property<?>[rotationProps.length + 1];
45-
System.arraycopy(rotationProps, 0, merged, 0, rotationProps.length);
46-
merged[rotationProps.length] = Properties.LIT;
47-
return statesEqualIgnoreProperties(state1, state2, merged);
48-
} else {
49-
return statesEqualIgnoreProperties(state1, state2, Properties.LIT);
50-
}
51-
}
5234
}

v1_21_4/src/main/java/me/aleksilassila/litematica/printer/v1_21_4/implementation/actions/AirPlaceAction.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import me.aleksilassila.litematica.printer.v1_21_4.LitematicaMixinMod;
44
import me.aleksilassila.litematica.printer.v1_21_4.actions.InteractAction;
5-
import me.aleksilassila.litematica.printer.v1_21_4.config.PrinterConfig;
65
import me.aleksilassila.litematica.printer.v1_21_4.implementation.PrinterPlacementContext;
76
import net.minecraft.block.BlockState;
87
import net.minecraft.block.FluidBlock;
@@ -21,7 +20,6 @@
2120
import net.minecraft.util.hit.BlockHitResult;
2221
import net.minecraft.util.math.BlockPos;
2322
import net.minecraft.util.math.Direction;
24-
import net.minecraft.util.math.Vec3d;
2523

2624
public class AirPlaceAction extends InteractAction {
2725
private final MinecraftClient mc = MinecraftClient.getInstance();
@@ -33,18 +31,15 @@ public AirPlaceAction(PrinterPlacementContext context) {
3331
protected ActionResult interact(MinecraftClient client, ClientPlayerEntity player, Hand hand, BlockHitResult hitResult) {
3432
BlockPos pos = hitResult.isInsideBlock() ? hitResult.getBlockPos() : hitResult.getBlockPos().offset(hitResult.getSide());
3533
BlockState currentState = mc.world.getBlockState(pos);
36-
34+
3735
// Allow placement in air or in fluids when REPLACE_FLUIDS_SOURCE_BLOCKS is enabled
38-
boolean canPlace = currentState.isAir() ||
39-
(LitematicaMixinMod.REPLACE_FLUIDS_SOURCE_BLOCKS.getBooleanValue() &&
36+
boolean canPlace = currentState.isAir() ||
37+
(LitematicaMixinMod.REPLACE_FLUIDS_SOURCE_BLOCKS.getBooleanValue() &&
4038
currentState.getBlock() instanceof FluidBlock);
41-
39+
4240
if (!canPlace) {
43-
if (PrinterConfig.PRINTER_DEBUG_LOG.getBooleanValue())
44-
System.out.println("InteractActionImpl.interact: block is not air and fluid replacement is disabled");
4541
return ActionResult.FAIL;
4642
}
47-
if (PrinterConfig.PRINTER_DEBUG_LOG.getBooleanValue()) System.out.println("InteractActionImpl.interact: attempting to air place block");
4843
airPlace(pos);
4944
return ActionResult.PASS;
5045
}
@@ -60,17 +55,8 @@ private void airPlace(BlockPos pos) {
6055
connection.sendPacket(new PlayerActionC2SPacket(PlayerActionC2SPacket.Action.SWAP_ITEM_WITH_OFFHAND, BlockPos.ORIGIN, Direction.DOWN));
6156

6257
Hand hand = Hand.OFF_HAND;
63-
// Build a synthetic hit result anchored on the target block position (inside-block) but preserving
64-
// the sampled side & hit vector for orientation. This avoids accidentally clicking a real neighbor
65-
// and placing a block one block ahead (issue seen with hoppers & slabs).
66-
BlockHitResult original = this.context.hitResult;
67-
BlockHitResult placeHit;
68-
if (original != null) {
69-
placeHit = new BlockHitResult(original.getPos(), original.getSide(), pos, true);
70-
} else {
71-
placeHit = new BlockHitResult(Vec3d.ofCenter(pos), Direction.DOWN, pos, true);
72-
}
73-
interactionManager.interactBlock(mc.player, hand, placeHit);
58+
BlockHitResult blockHitResult = new BlockHitResult(this.context.hitResult.getPos(), this.context.hitResult.getSide(), pos, true);
59+
interactionManager.interactBlock(mc.player, hand, blockHitResult);
7460
mc.player.swingHand(Hand.MAIN_HAND, false);
7561
connection.sendPacket(new HandSwingC2SPacket(hand));
7662
connection.sendPacket(new PlayerActionC2SPacket(PlayerActionC2SPacket.Action.SWAP_ITEM_WITH_OFFHAND, BlockPos.ORIGIN, Direction.DOWN));

0 commit comments

Comments
 (0)