From 8a4f28a7ccd8b9182947a722af1c87df9e1faba9 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Sun, 19 Sep 2021 21:39:00 +0100 Subject: [PATCH] StripNBT processor rather than extent --- .../core/extent/StripNBTExtent.java | 91 ++++++++++++++++++- .../core/util/EditSessionBuilder.java | 7 +- .../sk89q/worldedit/EditSessionBuilder.java | 7 +- 3 files changed, 98 insertions(+), 7 deletions(-) diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/StripNBTExtent.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/StripNBTExtent.java index b44cdb58b..cb4db9a48 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/StripNBTExtent.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/StripNBTExtent.java @@ -1,5 +1,13 @@ package com.fastasyncworldedit.core.extent; +import com.fastasyncworldedit.core.extent.processor.ProcessorScope; +import com.fastasyncworldedit.core.math.BlockVector3ChunkMap; +import com.fastasyncworldedit.core.queue.IBatchProcessor; +import com.fastasyncworldedit.core.queue.IChunk; +import com.fastasyncworldedit.core.queue.IChunkGet; +import com.fastasyncworldedit.core.queue.IChunkSet; +import com.fastasyncworldedit.core.util.ExtentTraverser; +import com.google.common.collect.ImmutableMap; import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.Tag; import com.sk89q.worldedit.WorldEditException; @@ -15,12 +23,19 @@ import com.sk89q.worldedit.world.block.BlockStateHolder; import javax.annotation.Nullable; import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; -public class StripNBTExtent extends AbstractDelegateExtent { +public class StripNBTExtent extends AbstractDelegateExtent implements IBatchProcessor { - private final String[] strip; + private final Set strip; /** * Create a new instance. @@ -29,7 +44,9 @@ public class StripNBTExtent extends AbstractDelegateExtent { */ public StripNBTExtent(Extent extent, Set strip) { super(extent); - this.strip = strip.toArray(new String[0]); + this.strip = new HashSet<>(strip) + .stream() + .map(String::toLowerCase).collect(Collectors.toUnmodifiableSet()); } @Override @@ -77,4 +94,72 @@ public class StripNBTExtent extends AbstractDelegateExtent { return entity; } + @Override + public IChunkSet processSet(final IChunk chunk, final IChunkGet get, final IChunkSet set) { + Map tiles = set.getTiles(); + Set entities = set.getEntities(); + if (tiles.isEmpty() && entities.isEmpty()) { + return set; + } + boolean isBv3ChunkMap = tiles instanceof BlockVector3ChunkMap; + for (final Map.Entry entry : tiles.entrySet()) { + ImmutableMap.Builder map = ImmutableMap.builder(); + final AtomicBoolean isStripped = new AtomicBoolean(false); + entry.getValue().getValue().forEach((k, v) -> { + if (strip.contains(k.toLowerCase())) { + isStripped.set(true); + } else { + map.put(k, v); + } + }); + if (isStripped.get()) { + if (isBv3ChunkMap) { + // Replace existing value with stripped value + tiles.put(entry.getKey(), new CompoundTag(map.build())); + } else { + entry.setValue(new CompoundTag(map.build())); + } + } + } + Set stripped = new HashSet<>(); + Iterator iterator = entities.iterator(); + while (iterator.hasNext()) { + CompoundTag entity = iterator.next(); + ImmutableMap.Builder map = ImmutableMap.builder(); + final AtomicBoolean isStripped = new AtomicBoolean(false); + entity.getValue().forEach((k, v) -> { + if (strip.contains(k.toUpperCase(Locale.ROOT))) { + isStripped.set(true); + } else { + map.put(k, v); + } + }); + if (isStripped.get()) { + iterator.remove(); + stripped.add(new CompoundTag(map.build())); + } + } + set.getEntities().addAll(stripped); + return set; + } + + @Override + public Future postProcessSet(final IChunk chunk, final IChunkGet get, final IChunkSet set) { + return CompletableFuture.completedFuture(set); + } + + @Nullable + @Override + public Extent construct(final Extent child) { + if (getExtent() != child) { + new ExtentTraverser(this).setNext(child); + } + return this; + } + + @Override + public ProcessorScope getScope() { + return ProcessorScope.CHANGING_BLOCKS; + } + } diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/EditSessionBuilder.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/EditSessionBuilder.java index 0d715331f..c4347e757 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/EditSessionBuilder.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/EditSessionBuilder.java @@ -433,8 +433,11 @@ public class EditSessionBuilder { this.extent = regionExtent; } if (this.limit != null && this.limit.STRIP_NBT != null && !this.limit.STRIP_NBT.isEmpty()) { - //TODO add batch processor for strip nbt - this.extent = new StripNBTExtent(this.extent, this.limit.STRIP_NBT); + if (placeChunks) { + extent.addProcessor(new StripNBTExtent(this.extent, this.limit.STRIP_NBT)); + } else { + this.extent = new StripNBTExtent(this.extent, this.limit.STRIP_NBT); + } } this.extent = wrapExtent(this.extent, eventBus, event, EditSession.Stage.BEFORE_HISTORY); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSessionBuilder.java b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSessionBuilder.java index adc3b1a02..219cb1a0a 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSessionBuilder.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSessionBuilder.java @@ -541,8 +541,11 @@ public final class EditSessionBuilder { this.extent = regionExtent; } if (this.limit != null && this.limit.STRIP_NBT != null && !this.limit.STRIP_NBT.isEmpty()) { - //TODO add batch processor for strip nbt - this.extent = new StripNBTExtent(this.extent, this.limit.STRIP_NBT); + if (placeChunks) { + extent.addProcessor(new StripNBTExtent(this.extent, this.limit.STRIP_NBT)); + } else { + this.extent = new StripNBTExtent(this.extent, this.limit.STRIP_NBT); + } } this.extent = wrapExtent(this.extent, eventBus, event, EditSession.Stage.BEFORE_HISTORY); }