More file moving.

This commit is contained in:
sk89q
2011-05-01 01:30:33 -07:00
parent 4bcbfa76ef
commit 582b98dad0
206 changed files with 133 additions and 2 deletions

View File

@ -0,0 +1,112 @@
// $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.bukkit;
import java.io.IOException;
import java.util.HashSet;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.util.config.Configuration;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.LogFormat;
import com.sk89q.worldedit.snapshots.SnapshotRepository;
public class BukkitConfiguration extends LocalConfiguration {
private Configuration config;
private Logger logger;
public boolean noOpPermissions = false;
public BukkitConfiguration(Configuration config, Logger logger) {
this.config = config;
this.logger = logger;
}
@Override
public void load() {
showFirstUseVersion = false;
profile = config.getBoolean("debug", profile);
wandItem = config.getInt("wand-item", wandItem);
defaultChangeLimit = Math.max(-1, config.getInt(
"limits.max-blocks-changed.default", defaultChangeLimit));
maxChangeLimit = Math.max(-1,
config.getInt("limits.max-blocks-changed.maximum", maxChangeLimit));
maxRadius = Math.max(-1, config.getInt("limits.max-radius", maxRadius));
maxSuperPickaxeSize = Math.max(1, config.getInt(
"limits.max-super-pickaxe-size", maxSuperPickaxeSize));
registerHelp = true;
logCommands = config.getBoolean("logging.log-commands", logCommands);
superPickaxeDrop = config.getBoolean("super-pickaxe.drop-items",
superPickaxeDrop);
superPickaxeManyDrop = config.getBoolean(
"super-pickaxe.many-drop-items", superPickaxeManyDrop);
noDoubleSlash = config.getBoolean("no-double-slash", noDoubleSlash);
useInventory = config.getBoolean("use-inventory.enable", useInventory);
useInventoryOverride = config.getBoolean("use-inventory.allow-override",
useInventoryOverride);
maxBrushRadius = config.getInt("limits.max-brush-radius", maxBrushRadius);
navigationWand = config.getInt("navigation-wand.item", navigationWand);
navigationWandMaxDistance = config.getInt("navigation-wand.max-distance", navigationWandMaxDistance);
scriptTimeout = config.getInt("scripting.timeout", scriptTimeout);
scriptsDir = config.getString("scripting.dir", scriptsDir);
saveDir = config.getString("saving.dir", saveDir);
disallowedBlocks = new HashSet<Integer>(config.getIntList("limits.disallowed-blocks", null));
allowedDataCycleBlocks = new HashSet<Integer>(config.getIntList("limits.allowed-data-cycle-blocks", null));
noOpPermissions = config.getBoolean("no-op-permissions", false);
LocalSession.MAX_HISTORY_SIZE = Math.max(15, config.getInt("history.size", 15));
String snapshotsDir = config.getString("snapshots.directory", "");
if (!snapshotsDir.trim().equals("")) {
snapshotRepo = new SnapshotRepository(snapshotsDir);
} else {
snapshotRepo = null;
}
String type = config.getString("shell-save-type", "").trim();
shellSaveType = type.equals("") ? null : type;
String logFile = config.getString("logging.file", "");
if (!logFile.equals("")) {
try {
FileHandler handler = new FileHandler(logFile, true);
handler.setFormatter(new LogFormat());
logger.addHandler(handler);
} catch (IOException e) {
logger.log(Level.WARNING, "Could not use log file " + logFile + ": "
+ e.getMessage());
}
} else {
for (Handler handler : logger.getHandlers()) {
logger.removeHandler(handler);
}
}
}
}

View File

@ -0,0 +1,136 @@
// $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.bukkit;
import org.bukkit.*;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.bags.BlockBag;
import com.sk89q.worldedit.cui.CUIEvent;
public class BukkitPlayer extends LocalPlayer {
private Player player;
private WorldEditPlugin plugin;
public BukkitPlayer(WorldEditPlugin plugin, ServerInterface server, Player player) {
super(server);
this.plugin = plugin;
this.player = player;
}
@Override
public int getItemInHand() {
ItemStack itemStack = player.getItemInHand();
return itemStack != null ? itemStack.getTypeId() : 0;
}
@Override
public String getName() {
return player.getName();
}
@Override
public WorldVector getPosition() {
Location loc = player.getLocation();
return new WorldVector(new BukkitWorld(loc.getWorld()),
loc.getX(), loc.getY(), loc.getZ());
}
@Override
public double getPitch() {
return player.getLocation().getPitch();
}
@Override
public double getYaw() {
return player.getLocation().getYaw();
}
@Override
public void giveItem(int type, int amt) {
player.getInventory().addItem(new ItemStack(type, amt));
}
@Override
public void printRaw(String msg) {
player.sendMessage(msg);
}
@Override
public void print(String msg) {
player.sendMessage("\u00A7d" + msg);
}
@Override
public void printDebug(String msg) {
player.sendMessage("\u00A77" + msg);
}
@Override
public void printError(String msg) {
player.sendMessage("\u00A7c" + msg);
}
@Override
public void setPosition(Vector pos, float pitch, float yaw) {
player.teleport(new Location(player.getWorld(), pos.getX(), pos.getY(),
pos.getZ(), yaw, pitch));
}
@Override
public String[] getGroups() {
return plugin.getPermissionsResolver().getGroups(player.getName());
}
@Override
public BlockBag getInventoryBlockBag() {
return new BukkitPlayerBlockBag(player);
}
@Override
public boolean hasPermission(String perm) {
return (!plugin.getLocalConfiguration().noOpPermissions && player.isOp())
|| plugin.getPermissionsResolver().hasPermission(player.getName(), perm);
}
@Override
public LocalWorld getWorld() {
return new BukkitWorld(player.getWorld());
}
@Override
public void dispatchCUIEvent(CUIEvent event) {
String[] params = event.getParameters();
if (params.length > 0) {
player.sendRawMessage("\u00A75\u00A76\u00A74\u00A75" + event.getTypeId()
+ "|" + StringUtil.joinString(params, "|"));
} else {
player.sendRawMessage("\u00A75\u00A76\u00A74\u00A75" + event.getTypeId());
}
}
@Override
public void dispatchCUIHandshake() {
player.sendRawMessage("\u00A75\u00A76\u00A74\u00A75");
}
}

