Skip to content

Commit 3a7eff5

Browse files
authored
Improve reliability of tree generator function (#2875)
* Improve reliability of tree generator function * Reimplement TreePlanter using generator
1 parent b692bc9 commit 3a7eff5

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/TreePlanter.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
import com.sk89q.worldedit.LocalConfiguration;
2424
import com.sk89q.worldedit.LocalSession;
2525
import com.sk89q.worldedit.MaxChangedBlocksException;
26+
import com.sk89q.worldedit.WorldEditException;
2627
import com.sk89q.worldedit.entity.Player;
2728
import com.sk89q.worldedit.extension.platform.Actor;
2829
import com.sk89q.worldedit.extension.platform.Platform;
29-
import com.sk89q.worldedit.math.BlockVector3;
30+
import com.sk89q.worldedit.function.generator.TreeGenerator;
3031
import com.sk89q.worldedit.util.Direction;
3132
import com.sk89q.worldedit.util.Location;
3233
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
@@ -55,21 +56,15 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla
5556

5657
try (EditSession editSession = BlockTool.createEditSession(player, session, clicked)) {
5758
try {
58-
boolean successful = false;
59-
60-
final BlockVector3 pos = clicked.toVector().add(0, 1, 0).toBlockPoint();
61-
for (int i = 0; i < 10; i++) {
62-
if (player.getWorld().generateTree(treeType, editSession, pos)) {
63-
successful = true;
64-
break;
65-
}
66-
}
59+
boolean successful = new TreeGenerator(editSession, treeType).apply(clicked.toVector().toBlockPoint());
6760

6861
if (!successful) {
6962
player.printError(TranslatableComponent.of("worldedit.tool.tree.obstructed"));
7063
}
7164
} catch (MaxChangedBlocksException e) {
7265
player.printError(TranslatableComponent.of("worldedit.tool.max-block-changes"));
66+
} catch (WorldEditException ignored) {
67+
// This should never happen
7368
} finally {
7469
session.remember(editSession);
7570
}

worldedit-core/src/main/java/com/sk89q/worldedit/function/generator/TreeGenerator.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ public TreeGenerator(EditSession editSession, TreeType treeType) {
4343

4444
@Override
4545
public boolean apply(BlockVector3 position) throws WorldEditException {
46-
return editSession.getWorld().generateTree(treeType, editSession, position);
46+
boolean successful = false;
47+
48+
final BlockVector3 pos = position.add(0, 1, 0);
49+
for (int i = 0; i < 10; i++) {
50+
if (editSession.getWorld().generateTree(treeType, editSession, pos)) {
51+
successful = true;
52+
break;
53+
}
54+
}
55+
56+
return successful;
4757
}
4858
}

0 commit comments

Comments
 (0)