Skip to content

Commit

Permalink
I think I got it ...
Browse files Browse the repository at this point in the history
  • Loading branch information
sakura-ryoko committed Jul 4, 2024
1 parent 83b73cd commit 928dc22
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 222 deletions.
63 changes: 6 additions & 57 deletions src/main/java/fi/dy/masa/tweakeroo/tweaks/PlacementHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.minecraft.block.*;
import net.minecraft.block.enums.BlockHalf;
import net.minecraft.block.enums.ComparatorMode;
import net.minecraft.block.enums.Orientation;
import net.minecraft.block.enums.SlabType;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.LivingEntity;
Expand All @@ -26,7 +25,6 @@
import fi.dy.masa.tweakeroo.config.Configs;
import fi.dy.masa.tweakeroo.data.DataManager;
import fi.dy.masa.tweakeroo.util.EasyPlacementProtocol;
import fi.dy.masa.tweakeroo.util.OrientationUtils;

public class PlacementHandler
{
Expand Down Expand Up @@ -184,8 +182,8 @@ else if (block instanceof ComparatorBlock)
public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(BlockState state, UseContext context)
{
int protocolValue = (int) (context.getHitVec().x - (double) context.getPos().getX()) - 2;
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("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);

if (protocolValue < 0)
{
Expand All @@ -197,7 +195,7 @@ public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(Bloc
// DirectionProperty - allow all except: VERTICAL_DIRECTION (PointedDripstone)
if (property != null && property != Properties.VERTICAL_DIRECTION)
{
System.out.printf("Direction applying: 0x%08X\n", protocolValue);
//System.out.printf("applying: 0x%08X\n", protocolValue);
state = applyDirectionProperty(state, context, property, protocolValue);

if (state == null)
Expand All @@ -208,19 +206,6 @@ public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(Bloc
// Consume the bits used for the facing
protocolValue >>>= 3;
}
else if (state.contains(Properties.ORIENTATION))
{
System.out.printf("Orientation applying: 0x%08X\n", protocolValue);
state = applyOrientationProperty(state, context, protocolValue);

if (state == null)
{
return null;
}

// Consume the bits used for the facing
protocolValue >>>= 3;
}
// Consume the lowest unused bit
protocolValue >>>= 1;

Expand All @@ -242,7 +227,7 @@ else if (state.contains(Properties.ORIENTATION))
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\n", valueIndex, requiredBits, protocolValue);
//System.out.printf("trying to apply valInd: %d, bits: %d, prot val: 0x%08X\n", valueIndex, requiredBits, protocolValue);

if (valueIndex >= 0 && valueIndex < list.size())
{
Expand All @@ -251,7 +236,7 @@ else if (state.contains(Properties.ORIENTATION))
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("applying %s: %s\n", prop.getName(), value);
state = state.with(prop, value);
}

Expand Down Expand Up @@ -289,7 +274,7 @@ else if (decodedFacingIndex >= 0 && decodedFacingIndex <= 5)
}
}

System.out.printf("plop facing: %s -> %s (raw: %d, dec: %d)\n", facingOrig, facing, protocolValue, decodedFacingIndex);
//System.out.printf("plop facing: %s -> %s (raw: %d, dec: %d)\n", facingOrig, facing, protocolValue, decodedFacingIndex);

if (facing != facingOrig && property.getValues().contains(facing))
{
Expand All @@ -310,42 +295,6 @@ else if (decodedFacingIndex >= 0 && decodedFacingIndex <= 5)
return state;
}

private static BlockState applyOrientationProperty(BlockState state, UseContext context,
int protocolValue)
{
Orientation orig = state.get(Properties.ORIENTATION);

if (orig == null)
{
return null;
}

Tweakeroo.debugLog("applyOrientationProperty(): orig: [{}]", orig.asString());

Direction facingOrig = orig.getFacing();
Orientation orientation = orig;
int decodedFacingIndex = (protocolValue & 0xF) >> 1;

if (decodedFacingIndex == 12) // the opposite of the normal facing requested
{
orientation = OrientationUtils.flipFacing(orig);
}
else if (decodedFacingIndex >= 0 && decodedFacingIndex <= 11)
{
Orientation[] values = Orientation.values();
orientation = values[decodedFacingIndex];
}

System.out.printf("plop orientation: F: %s -> O: %s (raw: %d, dec: %d)\n", facingOrig.getName(), orientation.asString(), protocolValue, decodedFacingIndex);

if (orientation != orig)
{
state = state.with(Properties.ORIENTATION, orientation);
}

return state;
}

public static class UseContext
{
private final World world;
Expand Down
79 changes: 47 additions & 32 deletions src/main/java/fi/dy/masa/tweakeroo/tweaks/PlacementTweaks.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import fi.dy.masa.malilib.util.PositionUtils.HitPart;
import fi.dy.masa.malilib.util.restrictions.BlockRestriction;
import fi.dy.masa.malilib.util.restrictions.ItemRestriction;
import fi.dy.masa.tweakeroo.Tweakeroo;
import fi.dy.masa.tweakeroo.config.Configs;
import fi.dy.masa.tweakeroo.config.FeatureToggle;
import fi.dy.masa.tweakeroo.config.Hotkeys;
Expand Down Expand Up @@ -121,7 +120,7 @@ public static boolean onProcessRightClickPre(PlayerEntity player, Hand hand)
{
if (isEmulatedClick == false)
{
System.out.printf("onProcessRightClickPre storing stack: %s\n", stackOriginal);
//System.out.printf("onProcessRightClickPre storing stack: %s\n", stackOriginal);
cacheStackInHand(hand);
}

Expand All @@ -136,7 +135,7 @@ public static boolean onProcessRightClickPre(PlayerEntity player, Hand hand)

public static void onProcessRightClickPost(PlayerEntity player, Hand hand)
{
System.out.printf("onProcessRightClickPost -> tryRestockHand with: %s, current: %s\n", stackBeforeUse[hand.ordinal()], player.getStackInHand(hand));
//System.out.printf("onProcessRightClickPost -> tryRestockHand with: %s, current: %s\n", stackBeforeUse[hand.ordinal()], player.getStackInHand(hand));
tryRestockHand(player, hand, stackBeforeUse[hand.ordinal()]);
}

Expand Down Expand Up @@ -350,7 +349,7 @@ public static ActionResult onProcessRightClickBlock(
return ActionResult.PASS;
}

System.out.printf("onProcessRightClickBlock() pos: %s, side: %s, part: %s, hitVec: %s\n", posIn, sideIn, hitPart, hitVec);
//System.out.printf("onProcessRightClickBlock() pos: %s, side: %s, part: %s, hitVec: %s\n", posIn, sideIn, hitPart, hitVec);
ActionResult result = tryPlaceBlock(controller, player, world, posIn, sideIn, sideRotated, yaw, hitVec, hand, hitPart, true);

// Store the initial click data for the fast placement mode
Expand All @@ -376,7 +375,7 @@ public static ActionResult onProcessRightClickBlock(
sideRotatedFirst = sideRotated;
playerYawFirst = yaw;
stackBeforeUse[hand.ordinal()] = stackPre;
System.out.printf("plop store @ %s\n", posFirst);
//System.out.printf("plop store @ %s\n", posFirst);
}

return result;
Expand Down Expand Up @@ -490,7 +489,7 @@ private static ActionResult tryPlaceBlock(
{
facing = facing.getOpposite();
}
System.out.printf("accurate - IN - facingOrig: %s, facingNew: %s\n", facing, facing.getOpposite());
//System.out.printf("accurate - IN - facingOrig: %s, facingNew: %s\n", facing, facing.getOpposite());
}
else if (flexible == false || rotation == false)
{
Expand All @@ -515,7 +514,7 @@ else if (flexible == false || rotation == false)
}

Direction facingTmp = BlockUtils.getFirstPropertyFacingValue(state);
//System.out.printf("accurate - sideIn: %s, state: %s, hit: %s, f: %s, posNew: %s\n", sideIn, state, hitVec, EnumFacing.getDirectionFromEntityLiving(posIn, player), posNew);
////System.out.printf("accurate - sideIn: %s, state: %s, hit: %s, f: %s, posNew: %s\n", sideIn, state, hitVec, EnumFacing.getDirectionFromEntityLiving(posIn, player), posNew);

if (facingTmp != null)
{
Expand All @@ -530,7 +529,7 @@ else if (flexible == false || rotation == false)

if (accurateReverse)
{
System.out.printf("accurate - REVERSE - facingOrig: %s, facingNew: %s\n", facing, facing.getOpposite());
//System.out.printf("accurate - REVERSE - facingOrig: %s, facingNew: %s\n", facing, facing.getOpposite());
if (accurateIn || flexible == false || rotation == false)
{
facing = facing.getOpposite();
Expand All @@ -547,41 +546,36 @@ else if (flexible == false || rotation == false)
double x = hitVec.x;
int afterClickerClickCount = MathHelper.clamp(Configs.Generic.AFTER_CLICKER_CLICK_COUNT.getIntegerValue(), 0, 32);

Tweakeroo.debugLog("accurate[0]: x {} // posNew.getX {} // relX {} // FACING [{}]", x, posNew.getX(), relX, facing.getName());
if (handleAccurate && isFacingValidFor(facing, stack))
{
x = posNew.getX() + relX + 2 + (facing.getId() * 2);
}
else if (handleAccurate && isFacingValidForOrientation(facing, stack))
{
int facingIndex = OrientationUtils.getFacingIndex(stack, facing);
int facingIndex = getOrientationFacingIndex(stack, facing);

if (facingIndex > 0)
{
x = posNew.getX() + relX + 2 + (facingIndex);
x = posNew.getX() + relX + 2 + (facingIndex * 2);
}
else
{
x = posNew.getX() + relX + 2 + (facing.getId() * 2);
}
}

Tweakeroo.debugLog("accurate[1]: x {} // posNew.getX {} // relX {} // FACING [{}]", x, posNew.getX(), relX, facing.getName());

if (afterClicker)
{
x += afterClickerClickCount * 16;
}

Tweakeroo.debugLog("accurate[2]: x {} // posNew.getX {} // relX {} // FACING [{}]", x, posNew.getX(), relX, facing.getName());

System.out.printf("accurate - pre hitVec: %s\n", hitVec);
System.out.printf("processRightClickBlockWrapper facing: %s, x: %.3f, pos: %s, side: %s\n", facing, x, posNew, side);
//System.out.printf("accurate - pre hitVec: %s\n", hitVec);
//System.out.printf("processRightClickBlockWrapper facing: %s, x: %.3f, pos: %s, side: %s\n", facing, x, posNew, side);
hitVec = new Vec3d(x, hitVec.y, hitVec.z);
System.out.printf("accurate - post hitVec: %s\n", hitVec);
//System.out.printf("accurate - post hitVec: %s\n", hitVec);
}

System.out.printf("accurate - facing: %s, side: %s, posNew: %s, hit: %s\n", facing, side, posNew, hitVec);
//System.out.printf("accurate - facing: %s, side: %s, posNew: %s, hit: %s\n", facing, side, posNew, hitVec);
return processRightClickBlockWrapper(controller, player, world, posNew, side, hitVec, hand);
}

Expand All @@ -592,7 +586,7 @@ else if (handleAccurate && isFacingValidForOrientation(facing, stack))

if (canPlaceBlockIntoPosition(world, posNew, ctx))
{
System.out.printf("tryPlaceBlock() pos: %s, side: %s, part: %s, hitVec: %s\n", posNew, side, hitPart, hitVec);
//System.out.printf("tryPlaceBlock() pos: %s, side: %s, part: %s, hitVec: %s\n", posNew, side, hitPart, hitVec);
return handleFlexibleBlockPlacement(controller, player, world, posNew, side, playerYaw, hitVec, hand, hitPart);
}
else
Expand Down Expand Up @@ -711,7 +705,7 @@ private static ActionResult processRightClickBlockWrapper(
Vec3d hitVecIn,
Hand hand)
{
System.out.printf("processRightClickBlockWrapper() start @ %s, side: %s, hand: %s\n", posIn, sideIn, hand);
//System.out.printf("processRightClickBlockWrapper() start @ %s, side: %s, hand: %s\n", posIn, sideIn, hand);
if (FeatureToggle.TWEAK_PLACEMENT_LIMIT.getBooleanValue() &&
placementCount >= Configs.Generic.PLACEMENT_LIMIT.getIntegerValue())
{
Expand Down Expand Up @@ -756,7 +750,7 @@ private static ActionResult processRightClickBlockWrapper(

if (posFirst != null && isPositionAllowedByPlacementRestriction(posIn, sideIn) == false)
{
System.out.printf("processRightClickBlockWrapper() PASS @ %s, side: %s\n", posIn, sideIn);
//System.out.printf("processRightClickBlockWrapper() PASS @ %s, side: %s\n", posIn, sideIn);
return ActionResult.PASS;
}

Expand All @@ -781,15 +775,12 @@ private static ActionResult processRightClickBlockWrapper(
//double x = posIn.getX() + relX + 2 + (facing.getId() * 2);
double x = posIn.getX() + 2 + (facing.getId() * 2);

Tweakeroo.debugLog("processRightClickBlockWrapper[0]: x {} // posIn.getX {}", x, posIn.getX());
if (FeatureToggle.TWEAK_AFTER_CLICKER.getBooleanValue())
{
x += afterClickerClickCount * 16;
}

Tweakeroo.debugLog("processRightClickBlockWrapper[1]: x {} // posIn.getX {}", x, posIn.getX());

System.out.printf("processRightClickBlockWrapper req facing: %s, x: %.3f, pos: %s, sideIn: %s\n", facing, x, posIn, sideIn);
//System.out.printf("processRightClickBlockWrapper req facing: %s, x: %.3f, pos: %s, sideIn: %s\n", facing, x, posIn, sideIn);
hitVecIn = new Vec3d(x, hitVecIn.y, hitVecIn.z);
}

Expand All @@ -812,7 +803,7 @@ private static ActionResult processRightClickBlockWrapper(

InventoryUtils.trySwapCurrentToolIfNearlyBroken();

System.out.printf("processRightClickBlockWrapper() pos: %s, side: %s, hitVec: %s\n", posIn, sideIn, hitVecIn);
//System.out.printf("processRightClickBlockWrapper() pos: %s, side: %s, hitVec: %s\n", posIn, sideIn, hitVecIn);
ActionResult result;

if (InventoryUtils.canUnstackingItemNotFitInInventory(stackOriginal, player))
Expand All @@ -821,9 +812,8 @@ private static ActionResult processRightClickBlockWrapper(
}
else
{
System.out.printf("processRightClickBlockWrapper() PLACE @ %s, side: %s, hit: %s\n", posIn, sideIn, hitVecIn);
//System.out.printf("processRightClickBlockWrapper() PLACE @ %s, side: %s, hit: %s\n", posIn, sideIn, hitVecIn);
BlockHitResult context = new BlockHitResult(hitVecIn, sideIn, posIn, false);
Tweakeroo.debugLog("processRightClickBlockWrapper(): BHR pos: {}, side {}, type: {}, inside {}", context.getBlockPos().toShortString(), context.getSide().toString(), context.getType().toString(), context.isInsideBlock());
result = controller.interactBlock(player, hand, context);
}

Expand All @@ -834,7 +824,7 @@ private static ActionResult processRightClickBlockWrapper(

// This restock needs to happen even with the pick-before-place tweak active,
// otherwise the fast placement mode's checks (getHandWithItem()) will fail...
System.out.printf("processRightClickBlockWrapper -> tryRestockHand with: %s, current: %s\n", stackOriginal, player.getStackInHand(hand));
//System.out.printf("processRightClickBlockWrapper -> tryRestockHand with: %s, current: %s\n", stackOriginal, player.getStackInHand(hand));
tryRestockHand(player, hand, stackOriginal);

if (FeatureToggle.TWEAK_AFTER_CLICKER.getBooleanValue() &&
Expand All @@ -844,7 +834,7 @@ private static ActionResult processRightClickBlockWrapper(
// TODO --> Add EasyPlacement handling?
for (int i = 0; i < afterClickerClickCount; i++)
{
System.out.printf("processRightClickBlockWrapper() after-clicker - i: %d, pos: %s, side: %s, hitVec: %s\n", i, posPlacement, sideIn, hitVecIn);
//System.out.printf("processRightClickBlockWrapper() after-clicker - i: %d, pos: %s, side: %s, hitVec: %s\n", i, posPlacement, sideIn, hitVecIn);
BlockHitResult context = new BlockHitResult(hitVecIn, sideIn, posPlacement, false);
result = controller.interactBlock(player, hand, context);
}
Expand Down Expand Up @@ -908,7 +898,7 @@ else if (hitPart == HitPart.RIGHT)
player.setYaw(yaw);
player.networkHandler.sendPacket(new PlayerMoveC2SPacket.LookAndOnGround(yaw, pitch, player.isOnGround()));

System.out.printf("handleFlexibleBlockPlacement() pos: %s, side: %s, facing orig: %s facing new: %s\n", pos, side, facingOrig, facing);
//System.out.printf("handleFlexibleBlockPlacement() pos: %s, side: %s, facing orig: %s facing new: %s\n", pos, side, facingOrig, facing);
ActionResult result = processRightClickBlockWrapper(controller, player, world, pos, side, hitVec, hand);

player.setYaw(yawOrig);
Expand Down Expand Up @@ -1090,6 +1080,31 @@ private static boolean isFacingValidForOrientation(Direction facing, ItemStack s
return false;
}

private static int getOrientationFacingIndex(ItemStack stackIn, Direction facing)
{
if (stackIn.getItem() instanceof BlockItem blockItem)
{
BlockState defaultState = blockItem.getBlock().getDefaultState();

if (defaultState.contains(Properties.ORIENTATION))
{
List<Orientation> list = Arrays.stream(Orientation.values()).toList();

for (int i = 0; i < list.size(); i++)
{
Orientation o = list.get(i);

if (o.getFacing().equals(facing))
{
return i;
}
}
}
}

return -1;
}

private static BlockPos getPlacementPositionForTargetedPosition(World world, BlockPos pos, Direction side, ItemPlacementContext useContext)
{
if (canPlaceBlockIntoPosition(world, pos, useContext))
Expand Down
Loading

0 comments on commit 928dc22

Please sign in to comment.