View File

@ -0,0 +1,193 @@
// $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.bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.bags.*;
public class BukkitPlayerBlockBag extends BlockBag {
/**
* Player instance.
*/
private Player player;
/**
* The player's inventory;
*/
private ItemStack[] items;
/**
* Construct the object.
*
* @param player
*/
public BukkitPlayerBlockBag(Player player) {
this.player = player;
}
/**
* Loads inventory on first use.
*/
private void loadInventory() {
if (items == null) {
items = player.getInventory().getContents();
}
}
/**
* Get the player.
*
* @return
*/
public Player getPlayer() {
return player;
}
/**
* Get a block.
*
* @param id
*/
@Override
public void fetchBlock(int id) throws BlockBagException {
if (id == 0) {
throw new IllegalArgumentException("Can't fetch air block");
}
loadInventory();
boolean found = false;
for (int slot = 0; slot < items.length; slot++) {
ItemStack item = items[slot];
if (item == null) continue;
if (item.getTypeId() == id) {
int amount = item.getAmount();
// Unlimited
if (amount < 0) {
return;
}
if (amount > 1) {
item.setAmount(amount - 1);
found = true;
} else {
items[slot] = null;
found = true;
}
break;
}
}
if (found) {
} else {
throw new OutOfBlocksException();
}
}
/**
* Store a block.
*
* @param id
*/
@Override
public void storeBlock(int id) throws BlockBagException {
if (id == 0) {
throw new IllegalArgumentException("Can't store air block");
}
loadInventory();
boolean found = false;
int freeSlot = -1;
for (int slot = 0; slot < items.length; slot++) {
ItemStack item = items[slot];
// Delay using up a free slot until we know there are no stacks
// of this item to merge into
if (item == null) {
if (freeSlot == -1) {
freeSlot = slot;
}
continue;
}
if (item.getTypeId() == id) {
int amount = item.getAmount();
// Unlimited
if (amount < 0) {
return;
}
if (amount < 64) {
item.setAmount(amount + 1);
found = true;
break;
}
}
}
if (!found && freeSlot > -1) {
items[freeSlot] = new ItemStack(id, 1);
found = true;
}
if (found) {
} else {
throw new OutOfSpaceException(id);
}
}
/**
* Flush any changes. This is called at the end.
*/
@Override
public void flushChanges() {
if (items != null) {
player.getInventory().setContents(items);
items = null;
}
}
/**
* Adds a position to be used a source.
*
* @param pos
*/
@Override
public void addSourcePosition(Vector pos) {
}
/**
* Adds a position to be used a source.
*
* @param pos
*/
@Override
public void addSingleSourcePosition(Vector pos) {
}
}

View File

@ -0,0 +1,51 @@
// $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.bukkit;
import org.bukkit.*;
import org.bukkit.entity.CreatureType;
import com.sk89q.worldedit.ServerInterface;
public class BukkitServerInterface extends ServerInterface {
public Server server;
public WorldEditPlugin plugin;
public BukkitServerInterface(WorldEditPlugin plugin, Server server) {
this.plugin = plugin;
this.server = server;
}
@Override
public int resolveItem(String name) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isValidMobType(String type) {
return CreatureType.fromName(type) != null;
}
@Override
public void reload() {
plugin.loadConfiguration();
}
}

View File

@ -0,0 +1,58 @@
// $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.bukkit;
import java.util.List;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector;
public class BukkitUtil {
private BukkitUtil() {
}
public static Location toLocation(World world, Vector loc) {
return new Location(world, loc.getX(), loc.getY(), loc.getZ());
}
public static BlockVector toVector(Block block) {
return new BlockVector(block.getX(), block.getY(), block.getZ());
}
public static Vector toVector(Location loc) {
return new Vector(loc.getX(), loc.getY(), loc.getZ());
}
public static Vector toVector(org.bukkit.util.Vector vector) {
return new Vector(vector.getX(), vector.getY(), vector.getZ());
}
public static Player matchSinglePlayer(Server server, String name) {
List<Player> players = server.matchPlayer(name);
if (players.size() == 0) {
return null;
}
return players.get(0);
}
}

View File

