Skip to content

Commit ed4a31a

Browse files
authored
create parity between //snow and //thaw (#2940)
* create parity between //snow and //thaw * use layer visitor + cylinder region * fix wildcard import * make simulate snow and thaw match javadocs
1 parent 40afb9b commit ed4a31a

3 files changed

Lines changed: 104 additions & 42 deletions

File tree

worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.sk89q.worldedit.function.block.Counter;
5555
import com.sk89q.worldedit.function.block.Naturalizer;
5656
import com.sk89q.worldedit.function.block.SnowSimulator;
57+
import com.sk89q.worldedit.function.block.Thawer;
5758
import com.sk89q.worldedit.function.generator.ForestGenerator;
5859
import com.sk89q.worldedit.function.generator.GardenPatchGenerator;
5960
import com.sk89q.worldedit.function.mask.BlockMask;
@@ -2075,49 +2076,24 @@ public final int thaw(BlockVector3 position, double radius)
20752076
*/
20762077
public int thaw(BlockVector3 position, double radius, int height)
20772078
throws MaxChangedBlocksException {
2078-
int affected = 0;
2079-
double radiusSq = radius * radius;
2080-
2081-
int ox = position.x();
2082-
int oy = position.y();
2083-
int oz = position.z();
2084-
2085-
BlockState air = BlockTypes.AIR.getDefaultState();
2086-
BlockState water = BlockTypes.WATER.getDefaultState();
2087-
2088-
int centerY = Math.max(getWorld().getMinY(), Math.min(getWorld().getMaxY(), oy));
2089-
int minY = Math.max(getWorld().getMinY(), centerY - height);
2090-
int maxY = Math.min(getWorld().getMaxY(), centerY + height);
2091-
2092-
int ceilRadius = (int) Math.ceil(radius);
2093-
for (int x = ox - ceilRadius; x <= ox + ceilRadius; ++x) {
2094-
for (int z = oz - ceilRadius; z <= oz + ceilRadius; ++z) {
2095-
if (BlockVector3.at(x, oy, z).distanceSq(position) > radiusSq) {
2096-
continue;
2097-
}
2098-
2099-
for (int y = maxY; y > minY; --y) {
2100-
BlockVector3 pt = BlockVector3.at(x, y, z);
2101-
BlockType id = getBlock(pt).getBlockType();
21022079

2103-
if (id == BlockTypes.ICE) {
2104-
if (setBlock(pt, water)) {
2105-
++affected;
2106-
}
2107-
} else if (id == BlockTypes.SNOW) {
2108-
if (setBlock(pt, air)) {
2109-
++affected;
2110-
}
2111-
} else if (id.getMaterial().isAir()) {
2112-
continue;
2113-
}
2080+
return thaw(new CylinderRegion(position, Vector2.at(radius, radius), position.y() - height, position.y() + height));
2081+
}
21142082

2115-
break;
2116-
}
2117-
}
2118-
}
2083+
/**
2084+
* Thaw blocks in a cylinder.
2085+
*
2086+
* @param region the region to thaw in
2087+
* @return number of blocks affected
2088+
* @throws MaxChangedBlocksException thrown if too many blocks are changed
2089+
*/
2090+
public final int thaw(FlatRegion region) throws MaxChangedBlocksException {
2091+
checkNotNull(region);
21192092

2120-
return affected;
2093+
Thawer thawer = new Thawer(this);
2094+
LayerVisitor layerVisitor = new LayerVisitor(region, region.getMinimumY(), region.getMaximumY(), thawer);
2095+
Operations.completeLegacy(layerVisitor);
2096+
return thawer.getAffected();
21212097
}
21222098

21232099
/**
@@ -2151,7 +2127,7 @@ public final int simulateSnow(BlockVector3 position, double radius) throws MaxCh
21512127
public int simulateSnow(BlockVector3 position, double radius, int height)
21522128
throws MaxChangedBlocksException {
21532129

2154-
return simulateSnow(new CylinderRegion(position, Vector2.at(radius, radius), position.y(), height), false);
2130+
return simulateSnow(new CylinderRegion(position, Vector2.at(radius, radius), position.y() - height, position.y() + height), false);
21552131
}
21562132

21572133

worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,10 @@ public int thaw(Actor actor, LocalSession session, EditSession editSession,
335335
height = Math.max(1, height);
336336
we.checkMaxRadius(size);
337337

338-
int affected = editSession.thaw(session.getPlacementPosition(actor), size, height);
338+
BlockVector3 position = session.getPlacementPosition(actor);
339+
340+
CylinderRegion region = new CylinderRegion(position, Vector2.at(size, size), position.y() - height, position.y() + height);
341+
int affected = editSession.thaw(region);
339342
actor.printInfo(TranslatableComponent.of(
340343
"worldedit.thaw.removed", TextComponent.of(affected)
341344
));
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* WorldEdit, a Minecraft world manipulation toolkit
3+
* Copyright (C) sk89q <http://www.sk89q.com>
4+
* Copyright (C) WorldEdit team and contributors
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
package com.sk89q.worldedit.function.block;
21+
22+
import com.sk89q.worldedit.WorldEditException;
23+
import com.sk89q.worldedit.extent.Extent;
24+
import com.sk89q.worldedit.function.LayerFunction;
25+
import com.sk89q.worldedit.math.BlockVector3;
26+
import com.sk89q.worldedit.world.block.BlockState;
27+
import com.sk89q.worldedit.world.block.BlockType;
28+
import com.sk89q.worldedit.world.block.BlockTypes;
29+
30+
public class Thawer implements LayerFunction {
31+
32+
private final BlockState air = BlockTypes.AIR.getDefaultState();
33+
private final BlockState water = BlockTypes.WATER.getDefaultState();
34+
35+
private final Extent extent;
36+
37+
private int affected;
38+
39+
/**
40+
* Create a new instance.
41+
*
42+
* @param extent the extent to thaw in
43+
*/
44+
public Thawer(Extent extent) {
45+
this.extent = extent;
46+
this.affected = 0;
47+
}
48+
49+
public int getAffected() {
50+
return this.affected;
51+
}
52+
53+
@Override
54+
public boolean isGround(BlockVector3 position) {
55+
BlockState block = this.extent.getBlock(position);
56+
57+
// Stop searching when we hit anything non-air
58+
return !block.getBlockType().getMaterial().isAir();
59+
}
60+
61+
@Override
62+
public boolean apply(BlockVector3 position, int depth) throws WorldEditException {
63+
if (depth > 0) {
64+
// We only care about the first layer.
65+
return false;
66+
}
67+
68+
BlockState block = this.extent.getBlock(position);
69+
BlockType blockType = block.getBlockType();
70+
71+
if (blockType == BlockTypes.ICE) {
72+
if (this.extent.setBlock(position, water)) {
73+
affected++;
74+
}
75+
} else if (blockType == BlockTypes.SNOW) {
76+
if (this.extent.setBlock(position, air)) {
77+
affected++;
78+
}
79+
}
80+
81+
return false;
82+
}
83+
}

0 commit comments

Comments
 (0)