Skip to content

Commit

Permalink
save progress pos (#715)
Browse files Browse the repository at this point in the history
Save the last iterator pos before setting a new one as "prev progress pos". Only reset to this point when restarting (saves going back too far).
  • Loading branch information
Raycoms committed Dec 9, 2024
1 parent 52be8f6 commit f5ff952
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@
*/
public abstract class AbstractBlueprintIterator implements IBlueprintIterator
{

/**
* The position we use as our uninitialized value.
*/
public static final BlockPos NULL_POS = new BlockPos(-1, -1, -1);

/**
* The Structure position we are at. Defaulted to NULL_POS.
*/
protected final BlockPos.MutableBlockPos progressPos = new BlockPos.MutableBlockPos(-1, -1, -1);

/**
* The previous position before the current progress position.
*/
protected final BlockPos.MutableBlockPos prevProgressPos = new BlockPos.MutableBlockPos(-1, -1, -1);

/**
* The size of the structure.
*/
Expand Down Expand Up @@ -117,6 +122,7 @@ else if (!isRemoving() && BlockUtils.areBlockStatesEqual(info.getBlockInfo().get
@Override
public void setProgressPos(final BlockPos localPosition)
{
this.prevProgressPos.set(this.progressPos);
if (localPosition.equals(NULL_POS))
{
this.progressPos.set(localPosition);
Expand Down Expand Up @@ -162,6 +168,7 @@ public boolean isRemoving()
@Override
public void reset()
{
prevProgressPos.set(NULL_POS);
progressPos.set(NULL_POS);
includeEntities = false;
isRemoving = false;
Expand All @@ -179,6 +186,12 @@ public BlockPos getProgressPos()
return progressPos.immutable();
}

@Override
public BlockPos getPrevProgressPos()
{
return prevProgressPos.immutable();
}

protected IStructureHandler getStructureHandler()
{
return structureHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public Result increment()

private Result iterate(boolean up)
{
this.prevProgressPos.set(this.progressPos);
if (this.progressPos.equals(NULL_POS))
{
this.progressPos.set(-1, up ? 0 : this.size.getY() -1, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public BlueprintIteratorHilbert(@NotNull final IStructureHandler structureHandle
@Override
public Result increment()
{
this.prevProgressPos.set(this.progressPos);
if (this.progressPos.equals(NULL_POS))
{
this.index = 0;
Expand All @@ -44,6 +45,7 @@ public Result increment()
@Override
public Result decrement()
{
this.prevProgressPos.set(this.progressPos);
if (this.progressPos.equals(NULL_POS))
{
this.index = (this.size.getY() & 1) == 0 ? this.positions.size() - 1 : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public BlueprintIteratorInwardCircle(final IStructureHandler structureHandler)
*/
public Result iterate(final boolean up)
{
this.prevProgressPos.set(this.progressPos);
if (this.progressPos.equals(NULL_POS))
{
this.progressPos.set(-1, up ? 0 : this.size.getY() - 1, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public BlueprintIteratorInwardCircleHeight(final IStructureHandler structureHand
@Override
public Result increment()
{
this.prevProgressPos.set(this.progressPos);
if (this.progressPos.equals(NULL_POS))
{
this.progressPos.set(0, 0, 0);
Expand All @@ -42,6 +43,7 @@ public Result increment()
@Override
public Result decrement()
{
this.prevProgressPos.set(this.progressPos);
if (this.progressPos.equals(NULL_POS))
{
this.progressPos.set(0, topRightCorner.getY(), 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public BlueprintIteratorRandom(final IStructureHandler structureHandler)
*/
public Result increment()
{
this.prevProgressPos.set(this.progressPos);
if (this.progressPos.equals(NULL_POS))
{
this.progressPos.set(this.positions.get(0).getX(), 0, this.positions.get(0).getZ());
Expand Down Expand Up @@ -75,6 +76,7 @@ public Result increment()
*/
public Result decrement()
{
this.prevProgressPos.set(this.progressPos);
if (this.progressPos.equals(NULL_POS))
{
this.progressPos.set(this.positions.get(0).getX(), this.size.getY() - 1, this.positions.get(0).getZ());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ public interface IBlueprintIterator
*/
BlockPos getProgressPos();

/**
* Get the last position before the progress pos of the iterator.
* @return the prev progress pos.
*/
BlockPos getPrevProgressPos();

/**
* Get the size of the blueprint which is iterated over
* @return the size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public StructurePhasePlacementResult executeStructureStep(
{
final BlockPos localPos = iterator.getProgressPos();
final BlockPos worldPos = handler.getProgressPosInWorld(localPos);
lastPos = iterator.getPrevProgressPos();

if (count >= handler.getStepsPerCall())
{
Expand All @@ -134,7 +135,6 @@ public StructurePhasePlacementResult executeStructureStep(
final BlockState localState = handler.getBluePrint().getBlockState(localPos);
if (localState == null || world.isOutsideBuildHeight(worldPos))
{
lastPos = localPos;
iterationResult = iterateFunction.get();
continue;
}
Expand Down

0 comments on commit f5ff952

Please sign in to comment.