@ -0,0 +1,613 @@
// $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.bukkit;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Furnace;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.Sign;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Boat;
import org.bukkit.entity.Creature;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Ghast;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Minecart;
import org.bukkit.entity.Painting;
import org.bukkit.entity.Slime;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.Location;
import org.bukkit.TreeType;
import org.bukkit.World;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalWorld;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.Vector2D;
import com.sk89q.worldedit.blocks.*;
import com.sk89q.worldedit.regions.Region;
public class BukkitWorld extends LocalWorld {
private World world;
/**
* Construct the object.
* @param world
*/
public BukkitWorld(World world) {
this.world = world;
}
/**
* Get the world handle.
*
* @return
*/
public World getWorld() {
return world;
}
/**
* Set block type.
*
* @param pt
* @param type
* @return
*/
@Override
public boolean setBlockType(Vector pt, int type) {
return world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()).setTypeId(type);
}
/**
* Get block type.
*
* @param pt
* @return
*/
@Override
public int getBlockType(Vector pt) {
return world.getBlockTypeIdAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
}
/**
* Set block data.
*
* @param pt
* @param data
*/
@Override
public void setBlockData(Vector pt, int data) {
world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()).setData((byte)data);
}
/**
* Get block data.
*
* @param pt
* @return
*/
@Override
public int getBlockData(Vector pt) {
return world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()).getData();
}
/**
* Get block light level.
*
* @param pt
* @return
*/
@Override
public int getBlockLightLevel(Vector pt) {
return world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()).getLightLevel();
}
/**
* Regenerate an area.
*
* @param region
* @param editSession
* @return
*/
@Override
public boolean regenerate(Region region, EditSession editSession) {
BaseBlock[] history = new BaseBlock[16 * 16 * 128];
for (Vector2D chunk : region.getChunks()) {
Vector min = new Vector(chunk.getBlockX() * 16, 0, chunk.getBlockZ() * 16);
// First save all the blocks inside
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 128; y++) {
for (int z = 0; z < 16; z++) {
Vector pt = min.add(x, y, z);
int index = y * 16 * 16 + z * 16 + x;
history[index] = editSession.getBlock(pt);
}
}
}
try {
world.regenerateChunk(chunk.getBlockX(), chunk.getBlockZ());
} catch (Throwable t) {
t.printStackTrace();
}
// Then restore
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 128; y++) {
for (int z = 0; z < 16; z++) {
Vector pt = min.add(x, y, z);
int index = y * 16 * 16 + z * 16 + x;
// We have to restore the block if it was outside
if (!region.contains(pt)) {
editSession.smartSetBlock(pt, history[index]);
} else { // Otherwise fool with history
editSession.rememberChange(pt, history[index],
editSession.rawGetBlock(pt));
}
}
}
}
}
return true;
}
/**
* Attempts to accurately copy a BaseBlock's extra data to the world.
*
* @param pt
* @param block
* @return
*/
@Override
public boolean copyToWorld(Vector pt, BaseBlock block) {
// Signs
if (block instanceof SignBlock) {
setSignText(pt, ((SignBlock)block).getText());
return true;
// Furnaces
} else if (block instanceof FurnaceBlock) {
Block bukkitBlock = world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
if (bukkitBlock == null) return false;
BlockState state = bukkitBlock.getState();
if (!(state instanceof Furnace)) return false;
Furnace bukkit = (Furnace)state;
FurnaceBlock we = (FurnaceBlock)block;
bukkit.setBurnTime(we.getBurnTime());
bukkit.setCookTime(we.getCookTime());
return setContainerBlockContents(pt, ((ContainerBlock)block).getItems());
// Chests/dispenser
} else if (block instanceof ContainerBlock) {
return setContainerBlockContents(pt, ((ContainerBlock)block).getItems());
// Mob spawners
} else if (block instanceof MobSpawnerBlock) {
Block bukkitBlock = world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
if (bukkitBlock == null) return false;
BlockState state = bukkitBlock.getState();
if (!(state instanceof CreatureSpawner)) return false;
CreatureSpawner bukkit = (CreatureSpawner)state;
MobSpawnerBlock we = (MobSpawnerBlock)block;
bukkit.setCreatureTypeId(we.getMobType());
bukkit.setDelay(we.getDelay());
return true;
// Note block
} else if (block instanceof NoteBlock) {
Block bukkitBlock = world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
if (bukkitBlock == null) return false;
BlockState state = bukkitBlock.getState();
if (!(state instanceof org.bukkit.block.NoteBlock)) return false;
org.bukkit.block.NoteBlock bukkit = (org.bukkit.block.NoteBlock)state;
NoteBlock we = (NoteBlock)block;
bukkit.setNote(we.getNote());
return true;
}
return false;
}
/**
* Attempts to read a BaseBlock's extra data from the world.
*
* @param pt
* @param block
* @return
*/
@Override
public boolean copyFromWorld(Vector pt, BaseBlock block) {
// Signs
if (block instanceof SignBlock) {
((SignBlock)block).setText(getSignText(pt));
return true;
// Furnaces
} else if (block instanceof FurnaceBlock) {
Block bukkitBlock = world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
if (bukkitBlock == null) return false;
BlockState state = bukkitBlock.getState();
if (!(state instanceof Furnace)) return false;
Furnace bukkit = (Furnace)state;
FurnaceBlock we = (FurnaceBlock)block;
we.setBurnTime(bukkit.getBurnTime());
we.setCookTime(bukkit.getCookTime());
((ContainerBlock)block).setItems(getContainerBlockContents(pt));
return true;
// Chests/dispenser
} else if (block instanceof ContainerBlock) {
((ContainerBlock)block).setItems(getContainerBlockContents(pt));
return true;
// Mob spawners
} else if (block instanceof MobSpawnerBlock) {
Block bukkitBlock = world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
if (bukkitBlock == null) return false;
BlockState state = bukkitBlock.getState();
if (!(state instanceof CreatureSpawner)) return false;
CreatureSpawner bukkit = (CreatureSpawner)state;
MobSpawnerBlock we = (MobSpawnerBlock)block;
we.setMobType(bukkit.getCreatureTypeId());
we.setDelay((short)bukkit.getDelay());
return true;
// Note block
} else if (block instanceof NoteBlock) {
Block bukkitBlock = world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
if (bukkitBlock == null) return false;
BlockState state = bukkitBlock.getState();
if (!(state instanceof org.bukkit.block.NoteBlock)) return false;
org.bukkit.block.NoteBlock bukkit = (org.bukkit.block.NoteBlock)state;
NoteBlock we = (NoteBlock)block;
we.setNote(bukkit.getNote());
}
return false;
}
/**
* Clear a chest's contents.
*
* @param pt
*/
@Override
public boolean clearContainerBlockContents(Vector pt) {
Block block = world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
if (block == null) {
return false;
}
BlockState state = block.getState();
if (!(state instanceof org.bukkit.block.ContainerBlock)) {
return false;
}
org.bukkit.block.ContainerBlock chest = (org.bukkit.block.ContainerBlock)state;
Inventory inven = chest.getInventory();
inven.clear();
return true;
}
/**
* Generate a tree at a location.
*
* @param pt
* @return
*/
@Override
public boolean generateTree(EditSession editSession, Vector pt) {
return world.generateTree(BukkitUtil.toLocation(world, pt), TreeType.TREE,
new EditSessionBlockChangeDelegate(editSession));
}
/**
* Generate a big tree at a location.
*
* @param pt
* @return
*/
@Override
public boolean generateBigTree(EditSession editSession, Vector pt) {
return world.generateTree(BukkitUtil.toLocation(world, pt), TreeType.BIG_TREE,
new EditSessionBlockChangeDelegate(editSession));
}
/**
* Generate a birch tree at a location.
*
* @param pt
* @return
*/
@Override
public boolean generateBirchTree(EditSession editSession, Vector pt) {
return world.generateTree(BukkitUtil.toLocation(world, pt), TreeType.BIRCH,
new EditSessionBlockChangeDelegate(editSession));
}
/**
* Generate a redwood tree at a location.
*
* @param pt
* @return
*/
@Override
public boolean generateRedwoodTree(EditSession editSession, Vector pt) {
return world.generateTree(BukkitUtil.toLocation(world, pt), TreeType.REDWOOD,
new EditSessionBlockChangeDelegate(editSession));
}
/**
* Generate a redwood tree at a location.
*
* @param pt
* @return
*/
@Override
public boolean generateTallRedwoodTree(EditSession editSession, Vector pt) {
return world.generateTree(BukkitUtil.toLocation(world, pt), TreeType.TALL_REDWOOD,
new EditSessionBlockChangeDelegate(editSession));
}
/**
* Drop an item.
*
* @param pt
* @param item
*/
@Override
public void dropItem(Vector pt, BaseItemStack item) {
ItemStack bukkitItem = new ItemStack(item.getType(), item.getAmount(),
(byte)item.getDamage());
world.dropItemNaturally(toLocation(pt), bukkitItem);
}
/**
* Kill mobs in an area.
*
* @param origin
* @param radius -1 for all mobs
* @return
*/
@Override
public int killMobs(Vector origin, int radius) {
int num = 0;
double radiusSq = Math.pow(radius, 2);
for (LivingEntity ent : world.getLivingEntities()) {
if (ent instanceof Creature || ent instanceof Ghast || ent instanceof Slime) {
if (radius == -1
|| origin.distanceSq(BukkitUtil.toVector(ent.getLocation())) <= radiusSq) {
ent.remove();
num++;
}
}
}
return num;
}
/**
* Remove entities in an area.
*
* @param origin
* @param radius
* @return
*/
@Override
public int removeEntities(EntityType type, Vector origin, int radius) {
int num = 0;
double radiusSq = Math.pow(radius, 2);
for (Entity ent : world.getEntities()) {
if (radius != -1
&& origin.distanceSq(BukkitUtil.toVector(ent.getLocation())) > radiusSq) {
continue;
}
switch (type) {
case ARROWS:
if (ent instanceof Arrow) {
ent.remove();
num++;
}
break;
case BOATS:
if (ent instanceof Boat) {
ent.remove();
num++;
}
break;
case ITEMS:
if (ent instanceof Item) {
ent.remove();
num++;
}
break;
case MINECARTS:
if (ent instanceof Minecart) {
ent.remove();
num++;
}
break;
case PAINTINGS:
if (ent instanceof Painting) {
ent.remove();
num++;
}
break;
case TNT:
if (ent instanceof TNTPrimed) {
ent.remove();
num++;
}
break;
default:
continue;
}
}
return num;
}
private Location toLocation(Vector pt) {
return new Location(world, pt.getX(), pt.getY(), pt.getZ());
}
/**
* Set a sign's text.
*
* @param pt
* @param text
* @return
*/
private boolean setSignText(Vector pt, String[] text) {
Block block = world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
if (block == null) return false;
BlockState state = block.getState();
if (state == null || !(state instanceof Sign)) return false;
Sign sign = (Sign)state;
sign.setLine(0, text[0]);
sign.setLine(1, text[1]);
sign.setLine(2, text[2]);
sign.setLine(3, text[3]);
sign.update();
return true;
}
/**
* Get a sign's text.
*
* @param pt
* @return
*/
private String[] getSignText(Vector pt) {
Block block = world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
if (block == null) return new String[] { "", "", "", "" };
BlockState state = block.getState();
if (state == null || !(state instanceof Sign)) return new String[] { "", "", "", "" };
Sign sign = (Sign)state;
String line0 = sign.getLine(0);
String line1 = sign.getLine(1);
String line2 = sign.getLine(2);
String line3 = sign.getLine(3);
return new String[] {
line0 != null ? line0 : "",
line1 != null ? line1 : "",
line2 != null ? line2 : "",
line3 != null ? line3 : "",
};
}
/**
* Get a container block's contents.
*
* @param pt
* @return
*/
private BaseItemStack[] getContainerBlockContents(Vector pt) {
Block block = world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
if (block == null) {
return new BaseItemStack[0];
}
BlockState state = block.getState();
if (!(state instanceof org.bukkit.block.ContainerBlock)) {
return new BaseItemStack[0];
}
org.bukkit.block.ContainerBlock container = (org.bukkit.block.ContainerBlock)state;
Inventory inven = container.getInventory();
int size = inven.getSize();
BaseItemStack[] contents = new BaseItemStack[size];
for (int i = 0; i < size; i++) {
ItemStack bukkitStack = inven.getItem(i);
if (bukkitStack.getTypeId() > 0) {
contents[i] = new BaseItemStack(
bukkitStack.getTypeId(),
bukkitStack.getAmount(),
bukkitStack.getDurability());
}
}
return contents;
}
/**
* Set a container block's contents.
*
* @param pt
* @param contents
* @return
*/
private boolean setContainerBlockContents(Vector pt, BaseItemStack[] contents) {
Block block = world.getBlockAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ());
if (block == null) {
return false;
}
BlockState state = block.getState();
if (!(state instanceof org.bukkit.block.ContainerBlock)) {
return false;
}
org.bukkit.block.ContainerBlock chest = (org.bukkit.block.ContainerBlock)state;
Inventory inven = chest.getInventory();
int size = inven.getSize();
for (int i = 0; i < size; i++) {
if (i >= contents.length) {
break;
}
if (contents[i] != null) {
inven.setItem(i, new ItemStack(contents[i].getType(),
contents[i].getAmount(),
(byte)contents[i].getDamage()));
} else {
inven.setItem(i, null);
}
}
return true;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof BukkitWorld)) {
return false;
}
return ((BukkitWorld)other).world.equals(world);
}
@Override
public int hashCode() {
return world.hashCode();
}
}

