Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes NBT-Data Leak from Spell Circle Looping #866

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,27 @@ public class CircleExecutionState {
TAG_ENTERED_FROM = "entered_from",
TAG_IMAGE = "image",
TAG_CASTER = "caster",
TAG_PIGMENT = "pigment";
TAG_PIGMENT = "pigment",
TAG_REACHED_NUMBER = "reached_slate";

public final BlockPos impetusPos;
public final Direction impetusDir;
// Does contain the starting impetus
public final Set<BlockPos> knownPositions;
public final List<BlockPos> reachedPositions;
public final HashSet<BlockPos> reachedPositions;
public BlockPos currentPos;
public Direction enteredFrom;
public CastingImage currentImage;
public @Nullable UUID caster;
public @Nullable FrozenPigment casterPigment;
public Integer reachedNumber;

public final AABB bounds;


protected CircleExecutionState(BlockPos impetusPos, Direction impetusDir, Set<BlockPos> knownPositions,
List<BlockPos> reachedPositions, BlockPos currentPos, Direction enteredFrom,
CastingImage currentImage, @Nullable UUID caster, @Nullable FrozenPigment casterPigment) {
HashSet<BlockPos> reachedPositions, BlockPos currentPos, Direction enteredFrom,
CastingImage currentImage, @Nullable UUID caster, @Nullable FrozenPigment casterPigment, Integer reachedSlate) {
this.impetusPos = impetusPos;
this.impetusDir = impetusDir;
this.knownPositions = knownPositions;
Expand All @@ -65,6 +67,7 @@ protected CircleExecutionState(BlockPos impetusPos, Direction impetusDir, Set<Bl
this.currentImage = currentImage;
this.caster = caster;
this.casterPigment = casterPigment;
this.reachedNumber = reachedSlate;

this.bounds = BlockEntityAbstractImpetus.getBounds(new ArrayList<>(this.knownPositions));
}
Expand All @@ -83,7 +86,7 @@ protected CircleExecutionState(BlockPos impetusPos, Direction impetusDir, Set<Bl

// Return OK if it succeeded; returns Err if it didn't close and the location
public static Result<CircleExecutionState, @Nullable BlockPos> createNew(BlockEntityAbstractImpetus impetus,
@Nullable ServerPlayer caster) {
@Nullable ServerPlayer caster) {
var level = (ServerLevel) impetus.getLevel();

if (level == null)
Expand Down Expand Up @@ -129,7 +132,7 @@ protected CircleExecutionState(BlockPos impetusPos, Direction impetusDir, Set<Bl
}

var knownPositions = new HashSet<>(seenGoodPositions);
var reachedPositions = new ArrayList<BlockPos>();
var reachedPositions = new HashSet<BlockPos>();
reachedPositions.add(impetus.getBlockPos());
var start = seenGoodPositions.get(0);

Expand All @@ -143,7 +146,7 @@ protected CircleExecutionState(BlockPos impetusPos, Direction impetusDir, Set<Bl
}
return new Result.Ok<>(
new CircleExecutionState(impetus.getBlockPos(), impetus.getStartDirection(), knownPositions,
reachedPositions, start, impetus.getStartDirection(), new CastingImage(), casterUUID, colorizer));
reachedPositions, start, impetus.getStartDirection(), new CastingImage(), casterUUID, colorizer, 0));
}

public CompoundTag save() {
Expand Down Expand Up @@ -173,7 +176,7 @@ public CompoundTag save() {

if (this.casterPigment != null)
out.put(TAG_PIGMENT, this.casterPigment.serializeToNBT());

out.putInt(TAG_REACHED_NUMBER, this.reachedNumber);
return out;
}

Expand All @@ -186,7 +189,7 @@ public static CircleExecutionState load(CompoundTag nbt, ServerLevel world) {
for (var tag : knownTag) {
knownPositions.add(NbtUtils.readBlockPos(HexUtils.downcast(tag, CompoundTag.TYPE)));
}
var reachedPositions = new ArrayList<BlockPos>();
var reachedPositions = new HashSet<BlockPos>();
var reachedTag = nbt.getList(TAG_REACHED_POSITIONS, Tag.TAG_COMPOUND);
for (var tag : reachedTag) {
reachedPositions.add(NbtUtils.readBlockPos(HexUtils.downcast(tag, CompoundTag.TYPE)));
Expand All @@ -204,8 +207,13 @@ public static CircleExecutionState load(CompoundTag nbt, ServerLevel world) {
if (nbt.contains(TAG_PIGMENT, Tag.TAG_COMPOUND))
pigment = FrozenPigment.fromNBT(nbt.getCompound(TAG_PIGMENT));

int reachedNumber = 0;
if (nbt.contains(TAG_REACHED_NUMBER, Tag.TAG_INT)) {
reachedNumber = nbt.getInt(TAG_REACHED_NUMBER);
}

return new CircleExecutionState(startPos, startDir, knownPositions, reachedPositions, currentPos,
enteredFrom, image, caster, pigment);
enteredFrom, image, caster, pigment, reachedNumber);
}

/**
Expand All @@ -231,6 +239,7 @@ public boolean tick(BlockEntityAbstractImpetus impetus) {

executorBlockState = executor.startEnergized(this.currentPos, executorBlockState, world);
this.reachedPositions.add(this.currentPos);
this.reachedNumber = this.reachedNumber +1;

// Do the execution!
boolean halt = false;
Expand All @@ -257,7 +266,7 @@ public boolean tick(BlockEntityAbstractImpetus impetus) {
Component.literal(this.currentPos.toShortString()).withStyle(ChatFormatting.RED)),
new ItemStack(Items.COMPASS));
ICircleComponent.sfx(this.currentPos, executorBlockState, world,
Objects.requireNonNull(env.getImpetus()), false);
Objects.requireNonNull(env.getImpetus()), false);
halt = true;
break;
} else {
Expand Down Expand Up @@ -289,7 +298,7 @@ public boolean tick(BlockEntityAbstractImpetus impetus) {
* How many ticks should pass between activations, given the number of blocks encountered so far.
*/
protected int getTickSpeed() {
return Math.max(2, 10 - (this.reachedPositions.size() - 1) / 3);
return Math.max(2, 10 - (this.reachedNumber - 1) / 3);
}

public void endExecution(BlockEntityAbstractImpetus impetus) {
Expand Down
Loading