-
-
Notifications
You must be signed in to change notification settings - Fork 824
/
ForwardExtentCopy.java
331 lines (295 loc) · 11.1 KB
/
ForwardExtentCopy.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
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.operation;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.Lists;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.entity.metadata.EntityProperties;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.CombinedRegionFunction;
import com.sk89q.worldedit.function.FlatRegionFunction;
import com.sk89q.worldedit.function.FlatRegionMaskingFilter;
import com.sk89q.worldedit.function.RegionFunction;
import com.sk89q.worldedit.function.RegionMaskingFilter;
import com.sk89q.worldedit.function.biome.ExtentBiomeCopy;
import com.sk89q.worldedit.function.block.ExtentBlockCopy;
import com.sk89q.worldedit.function.entity.ExtentEntityCopy;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.Mask2D;
import com.sk89q.worldedit.function.mask.Masks;
import com.sk89q.worldedit.function.visitor.EntityVisitor;
import com.sk89q.worldedit.function.visitor.FlatRegionVisitor;
import com.sk89q.worldedit.function.visitor.RegionVisitor;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.transform.Identity;
import com.sk89q.worldedit.math.transform.Transform;
import com.sk89q.worldedit.regions.FlatRegion;
import com.sk89q.worldedit.regions.Region;
import java.util.List;
/**
* Makes a copy of a portion of one extent to another extent or another point.
*
* <p>This is a forward extent copy, meaning that it iterates over the blocks
* in the source extent, and will copy as many blocks as there are in the
* source. Therefore, interpolation will not occur to fill in the gaps.</p>
*/
public class ForwardExtentCopy implements Operation {
private final Extent source;
private final Extent destination;
private final Region region;
private final BlockVector3 from;
private final BlockVector3 to;
private int repetitions = 1;
private Mask sourceMask = Masks.alwaysTrue();
private boolean removingEntities;
private boolean copyingEntities = true; // default to true for backwards compatibility, sort of
private boolean copyingBiomes;
private RegionFunction sourceFunction = null;
private Transform transform = new Identity();
private Transform currentTransform = null;
private RegionVisitor lastVisitor;
private int affected;
/**
* Create a new copy using the region's lowest minimum point as the
* "from" position.
*
* @param source the source extent
* @param region the region to copy
* @param destination the destination extent
* @param to the destination position
* @see #ForwardExtentCopy(Extent, Region, BlockVector3, Extent, BlockVector3) the main constructor
*/
public ForwardExtentCopy(Extent source, Region region, Extent destination, BlockVector3 to) {
this(source, region, region.getMinimumPoint(), destination, to);
}
/**
* Create a new copy.
*
* @param source the source extent
* @param region the region to copy
* @param from the source position
* @param destination the destination extent
* @param to the destination position
*/
public ForwardExtentCopy(Extent source, Region region, BlockVector3 from, Extent destination, BlockVector3 to) {
checkNotNull(source);
checkNotNull(region);
checkNotNull(from);
checkNotNull(destination);
checkNotNull(to);
this.source = source;
this.destination = destination;
this.region = region;
this.from = from;
this.to = to;
}
/**
* Get the transformation that will occur on every point.
*
* <p>The transformation will stack with each repetition.</p>
*
* @return a transformation
*/
public Transform getTransform() {
return transform;
}
/**
* Set the transformation that will occur on every point.
*
* @param transform a transformation
* @see #getTransform()
*/
public void setTransform(Transform transform) {
checkNotNull(transform);
this.transform = transform;
}
/**
* Get the mask that gets applied to the source extent.
*
* <p>This mask can be used to filter what will be copied from the source.</p>
*
* @return a source mask
*/
public Mask getSourceMask() {
return sourceMask;
}
/**
* Set a mask that gets applied to the source extent.
*
* @param sourceMask a source mask
* @see #getSourceMask()
*/
public void setSourceMask(Mask sourceMask) {
checkNotNull(sourceMask);
this.sourceMask = sourceMask;
}
/**
* Get the function that gets applied to all source blocks <em>after</em>
* the copy has been made.
*
* @return a source function, or null if none is to be applied
*/
public RegionFunction getSourceFunction() {
return sourceFunction;
}
/**
* Set the function that gets applied to all source blocks <em>after</em>
* the copy has been made.
*
* @param function a source function, or null if none is to be applied
*/
public void setSourceFunction(RegionFunction function) {
this.sourceFunction = function;
}
/**
* Get the number of repetitions left.
*
* @return the number of repetitions
*/
public int getRepetitions() {
return repetitions;
}
/**
* Set the number of repetitions left.
*
* @param repetitions the number of repetitions
*/
public void setRepetitions(int repetitions) {
checkArgument(repetitions >= 0, "number of repetitions must be non-negative");
this.repetitions = repetitions;
}
/**
* Return whether entities should be copied along with blocks.
*
* @return true if copying
*/
public boolean isCopyingEntities() {
return copyingEntities;
}
/**
* Set whether entities should be copied along with blocks.
*
* @param copyingEntities true if copying
*/
public void setCopyingEntities(boolean copyingEntities) {
this.copyingEntities = copyingEntities;
}
/**
* Return whether entities that are copied should be removed.
*
* @return true if removing
*/
public boolean isRemovingEntities() {
return removingEntities;
}
/**
* Set whether entities that are copied should be removed.
*
* @param removingEntities true if removing
*/
public void setRemovingEntities(boolean removingEntities) {
this.removingEntities = removingEntities;
}
/**
* Return whether biomes should be copied along with blocks.
*
* @return true if copying biomes
*/
public boolean isCopyingBiomes() {
return copyingBiomes;
}
/**
* Set whether biomes should be copies along with blocks.
*
* @param copyingBiomes true if copying
*/
public void setCopyingBiomes(boolean copyingBiomes) {
if (copyingBiomes && !(region instanceof FlatRegion)) {
throw new UnsupportedOperationException("Can't copy biomes from region that doesn't implement FlatRegion");
}
this.copyingBiomes = copyingBiomes;
}
/**
* Get the number of affected objects.
*
* @return the number of affected
*/
public int getAffected() {
return affected;
}
@Override
public Operation resume(RunContext run) throws WorldEditException {
if (lastVisitor != null) {
affected += lastVisitor.getAffected();
lastVisitor = null;
}
if (repetitions > 0) {
repetitions--;
if (currentTransform == null) {
currentTransform = transform;
} else {
currentTransform = currentTransform.combine(transform);
}
ExtentBlockCopy blockCopy = new ExtentBlockCopy(source, from, destination, to, currentTransform);
RegionMaskingFilter filteredFunction = new RegionMaskingFilter(sourceMask,
sourceFunction == null ? blockCopy : new CombinedRegionFunction(blockCopy, sourceFunction));
RegionVisitor blockVisitor = new RegionVisitor(region, filteredFunction);
lastVisitor = blockVisitor;
if (!copyingBiomes && !copyingEntities) {
return new DelegateOperation(this, blockVisitor);
}
List<Operation> ops = Lists.newArrayList(blockVisitor);
if (copyingBiomes && region instanceof FlatRegion) { // double-check here even though we checked before
ExtentBiomeCopy biomeCopy = new ExtentBiomeCopy(source, from.toBlockVector2(),
destination, to.toBlockVector2(), currentTransform);
Mask2D biomeMask = sourceMask.toMask2D();
if (biomeMask != null) {
FlatRegionMaskingFilter filteredBiomeCopy = new FlatRegionMaskingFilter(biomeMask, biomeCopy);
FlatRegionVisitor biomeVisitor = new FlatRegionVisitor(((FlatRegion) region), filteredBiomeCopy);
ops.add(biomeVisitor);
} else {
ops.add(new FlatRegionVisitor(((FlatRegion) region), biomeCopy));
}
}
if (copyingEntities) {
ExtentEntityCopy entityCopy = new ExtentEntityCopy(from.toVector3(), destination, to.toVector3(), currentTransform);
entityCopy.setRemoving(removingEntities);
List<? extends Entity> entities = Lists.newArrayList(source.getEntities(region));
entities.removeIf(entity -> {
EntityProperties properties = entity.getFacet(EntityProperties.class);
return properties != null && !properties.isPasteable();
});
EntityVisitor entityVisitor = new EntityVisitor(entities.iterator(), entityCopy);
ops.add(entityVisitor);
}
return new DelegateOperation(this, new OperationQueue(ops));
} else {
return null;
}
}
@Override
public void cancel() {
}
@Override
public void addStatusMessages(List<String> messages) {
}
}