View File

@ -0,0 +1,59 @@
// $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.bukkit;
import org.bukkit.BlockChangeDelegate;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.blocks.BaseBlock;
/**
* Proxy class to catch calls to set blocks.
*
* @author sk89q
*/
public class EditSessionBlockChangeDelegate implements BlockChangeDelegate {
private EditSession editSession;
public EditSessionBlockChangeDelegate(EditSession editSession) {
this.editSession = editSession;
}
public boolean setRawTypeId(int x, int y, int z, int typeId) {
try {
return editSession.setBlock(new Vector(x, y, z), new BaseBlock(typeId));
} catch (MaxChangedBlocksException ex) {
return false;
}
}
public boolean setRawTypeIdAndData(int x, int y, int z, int typeId, int data) {
try {
return editSession.setBlock(new Vector(x, y, z), new BaseBlock(typeId, data));
} catch (MaxChangedBlocksException ex) {
return false;
}
}
public int getTypeId(int x, int y, int z) {
return editSession.getBlockType(new Vector(x, y, z));
}
}

View File

@ -0,0 +1,42 @@
// $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.bukkit;
import org.bukkit.entity.Player;
import com.sk89q.worldedit.LocalSession;
public class WorldEditAPI {
private WorldEditPlugin plugin;
public WorldEditAPI(WorldEditPlugin plugin) {
this.plugin = plugin;
}
/**
* Get the session for a player.
*
* @param player
* @return
*/
public LocalSession getSession(Player player) {
return plugin.getWorldEdit().getSession(
new BukkitPlayer(plugin, plugin.getServerInterface(), player));
}
}

