-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
LimitExtent.java
528 lines (476 loc) · 17.2 KB
/
LimitExtent.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package com.fastasyncworldedit.core.extent;
import com.fastasyncworldedit.core.extent.filter.block.ExtentFilterBlock;
import com.fastasyncworldedit.core.function.generator.GenBase;
import com.fastasyncworldedit.core.function.generator.Resource;
import com.fastasyncworldedit.core.internal.exception.FaweException;
import com.fastasyncworldedit.core.limit.FaweLimit;
import com.fastasyncworldedit.core.queue.Filter;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.MaxChangedBlocksException;
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.function.mask.Mask;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.util.Countable;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.biome.BiomeTypes;
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 javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
public final class LimitExtent extends AbstractDelegateExtent {
private final FaweLimit limit;
private final boolean[] faweExceptionReasonsUsed = new boolean[FaweException.Type.values().length];
private final Consumer<Component> onErrorMessage;
/**
* Create a new instance.
*
* @param extent the extent
* @param limit the limit
*/
public LimitExtent(Extent extent, FaweLimit limit) {
this(extent, limit, c -> {
});
}
/**
* Create a new instance.
*
* @param extent the extent
* @param limit the limit
* @param onErrorMessage consumer to handle a component generated by exceptions
*/
public LimitExtent(Extent extent, FaweLimit limit, Consumer<Component> onErrorMessage) {
super(extent);
this.limit = limit;
this.onErrorMessage = onErrorMessage;
}
private void handleException(FaweException e) {
if (e.ignorable() || !limit.MAX_FAILS()) {
throw e;
}
if (!faweExceptionReasonsUsed[e.getType().ordinal()]) {
faweExceptionReasonsUsed[e.getType().ordinal()] = true;
onErrorMessage.accept(e.getComponent());
}
}
@Override
public List<? extends Entity> getEntities(Region region) {
limit.THROW_MAX_CHECKS(region.getVolume());
try {
return super.getEntities(region);
} catch (FaweException e) {
handleException(e);
return Collections.emptyList();
}
}
@Override
public List<? extends Entity> getEntities() {
limit.THROW_MAX_CHECKS();
try {
return super.getEntities();
} catch (FaweException e) {
handleException(e);
return Collections.emptyList();
}
}
@Override
@Nullable
public Entity createEntity(Location location, BaseEntity entity) {
limit.THROW_MAX_CHANGES();
limit.THROW_MAX_ENTITIES();
try {
return super.createEntity(location, entity);
} catch (FaweException e) {
handleException(e);
return null;
}
}
@Override
@Nullable
public Entity createEntity(Location location, BaseEntity entity, UUID uuid) {
limit.THROW_MAX_CHANGES();
limit.THROW_MAX_ENTITIES();
try {
return super.createEntity(location, entity, uuid);
} catch (FaweException e) {
handleException(e);
return null;
}
}
@Override
public void removeEntity(int x, int y, int z, UUID uuid) {
limit.THROW_MAX_CHANGES();
limit.THROW_MAX_ENTITIES();
try {
super.removeEntity(x, y, z, uuid);
} catch (FaweException e) {
handleException(e);
}
}
@Override
public boolean regenerateChunk(int x, int z, @Nullable BiomeType type, @Nullable Long seed) {
limit.THROW_MAX_CHANGES(Character.MAX_VALUE);
try {
return super.regenerateChunk(x, z, type, seed);
} catch (FaweException e) {
handleException(e);
return false;
}
}
@Override
public int getHighestTerrainBlock(int x, int z, int minY, int maxY) {
limit.THROW_MAX_CHECKS(maxY - minY + 1);
try {
return super.getHighestTerrainBlock(x, z, minY, maxY);
} catch (FaweException e) {
handleException(e);
return minY;
}
}
@Override
public int getHighestTerrainBlock(int x, int z, int minY, int maxY, Mask filter) {
limit.THROW_MAX_CHECKS(maxY - minY + 1);
try {
return super.getHighestTerrainBlock(x, z, minY, maxY, filter);
} catch (FaweException e) {
handleException(e);
return minY;
}
}
@Override
public int getNearestSurfaceLayer(int x, int z, int y, int minY, int maxY) {
limit.THROW_MAX_CHECKS(maxY - minY + 1);
try {
return super.getNearestSurfaceLayer(x, z, y, minY, maxY);
} catch (FaweException e) {
handleException(e);
return minY;
}
}
@Override
public int getNearestSurfaceTerrainBlock(int x, int z, int y, int minY, int maxY, boolean ignoreAir) {
limit.THROW_MAX_CHECKS(maxY - minY + 1);
try {
return super.getNearestSurfaceTerrainBlock(x, z, y, minY, maxY, ignoreAir);
} catch (FaweException e) {
handleException(e);
return minY;
}
}
@Override
public int getNearestSurfaceTerrainBlock(int x, int z, int y, int minY, int maxY) {
limit.THROW_MAX_CHECKS(maxY - minY + 1);
try {
return super.getNearestSurfaceTerrainBlock(x, z, y, minY, maxY);
} catch (FaweException e) {
handleException(e);
return minY;
}
}
@Override
public int getNearestSurfaceTerrainBlock(int x, int z, int y, int minY, int maxY, int failedMin, int failedMax) {
limit.THROW_MAX_CHECKS(maxY - minY + 1);
try {
return super.getNearestSurfaceTerrainBlock(x, z, y, minY, maxY, failedMin, failedMax);
} catch (FaweException e) {
handleException(e);
return minY;
}
}
@Override
public int getNearestSurfaceTerrainBlock(int x, int z, int y, int minY, int maxY, int failedMin, int failedMax, Mask mask) {
limit.THROW_MAX_CHECKS(maxY - minY + 1);
try {
return super.getNearestSurfaceTerrainBlock(x, z, y, minY, maxY, failedMin, failedMax, mask);
} catch (FaweException e) {
handleException(e);
return minY;
}
}
@Override
public int getNearestSurfaceTerrainBlock(
int x,
int z,
int y,
int minY,
int maxY,
int failedMin,
int failedMax,
boolean ignoreAir
) {
limit.THROW_MAX_CHECKS(maxY - minY + 1);
try {
return super.getNearestSurfaceTerrainBlock(x, z, y, minY, maxY, failedMin, failedMax, ignoreAir);
} catch (FaweException e) {
handleException(e);
return minY;
}
}
@Override
public void addCaves(Region region) throws WorldEditException {
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
super.addCaves(region);
}
@Override
public void generate(Region region, GenBase gen) throws WorldEditException {
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
super.generate(region, gen);
}
@Override
public void addSchems(Region region, Mask mask, List<ClipboardHolder> clipboards, int rarity, boolean rotate) throws
WorldEditException {
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
super.addSchems(region, mask, clipboards, rarity, rotate);
}
@Override
public void spawnResource(Region region, Resource gen, int rarity, int frequency) throws WorldEditException {
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
super.spawnResource(region, gen, rarity, frequency);
}
@Override
public void addOre(Region region, Mask mask, Pattern material, int size, int frequency, int rarity, int minY, int maxY) throws
WorldEditException {
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
super.addOre(region, mask, material, size, frequency, rarity, minY, maxY);
}
@Override
public void addOres(Region region, Mask mask) throws WorldEditException {
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
super.addOres(region, mask);
}
@Override
public List<Countable<BlockType>> getBlockDistribution(Region region) {
limit.THROW_MAX_CHECKS(region.getVolume());
return super.getBlockDistribution(region);
}
@Override
public List<Countable<BlockState>> getBlockDistributionWithData(Region region) {
limit.THROW_MAX_CHECKS(region.getVolume());
return super.getBlockDistributionWithData(region);
}
@Override
public int countBlocks(Region region, Set<BaseBlock> searchBlocks) {
limit.THROW_MAX_CHECKS(region.getVolume());
return super.countBlocks(region, searchBlocks);
}
@Override
public int countBlocks(Region region, Mask searchMask) {
limit.THROW_MAX_CHECKS(region.getVolume());
return super.countBlocks(region, searchMask);
}
@Override
public <B extends BlockStateHolder<B>> int setBlocks(Region region, B block) throws MaxChangedBlocksException {
limit.THROW_MAX_CHANGES(region.getVolume());
return super.setBlocks(region, block);
}
@Override
public int setBlocks(Region region, Pattern pattern) throws MaxChangedBlocksException {
limit.THROW_MAX_CHANGES(region.getVolume());
return super.setBlocks(region, pattern);
}
@Override
public <B extends BlockStateHolder<B>> int replaceBlocks(Region region, Set<BaseBlock> filter, B replacement) throws
MaxChangedBlocksException {
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
return super.replaceBlocks(region, filter, replacement);
}
@Override
public int replaceBlocks(Region region, Set<BaseBlock> filter, Pattern pattern) throws MaxChangedBlocksException {
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
return super.replaceBlocks(region, filter, pattern);
}
@Override
public int replaceBlocks(Region region, Mask mask, Pattern pattern) throws MaxChangedBlocksException {
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
return super.replaceBlocks(region, mask, pattern);
}
@Override
public int center(Region region, Pattern pattern) throws MaxChangedBlocksException {
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
return super.center(region, pattern);
}
@Override
public int setBlocks(Set<BlockVector3> vset, Pattern pattern) {
limit.THROW_MAX_CHANGES(vset.size());
return super.setBlocks(vset, pattern);
}
@Override
public <T extends Filter> T apply(Region region, T filter, boolean full) {
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
return super.apply(region, filter, full);
}
@Override
public <T extends Filter> T apply(Iterable<BlockVector3> positions, T filter) {
int size;
if (positions instanceof Collection) {
size = ((Collection<BlockVector3>) positions).size();
} else if (positions instanceof Region) {
BlockVector3 dim = ((Region) positions).getDimensions();
size = dim.getX() * dim.getY() * dim.getZ();
} else if (positions instanceof Extent) {
BlockVector3 min = ((Extent) positions).getMinimumPoint();
BlockVector3 max = ((Extent) positions).getMinimumPoint();
BlockVector3 dim = max.subtract(min).add(BlockVector3.ONE);
size = dim.getX() * dim.getY() * dim.getZ();
} else {
ExtentFilterBlock block = new ExtentFilterBlock(this);
for (BlockVector3 pos : positions) {
limit.THROW_MAX_CHECKS();
try {
filter.applyBlock(block.init(pos));
} catch (FaweException e) {
handleException(e);
}
}
return filter;
}
limit.THROW_MAX_CHECKS(size);
limit.THROW_MAX_CHANGES(size);
return super.apply(positions, filter);
}
@Override
public BlockState getBlock(BlockVector3 position) {
limit.THROW_MAX_CHECKS();
try {
return super.getBlock(position);
} catch (FaweException e) {
handleException(e);
return BlockTypes.AIR.getDefaultState();
}
}
@Override
public BlockState getBlock(int x, int y, int z) {
limit.THROW_MAX_CHECKS();
try {
return super.getBlock(x, y, z);
} catch (FaweException e) {
handleException(e);
return BlockTypes.AIR.getDefaultState();
}
}
@Override
public BaseBlock getFullBlock(BlockVector3 position) {
limit.THROW_MAX_CHECKS();
try {
return super.getFullBlock(position);
} catch (FaweException e) {
handleException(e);
return BlockTypes.AIR.getDefaultState().toBaseBlock();
}
}
@Override
public BaseBlock getFullBlock(int x, int y, int z) {
limit.THROW_MAX_CHECKS();
try {
return super.getFullBlock(x, y, z);
} catch (FaweException e) {
handleException(e);
return BlockTypes.AIR.getDefaultState().toBaseBlock();
}
}
@Override
public BiomeType getBiome(BlockVector3 position) {
limit.THROW_MAX_CHECKS();
try {
return super.getBiome(position);
} catch (FaweException e) {
handleException(e);
return BiomeTypes.FOREST;
}
}
@Override
public BiomeType getBiomeType(int x, int y, int z) {
limit.THROW_MAX_CHECKS();
try {
return super.getBiomeType(x, y, z);
} catch (FaweException e) {
handleException(e);
return BiomeTypes.FOREST;
}
}
@Override
@Deprecated
public <T extends BlockStateHolder<T>> boolean setBlock(BlockVector3 position, T block) throws WorldEditException {
limit.THROW_MAX_CHANGES();
if (block.getNbtData() != null || block.getBlockType().getMaterial().isTile()) {
limit.THROW_MAX_BLOCKSTATES();
}
try {
return super.setBlock(position, block);
} catch (FaweException e) {
handleException(e);
return false;
}
}
@Override
public <T extends BlockStateHolder<T>> boolean setBlock(int x, int y, int z, T block) throws WorldEditException {
limit.THROW_MAX_CHANGES();
if (block.getNbtData() != null || block.getBlockType().getMaterial().isTile()) {
limit.THROW_MAX_BLOCKSTATES();
}
try {
return super.setBlock(x, y, z, block);
} catch (FaweException e) {
handleException(e);
return false;
}
}
@Override
public boolean setTile(int x, int y, int z, CompoundTag tile) throws WorldEditException {
limit.THROW_MAX_CHANGES();
limit.MAX_BLOCKSTATES();
try {
return super.setTile(x, y, z, tile);
} catch (FaweException e) {
handleException(e);
return false;
}
}
@Override
public boolean setBiome(BlockVector3 position, BiomeType biome) {
limit.THROW_MAX_CHANGES();
try {
return super.setBiome(position, biome);
} catch (FaweException e) {
handleException(e);
return false;
}
}
@Override
public boolean setBiome(int x, int y, int z, BiomeType biome) {
limit.THROW_MAX_CHANGES();
try {
return super.setBiome(x, y, z, biome);
} catch (FaweException e) {
handleException(e);
return false;
}
}
}