From 12b29cc185d582bda4298ea49cc0038a2e7c21b6 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Fri, 24 Mar 2023 23:33:28 +0100 Subject: [PATCH] fix: 1-block previews on consecutive placements --- .../constructionwand/client/RenderBlockPreview.java | 12 ++++++++++-- .../java/thetadev/constructionwand/wand/WandJob.java | 4 ++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/thetadev/constructionwand/client/RenderBlockPreview.java b/src/main/java/thetadev/constructionwand/client/RenderBlockPreview.java index 39cb2c1..8ce9ab7 100644 --- a/src/main/java/thetadev/constructionwand/client/RenderBlockPreview.java +++ b/src/main/java/thetadev/constructionwand/client/RenderBlockPreview.java @@ -22,7 +22,7 @@ public class RenderBlockPreview { - public WandJob wandJob; + private WandJob wandJob; public Set undoBlocks; @SubscribeEvent @@ -39,7 +39,11 @@ public void renderBlockHighlight(RenderHighlightEvent.Block event) { if(wand == null) return; if(!(player.isCrouching() && ClientEvents.isOptKeyDown())) { - if(wandJob == null || !compareRTR(wandJob.rayTraceResult, rtr) || !(wandJob.wand.equals(wand))) { + // Use cached wandJob for previews of the same target pos/dir + // Exception: always update if blockCount < 2 to prevent 1-block previews when block updates + // from the last placement are lagging + if(wandJob == null || !compareRTR(wandJob.rayTraceResult, rtr) || !(wandJob.wand.equals(wand)) + || wandJob.blockCount() < 2) { wandJob = ItemWand.getWandJob(player, player.level, rtr, wand); } blocks = wandJob.getBlockPositions(); @@ -68,6 +72,10 @@ public void renderBlockHighlight(RenderHighlightEvent.Block event) { event.setCanceled(true); } + public void reset() { + wandJob = null; + } + private static boolean compareRTR(BlockHitResult rtr1, BlockHitResult rtr2) { return rtr1.getBlockPos().equals(rtr2.getBlockPos()) && rtr1.getDirection().equals(rtr2.getDirection()); } diff --git a/src/main/java/thetadev/constructionwand/wand/WandJob.java b/src/main/java/thetadev/constructionwand/wand/WandJob.java index 6ef8e44..958f60d 100644 --- a/src/main/java/thetadev/constructionwand/wand/WandJob.java +++ b/src/main/java/thetadev/constructionwand/wand/WandJob.java @@ -86,6 +86,10 @@ public Set getBlockPositions() { return placeSnapshots.stream().map(ISnapshot::getPos).collect(Collectors.toSet()); } + public int blockCount() { + return placeSnapshots.size(); + } + public boolean doIt() { ArrayList executed = new ArrayList<>();