-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
StripNBTExtent.java
167 lines (152 loc) · 5.8 KB
/
StripNBTExtent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.NbtValued;
import com.sk89q.worldedit.world.block.BaseBlock;
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.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
public final class StripNBTExtent extends AbstractDelegateExtent implements IBatchProcessor {
private final Set<String> strip;
/**
* Create a new instance.
*
* @param extent the extent
*/
public StripNBTExtent(Extent extent, Set<String> strip) {
super(extent);
this.strip = new HashSet<>(strip)
.stream()
.map(String::toLowerCase).collect(Collectors.toUnmodifiableSet());
}
@Override
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B block) throws WorldEditException {
return super.setBlock(location, stripBlockNBT(block));
}
@Override
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));
}
@Nullable
@Override
public Entity createEntity(Location location, BaseEntity entity) {
return super.createEntity(location, stripEntityNBT(entity));
}
@Nullable
@Override
public Entity createEntity(Location location, BaseEntity entity, UUID uuid) {
return super.createEntity(location, stripEntityNBT(entity), uuid);
}
@SuppressWarnings("unchecked")
public <B extends BlockStateHolder<B>> B stripBlockNBT(B block) {
if (!(block instanceof BaseBlock localBlock)) {
return block;
}
if (!localBlock.hasNbtData()) {
return block;
}
CompoundTag nbt = localBlock.getNbtData();
Map<String, Tag> value = new HashMap<>(nbt.getValue());
for (String key : strip) {
value.remove(key);
}
return (B) localBlock.toBaseBlock(new CompoundTag(value));
}
public <T extends NbtValued> T stripEntityNBT(T entity) {
if (!entity.hasNbtData()) {
return entity;
}
CompoundTag nbt = entity.getNbtData();
Map<String, Tag> value = new HashMap<>(nbt.getValue());
for (String key : strip) {
value.remove(key);
}
entity.setNbtData(new CompoundTag(value));
return entity;
}
@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;
}
@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;
}
}