Pick a better side

- Pass local coordinates to blocks when calling pick() in identify mode
This commit is contained in:
simibubi 2022-10-07 19:47:22 +02:00
parent 7cd149ba06
commit 959aa4afbd
2 changed files with 14 additions and 14 deletions

View file

@ -35,7 +35,6 @@ import com.simibubi.create.foundation.ponder.ui.PonderUI;
import com.simibubi.create.foundation.render.ForcedDiffuseState; import com.simibubi.create.foundation.render.ForcedDiffuseState;
import com.simibubi.create.foundation.render.SuperRenderTypeBuffer; import com.simibubi.create.foundation.render.SuperRenderTypeBuffer;
import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.BlockFace;
import com.simibubi.create.foundation.utility.Pair; import com.simibubi.create.foundation.utility.Pair;
import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.animation.LerpedFloat; import com.simibubi.create.foundation.utility.animation.LerpedFloat;
@ -135,14 +134,14 @@ public class PonderScene {
} }
public Pair<ItemStack, BlockPos> rayTraceScene(Vec3 from, Vec3 to) { public Pair<ItemStack, BlockPos> rayTraceScene(Vec3 from, Vec3 to) {
MutableObject<Pair<WorldSectionElement, Pair<Vec3, BlockFace>>> nearestHit = new MutableObject<>(); MutableObject<Pair<WorldSectionElement, Pair<Vec3, BlockHitResult>>> nearestHit = new MutableObject<>();
MutableDouble bestDistance = new MutableDouble(0); MutableDouble bestDistance = new MutableDouble(0);
forEach(WorldSectionElement.class, wse -> { forEach(WorldSectionElement.class, wse -> {
wse.resetSelectedBlock(); wse.resetSelectedBlock();
if (!wse.isVisible()) if (!wse.isVisible())
return; return;
Pair<Vec3, BlockFace> rayTrace = wse.rayTrace(world, from, to); Pair<Vec3, BlockHitResult> rayTrace = wse.rayTrace(world, from, to);
if (rayTrace == null) if (rayTrace == null)
return; return;
double distanceTo = rayTrace.getFirst() double distanceTo = rayTrace.getFirst()
@ -157,13 +156,10 @@ public class PonderScene {
if (nearestHit.getValue() == null) if (nearestHit.getValue() == null)
return Pair.of(ItemStack.EMPTY, null); return Pair.of(ItemStack.EMPTY, null);
Pair<Vec3, BlockFace> selectedHit = nearestHit.getValue() Pair<Vec3, BlockHitResult> selectedHit = nearestHit.getValue()
.getSecond(); .getSecond();
BlockPos selectedPos = selectedHit.getSecond() BlockPos selectedPos = selectedHit.getSecond()
.getPos(); .getBlockPos();
Direction selectedFace = selectedHit.getSecond()
.getFace();
Vec3 selectedVec = selectedHit.getFirst();
BlockPos origin = new BlockPos(basePlateOffsetX, 0, basePlateOffsetZ); BlockPos origin = new BlockPos(basePlateOffsetX, 0, basePlateOffsetZ);
if (!world.getBounds() if (!world.getBounds()
@ -182,9 +178,14 @@ public class PonderScene {
.getFirst() .getFirst()
.selectBlock(selectedPos); .selectBlock(selectedPos);
BlockState blockState = world.getBlockState(selectedPos); BlockState blockState = world.getBlockState(selectedPos);
ItemStack pickBlock =
blockState.getCloneItemStack(new BlockHitResult(selectedVec, selectedFace, selectedPos, true), world, Direction direction = selectedHit.getSecond()
selectedPos, Minecraft.getInstance().player); .getDirection();
Vec3 location = selectedHit.getSecond()
.getLocation();
ItemStack pickBlock = blockState.getCloneItemStack(new BlockHitResult(location, direction, selectedPos, true),
world, selectedPos, Minecraft.getInstance().player);
return Pair.of(pickBlock, selectedPos); return Pair.of(pickBlock, selectedPos);
} }

View file

@ -26,7 +26,6 @@ import com.simibubi.create.foundation.render.SuperByteBufferCache;
import com.simibubi.create.foundation.render.SuperRenderTypeBuffer; import com.simibubi.create.foundation.render.SuperRenderTypeBuffer;
import com.simibubi.create.foundation.render.TileEntityRenderHelper; import com.simibubi.create.foundation.render.TileEntityRenderHelper;
import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.BlockFace;
import com.simibubi.create.foundation.utility.Pair; import com.simibubi.create.foundation.utility.Pair;
import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.VecHelper;
import com.simibubi.create.foundation.utility.outliner.AABBOutline; import com.simibubi.create.foundation.utility.outliner.AABBOutline;
@ -182,7 +181,7 @@ public class WorldSectionElement extends AnimatedSceneElement {
BlockPos worldPos; BlockPos worldPos;
} }
public Pair<Vec3, BlockFace> rayTrace(PonderWorld world, Vec3 source, Vec3 target) { public Pair<Vec3, BlockHitResult> rayTrace(PonderWorld world, Vec3 source, Vec3 target) {
world.setMask(this.section); world.setMask(this.section);
Vec3 transformedTarget = reverseTransformVec(target); Vec3 transformedTarget = reverseTransformVec(target);
BlockHitResult rayTraceBlocks = world.clip(new ClipContext(reverseTransformVec(source), transformedTarget, BlockHitResult rayTraceBlocks = world.clip(new ClipContext(reverseTransformVec(source), transformedTarget,
@ -200,7 +199,7 @@ public class WorldSectionElement extends AnimatedSceneElement {
/ source.subtract(target) / source.subtract(target)
.lengthSqr(); .lengthSqr();
Vec3 actualHit = VecHelper.lerp((float) t, target, source); Vec3 actualHit = VecHelper.lerp((float) t, target, source);
return Pair.of(actualHit, new BlockFace(rayTraceBlocks.getBlockPos(), rayTraceBlocks.getDirection())); return Pair.of(actualHit, rayTraceBlocks);
} }
private Vec3 reverseTransformVec(Vec3 in) { private Vec3 reverseTransformVec(Vec3 in) {