View File

@ -0,0 +1,57 @@
// $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.bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerListener;
/**
* Handles all events thrown in relation to a Player
*/
public class WorldEditCriticalPlayerListener extends PlayerListener {
/**
* Plugin.
*/
private WorldEditPlugin plugin;
/**
* Construct the object;
*
* @param plugin
*/
public WorldEditCriticalPlayerListener(WorldEditPlugin plugin) {
this.plugin = plugin;
}
/**
* Called when a player joins a server
*
* @param event Relevant event details
*/
@Override
public void onPlayerJoin(PlayerJoinEvent event) {
wrapPlayer(event.getPlayer()).dispatchCUIHandshake();
}
private BukkitPlayer wrapPlayer(Player player) {
return new BukkitPlayer(plugin, plugin.getServerInterface(), player);
}
}

View File

@ -0,0 +1,138 @@
// $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.bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerAnimationEvent;
import org.bukkit.event.player.PlayerAnimationType;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerQuitEvent;
import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.LocalWorld;
import com.sk89q.worldedit.WorldVector;
import com.sk89q.worldedit.blocks.BlockID;
/**
* Handles all events thrown in relation to a Player
*/
public class WorldEditPlayerListener extends PlayerListener {
/**
* Plugin.
*/
private WorldEditPlugin plugin;
/**
* Called when a player plays an animation, such as an arm swing
*
* @param event Relevant event details
*/
@Override
public void onPlayerAnimation(PlayerAnimationEvent event) {
LocalPlayer localPlayer = wrapPlayer(event.getPlayer());
if (event.getAnimationType() == PlayerAnimationType.ARM_SWING) {
plugin.getWorldEdit().handleArmSwing(localPlayer);
}
// As of Minecraft 1.3, a block dig packet is no longer sent for
// bedrock, so we have to do an (inaccurate) detection ourself
WorldVector pt = localPlayer.getBlockTrace(5);
if (pt != null && pt.getWorld().getBlockType(pt) == BlockID.BEDROCK) {
if (plugin.getWorldEdit().handleBlockLeftClick(localPlayer, pt)) {
}
}
}
/**
* Construct the object;
*
* @param plugin
*/
public WorldEditPlayerListener(WorldEditPlugin plugin) {
this.plugin = plugin;
}
/**
* Called when a player leaves a server
*
* @param event Relevant event details
*/
@Override
public void onPlayerQuit(PlayerQuitEvent event) {
plugin.getWorldEdit().handleDisconnect(wrapPlayer(event.getPlayer()));
}
/**
* Called when a player attempts to use a command
*
* @param event Relevant event details
*/
@Override
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
String[] split = event.getMessage().split(" ");
if (plugin.getWorldEdit().handleCommand(wrapPlayer(event.getPlayer()), split)) {
event.setCancelled(true);
}
}
/**
* Called when a player interacts
*
* @param event Relevant event details
*/
@Override
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
LocalWorld world = new BukkitWorld(event.getClickedBlock().getWorld());
WorldVector pos = new WorldVector(world, event.getClickedBlock().getX(),
event.getClickedBlock().getY(), event.getClickedBlock().getZ());
LocalPlayer player = wrapPlayer(event.getPlayer());
if (plugin.getWorldEdit().handleBlockLeftClick(player, pos)) {
event.setCancelled(true);
}
} else if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
LocalWorld world = new BukkitWorld(event.getClickedBlock().getWorld());
WorldVector pos = new WorldVector(world, event.getClickedBlock().getX(),
event.getClickedBlock().getY(), event.getClickedBlock().getZ());
LocalPlayer player = wrapPlayer(event.getPlayer());
if (plugin.getWorldEdit().handleBlockRightClick(player, pos)) {
event.setCancelled(true);
}
if (plugin.getWorldEdit().handleRightClick(wrapPlayer(event.getPlayer()))) {
event.setCancelled(true);
}
} else if (event.getAction() == Action.RIGHT_CLICK_AIR) {
if (plugin.getWorldEdit().handleRightClick(wrapPlayer(event.getPlayer()))) {
event.setCancelled(true);
}
}
}
private BukkitPlayer wrapPlayer(Player player) {
return new BukkitPlayer(plugin, plugin.getServerInterface(), player);
}
}

