Skip to content

Commit

Permalink
iterative push
Browse files Browse the repository at this point in the history
  • Loading branch information
sakura-ryoko committed Jul 4, 2024
1 parent 2161b07 commit 83b73cd
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 11 deletions.
53 changes: 52 additions & 1 deletion src/main/java/fi/dy/masa/tweakeroo/tweaks/PlacementHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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 @@ -25,6 +26,7 @@
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 @@ -195,7 +197,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("applying: 0x%08X\n", protocolValue);
System.out.printf("Direction applying: 0x%08X\n", protocolValue);
state = applyDirectionProperty(state, context, property, protocolValue);

if (state == null)
Expand All @@ -206,6 +208,19 @@ 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 Down Expand Up @@ -295,6 +310,42 @@ 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
37 changes: 33 additions & 4 deletions src/main/java/fi/dy/masa/tweakeroo/tweaks/PlacementTweaks.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package fi.dy.masa.tweakeroo.tweaks;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.enums.Orientation;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.network.ClientPlayerInteractionManager;
Expand Down Expand Up @@ -544,20 +547,33 @@ 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 {}", x, posNew.getX(), relX);
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);

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

Tweakeroo.debugLog("accurate[1]: x {} // posNew.getX {} // relX {}", x, posNew.getX(), relX);
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 {}", x, posNew.getX(), relX);
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);
Expand Down Expand Up @@ -758,7 +774,7 @@ private static ActionResult processRightClickBlockWrapper(
// Carpet-Extra mod accurate block placement protocol support
if (flexible && rotation && accurate == false &&
Configs.Generic.ACCURATE_PLACEMENT_PROTOCOL.getBooleanValue() &&
isFacingValidFor(facing, stackOriginal))
(isFacingValidFor(facing, stackOriginal) || isFacingValidForOrientation(facing, stackOriginal)))
{
facing = facing.getOpposite(); // go from block face to click on to the requested facing
//double relX = hitVecIn.x - posIn.getX();
Expand Down Expand Up @@ -1054,6 +1070,19 @@ private static boolean isFacingValidFor(Direction facing, ItemStack stack)
return ((DirectionProperty) prop).getValues().contains(facing);
}
}
}

return false;
}

private static boolean isFacingValidForOrientation(Direction facing, ItemStack stack)
{
Item item = stack.getItem();

if (stack.isEmpty() == false && item instanceof BlockItem)
{
Block block = ((BlockItem) item).getBlock();
BlockState state = block.getDefaultState();

return state.contains(Properties.ORIENTATION);
}
Expand Down
56 changes: 50 additions & 6 deletions src/main/java/fi/dy/masa/tweakeroo/util/OrientationUtils.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
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 Orientation flipFacing(Orientation orig)
public static Orientation flipFacing(Orientation orig)
{
Direction facing = orig.getFacing();
Direction rot = orig.getRotation();
Expand All @@ -14,7 +21,44 @@ public Orientation flipFacing(Orientation orig)
return Orientation.byDirections(adjusted, rot);
}

public Orientation flipRotation(Orientation orig)
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<Orientation> 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();
Expand All @@ -23,7 +67,7 @@ public Orientation flipRotation(Orientation orig)
return Orientation.byDirections(facing, adjusted);
}

public Orientation rotateFacingCW(Orientation orig, Direction.Axis axis)
public static Orientation rotateFacingCW(Orientation orig, Direction.Axis axis)
{
Direction facing = orig.getFacing();
Direction rot = orig.getRotation();
Expand All @@ -39,7 +83,7 @@ public Orientation rotateFacingCW(Orientation orig, Direction.Axis axis)
}
}

public Orientation rotateRotationCW(Orientation orig, Direction.Axis axis)
public static Orientation rotateRotationCW(Orientation orig, Direction.Axis axis)
{
Direction facing = orig.getFacing();
Direction rot = orig.getRotation();
Expand All @@ -55,7 +99,7 @@ public Orientation rotateRotationCW(Orientation orig, Direction.Axis axis)
}
}

public Orientation rotateFacingCCW(Orientation orig, Direction.Axis axis)
public static Orientation rotateFacingCCW(Orientation orig, Direction.Axis axis)
{
Direction facing = orig.getFacing();
Direction rot = orig.getRotation();
Expand All @@ -71,7 +115,7 @@ public Orientation rotateFacingCCW(Orientation orig, Direction.Axis axis)
}
}

public Orientation rotateRotationCCW(Orientation orig, Direction.Axis axis)
public static Orientation rotateRotationCCW(Orientation orig, Direction.Axis axis)
{
Direction facing = orig.getFacing();
Direction rot = orig.getRotation();
Expand Down

0 comments on commit 83b73cd

Please sign in to comment.