From 928dc22a878d9ea95e82dda520cb0392df4435c8 Mon Sep 17 00:00:00 2001 From: Sakura Ryoko Date: Wed, 3 Jul 2024 21:48:56 -0400 Subject: [PATCH] I think I got it ... --- .../tweakeroo/tweaks/PlacementHandler.java | 63 +-------- .../tweakeroo/tweaks/PlacementTweaks.java | 79 ++++++----- .../masa/tweakeroo/util/OrientationUtils.java | 133 ------------------ 3 files changed, 53 insertions(+), 222 deletions(-) delete mode 100644 src/main/java/fi/dy/masa/tweakeroo/util/OrientationUtils.java diff --git a/src/main/java/fi/dy/masa/tweakeroo/tweaks/PlacementHandler.java b/src/main/java/fi/dy/masa/tweakeroo/tweaks/PlacementHandler.java index 436f1cc0f..373721d01 100644 --- a/src/main/java/fi/dy/masa/tweakeroo/tweaks/PlacementHandler.java +++ b/src/main/java/fi/dy/masa/tweakeroo/tweaks/PlacementHandler.java @@ -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; @@ -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 { @@ -184,8 +182,8 @@ else if (block instanceof ComparatorBlock) public static > 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) { @@ -197,7 +195,7 @@ public static > 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) @@ -208,19 +206,6 @@ public static > 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; @@ -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()) { @@ -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); } @@ -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)) { @@ -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; diff --git a/src/main/java/fi/dy/masa/tweakeroo/tweaks/PlacementTweaks.java b/src/main/java/fi/dy/masa/tweakeroo/tweaks/PlacementTweaks.java index b76ab237a..d1de2f598 100644 --- a/src/main/java/fi/dy/masa/tweakeroo/tweaks/PlacementTweaks.java +++ b/src/main/java/fi/dy/masa/tweakeroo/tweaks/PlacementTweaks.java @@ -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; @@ -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); } @@ -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()]); } @@ -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 @@ -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; @@ -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) { @@ -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) { @@ -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(); @@ -547,18 +546,17 @@ 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 { @@ -566,22 +564,18 @@ else if (handleAccurate && isFacingValidForOrientation(facing, stack)) } } - 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); } @@ -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 @@ -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()) { @@ -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; } @@ -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); } @@ -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)) @@ -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); } @@ -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() && @@ -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); } @@ -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); @@ -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 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)) diff --git a/src/main/java/fi/dy/masa/tweakeroo/util/OrientationUtils.java b/src/main/java/fi/dy/masa/tweakeroo/util/OrientationUtils.java deleted file mode 100644 index 2391b41b5..000000000 --- a/src/main/java/fi/dy/masa/tweakeroo/util/OrientationUtils.java +++ /dev/null @@ -1,133 +0,0 @@ -package fi.dy.masa.tweakeroo.util; - -import java.util.Arrays; -import java.util.List; -import net.minecraft.block.BlockState; -import net.minecraft.block.enums.Orientation; -import net.minecraft.item.BlockItem; -import net.minecraft.item.ItemStack; -import net.minecraft.state.property.Properties; -import net.minecraft.util.math.Direction; -import fi.dy.masa.tweakeroo.Tweakeroo; - -public class OrientationUtils -{ - public static Orientation flipFacing(Orientation orig) - { - Direction facing = orig.getFacing(); - Direction rot = orig.getRotation(); - Direction adjusted = facing.getOpposite(); - - return Orientation.byDirections(adjusted, rot); - } - - public static int getFacingIndex(ItemStack stackIn, Direction facing) - { - Tweakeroo.debugLog("getFacingIndex: facing: [{}]", facing.getName()); - - if (stackIn.getItem() instanceof BlockItem blockItem) - { - BlockState defaultState = blockItem.getBlock().getDefaultState(); - - if (defaultState.contains(Properties.ORIENTATION)) - { - List list = Arrays.stream(Orientation.values()).toList(); - - for (int i = 0; i < list.size(); i++) - { - Orientation o = list.get(i); - - Tweakeroo.debugLog("getFacingIndex[{}]: name: [{}]", i, o.asString()); - - if (o.getFacing().equals(facing)) - { - /* - if (facing.equals(Direction.UP) || facing.equals(Direction.DOWN)) - { - // North Rotation. - i++; - } - */ - - return i; - } - } - } - } - - return -1; - } - - public static Orientation flipRotation(Orientation orig) - { - Direction facing = orig.getFacing(); - Direction rot = orig.getRotation(); - Direction adjusted = rot.getOpposite(); - - return Orientation.byDirections(facing, adjusted); - } - - public static Orientation rotateFacingCW(Orientation orig, Direction.Axis axis) - { - Direction facing = orig.getFacing(); - Direction rot = orig.getRotation(); - - try - { - Direction adjusted = facing.rotateClockwise(axis); - return Orientation.byDirections(adjusted, rot); - } - catch (Exception ignored) - { - return orig; - } - } - - public static Orientation rotateRotationCW(Orientation orig, Direction.Axis axis) - { - Direction facing = orig.getFacing(); - Direction rot = orig.getRotation(); - - try - { - Direction adjusted = rot.rotateClockwise(axis); - return Orientation.byDirections(facing, adjusted); - } - catch (Exception ignored) - { - return orig; - } - } - - public static Orientation rotateFacingCCW(Orientation orig, Direction.Axis axis) - { - Direction facing = orig.getFacing(); - Direction rot = orig.getRotation(); - - try - { - Direction adjusted = facing.rotateCounterclockwise(axis); - return Orientation.byDirections(adjusted, rot); - } - catch (Exception ignored) - { - return orig; - } - } - - public static Orientation rotateRotationCCW(Orientation orig, Direction.Axis axis) - { - Direction facing = orig.getFacing(); - Direction rot = orig.getRotation(); - - try - { - Direction adjusted = rot.rotateCounterclockwise(axis); - return Orientation.byDirections(facing, adjusted); - } - catch (Exception ignored) - { - return orig; - } - } -}