View File

@ -0,0 +1,404 @@
// $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.bukkit;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.plugin.java.JavaPlugin;
import com.sk89q.bukkit.migration.PermissionsResolverManager;
import com.sk89q.bukkit.migration.PermissionsResolverServerListener;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.bags.BlockBag;
import com.sk89q.worldedit.bukkit.selections.*;
import com.sk89q.worldedit.regions.*;
/**
* Plugin for Bukkit.
*
* @author sk89q
*/
public class WorldEditPlugin extends JavaPlugin {
/**
* WorldEdit messages get sent here.
*/
private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit");
/**
* The server interface that all server-related API goes through.
*/
private ServerInterface server;
/**
* Main WorldEdit instance.
*/
private WorldEdit controller;
/**
* Deprecated API.
*/
private WorldEditAPI api;
/**
* Holds the configuration for WorldEdit.
*/
private BukkitConfiguration config;
/**
* The permissions resolver in use.
*/
private PermissionsResolverManager perms;
/**
* Called on plugin enable.
*/
public void onEnable() {
logger.info("WorldEdit " + getDescription().getVersion() + " enabled.");
// Make the data folders that WorldEdit uses
getDataFolder().mkdirs();
// Create the default configuration file
createDefaultConfiguration("config.yml");
// Set up configuration and such, including the permissions
// resolver
config = new BukkitConfiguration(getConfiguration(), logger);
perms = new PermissionsResolverManager(
getConfiguration(), getServer(), "WorldEdit", logger);
// Load the configuration
loadConfiguration();
// Setup interfaces
server = new BukkitServerInterface(this, getServer());
controller = new WorldEdit(server, config);
api = new WorldEditAPI(this);
// Now we can register events!
registerEvents();
}
/**
* Called on plugin disable.
*/
public void onDisable() {
controller.clearSessions();
}
/**
* Loads and reloads all configuration.
*/
protected void loadConfiguration() {
getConfiguration().load();
config.load();
perms.load();
}
/**
* Register the events used by WorldEdit.
*/
protected void registerEvents() {
PlayerListener playerListener = new WorldEditPlayerListener(this);
PlayerListener criticalPlayerListener = new WorldEditCriticalPlayerListener(this);
registerEvent(Event.Type.PLAYER_QUIT, playerListener);
registerEvent(Event.Type.PLAYER_ANIMATION, playerListener);
registerEvent(Event.Type.PLAYER_INTERACT, playerListener);
registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener);
registerEvent(Event.Type.PLAYER_JOIN, criticalPlayerListener, Priority.Lowest);
// The permissions resolver has some hooks of its own
(new PermissionsResolverServerListener(perms)).register(this);
}
/**
* Register an event.
*
* @param type
* @param listener
* @param priority
*/
protected void registerEvent(Event.Type type, Listener listener, Priority priority) {
getServer().getPluginManager()
.registerEvent(type, listener, priority, this);
}
/**
* Register an event at normal priority.
*
* @param type
* @param listener
*/
protected void registerEvent(Event.Type type, Listener listener) {
getServer().getPluginManager()
.registerEvent(type, listener, Priority.Normal, this);
}
/**
* Create a default configuration file from the .jar.
*
* @param name
*/
protected void createDefaultConfiguration(String name) {
File actual = new File(getDataFolder(), name);
if (!actual.exists()) {
InputStream input =
WorldEdit.class.getResourceAsStream("/defaults/" + name);
if (input != null) {
FileOutputStream output = null;
try {
output = new FileOutputStream(actual);
byte[] buf = new byte[8192];
int length = 0;
while ((length = input.read(buf)) > 0) {
output.write(buf, 0, length);
}
logger.info(getDescription().getName()
+ ": Default configuration file written: " + name);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (input != null)
input.close();
} catch (IOException e) {}
try {
if (output != null)
output.close();
} catch (IOException e) {}
}
}
}
}
/**
* Called on WorldEdit command.
*/
@Override
public boolean onCommand(CommandSender sender, Command cmd,
String commandLabel, String[] args) {
// Since WorldEdit is primarily made for use in-game, we're going
// to ignore the situation where the command sender is not aplayer.
if (!(sender instanceof Player)) {
return true;
}
Player player = (Player)sender;
// Add the command to the array because the underlying command handling
// code of WorldEdit expects it
String[] split = new String[args.length + 1];
System.arraycopy(args, 0, split, 1, args.length);
split[0] = "/" + cmd.getName();
controller.handleCommand(wrapPlayer(player), split);
return true;
}
/**
* Gets the session for the player.
*
* @param player
* @return
*/
public LocalSession getSession(Player player) {
return controller.getSession(wrapPlayer(player));
}
/**
* Gets the session for the player.
*
* @param player
* @return
*/
public EditSession createEditSession(Player player) {
LocalPlayer wePlayer = wrapPlayer(player);
LocalSession session = controller.getSession(wePlayer);
BlockBag blockBag = session.getBlockBag(wePlayer);
EditSession editSession =
new EditSession(wePlayer.getWorld(),
session.getBlockChangeLimit(), blockBag);
editSession.enableQueue();
return editSession;
}
/**
* Remember an edit session.
*
* @param player
* @param editSession
*/
public void remember(Player player, EditSession editSession) {
LocalPlayer wePlayer = wrapPlayer(player);
LocalSession session = controller.getSession(wePlayer);
session.remember(editSession);
editSession.flushQueue();
controller.flushBlockBag(wePlayer, editSession);
}
/**
* Wrap an operation into an EditSession.
*
* @param player
* @param op
* @throws Throwable
*/
public void perform(Player player, WorldEditOperation op)
throws Throwable {
LocalPlayer wePlayer = wrapPlayer(player);
LocalSession session = controller.getSession(wePlayer);
EditSession editSession = createEditSession(player);
try {
op.run(session, wePlayer, editSession);
} finally {
remember(player, editSession);
}
}
/**
* Get the API.
*
* @return
*/
@Deprecated
public WorldEditAPI getAPI() {
return api;
}
/**
* Returns the configuration used by WorldEdit.
*
* @return
*/
public BukkitConfiguration getLocalConfiguration() {
return config;
}
/**
* Get the permissions resolver in use.
*
* @return
*/
public PermissionsResolverManager getPermissionsResolver() {
return perms;
}
/**
* Used to wrap a Bukkit Player as a LocalPlayer.
*
* @param player
* @return
*/
public BukkitPlayer wrapPlayer(Player player) {
return new BukkitPlayer(this, this.server, player);
}
/**
* Get the server interface.
*
* @return
*/
public ServerInterface getServerInterface() {
return server;
}
/**
* Get WorldEdit.
*
* @return
*/
public WorldEdit getWorldEdit() {
return controller;
}
/**
* Gets the region selection for the player.
*
* @param player
* @return the selection or null if there was none
*/
public Selection getSelection(Player player) {
if (player == null) {
throw new IllegalArgumentException("Null player not allowed");
}
if (!player.isOnline()) {
throw new IllegalArgumentException("Offline player not allowed");
}
LocalSession session = controller.getSession(wrapPlayer(player));
RegionSelector selector = session.getRegionSelector();
try {
Region region = selector.getRegion();
World world = ((BukkitWorld) session.getSelectionWorld()).getWorld();
if (region instanceof CuboidRegion) {
return new CuboidSelection(world, selector, (CuboidRegion)region);
} else if (region instanceof Polygonal2DRegion) {
return new Polygonal2DSelection(world, selector, (Polygonal2DRegion)region);
} else {
return null;
}
} catch (IncompleteRegionException e) {
return null;
}
}
/**
* Sets the region selection for a player.
*
* @param player
* @param selection
*/
public void setSelection(Player player, Selection selection) {
if (player == null) {
throw new IllegalArgumentException("Null player not allowed");
}
if (!player.isOnline()) {
throw new IllegalArgumentException("Offline player not allowed");
}
if (selection == null) {
throw new IllegalArgumentException("Null selection not allowed");
}
LocalSession session = controller.getSession(wrapPlayer(player));
RegionSelector sel = selection.getRegionSelector();
session.setRegionSelector(new BukkitWorld(player.getWorld()), sel);
session.dispatchCUISelection(wrapPlayer(player));
}
}

