Skip to content

Commit

Permalink
fix: 1-block previews on consecutive placements
Browse files Browse the repository at this point in the history
  • Loading branch information
Theta-Dev committed Mar 25, 2023
1 parent 9f6344e commit 12b29cc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public class RenderBlockPreview
{
public WandJob wandJob;
private WandJob wandJob;
public Set<BlockPos> undoBlocks;

@SubscribeEvent
Expand All @@ -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();
Expand Down Expand Up @@ -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());
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/thetadev/constructionwand/wand/WandJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public Set<BlockPos> getBlockPositions() {
return placeSnapshots.stream().map(ISnapshot::getPos).collect(Collectors.toSet());
}

public int blockCount() {
return placeSnapshots.size();
}

public boolean doIt() {
ArrayList<ISnapshot> executed = new ArrayList<>();

Expand Down

0 comments on commit 12b29cc

Please # to comment.