Plex-FAWE/src/main/java/com/sk89q/worldedit/tools/FloatingTreeRemover.java

156 lines
5.4 KiB
Java
Raw Normal View History

// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.tools;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockID;
/**
* A pickaxe mode that removes floating treetops (logs and leaves not connected
* to anything else)
*
* @author Moo0
*/
public class FloatingTreeRemover implements BlockTool {
private static final BaseBlock AIR = new BaseBlock(BlockID.AIR);
private int rangeSq;
2011-11-23 01:29:48 +00:00
public FloatingTreeRemover() {
rangeSq = 100*100;
}
2011-11-23 01:29:48 +00:00
public boolean canUse(LocalPlayer player) {
return player.hasPermission("worldedit.tool.deltree");
}
2011-11-23 01:29:48 +00:00
public boolean actPrimary(ServerInterface server, LocalConfiguration config,
LocalPlayer player, LocalSession session, WorldVector clicked) {
2011-11-23 01:29:48 +00:00
final LocalWorld world = clicked.getWorld();
switch (world.getBlockType(clicked)) {
case BlockID.LOG:
case BlockID.LEAVES:
case BlockID.BROWN_MUSHROOM_CAP:
case BlockID.RED_MUSHROOM_CAP:
break;
default:
player.printError("That's not a tree.");
return true;
}
2011-11-23 01:29:48 +00:00
final EditSession editSession = session.createEditSession(player);
try {
final Set<Vector> blockSet = bfs(world, clicked);
if (blockSet == null) {
player.printError("That's not a floating tree.");
return true;
}
for (Vector blockVector : blockSet) {
final int typeId = editSession.getBlock(blockVector).getType();
switch (typeId) {
case BlockID.LOG:
case BlockID.LEAVES:
case BlockID.BROWN_MUSHROOM_CAP:
case BlockID.RED_MUSHROOM_CAP:
editSession.setBlock(blockVector, AIR);
}
}
} catch (MaxChangedBlocksException e) {
player.printError("Max blocks change limit reached.");
} finally {
session.remember(editSession);
}
return true;
}
Vector[] recurseDirections = {
PlayerDirection.NORTH.vector(),
PlayerDirection.EAST.vector(),
PlayerDirection.SOUTH.vector(),
PlayerDirection.WEST.vector(),
PlayerDirection.UP.vector(),
PlayerDirection.DOWN.vector(),
};
/**
* Helper method.
*
* @param world the world that contains the tree
* @param origin any point contained in the floating tree
* @return a set containing all blocks in the tree/shroom or null if this is not a floating tree/shroom.
*/
private Set<Vector> bfs(LocalWorld world, Vector origin) throws MaxChangedBlocksException {
final Set<Vector> visited = new HashSet<Vector>();
final LinkedList<Vector> queue = new LinkedList<Vector>();
queue.addLast(origin);
visited.add(origin);
while (!queue.isEmpty()) {
final Vector current = queue.removeFirst();
for (Vector recurseDirection : recurseDirections) {
final Vector next = current.add(recurseDirection);
if (origin.distanceSq(next) > rangeSq) {
// Maximum range exceeded => stop walking
continue;
}
if (visited.add(next)) {
switch (world.getBlockType(next)) {
case BlockID.AIR:
case BlockID.SNOW:
// we hit air or snow => stop walking this route
continue;
case BlockID.LOG:
case BlockID.LEAVES:
case BlockID.BROWN_MUSHROOM_CAP:
case BlockID.RED_MUSHROOM_CAP:
// queue next point
queue.addLast(next);
break;
default:
// we hit something solid - evaluate where we came from
if (world.getBlockType(current) == BlockID.LEAVES) {
// leaves touching a wall/the ground => stop walking this route
continue;
} else {
// log/shroom touching a wall/the ground => this is not a floating tree, bail out
return null;
}
} // switch
} // if
} // for
} // while
return visited;
} // bfs
}