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;
|
|
|
|
import java.util.Iterator;
|
|
|
|
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-09-18 06:37:11 +00:00
|
|
|
private static final BaseBlock air = new BaseBlock(BlockID.AIR);
|
2011-05-10 22:31:24 +00:00
|
|
|
private int range;
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-05-10 22:31:24 +00:00
|
|
|
public FloatingTreeRemover() {
|
|
|
|
this.range = 100;
|
|
|
|
}
|
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) {
|
|
|
|
LocalWorld world = clicked.getWorld();
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-05-10 22:31:24 +00:00
|
|
|
int initialType = world.getBlockType(clicked);
|
|
|
|
int block;
|
2011-06-04 17:30:45 +00:00
|
|
|
|
|
|
|
EditSession editSession = session.createEditSession(player);
|
2011-05-10 22:31:24 +00:00
|
|
|
|
2011-09-18 06:37:11 +00:00
|
|
|
if (initialType != BlockID.LOG
|
|
|
|
&& initialType != BlockID.LEAVES
|
|
|
|
&& initialType != BlockID.BROWN_MUSHROOM_CAP
|
|
|
|
&& initialType != BlockID.RED_MUSHROOM_CAP) {
|
2011-05-10 22:31:24 +00:00
|
|
|
player.printError("That's not a floating tree.");
|
|
|
|
return true;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-05-10 22:31:24 +00:00
|
|
|
HashSet<BlockVector> blockSet = new HashSet<BlockVector>();
|
|
|
|
try {
|
|
|
|
if (!recurse(server, editSession, world, clicked.toBlockVector(),
|
|
|
|
clicked, range, blockSet, 0)) {
|
|
|
|
player.printError("That's not a floating tree.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (Iterator<BlockVector> iterator = blockSet.iterator(); iterator.hasNext();) {
|
|
|
|
BlockVector blockVector = iterator.next();
|
|
|
|
block = editSession.getBlock(blockVector).getType();
|
2011-09-18 06:37:11 +00:00
|
|
|
if (block == BlockID.LOG
|
|
|
|
|| block == BlockID.LEAVES
|
|
|
|
|| block == BlockID.BROWN_MUSHROOM_CAP
|
|
|
|
|| block == BlockID.RED_MUSHROOM_CAP) {
|
2011-05-10 22:31:24 +00:00
|
|
|
editSession.setBlock(blockVector, air);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (MaxChangedBlocksException e) {
|
|
|
|
player.printError("Max blocks change limit reached.");
|
|
|
|
} finally {
|
|
|
|
session.remember(editSession);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method.
|
|
|
|
*
|
|
|
|
* @param server
|
2011-09-18 06:37:11 +00:00
|
|
|
* @param editSession
|
2011-05-10 22:31:24 +00:00
|
|
|
* @param world
|
|
|
|
* @param pos
|
|
|
|
* @param origin
|
|
|
|
* @param size
|
|
|
|
* @param initialType
|
|
|
|
* @param visited
|
2011-09-18 06:37:11 +00:00
|
|
|
* @param lastBlock
|
2011-05-10 22:31:24 +00:00
|
|
|
*/
|
|
|
|
private boolean recurse(ServerInterface server, EditSession editSession,
|
|
|
|
LocalWorld world, BlockVector pos,
|
|
|
|
Vector origin, int size,
|
|
|
|
Set<BlockVector> visited, int lastBlock)
|
|
|
|
throws MaxChangedBlocksException {
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-05-10 22:31:24 +00:00
|
|
|
if (origin.distance(pos) > size || visited.contains(pos)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
visited.add(pos);
|
|
|
|
|
|
|
|
int block = editSession.getBlock(pos).getType();
|
2011-11-23 01:29:48 +00:00
|
|
|
if (block == BlockID.AIR || block == BlockID.SNOW) {
|
2011-05-10 22:31:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-09-18 06:37:11 +00:00
|
|
|
if (block != BlockID.LOG
|
|
|
|
&& block != BlockID.LEAVES
|
|
|
|
&& block != BlockID.BROWN_MUSHROOM_CAP
|
|
|
|
&& block != BlockID.RED_MUSHROOM_CAP) {
|
2011-05-10 22:31:24 +00:00
|
|
|
if (lastBlock == BlockID.LEAVES) {
|
|
|
|
return true;
|
2011-09-18 06:37:11 +00:00
|
|
|
} else {
|
2011-05-10 22:31:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-05-10 22:31:24 +00:00
|
|
|
}
|
|
|
|
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int i = -1; i <= 1; ++i) {
|
|
|
|
for (int j = -1; j <= 1; ++j) {
|
|
|
|
for (int k = -1; k <= 1; ++k) {
|
2011-05-10 22:31:24 +00:00
|
|
|
if (Math.abs(i) + Math.abs(j) + Math.abs(k) == 1) {
|
|
|
|
if (!recurse(server, editSession, world, pos.add(i, j, k).toBlockVector(),
|
|
|
|
origin, size, visited, block)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|