View File

@ -0,0 +1,66 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010, 2011 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.bukkit.selections;
import org.bukkit.Location;
import org.bukkit.World;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.bukkit.BukkitUtil;
import com.sk89q.worldedit.regions.*;
public class CuboidSelection extends RegionSelection {
protected CuboidRegion cuboid;
public CuboidSelection(World world, Location pt1, Location pt2) {
this(world, BukkitUtil.toVector(pt1), BukkitUtil.toVector(pt2));
}
public CuboidSelection(World world, Vector pt1, Vector pt2) {
super(world);
if (pt1 == null) {
throw new IllegalArgumentException("Null point 1 not permitted");
}
if (pt2 == null) {
throw new IllegalArgumentException("Null point 2 not permitted");
}
CuboidRegionSelector sel = new CuboidRegionSelector();
sel.selectPrimary(pt1);
sel.selectSecondary(pt2);
try {
cuboid = sel.getRegion();
} catch (IncompleteRegionException e) {
throw new RuntimeException("IncompleteRegionException unexpectedly thrown");
}
setRegionSelector(sel);
setRegion(cuboid);
}
public CuboidSelection(World world, RegionSelector sel, CuboidRegion region) {
super(world, sel, region);
this.cuboid = region;
}
}

View File

@ -0,0 +1,66 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010, 2011 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.bukkit.selections;
import java.util.Collections;
import java.util.List;
import org.bukkit.World;
import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldedit.regions.*;
public class Polygonal2DSelection extends RegionSelection {
protected Polygonal2DRegion poly2d;
public Polygonal2DSelection(World world, RegionSelector sel, Polygonal2DRegion region) {
super(world, sel, region);
this.poly2d = region;
}
public Polygonal2DSelection(World world, List<BlockVector2D> points, int minY, int maxY) {
super(world);
minY = Math.min(Math.max(0, minY), 127);
maxY = Math.min(Math.max(0, maxY), 127);
Polygonal2DRegionSelector sel = new Polygonal2DRegionSelector();
poly2d = sel.getIncompleteRegion();
for (BlockVector2D pt : points) {
if (pt == null) {
throw new IllegalArgumentException("Null point not permitted");
}
poly2d.addPoint(pt);
}
poly2d.setMinimumY(minY);
poly2d.setMaximumY(maxY);
sel.learnChanges();
setRegionSelector(sel);
setRegion(poly2d);
}
public List<BlockVector2D> getNativePoints() {
return Collections.unmodifiableList(poly2d.getPoints());
}
}

