2021-07-23 15:48:51 +00:00
|
|
|
package com.fastasyncworldedit.core.extent;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2021-09-19 20:39:00 +00:00
|
|
|
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;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
|
|
|
import com.sk89q.jnbt.Tag;
|
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
|
|
|
import com.sk89q.worldedit.entity.BaseEntity;
|
|
|
|
import com.sk89q.worldedit.entity.Entity;
|
|
|
|
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
|
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
2018-12-23 16:19:33 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.util.Location;
|
|
|
|
import com.sk89q.worldedit.world.NbtValued;
|
2019-07-22 20:42:40 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BaseBlock;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
2020-07-14 02:50:59 +00:00
|
|
|
|
2021-07-24 15:34:05 +00:00
|
|
|
import javax.annotation.Nullable;
|
2021-01-06 18:00:40 +00:00
|
|
|
import java.util.HashMap;
|
2021-09-19 20:39:00 +00:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Locale;
|
2018-08-12 14:03:07 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2021-09-19 20:39:00 +00:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
import java.util.concurrent.Future;
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
import java.util.stream.Collectors;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2021-09-19 20:39:00 +00:00
|
|
|
public class StripNBTExtent extends AbstractDelegateExtent implements IBatchProcessor {
|
2021-07-24 15:34:05 +00:00
|
|
|
|
2021-09-19 20:39:00 +00:00
|
|
|
private final Set<String> strip;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance.
|
|
|
|
*
|
|
|
|
* @param extent the extent
|
|
|
|
*/
|
|
|
|
public StripNBTExtent(Extent extent, Set<String> strip) {
|
|
|
|
super(extent);
|
2021-09-19 20:39:00 +00:00
|
|
|
this.strip = new HashSet<>(strip)
|
|
|
|
.stream()
|
|
|
|
.map(String::toLowerCase).collect(Collectors.toUnmodifiableSet());
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-25 17:31:12 +00:00
|
|
|
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B block) throws WorldEditException {
|
|
|
|
return super.setBlock(location, stripBlockNBT(block));
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-25 17:31:12 +00:00
|
|
|
public <B extends BlockStateHolder<B>> boolean setBlock(int x, int y, int z, B block) throws WorldEditException {
|
|
|
|
return super.setBlock(x, y, z, stripBlockNBT(block));
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public Entity createEntity(Location location, BaseEntity entity) {
|
2019-03-25 17:31:12 +00:00
|
|
|
return super.createEntity(location, stripEntityNBT(entity));
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 17:31:12 +00:00
|
|
|
public <B extends BlockStateHolder<B>> B stripBlockNBT(B block) {
|
2020-10-05 17:41:41 +00:00
|
|
|
if (!(block instanceof BaseBlock)) {
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
BaseBlock localBlock = (BaseBlock) block;
|
|
|
|
if (!localBlock.hasNbtData()) {
|
|
|
|
return block;
|
|
|
|
}
|
2019-03-25 17:31:12 +00:00
|
|
|
CompoundTag nbt = localBlock.getNbtData();
|
2021-01-06 18:00:40 +00:00
|
|
|
Map<String, Tag> value = new HashMap<>(nbt.getValue());
|
2019-03-25 17:31:12 +00:00
|
|
|
for (String key : strip) {
|
|
|
|
value.remove(key);
|
|
|
|
}
|
2021-01-06 21:13:32 +00:00
|
|
|
return (B) localBlock.toBaseBlock(new CompoundTag(value));
|
2019-03-25 17:31:12 +00:00
|
|
|
}
|
2019-07-09 19:50:13 +00:00
|
|
|
|
2019-03-25 17:31:12 +00:00
|
|
|
public <T extends NbtValued> T stripEntityNBT(T entity) {
|
2020-10-05 17:41:41 +00:00
|
|
|
if (!entity.hasNbtData()) {
|
|
|
|
return entity;
|
|
|
|
}
|
2019-03-25 17:31:12 +00:00
|
|
|
CompoundTag nbt = entity.getNbtData();
|
2021-01-06 18:00:40 +00:00
|
|
|
Map<String, Tag> value = new HashMap<>(nbt.getValue());
|
2018-08-12 14:03:07 +00:00
|
|
|
for (String key : strip) {
|
|
|
|
value.remove(key);
|
|
|
|
}
|
2021-01-06 18:00:40 +00:00
|
|
|
entity.setNbtData(new CompoundTag(value));
|
2019-03-25 17:31:12 +00:00
|
|
|
return entity;
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
2021-07-24 15:34:05 +00:00
|
|
|
|
2021-09-19 20:39:00 +00:00
|
|
|
@Override
|
|
|
|
public IChunkSet processSet(final IChunk chunk, final IChunkGet get, final IChunkSet set) {
|
|
|
|
Map<BlockVector3, CompoundTag> tiles = set.getTiles();
|
|
|
|
Set<CompoundTag> entities = set.getEntities();
|
|
|
|
if (tiles.isEmpty() && entities.isEmpty()) {
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
boolean isBv3ChunkMap = tiles instanceof BlockVector3ChunkMap;
|
|
|
|
for (final Map.Entry<BlockVector3, CompoundTag> entry : tiles.entrySet()) {
|
|
|
|
ImmutableMap.Builder<String, Tag> 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<CompoundTag> stripped = new HashSet<>();
|
|
|
|
Iterator<CompoundTag> iterator = entities.iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
CompoundTag entity = iterator.next();
|
|
|
|
ImmutableMap.Builder<String, Tag> 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<IChunkSet> 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<Extent>(this).setNext(child);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ProcessorScope getScope() {
|
|
|
|
return ProcessorScope.CHANGING_BLOCKS;
|
|
|
|
}
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|