2011-02-18 23:14:43 +00:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* WorldEdit
|
2012-01-05 21:38:23 +00:00
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com> and contributors
|
2011-02-18 23:14:43 +00:00
|
|
|
*
|
|
|
|
* 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 com.sk89q.worldedit.*;
|
|
|
|
import com.sk89q.worldedit.bags.BlockBag;
|
|
|
|
import com.sk89q.worldedit.blocks.BaseBlock;
|
|
|
|
import com.sk89q.worldedit.blocks.BlockID;
|
2011-06-04 19:21:13 +00:00
|
|
|
import com.sk89q.worldedit.masks.CombinedMask;
|
2011-02-18 23:14:43 +00:00
|
|
|
import com.sk89q.worldedit.masks.Mask;
|
|
|
|
import com.sk89q.worldedit.patterns.Pattern;
|
|
|
|
import com.sk89q.worldedit.patterns.SingleBlockPattern;
|
2011-02-19 07:36:08 +00:00
|
|
|
import com.sk89q.worldedit.tools.brushes.Brush;
|
2011-02-18 23:14:43 +00:00
|
|
|
import com.sk89q.worldedit.tools.brushes.SphereBrush;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a shape at the place being looked at.
|
|
|
|
*
|
|
|
|
* @author sk89q
|
|
|
|
*/
|
2011-02-19 07:36:08 +00:00
|
|
|
public class BrushTool implements TraceTool {
|
2011-08-06 14:22:02 +00:00
|
|
|
protected static int MAX_RANGE = 500;
|
|
|
|
protected int range = -1;
|
2011-02-18 23:14:43 +00:00
|
|
|
private Mask mask = null;
|
2011-02-19 07:36:08 +00:00
|
|
|
private Brush brush = new SphereBrush();
|
2011-02-18 23:14:43 +00:00
|
|
|
private Pattern material = new SingleBlockPattern(new BaseBlock(BlockID.COBBLESTONE));
|
2011-08-07 03:10:59 +00:00
|
|
|
private double size = 1;
|
2011-05-02 00:06:40 +00:00
|
|
|
private String permission;
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-05-02 00:06:40 +00:00
|
|
|
/**
|
|
|
|
* Construct the tool.
|
|
|
|
*
|
|
|
|
* @param permission
|
|
|
|
*/
|
|
|
|
public BrushTool(String permission) {
|
|
|
|
this.permission = permission;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-05-02 00:06:40 +00:00
|
|
|
/**
|
|
|
|
* Checks to see if the player can still be using this tool (considering
|
|
|
|
* permissions and such).
|
|
|
|
*
|
|
|
|
* @param player
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public boolean canUse(LocalPlayer player) {
|
|
|
|
return player.hasPermission(permission);
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-18 23:14:43 +00:00
|
|
|
/**
|
|
|
|
* Get the filter.
|
|
|
|
*
|
|
|
|
* @return the filter
|
|
|
|
*/
|
|
|
|
public Mask getMask() {
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the block filter used for identifying blocks to replace.
|
|
|
|
*
|
|
|
|
* @param filter the filter to set
|
|
|
|
*/
|
|
|
|
public void setMask(Mask filter) {
|
|
|
|
this.mask = filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the brush.
|
|
|
|
*
|
|
|
|
* @param brush
|
2011-05-02 00:37:05 +00:00
|
|
|
* @param perm
|
2011-02-18 23:14:43 +00:00
|
|
|
*/
|
2011-05-02 00:06:40 +00:00
|
|
|
public void setBrush(Brush brush, String perm) {
|
2011-02-18 23:14:43 +00:00
|
|
|
this.brush = brush;
|
2011-05-02 00:06:40 +00:00
|
|
|
this.permission = perm;
|
2011-02-18 23:14:43 +00:00
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-18 23:14:43 +00:00
|
|
|
/**
|
|
|
|
* Get the current brush.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2011-02-19 07:36:08 +00:00
|
|
|
public Brush getBrush() {
|
2011-02-18 23:14:43 +00:00
|
|
|
return brush;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-18 23:14:43 +00:00
|
|
|
/**
|
|
|
|
* Set the material.
|
|
|
|
*
|
|
|
|
* @param material
|
|
|
|
*/
|
|
|
|
public void setFill(Pattern material) {
|
|
|
|
this.material = material;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-18 23:14:43 +00:00
|
|
|
/**
|
|
|
|
* Get the material.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Pattern getMaterial() {
|
|
|
|
return material;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the set brush size.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2011-08-07 03:10:59 +00:00
|
|
|
public double getSize() {
|
2011-02-18 23:14:43 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the set brush size.
|
|
|
|
*
|
2011-08-07 03:10:59 +00:00
|
|
|
* @param radius
|
2011-02-18 23:14:43 +00:00
|
|
|
*/
|
2011-08-07 03:10:59 +00:00
|
|
|
public void setSize(double radius) {
|
|
|
|
this.size = radius;
|
2011-02-18 23:14:43 +00:00
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-06-22 22:28:56 +00:00
|
|
|
/**
|
|
|
|
* Get the set brush range.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public int getRange() {
|
|
|
|
return (range < 0) ? MAX_RANGE : Math.min(range, MAX_RANGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the set brush range.
|
|
|
|
*
|
|
|
|
* @param size
|
|
|
|
*/
|
|
|
|
public void setRange(int range) {
|
|
|
|
this.range = range;
|
|
|
|
}
|
2011-02-18 23:14:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform the action. Should return true to deny the default
|
|
|
|
* action.
|
|
|
|
*
|
|
|
|
* @param player
|
|
|
|
* @param session
|
|
|
|
* @return true to deny
|
|
|
|
*/
|
2011-08-06 14:22:02 +00:00
|
|
|
public boolean actPrimary(ServerInterface server, LocalConfiguration config,
|
2011-02-18 23:14:43 +00:00
|
|
|
LocalPlayer player, LocalSession session) {
|
2011-06-22 22:28:56 +00:00
|
|
|
WorldVector target = null;
|
2011-09-19 01:49:45 +00:00
|
|
|
target = player.getBlockTrace(getRange(), true);
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-18 23:14:43 +00:00
|
|
|
if (target == null) {
|
|
|
|
player.printError("No block in sight!");
|
|
|
|
return true;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-18 23:14:43 +00:00
|
|
|
BlockBag bag = session.getBlockBag(player);
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-06-04 19:21:13 +00:00
|
|
|
EditSession editSession = session.createEditSession(player);
|
2012-03-15 15:42:56 +00:00
|
|
|
if (mask != null) {
|
2012-03-21 09:15:29 +00:00
|
|
|
mask.prepare(session, player, target);
|
2012-03-15 15:42:56 +00:00
|
|
|
Mask existingMask = editSession.getMask();
|
|
|
|
if (existingMask == null) {
|
|
|
|
editSession.setMask(mask);
|
|
|
|
} else if (existingMask instanceof CombinedMask) {
|
|
|
|
((CombinedMask) existingMask).add(mask);
|
|
|
|
} else {
|
|
|
|
CombinedMask newMask = new CombinedMask(existingMask);
|
|
|
|
newMask.add(mask);
|
|
|
|
editSession.setMask(newMask);
|
|
|
|
}
|
2011-02-18 23:14:43 +00:00
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-18 23:14:43 +00:00
|
|
|
try {
|
|
|
|
brush.build(editSession, target, material, size);
|
|
|
|
} catch (MaxChangedBlocksException e) {
|
|
|
|
player.printError("Max blocks change limit reached.");
|
|
|
|
} finally {
|
|
|
|
if (bag != null) {
|
|
|
|
bag.flushChanges();
|
|
|
|
}
|
|
|
|
session.remember(editSession);
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-02-18 23:14:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|