-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
DisallowedBlocksExtent.java
184 lines (172 loc) · 7.18 KB
/
DisallowedBlocksExtent.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.fastasyncworldedit.core.extent;
import com.fastasyncworldedit.core.extent.processor.ProcessorScope;
import com.fastasyncworldedit.core.limit.PropertyRemap;
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.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extension.factory.parser.DefaultBlockParser;
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.block.BlockTypesCache;
import com.sk89q.worldedit.world.block.FuzzyBlockState;
import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
public final class DisallowedBlocksExtent extends AbstractDelegateExtent implements IBatchProcessor {
private static final BlockState RESERVED = BlockTypes.__RESERVED__.getDefaultState();
private final Set<PropertyRemap<?>> remaps;
private Set<FuzzyBlockState> blockedStates = null;
private Set<String> blockedBlocks = null;
/**
* Create a new instance.
*
* @param extent the extent
* @param blockedBlocks block types to disallow
* @param remaps property remaps to apply, e.g. waterlogged true -> false
*/
public DisallowedBlocksExtent(Extent extent, Set<String> blockedBlocks, Set<PropertyRemap<?>> remaps) {
super(extent);
this.remaps = remaps;
if (blockedBlocks != null && !blockedBlocks.isEmpty()) {
blockedBlocks = blockedBlocks.stream()
.map(s -> s.contains(":") ? s.toLowerCase(Locale.ROOT) : ("minecraft:" + s).toLowerCase(Locale.ROOT))
.collect(Collectors.toSet());
this.blockedBlocks = new HashSet<>();
for (String block : blockedBlocks) {
if (block.indexOf('[') == -1 || block.indexOf(']') == -1) {
this.blockedBlocks.add(block);
continue;
}
String[] properties = block.substring(block.indexOf('[') + 1, block.indexOf(']')).split(",");
if (properties.length == 0) {
continue;
}
BlockType type = BlockTypes.get(block.substring(0, block.indexOf('[')));
Map<Property<?>, Object> values =
DefaultBlockParser.parseProperties(type, properties, null, true);
if (values == null || values.isEmpty()) {
continue;
}
if (blockedStates == null) {
blockedStates = new HashSet<>();
}
blockedStates.add(new FuzzyBlockState(type.getDefaultState(), values));
}
}
}
@Override
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B block) throws WorldEditException {
if (block instanceof BaseBlock || block instanceof BlockState) {
B newBlock = checkBlock(block);
if (newBlock.getBlockType() == BlockTypes.__RESERVED__) {
return false;
}
return super.setBlock(location, newBlock);
}
return super.setBlock(location, block);
}
@Override
public <B extends BlockStateHolder<B>> boolean setBlock(int x, int y, int z, B block) throws WorldEditException {
if (block instanceof BaseBlock || block instanceof BlockState) {
B newBlock = checkBlock(block);
if (newBlock.getBlockType() == BlockTypes.__RESERVED__) {
return false;
}
return super.setBlock(x, y, z, newBlock);
}
return super.setBlock(x, y, z, block);
}
@SuppressWarnings("unchecked")
private <B extends BlockStateHolder<B>> B checkBlock(B block) {
if (blockedBlocks != null) {
if (blockedBlocks.contains(block.getBlockType().getId())) {
return (B) (block instanceof BlockState ? RESERVED : RESERVED.toBaseBlock()); // set to reserved/empty
}
}
if (blockedStates == null) {
return block;
}
for (FuzzyBlockState state : blockedStates) {
if (state.equalsFuzzy(block)) {
return (B) (block instanceof BlockState ? RESERVED : RESERVED.toBaseBlock());
}
}
if (remaps == null || remaps.isEmpty()) {
return block;
}
for (PropertyRemap<?> remap : remaps) {
block = remap.apply(block);
}
return block;
}
@Override
public IChunkSet processSet(final IChunk chunk, final IChunkGet get, final IChunkSet set) {
if (blockedStates == null && blockedBlocks == null) { // Shouldn't be possible, but make sure
return set;
}
for (int layer = set.getMinSectionPosition(); layer <= set.getMaxSectionPosition(); layer++) {
if (!set.hasSection(layer)) {
continue;
}
char[] blocks = Objects.requireNonNull(set.loadIfPresent(layer));
it:
for (int i = 0; i < blocks.length; i++) {
char block = blocks[i];
if (block == BlockTypesCache.ReservedIDs.__RESERVED__) {
continue;
}
BlockState state = BlockTypesCache.states[block];
if (blockedBlocks != null) {
if (blockedBlocks.contains(state.getBlockType().getId())) {
blocks[i] = BlockTypesCache.ReservedIDs.__RESERVED__;
continue;
}
}
if (blockedStates == null) {
continue;
}
for (FuzzyBlockState fuzzy : blockedStates) {
if (fuzzy.equalsFuzzy(state)) {
blocks[i] = BlockTypesCache.ReservedIDs.__RESERVED__;
continue it;
}
}
if (remaps == null || remaps.isEmpty()) {
blocks[i] = block;
continue;
}
for (PropertyRemap<?> remap : remaps) {
state = remap.apply(state);
}
blocks[i] = state.getOrdinalChar();
}
}
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.REMOVING_BLOCKS;
}
}