Skip to content

Commit

Permalink
fix: Easy Place V3 for non-Direction Property blocks, such as Logs.
Browse files Browse the repository at this point in the history
  • Loading branch information
sakura-ryoko committed Dec 17, 2024
1 parent 516de98 commit e0afceb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
32 changes: 18 additions & 14 deletions src/main/java/fi/dy/masa/litematica/util/PlacementHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ public static BlockState applyPlacementProtocolV2(BlockState state, UseContext c

if (property.isPresent())
{
//System.out.printf("applying: 0x%08X (getFirstDirectionProperty() -> %s)\n", protocolValue, property.get().getName());

//System.out.printf("[PHv2] applying: 0x%08X (getFirstDirectionProperty() -> %s)\n", protocolValue, property.get().getName());
state = applyDirectionProperty(state, context, property.get(), protocolValue);

if (state == null)
Expand All @@ -147,10 +146,12 @@ public static BlockState applyPlacementProtocolV2(BlockState state, UseContext c
else if (state.contains(Properties.AXIS))
{
Direction.Axis axis = Direction.Axis.VALUES[((protocolValue >> 1) & 0x3) % 3];
//System.out.printf("[PHv2] applying: 0x%08X (Axis -> %s)\n", protocolValue, axis.getName());

if (Properties.AXIS.getValues().contains(axis))
{
state = state.with(Properties.AXIS, axis);
//System.out.printf("[PHv2] axis stateOut: %s\n", state.toString());
}
}

Expand Down Expand Up @@ -181,15 +182,17 @@ else if (block instanceof ComparatorBlock)
state = state.with(Properties.BLOCK_HALF, protocolValue > 0 ? BlockHalf.TOP : BlockHalf.BOTTOM);
}

//System.out.printf("[PHv2] stateOut: %s\n", state.toString());

return state;
}

public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(BlockState state, UseContext context)
{
int protocolValue = (int) (context.getHitVec().x - (double) context.getPos().getX()) - 2;
BlockState oldState = state;
//System.out.printf("hit vec.x %s, pos.x: %s\n", context.getHitVec().getX(), context.getPos().getX());
//System.out.printf("raw protocol value in: 0x%08X\n", protocolValue);
//System.out.printf("[PHv3] hit vec.x %s, pos.x: %s\n", context.getHitVec().getX(), context.getPos().getX());
//System.out.printf("[PHv3] raw protocol value in: 0x%08X\n", protocolValue);

if (protocolValue < 0)
{
Expand All @@ -201,7 +204,7 @@ public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(Bloc
// DirectionProperty - allow all except: VERTICAL_DIRECTION (PointedDripstone)
if (property.isPresent() && property.get() != Properties.VERTICAL_DIRECTION)
{
//System.out.printf("applying: 0x%08X (getFirstDirectionProperty() -> %s)\n", protocolValue, property.get().getName());
//System.out.printf("[PHv3] applying: 0x%08X (getFirstDirectionProperty() -> %s)\n", protocolValue, property.get().getName());
state = applyDirectionProperty(state, context, property.get(), protocolValue);

if (state == null)
Expand All @@ -211,12 +214,12 @@ public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(Bloc

if (state.canPlaceAt(context.getWorld(), context.getPos()))
{
//System.out.printf("validator passed for \"%s\"\n", property.get().getName());
//System.out.printf("[PHv3] validator passed for \"%s\"\n", property.get().getName());
oldState = state;
}
else
{
//System.out.printf("validator failed for \"%s\"\n", property.get().getName());
//System.out.printf("[PHv3] validator failed for \"%s\"\n", property.get().getName());
state = oldState;
}

Expand All @@ -235,7 +238,8 @@ public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(Bloc
for (Property<?> p : propList)
{
//if (((p instanceof EnumProperty<?> ep) && ep.getType().equals(Direction.class) == false) &&
if (property.isPresent() && !property.get().equals(p) &&
if ((property.isPresent() && !property.get().equals(p)) ||
(property.isEmpty()) &&
WHITELISTED_PROPERTIES.contains(p))
{
@SuppressWarnings("unchecked")
Expand All @@ -246,7 +250,7 @@ public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(Bloc
int requiredBits = MathHelper.floorLog2(MathHelper.smallestEncompassingPowerOfTwo(list.size()));
int bitMask = ~(0xFFFFFFFF << requiredBits);
int valueIndex = protocolValue & bitMask;
//System.out.printf("trying to apply valInd: %d, bits: %d, prot val: 0x%08X [Property %s]\n", valueIndex, requiredBits, protocolValue, prop.getName());
//System.out.printf("[PHv3] trying to apply valInd: %d, bits: %d, prot val: 0x%08X [Property %s]\n", valueIndex, requiredBits, protocolValue, prop.getName());

if (valueIndex >= 0 && valueIndex < list.size())
{
Expand All @@ -255,17 +259,17 @@ public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(Bloc
if (state.get(prop).equals(value) == false &&
value != SlabType.DOUBLE) // don't allow duping slabs by forcing a double slab via the protocol
{
//System.out.printf("applying \"%s\": %s\n", prop.getName(), value);
//System.out.printf("[PHv3] applying \"%s\": %s\n", prop.getName(), value);
state = state.with(prop, value);

if (state.canPlaceAt(context.getWorld(), context.getPos()))
{
//System.out.printf("validator passed for \"%s\"\n", prop.getName());
//System.out.printf("[PHv3] validator passed for \"%s\"\n", prop.getName());
oldState = state;
}
else
{
//System.out.printf("validator failed for \"%s\"\n", prop.getName());
//System.out.printf("[PHv3] validator failed for \"%s\"\n", prop.getName());
state = oldState;
}
}
Expand All @@ -282,12 +286,12 @@ public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(Bloc

if (state.canPlaceAt(context.getWorld(), context.getPos()))
{
//System.out.printf("validator passed for \"%s\"\n", state);
//System.out.printf("[PHv3] validator passed for \"%s\"\n", state);
return state;
}
else
{
//System.out.printf("validator failed for \"%s\"\n", state);
//System.out.printf("[PHv3] validator failed for \"%s\"\n", state);
return null;
}
}
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/fi/dy/masa/litematica/util/WorldUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -589,12 +589,13 @@ private static ActionResult doEasyPlaceAction(MinecraftClient mc)
}
}

//System.out.printf("doEasyPlaceAction - stateSchematic [%s] // sideOrig [%s]", stateSchematic.toString(), sideOrig.getName());
//System.out.printf("doEasyPlaceAction - stateSchematic [%s] // sideOrig [%s]\n", stateSchematic.toString(), sideOrig.getName());

Direction side = applyPlacementFacing(stateSchematic, sideOrig, stateClient);

// Support for special cases
PlacementProtocolData placementData = applyPlacementProtocolAll(pos, stateSchematic, hitPos);

if (placementData.mustFail)
{
return ActionResult.FAIL; //disallowed cases (e.g. trying to place torch with no support block)
Expand Down Expand Up @@ -765,14 +766,19 @@ public static Vec3d applyCarpetProtocolHitVec(BlockPos pos, BlockState state, Ve

if (facing.isPresent())
{
//System.out.printf("(WorldUtils):v2: applying: 0x%08X (getFirstDirectionProperty() -> %s)\n", protocolValue, facing.get().getName());

protocolValue = facing.get().getId();
hasData = true; // without this down rotation would not be detected >_>
}
else if (state.contains(Properties.AXIS))
{
Direction.Axis axis = state.get(Properties.AXIS);
//System.out.printf("(WorldUtils):v2: 0x%08X (current axis %s)\n", protocolValue, axis.getName());

protocolValue = axis.ordinal();
hasData = true; // without this id 0 would not be detected >_>
//System.out.printf("(WorldUtils):v2: axis current state: %s, protocolValue 0x%08X\n", state.toString(), protocolValue);
}

if (block instanceof RepeaterBlock)
Expand All @@ -799,6 +805,8 @@ else if (state.contains(Properties.SLAB_TYPE) && state.get(Properties.SLAB_TYPE)
x += (protocolValue * 2) + 2;
}

//System.out.printf("(WorldUtils):v2: stateIn: %s // Vec3d Out [%s]\n", state.toString(), new Vec3d(x, y, z).toString());

return new Vec3d(x, y, z);
}

Expand Down Expand Up @@ -848,8 +856,8 @@ public static <T extends Comparable<T>> Vec3d applyPlacementProtocolV3(BlockPos
int shiftAmount = 1;
int propCount = 0;

//System.out.printf("(WorldUtils) hit vec.x %s, pos.x: %s\n", hitVecIn.getX(), pos.getX());
//System.out.printf("(WorldUtils) raw protocol value in: 0x%08X\n", protocolValue);
//System.out.printf("(WorldUtils):v3: hit vec.x %s, pos.x: %s\n", hitVecIn.getX(), pos.getX());
//System.out.printf("(WorldUtils):v3: raw protocol value in: 0x%08X\n", protocolValue);

Optional<EnumProperty<Direction>> property = BlockUtils.getFirstDirectionProperty(state);

Expand All @@ -858,7 +866,7 @@ public static <T extends Comparable<T>> Vec3d applyPlacementProtocolV3(BlockPos
{
Direction direction = state.get(property.get());
protocolValue |= direction.getId() << shiftAmount;
//System.out.printf("(WorldUtils) applying: 0x%08X (getFirstDirection %s)\n", protocolValue, property.get().getName());
//System.out.printf("(WorldUtils):v3: applying: 0x%08X (getFirstDirection %s)\n", protocolValue, property.get().getName());
shiftAmount += 3;
++propCount;
}
Expand All @@ -871,7 +879,8 @@ public static <T extends Comparable<T>> Vec3d applyPlacementProtocolV3(BlockPos
for (Property<?> p : propList)
{
//if (((p instanceof EnumProperty<?> ep) && ep.getType().equals(Direction.class) == false) &&
if (property.isPresent() && !property.get().equals(p) &&
if ((property.isPresent() && !property.get().equals(p)) ||
(property.isEmpty()) &&
PlacementHandler.WHITELISTED_PROPERTIES.contains(p))
{
@SuppressWarnings("unchecked")
Expand All @@ -882,11 +891,11 @@ public static <T extends Comparable<T>> Vec3d applyPlacementProtocolV3(BlockPos
int requiredBits = MathHelper.floorLog2(MathHelper.smallestEncompassingPowerOfTwo(list.size()));
int valueIndex = list.indexOf(state.get(prop));

//System.out.printf("(WorldUtils) trying to apply valInd: %d, bits: %d, prot val: 0x%08X [Property %s]\n", valueIndex, requiredBits, protocolValue, prop.getName());
//System.out.printf("(WorldUtils):v3: trying to apply valInd: %d, bits: %d, prot val: 0x%08X [Property %s]\n", valueIndex, requiredBits, protocolValue, prop.getName());

if (valueIndex != -1)
{
//System.out.printf("(WorldUtils) requesting: %s = %s, index: %d\n", prop.getName(), state.get(prop), valueIndex);
//System.out.printf("(WorldUtils):v3: requesting: %s = %s, index: %d\n", prop.getName(), state.get(prop), valueIndex);
protocolValue |= (valueIndex << shiftAmount);
shiftAmount += requiredBits;
++propCount;
Expand All @@ -902,7 +911,7 @@ public static <T extends Comparable<T>> Vec3d applyPlacementProtocolV3(BlockPos
if (propCount > 0)
{
double x = pos.getX() + relX + 2 + protocolValue;
//System.out.printf("(WorldUtils) request prot value 0x%08X\n", protocolValue + 2);
//System.out.printf("(WorldUtils):v3: request prot value 0x%08X\n", protocolValue + 2);
return new Vec3d(x, hitVecIn.y, hitVecIn.z);
}

Expand Down

0 comments on commit e0afceb

Please sign in to comment.