-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
MultiRegionExtent.java
190 lines (177 loc) · 5.84 KB
/
MultiRegionExtent.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
package com.fastasyncworldedit.core.extent;
import com.fastasyncworldedit.core.limit.FaweLimit;
import com.fastasyncworldedit.core.queue.IChunk;
import com.fastasyncworldedit.core.queue.IChunkGet;
import com.fastasyncworldedit.core.queue.IChunkSet;
import com.fastasyncworldedit.core.regions.RegionWrapper;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionIntersection;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Future;
public final class MultiRegionExtent extends FaweRegionExtent {
@Nullable
private final RegionIntersection intersection;
@Nullable
private final RegionIntersection disallowedIntersection;
@Nullable
private final Region[] allowed;
@Nullable
private final Region[] disallowed;
@Nullable
private Region region;
private int index;
/**
* Create a new instance. Has both allowed and disallowed regions. Assumes that disallowed regions are encompassed by
* allowed regions.
*
* @param extent the extent
* @param limit the limit to be used
* @param allowed the allowed regions or null for global editing
* @param disallowed the disallowed regions or null for no disallowed regions
*/
public MultiRegionExtent(Extent extent, FaweLimit limit, @Nullable Region[] allowed, @Nullable Region[] disallowed) {
super(extent, limit);
this.index = 0;
if (allowed != null && !allowed[0].isGlobal()) {
this.region = allowed[0];
this.allowed = allowed;
this.intersection = new RegionIntersection(Arrays.asList(allowed));
} else {
this.region = null;
this.allowed = null;
this.intersection = null;
}
if (disallowed != null && disallowed.length > 0) {
this.disallowed = disallowed;
this.disallowedIntersection = new RegionIntersection(Arrays.asList(disallowed));
} else {
this.disallowed = null;
this.disallowedIntersection = null;
}
}
@Override
public boolean contains(int x, int y, int z) {
if (region != null && region.contains(x, y, z)) {
if (disallowed != null) {
for (final Region disallow : disallowed) {
if (disallow.contains(x, y, z)) {
return false;
}
}
}
return true;
}
boolean result = allowed == null;
if (!result) {
for (int i = 0; i < allowed.length; i++) {
if (i != index) {
Region current = allowed[i];
if (current.contains(x, y, z)) {
region = current;
index = i;
result = true;
break;
}
}
}
}
if (!result || disallowed == null) {
return result;
}
for (final Region disallow : disallowed) {
if (disallow.contains(x, y, z)) {
return false;
}
}
return true;
}
@Override
public boolean contains(int x, int z) {
if (region != null && region.contains(x, z)) {
if (disallowed != null) {
for (final Region disallow : disallowed) {
if (disallow.contains(x, z)) {
return false;
}
}
}
return true;
}
boolean result = allowed == null;
if (!result) {
for (int i = 0; i < allowed.length; i++) {
if (i != index) {
Region current = allowed[i];
if (current.contains(x, z)) {
region = current;
index = i;
result = true;
break;
}
}
}
}
if (!result || disallowed == null) {
return result;
}
for (final Region disallow : disallowed) {
if (disallow.contains(x, z)) {
return false;
}
}
return true;
}
/**
* Get all allowed regions
*/
@Override
public Collection<Region> getRegions() {
if (allowed == null) {
return List.of(RegionWrapper.GLOBAL());
}
return Arrays.asList(allowed);
}
@Override
public boolean processGet(int chunkX, int chunkZ) {
boolean result = allowed == null;
if (!result) {
for (Region region : allowed) {
if (region.containsChunk(chunkX, chunkZ)) {
result = true;
break;
}
}
}
if (!result || disallowed == null) {
return result;
}
for (Region region : disallowed) {
if (region.containsChunk(chunkX, chunkZ)) {
return false;
}
}
return true;
}
@Override
public IChunkSet processSet(IChunk chunk, IChunkGet get, IChunkSet set) {
if (intersection != null) {
set = intersection.processSet(chunk, get, set);
}
if (disallowedIntersection != null) {
set = disallowedIntersection.processSet(chunk, get, set, true);
}
return set;
}
@Override
public Future<?> postProcessSet(final IChunk chunk, final IChunkGet get, final IChunkSet set) {
return intersection.postProcessSet(chunk, get, set);
}
@Override
public void postProcess(IChunk chunk, IChunkGet get, IChunkSet set) {
intersection.postProcess(chunk, get, set);
}
}