View File

@ -0,0 +1,106 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010, 2011 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.bukkit.selections;
import static com.sk89q.worldedit.bukkit.BukkitUtil.toLocation;
import static com.sk89q.worldedit.bukkit.BukkitUtil.toVector;
import org.bukkit.Location;
import org.bukkit.World;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionSelector;
public abstract class RegionSelection implements Selection {
private World world;
private RegionSelector selector;
private Region region;
public RegionSelection(World world) {
this.world = world;
}
public RegionSelection(World world, RegionSelector selector, Region region) {
this.world = world;
this.region = region;
this.selector = selector;
}
protected Region getRegion() {
return region;
}
protected void setRegion(Region region) {
this.region = region;
}
public RegionSelector getRegionSelector() {
return selector;
}
protected void setRegionSelector(RegionSelector selector) {
this.selector = selector;
}
public Location getMinimumPoint() {
return toLocation(world, region.getMinimumPoint());
}
public Vector getNativeMinimumPoint() {
return region.getMinimumPoint();
}
public Location getMaximumPoint() {
return toLocation(world, region.getMaximumPoint());
}
public Vector getNativeMaximumPoint() {
return region.getMaximumPoint();
}
public World getWorld() {
return world;
}
public int getArea() {
return region.getArea();
}
public int getWidth() {
return region.getWidth();
}
public int getHeight() {
return region.getHeight();
}
public int getLength() {
return region.getLength();
}
public boolean contains(Location pt) {
if (!pt.getWorld().equals(world)) {
return false;
}
return region.contains(toVector(pt));
}
}

View File

@ -0,0 +1,105 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010, 2011 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.bukkit.selections;
import org.bukkit.Location;
import org.bukkit.World;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.regions.RegionSelector;
public interface Selection {
/**
* Get the lower point of a region.
*
* @return min. point
*/
public Location getMinimumPoint();
/**
* Get the lower point of a region.
*
* @return min. point
*/
public Vector getNativeMinimumPoint();
/**
* Get the upper point of a region.
*
* @return max. point
*/
public Location getMaximumPoint();
/**
* Get the upper point of a region.
*
* @return max. point
*/
public Vector getNativeMaximumPoint();
/**
* Get the region selector. This is for internal use.
*
* @return
*/
public RegionSelector getRegionSelector();
/**
* Get the world.
*
* @return
*/
public World getWorld();
/**
* Get the number of blocks in the region.
*
* @return number of blocks
*/
public int getArea();
/**
* Get X-size.
*
* @return width
*/
public int getWidth();
/**
* Get Y-size.
*
* @return height
*/
public int getHeight();
/**
* Get Z-size.
*
* @return length
*/
public int getLength();
/**
* Returns true based on whether the region contains the point,
*
* @param pt
* @return
*/
public boolean contains(Location pt);
}