From d4b68b384bf7d5d1baa82ec7a023b371ec550ec9 Mon Sep 17 00:00:00 2001 From: Jordan Date: Tue, 12 Dec 2023 07:00:40 +0000 Subject: [PATCH] fix: add chunk loc to tile entity location when trimming (#2500) - fixes #2499 --- .../core/extent/HeightBoundExtent.java | 6 ++--- .../core/queue/IBatchProcessor.java | 22 +++++++++++++++++++ .../fastasyncworldedit/core/queue/IChunk.java | 11 ++++++++++ .../sk89q/worldedit/regions/CuboidRegion.java | 14 +++++++----- .../com/sk89q/worldedit/regions/Region.java | 6 +++-- 5 files changed, 48 insertions(+), 11 deletions(-) diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/HeightBoundExtent.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/HeightBoundExtent.java index ff57e00da..c22d9e6be 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/HeightBoundExtent.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/HeightBoundExtent.java @@ -6,12 +6,11 @@ import com.fastasyncworldedit.core.queue.IChunkGet; import com.fastasyncworldedit.core.queue.IChunkSet; import com.fastasyncworldedit.core.regions.RegionWrapper; import com.sk89q.worldedit.extent.Extent; +import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.regions.Region; import java.util.Collection; import java.util.Collections; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Future; public class HeightBoundExtent extends FaweRegionExtent { @@ -50,7 +49,8 @@ public class HeightBoundExtent extends FaweRegionExtent { @Override public IChunkSet processSet(IChunk chunk, IChunkGet get, IChunkSet set) { - if (trimY(set, min, max, true) | trimNBT(set, this::contains)) { + BlockVector3 chunkPos = chunk.getChunkBlockCoord().withY(0); + if (trimY(set, min, max, true) | trimNBT(set, this::contains, pos -> this.contains(pos.add(chunkPos)))) { return set; } return null; diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IBatchProcessor.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IBatchProcessor.java index 5eeafe28e..b096b93fe 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IBatchProcessor.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IBatchProcessor.java @@ -155,7 +155,9 @@ public interface IBatchProcessor { * Utility method to trim entity and blocks with a provided contains function. * * @return false if chunk is empty of NBT + * @deprecated tiles are stored in chunk-normalised coordinate space and thus cannot use the same function as entities */ + @Deprecated(forRemoval = true, since = "TODO") default boolean trimNBT(IChunkSet set, Function contains) { Set ents = set.getEntities(); if (!ents.isEmpty()) { @@ -169,6 +171,26 @@ public interface IBatchProcessor { return !tiles.isEmpty() || !ents.isEmpty(); } + /** + * Utility method to trim entity and blocks with a provided contains function. + * + * @return false if chunk is empty of NBT + * @since TODO + */ + default boolean trimNBT( + IChunkSet set, Function containsEntity, Function containsTile + ) { + Set ents = set.getEntities(); + if (!ents.isEmpty()) { + ents.removeIf(ent -> !containsEntity.apply(ent.getEntityPosition().toBlockPoint())); + } + Map tiles = set.getTiles(); + if (!tiles.isEmpty()) { + tiles.entrySet().removeIf(blockVector3CompoundTagEntry -> !containsTile.apply(blockVector3CompoundTagEntry.getKey())); + } + return !tiles.isEmpty() || !ents.isEmpty(); + } + /** * Join two processors and return the result. */ diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IChunk.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IChunk.java index 0acfa4a22..5a03a9987 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IChunk.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/IChunk.java @@ -1,6 +1,7 @@ package com.fastasyncworldedit.core.queue; import com.fastasyncworldedit.core.extent.filter.block.ChunkFilterBlock; +import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.regions.Region; import javax.annotation.Nullable; @@ -33,6 +34,16 @@ public interface IChunk extends Trimable, IChunkGet, IChunkSet { */ int getZ(); + /** + * Return the minimum block coordinate of the chunk + * + * @return BlockVector3 of minimum block coordinate + * @since TODO + */ + default BlockVector3 getChunkBlockCoord() { + return BlockVector3.at(getX() << 4, getMinY(), getZ() << 4); + } + /** * If the chunk is a delegate, returns its parent's root * diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java b/worldedit-core/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java index cc2f03aa2..d04e5aa8c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java @@ -22,7 +22,6 @@ package com.sk89q.worldedit.regions; import com.fastasyncworldedit.core.configuration.Settings; import com.fastasyncworldedit.core.extent.filter.block.ChunkFilterBlock; import com.fastasyncworldedit.core.math.BlockVectorSet; -import com.fastasyncworldedit.core.math.MutableBlockVector2; import com.fastasyncworldedit.core.math.MutableBlockVector3; import com.fastasyncworldedit.core.queue.Filter; import com.fastasyncworldedit.core.queue.IChunk; @@ -805,7 +804,8 @@ public class CuboidRegion extends AbstractRegion implements FlatRegion { return set; } trimY(set, minY, maxY, true); - trimNBT(set, this::contains); + BlockVector3 chunkPos = chunk.getChunkBlockCoord().withY(0); + trimNBT(set, this::contains, pos -> this.contains(pos.add(chunkPos))); return set; } if (tx >= minX && bx <= maxX && tz >= minZ && bz <= maxZ) { @@ -868,8 +868,8 @@ public class CuboidRegion extends AbstractRegion implements FlatRegion { } set.setBlocks(layer, arr); } - - trimNBT(set, this::contains); + final BlockVector3 chunkPos = BlockVector3.at(chunk.getX() << 4, 0, chunk.getZ() << 4); + trimNBT(set, this::contains, pos -> this.contains(pos.add(chunkPos))); return set; } return null; @@ -893,7 +893,8 @@ public class CuboidRegion extends AbstractRegion implements FlatRegion { return null; } trimY(set, minY, maxY, false); - trimNBT(set, this::contains); + BlockVector3 chunkPos = chunk.getChunkBlockCoord().withY(0); + trimNBT(set, this::contains, pos -> this.contains(pos.add(chunkPos))); return set; } if (tx >= minX && bx <= maxX && tz >= minZ && bz <= maxZ) { @@ -943,7 +944,8 @@ public class CuboidRegion extends AbstractRegion implements FlatRegion { } set.setBlocks(layer, arr); } - trimNBT(set, bv3 -> !this.contains(bv3)); + BlockVector3 chunkPos = chunk.getChunkBlockCoord().withY(0); + trimNBT(set, bv3 -> !this.contains(bv3), bv3 -> !this.contains(bv3.add(chunkPos))); return set; } return set; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/regions/Region.java b/worldedit-core/src/main/java/com/sk89q/worldedit/regions/Region.java index b89536520..d2fa35310 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/regions/Region.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/regions/Region.java @@ -425,7 +425,8 @@ public interface Region extends Iterable, Cloneable, IBatchProcess } } if (processExtra) { - trimNBT(set, this::contains); + BlockVector3 chunkPos = chunk.getChunkBlockCoord().withY(0); + trimNBT(set, this::contains, pos -> this.contains(pos.add(chunkPos))); } return set; } else { @@ -477,7 +478,8 @@ public interface Region extends Iterable, Cloneable, IBatchProcess } } if (processExtra) { - trimNBT(set, bv3 -> !this.contains(bv3)); + BlockVector3 chunkPos = chunk.getChunkBlockCoord().withY(0); + trimNBT(set, bv3 -> !this.contains(bv3), bv3 -> !this.contains(bv3.add(chunkPos))); } return set; } else {