2011-05-10 22:31:24 +00:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* WorldEdit
|
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
|
|
|
*
|
|
|
|
* 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;
|
2011-12-02 22:53:06 +00:00
|
|
|
import java.util.LinkedList;
|
2011-05-10 22:31:24 +00:00
|
|
|
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 {
|
2011-12-02 22:53:06 +00:00
|
|
|
private static final BaseBlock AIR = new BaseBlock(BlockID.AIR);
|
|
|
|
private int rangeSq;
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-05-10 22:31:24 +00:00
|
|
|
public FloatingTreeRemover() {
|
2011-12-02 22:53:06 +00:00
|
|
|
rangeSq = 100*100;
|
2011-05-10 22:31:24 +00:00
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-05-10 22:31:24 +00:00
|
|
|
public boolean canUse(LocalPlayer player) {
|
|
|
|
return player.hasPermission("worldedit.tool.deltree");
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-05-10 22:31:24 +00:00
|
|
|
public boolean actPrimary(ServerInterface server, LocalConfiguration config,
|
|
|
|
LocalPlayer player, LocalSession session, WorldVector clicked) {
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-12-02 22:53:06 +00:00
|
|
|
final LocalWorld world = clicked.getWorld();
|
2011-06-04 17:30:45 +00:00
|
|
|
|
2011-12-02 22:53:06 +00:00
|
|
|
switch (world.getBlockType(clicked)) {
|
|
|
|
case BlockID.LOG:
|
|
|
|
case BlockID.LEAVES:
|
|
|
|
case BlockID.BROWN_MUSHROOM_CAP:
|
|
|
|
case BlockID.RED_MUSHROOM_CAP:
|
|
|
|
break;
|
2011-05-10 22:31:24 +00:00
|
|
|
|
2011-12-02 22:53:06 +00:00
|
|
|
default:
|
|
|
|
player.printError("That's not a tree.");
|
2011-05-10 22:31:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-12-02 22:53:06 +00:00
|
|
|
final EditSession editSession = session.createEditSession(player);
|
|
|
|
|
2011-05-10 22:31:24 +00:00
|
|
|
try {
|
2011-12-02 22:53:06 +00:00
|
|
|
final Set<Vector> blockSet = bfs(world, clicked);
|
|
|
|
if (blockSet == null) {
|
2011-05-10 22:31:24 +00:00
|
|
|
player.printError("That's not a floating tree.");
|
|
|
|
return true;
|
|
|
|
}
|
2011-12-02 22:53:06 +00:00
|
|
|
|
|
|
|
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);
|
2011-05-10 22:31:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (MaxChangedBlocksException e) {
|
|
|
|
player.printError("Max blocks change limit reached.");
|
|
|
|
} finally {
|
|
|
|
session.remember(editSession);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-02 22:53:06 +00:00
|
|
|
Vector[] recurseDirections = {
|
|
|
|
PlayerDirection.NORTH.vector(),
|
|
|
|
PlayerDirection.EAST.vector(),
|
|
|
|
PlayerDirection.SOUTH.vector(),
|
|
|
|
PlayerDirection.WEST.vector(),
|
|
|
|
PlayerDirection.UP.vector(),
|
|
|
|
PlayerDirection.DOWN.vector(),
|
|
|
|
};
|
|
|
|
|
2011-05-10 22:31:24 +00:00
|
|
|
/**
|
|
|
|
* Helper method.
|
2011-12-02 22:53:06 +00:00
|
|
|
*
|
|
|
|
* @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.
|
2011-05-10 22:31:24 +00:00
|
|
|
*/
|
2011-12-02 22:53:06 +00:00
|
|
|
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;
|
|
|
|
}
|
2011-05-10 22:31:24 +00:00
|
|
|
|
2011-12-02 22:53:06 +00:00
|
|
|
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;
|
2011-05-10 22:31:24 +00:00
|
|
|
}
|
2011-12-02 22:53:06 +00:00
|
|
|
} // switch
|
|
|
|
} // if
|
|
|
|
} // for
|
|
|
|
} // while
|
2011-05-10 22:31:24 +00:00
|
|
|
|
2011-12-02 22:53:06 +00:00
|
|
|
return visited;
|
|
|
|
} // bfs
|
2011-05-10 22:31:24 +00:00
|
|
|
}
|