From 138787c1e2f013095ad75b139db64d33b9dbcd47 Mon Sep 17 00:00:00 2001 From: sk89q Date: Fri, 5 Nov 2010 16:42:16 -0700 Subject: [PATCH] Moved all the classes around again (re-coupled with Hmod). --- src/EditSession.java | 32 +- src/HmodWorldEditListener.java | 206 ----------- src/HmodWorldEditPlayer.java | 239 ------------- ...verInterface.java => ServerInterface.java} | 18 +- src/WorldEdit.java | 63 +--- ...Controller.java => WorldEditListener.java} | 325 ++++++++++++------ src/WorldEditPlayer.java | 314 +++++++++++------ src/com/sk89q/worldedit/ServerInterface.java | 92 ----- 8 files changed, 458 insertions(+), 831 deletions(-) delete mode 100644 src/HmodWorldEditListener.java delete mode 100644 src/HmodWorldEditPlayer.java rename src/{HmodServerInterface.java => ServerInterface.java} (88%) rename src/{WorldEditController.java => WorldEditListener.java} (83%) delete mode 100644 src/com/sk89q/worldedit/ServerInterface.java diff --git a/src/EditSession.java b/src/EditSession.java index bf08991a6..be087ff01 100644 --- a/src/EditSession.java +++ b/src/EditSession.java @@ -42,11 +42,6 @@ import java.util.Random; * @author sk89q */ public class EditSession { - /** - * Server interface. - */ - protected ServerInterface server; - /** * Stores the original blocks before modification. */ @@ -75,13 +70,6 @@ public class EditSession { */ private static Random prng = new Random(); - /** - * Default constructor. There is no maximum blocks limit. - */ - public EditSession() { - server = WorldEditController.getServer(); - } - /** * Construct the object with a maximum number of blocks. */ @@ -90,8 +78,6 @@ public class EditSession { throw new IllegalArgumentException("Max blocks must be >= -1"); } this.maxBlocks = maxBlocks; - - server = WorldEditController.getServer(); } /** @@ -107,15 +93,15 @@ public class EditSession { return false; } - boolean result = server.setBlockType(pt, block.getID()); + boolean result = ServerInterface.setBlockType(pt, block.getID()); if (block.getID() != 0) { - server.setBlockData(pt, block.getData()); + ServerInterface.setBlockData(pt, block.getData()); // Signs if (block instanceof SignBlock) { SignBlock signBlock = (SignBlock)block; String[] text = signBlock.getText(); - server.setSignText(pt, text); + ServerInterface.setSignText(pt, text); // Chests } else if (block instanceof ChestBlock) { ChestBlock chestBlock = (ChestBlock)block; @@ -124,10 +110,10 @@ public class EditSession { for (byte i = 0; i <= 26; i++) { Countable item = items.get(i); if (item != null) { - server.setChestSlot(pt, i, item.getID(), + ServerInterface.setChestSlot(pt, i, item.getID(), item.getAmount()); } else { - server.setChestSlot(pt, i, blankItem, 0); + ServerInterface.setChestSlot(pt, i, blankItem, 0); } } } @@ -228,16 +214,16 @@ public class EditSession { * @return BaseBlock */ public BaseBlock rawGetBlock(Vector pt) { - int type = server.getBlockType(pt); - int data = server.getBlockData(pt); + int type = ServerInterface.getBlockType(pt); + int data = ServerInterface.getBlockData(pt); // Sign if (type == 63 || type == 68) { - String[] text = server.getSignText(pt); + String[] text = ServerInterface.getSignText(pt); return new SignBlock(type, data, text); // Chest } else if (type == 54) { - Map> items = server.getChestContents(pt); + Map> items = ServerInterface.getChestContents(pt); return new ChestBlock(data, items); } else { return new BaseBlock(type, data); diff --git a/src/HmodWorldEditListener.java b/src/HmodWorldEditListener.java deleted file mode 100644 index 1594e01a5..000000000 --- a/src/HmodWorldEditListener.java +++ /dev/null @@ -1,206 +0,0 @@ -// $Id$ -/* - * WorldEdit - * Copyright (C) 2010 sk89q - * - * 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 . -*/ - -import com.sk89q.worldedit.*; - -/** - * Event listener for Hey0's server mod plugin. - * - * @author sk89q - */ -public class HmodWorldEditListener extends PluginListener { - /** - * - * @param player - */ - @Override - public void onDisconnect(Player player) { - WorldEditController worldEdit = WorldEditController.getInstance(); - worldEdit.removeSession(new HmodWorldEditPlayer(player)); - } - - /** - * Called on right click. - * - * @param modPlayer - * @param blockPlaced - * @param blockClicked - * @param itemInHand - * @return false if you want the action to go through - */ - @Override - public boolean onBlockCreate(Player modPlayer, Block blockPlaced, - Block blockClicked, int itemInHand) { - WorldEditController worldEdit = WorldEditController.getInstance(); - WorldEditPlayer player = new HmodWorldEditPlayer(modPlayer); - - if (itemInHand != 271) { return false; } - if (!canUseCommand(modPlayer, "//pos2")) { return false; } - - WorldEditSession session = worldEdit.getSession(player); - - if (session.isToolControlEnabled()) { - Vector cur = Vector.toBlockPoint(blockClicked.getX(), - blockClicked.getY(), - blockClicked.getZ()); - - session.setPos2(cur); - player.print("Second edit position set."); - - return true; - } - - return false; - } - - /** - * Called on left click. - * - * @param modPlayer - * @param blockClicked - * @param itemInHand - * @return false if you want the action to go through - */ - @Override - public boolean onBlockDestroy(Player modPlayer, Block blockClicked) { - if (!canUseCommand(modPlayer, "//pos1") - && !canUseCommand(modPlayer, "//")) { return false; } - - WorldEditController worldEdit = WorldEditController.getInstance(); - WorldEditPlayer player = new HmodWorldEditPlayer(modPlayer); - WorldEditSession session = worldEdit.getSession(player); - - if (player.getItemInHand() == 271) { - if (session.isToolControlEnabled()) { - Vector cur = Vector.toBlockPoint(blockClicked.getX(), - blockClicked.getY(), - blockClicked.getZ()); - - // Bug workaround - if (cur.getBlockX() == 0 && cur.getBlockY() == 0 - && cur.getBlockZ() == 0) { - return false; - } - - try { - if (session.getPos1().equals(cur)) { - return false; - } - } catch (IncompleteRegionException e) { - } - - session.setPos1(cur); - player.print("First edit position set."); - - return true; - } - } else if (player.isHoldingPickAxe()) { - if (session.hasSuperPickAxe()) { - Vector pos = new Vector(blockClicked.getX(), - blockClicked.getY(), blockClicked.getZ()); - if (WorldEditController.getServer().getBlockType(pos) == 7 - && !canUseCommand(modPlayer, "/worldeditbedrock")) { - return true; - } else if (WorldEditController.getServer().getBlockType(pos) == 46) { - return false; - } - - WorldEditController.getServer().setBlockType(pos, 0); - - return true; - } - } - - return false; - } - - /** - * - * @param modPlayer - * @param split - * @return whether the command was processed - */ - @Override - public boolean onCommand(Player modPlayer, String[] split) { - WorldEditController worldEdit = WorldEditController.getInstance(); - - try { - // Legacy /, command - if (split[0].equals("/,")) { - split[0] = "//"; - } - - if (worldEdit.getCommands().containsKey(split[0].toLowerCase())) { - if (canUseCommand(modPlayer, split[0])) { - WorldEditPlayer player = new HmodWorldEditPlayer(modPlayer); - WorldEditSession session = worldEdit.getSession(player); - EditSession editSession = - new EditSession(session.getBlockChangeLimit()); - editSession.enableQueue(); - - try { - return worldEdit.performCommand(player, session, editSession, split); - } finally { - session.remember(editSession); - editSession.flushQueue(); - } - } - } - - return false; - } catch (NumberFormatException e) { - modPlayer.sendMessage(Colors.Rose + "Number expected; string given."); - } catch (IncompleteRegionException e2) { - modPlayer.sendMessage(Colors.Rose + "The edit region has not been fully defined."); - } catch (UnknownItemException e3) { - modPlayer.sendMessage(Colors.Rose + "Block name was not recognized."); - } catch (DisallowedItemException e4) { - modPlayer.sendMessage(Colors.Rose + "Block not allowed (see WorldEdit configuration)."); - } catch (MaxChangedBlocksException e5) { - modPlayer.sendMessage(Colors.Rose + "The maximum number of blocks changed (" - + e5.getBlockLimit() + ") in an instance was reached."); - } catch (UnknownDirectionException ue) { - modPlayer.sendMessage(Colors.Rose + "Unknown direction: " + ue.getDirection()); - } catch (InsufficientArgumentsException e6) { - modPlayer.sendMessage(Colors.Rose + e6.getMessage()); - } catch (EmptyClipboardException ec) { - modPlayer.sendMessage(Colors.Rose + "Your clipboard is empty."); - } catch (WorldEditException e7) { - modPlayer.sendMessage(Colors.Rose + e7.getMessage()); - } catch (Throwable excp) { - modPlayer.sendMessage(Colors.Rose + "Please report this error: [See console]"); - modPlayer.sendMessage(excp.getClass().getName() + ": " + excp.getMessage()); - excp.printStackTrace(); - } - - return true; - } - - /** - * Checks to see if the player can use a command or /worldedit. - * - * @param player - * @param command - * @return - */ - private boolean canUseCommand(Player player, String command) { - return player.canUseCommand(command.replace("air", "")) - || player.canUseCommand("/worldedit"); - } -} \ No newline at end of file diff --git a/src/HmodWorldEditPlayer.java b/src/HmodWorldEditPlayer.java deleted file mode 100644 index 810ab46a1..000000000 --- a/src/HmodWorldEditPlayer.java +++ /dev/null @@ -1,239 +0,0 @@ -// $Id$ -/* - * WorldEdit - * Copyright (C) 2010 sk89q - * - * 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 . -*/ - -import com.sk89q.worldedit.Vector; -import com.sk89q.worldedit.ServerInterface; - -/** - * - * @author sk89q - */ -public class HmodWorldEditPlayer extends WorldEditPlayer { - private Player player; - - /** - * Construct a WorldEditPlayer. - * - * @param player - */ - public HmodWorldEditPlayer(Player player) { - super(); - this.player = player; - } - - /** - * Get the name of the player. - * - * @return String - */ - public String getName() { - return player.getName(); - } - - /** - * Get the point of the block that is being stood upon. - * - * @return point - */ - public Vector getBlockOn() { - return Vector.toBlockPoint(player.getX(), player.getY() - 1, player.getZ()); - } - - /** - * Get the point of the block that is being stood in. - * - * @return point - */ - public Vector getBlockIn() { - return Vector.toBlockPoint(player.getX(), player.getY(), player.getZ()); - } - - /** - * Get the point of the block being looked at. May return null. - * - * @param range - * @return point - */ - public Vector getBlockTrace(int range) { - HitBlox hitBlox = new HitBlox(player, range, 0.2); - Block block = hitBlox.getTargetBlock(); - if (block == null) { - return null; - } - return new Vector(block.getX(), block.getY(), block.getZ()); - } - - /** - * Pass through the wall that you are looking at. - * - * @param range - * @return whether the player was pass through - */ - public boolean passThroughForwardWall(int range) { - boolean foundNext = false; - int searchDist = 0; - - HitBlox hitBlox = new HitBlox(player, range, 0.2); - Block block; - - while ((block = hitBlox.getNextBlock()) != null) { - searchDist++; - - if (searchDist > 20) { - return false; - } - - if (block.getType() == 0) { - if (foundNext) { - Vector v = new Vector(block.getX(), block.getY() - 1, block.getZ()); - - if (server.getBlockType(v) == 0) { - setPosition(v.add(0.5, 0, 0.5)); - return true; - } - } - } else { - foundNext = true; - } - } - - return false; - } - - /** - * Get the player's position. - * - * @return point - */ - public Vector getPosition() { - return new Vector(player.getX(), player.getY(), player.getZ()); - } - - /** - * Get the player's view pitch. - * - * @return pitch - */ - public double getPitch() { - return player.getPitch(); - } - - /** - * Get the player's view yaw. - * - * @return yaw - */ - public double getYaw() { - return player.getRotation(); - } - - /** - * Get the ID of the item that the player is holding. - * - * @return - */ - public int getItemInHand() { - return player.getItemInHand(); - } - - /** - * Get the player's cardinal direction (N, W, NW, etc.). - * - * @return - */ - public String getCardinalDirection() { - // From hey0's code - double rot = (getYaw() - 90) % 360; - if (rot < 0) { - rot += 360.0; - } - - return etc.getCompassPointForDirection(rot).toLowerCase(); - } - - /** - * Print a WorldEdit message. - * - * @param msg - */ - public void print(String msg) { - player.sendMessage(Colors.LightPurple + msg); - } - - /** - * Print a WorldEdit error. - * - * @param msg - */ - public void printError(String msg) { - player.sendMessage(Colors.Rose + msg); - } - - /** - * Move the player. - * - * @param pos - * @param pitch - * @param yaw - */ - public void setPosition(Vector pos, float pitch, float yaw) { - Location loc = new Location(); - loc.x = pos.getX(); - loc.y = pos.getY(); - loc.z = pos.getZ(); - loc.rotX = (float)yaw; - loc.rotY = (float)pitch; - player.teleportTo(loc); - } - - /** - * Gives the player an item. - * - * @param type - * @param amt - */ - public void giveItem(int type, int amt) { - player.giveItem(type, amt); - } - - /** - * Returns true if equal. - * - * @param other - * @return whether the other object is equivalent - */ - @Override - public boolean equals(Object other) { - if (!(other instanceof WorldEditPlayer)) { - return false; - } - WorldEditPlayer other2 = (WorldEditPlayer)other; - return other2.getName().equals(player.getName()); - } - - /** - * Gets the hash code. - * - * @return hash code - */ - @Override - public int hashCode() { - return getName().hashCode(); - } -} diff --git a/src/HmodServerInterface.java b/src/ServerInterface.java similarity index 88% rename from src/HmodServerInterface.java rename to src/ServerInterface.java index 2b8af2cc2..2aafadea5 100644 --- a/src/HmodServerInterface.java +++ b/src/ServerInterface.java @@ -26,7 +26,7 @@ import java.util.HashMap; * * @author sk89q */ -public class HmodServerInterface implements ServerInterface { +public class ServerInterface { /** * Set block type. * @@ -34,7 +34,7 @@ public class HmodServerInterface implements ServerInterface { * @param type * @return */ - public boolean setBlockType(Vector pt, int type) { + public static boolean setBlockType(Vector pt, int type) { // Can't set colored cloth or crash if ((type >= 21 && type <= 34) || type == 36) { return false; @@ -49,7 +49,7 @@ public class HmodServerInterface implements ServerInterface { * @param pt * @return */ - public int getBlockType(Vector pt) { + public static int getBlockType(Vector pt) { return etc.getServer().getBlockIdAt(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); } @@ -61,7 +61,7 @@ public class HmodServerInterface implements ServerInterface { * @param data * @return */ - public void setBlockData(Vector pt, int data) { + public static void setBlockData(Vector pt, int data) { etc.getServer().setBlockData(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ(), data); } @@ -72,7 +72,7 @@ public class HmodServerInterface implements ServerInterface { * @param pt * @return */ - public int getBlockData(Vector pt) { + public static int getBlockData(Vector pt) { return etc.getServer().getBlockData(pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); } @@ -83,7 +83,7 @@ public class HmodServerInterface implements ServerInterface { * @param pt * @param text */ - public void setSignText(Vector pt, String[] text) { + public static void setSignText(Vector pt, String[] text) { Sign signData = (Sign)etc.getServer().getComplexBlock( pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); if (signData == null) { @@ -101,7 +101,7 @@ public class HmodServerInterface implements ServerInterface { * @param pt * @return */ - public String[] getSignText(Vector pt) { + public static String[] getSignText(Vector pt) { Sign signData = (Sign)etc.getServer().getComplexBlock( pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); if (signData == null) { @@ -120,7 +120,7 @@ public class HmodServerInterface implements ServerInterface { * @param pt * @return */ - public Map> getChestContents(Vector pt) { + public static Map> getChestContents(Vector pt) { ComplexBlock cblock = etc.getServer().getComplexBlock( pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); @@ -151,7 +151,7 @@ public class HmodServerInterface implements ServerInterface { * @param amount * @return */ - public boolean setChestSlot(Vector pt, byte slot, BaseItem item, int amount) { + public static boolean setChestSlot(Vector pt, byte slot, BaseItem item, int amount) { ComplexBlock cblock = etc.getServer().getComplexBlock( pt.getBlockX(), pt.getBlockY(), pt.getBlockZ()); diff --git a/src/WorldEdit.java b/src/WorldEdit.java index 6191735ba..91f70bc04 100644 --- a/src/WorldEdit.java +++ b/src/WorldEdit.java @@ -17,15 +17,12 @@ * along with this program. If not, see . */ -import com.sk89q.worldedit.snapshots.SnapshotRepository; -import java.util.Map; -import java.util.HashSet; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.jar.Manifest; import java.util.jar.Attributes; import java.net.URL; import java.io.IOException; -import java.util.logging.Level; -import java.util.logging.Logger; /** * Entry point for the plugin for hey0's mod. @@ -37,20 +34,10 @@ public class WorldEdit extends Plugin { * Logger. */ private static final Logger logger = Logger.getLogger("Minecraft"); - /** - * WorldEditLibrary's properties file. - */ - private PropertiesFile properties; /** * WorldEditLibrary instance. */ - private static final WorldEditController worldEdit = - WorldEditController.setup(new HmodServerInterface()); - /** - * Listener for the plugin system. - */ - private static final HmodWorldEditListener listener = - new HmodWorldEditListener(); + private static final WorldEditListener listener = new WorldEditListener(); /** * Initializes the plugin. @@ -70,7 +57,7 @@ public class WorldEdit extends Plugin { loader.addListener(PluginLoader.Hook.LOGIN, listener, this, PluginListener.Priority.MEDIUM); - logger.log(Level.INFO, "WorldEdit version " + getWorldEditVersion() + " loaded."); + logger.log(Level.INFO, "WorldEdit version " + getVersion() + " loaded."); } /** @@ -78,37 +65,8 @@ public class WorldEdit extends Plugin { */ @Override public void enable() { - if (properties == null) { - properties = new PropertiesFile("worldedit.properties"); - } else { - properties.load(); - } - - // Get allowed blocks - HashSet allowedBlocks = new HashSet(); - for (String b : properties.getString("allowed-blocks", - WorldEditController.getDefaultAllowedBlocks()).split(",")) { - try { - allowedBlocks.add(Integer.parseInt(b)); - } catch (NumberFormatException e) { - } - } - worldEdit.setAllowedBlocks(allowedBlocks); - - worldEdit.setDefaultChangeLimit( - Math.max(-1, properties.getInt("max-blocks-changed", -1))); - - String snapshotsDir = properties.getString("snapshots-dir", ""); - if (!snapshotsDir.trim().equals("")) { - worldEdit.setSnapshotRepository(new SnapshotRepository(snapshotsDir)); - } - - String shellSaveType = properties.getString("shell-save-type", "").trim(); - worldEdit.setShellSaveType(shellSaveType.equals("") ? null : shellSaveType); - - for (Map.Entry entry : worldEdit.getCommands().entrySet()) { - etc.getInstance().addCommand(entry.getKey(), entry.getValue()); - } + listener.loadConfiguration(); + listener.registerCommands(); } /** @@ -116,11 +74,8 @@ public class WorldEdit extends Plugin { */ @Override public void disable() { - for (String key : worldEdit.getCommands().keySet()) { - etc.getInstance().removeCommand(key); - } - - worldEdit.clearSessions(); + listener.deregisterCommands(); + listener.clearSessions(); } /** @@ -128,7 +83,7 @@ public class WorldEdit extends Plugin { * * @return */ - private String getWorldEditVersion() { + private String getVersion() { try { String classContainer = WorldEdit.class.getProtectionDomain() .getCodeSource().getLocation().toString(); diff --git a/src/WorldEditController.java b/src/WorldEditListener.java similarity index 83% rename from src/WorldEditController.java rename to src/WorldEditListener.java index efe0b7832..11a1bada0 100644 --- a/src/WorldEditController.java +++ b/src/WorldEditListener.java @@ -17,65 +17,64 @@ * along with this program. If not, see . */ -import com.sk89q.worldedit.snapshots.SnapshotRepository; -import com.sk89q.worldedit.snapshots.Snapshot; -import com.sk89q.worldedit.*; -import com.sk89q.worldedit.blocks.*; -import com.sk89q.worldedit.data.*; -import com.sk89q.worldedit.regions.Region; -import com.sk89q.worldedit.snapshots.InvalidSnapshotException; +import java.util.Map; import java.util.HashMap; import java.util.Set; import java.util.HashSet; -import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; import java.io.*; +import com.sk89q.worldedit.*; +import com.sk89q.worldedit.blocks.*; +import com.sk89q.worldedit.data.*; +import com.sk89q.worldedit.snapshots.*; +import com.sk89q.worldedit.regions.*; /** * Plugin base. * * @author sk89q */ -public class WorldEditController { +public class WorldEditListener extends PluginListener { /** - * WorldEditLibrary instance. + * Logger. */ - private static WorldEditController instance; - /** - * Server interface. - */ - private ServerInterface server; + private static final Logger logger = Logger.getLogger("Minecraft"); /** - * List of default allowed blocks. + * Default list of allowed block types. */ private final static Integer[] DEFAULT_ALLOWED_BLOCKS = { 0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 35, 41, 42, 43, 44, 45, 47, 48, 49, 52, 53, 54, 56, 57, 58, 60, 61, 62, 67, 73, 78, 79, 80, 82, 85, 86, 87, 88, 89, 91 }; - /** - * Logger. + * WorldEditLibrary's properties file. */ - private static final Logger logger = Logger.getLogger("Minecraft"); + private PropertiesFile properties; /** - * Stores the WorldEditLibrary sessions of players. + * Stores a list of WorldEdit sessions, keyed by players' names. Sessions + * persist only for the user's session. On disconnect, the session will be + * removed. Sessions are created only when they are needed and those + * without any WorldEdit abilities or never use WorldEdit in a session will + * not have a session object generated for them. */ private HashMap sessions = new HashMap(); /** - * Stores the commands. + * List of commands. These are checked when onCommand() is called, so + * the list must know about every command. On plugin load, the commands + * will be loaded into help. On unload, they will be removed. */ private HashMap commands = new HashMap(); /** - * List of the blocks that can be used. If null, all blocks can be used. + * List of allowed blocks. */ private HashSet allowedBlocks; /** - * Default block change limit. -1 for no limit. + * Default maximum number of blocks that can be changed in one operation. */ private int defaultChangeLimit = -1; /** @@ -83,45 +82,15 @@ public class WorldEditController { */ private String shellSaveType; /** - * Stores the snapshot repository. May be null; + * Snapshot repository used for restoring from backups. This may be null + * if snapshot restoration is not configured. */ private SnapshotRepository snapshotRepo; - /** - * Set up an instance. - * - * @param server - * @return - */ - public static WorldEditController setup(ServerInterface server) { - WorldEditController worldEdit = new WorldEditController(); - worldEdit.server = server; - instance = worldEdit; - return worldEdit; - } - - /** - * Get WorldEditLibrary instance. - * - * @return - */ - public static WorldEditController getInstance() { - return instance; - } - - /** - * Get server interface. - * - * @return - */ - public static ServerInterface getServer() { - return instance.server; - } - /** * Construct an instance of the plugin. */ - private WorldEditController() { + public WorldEditListener() { // Note: Commands should only have the phrase 'air' at the end // for now (see SMWorldEditListener.canUseCommand) commands.put("//pos1", "Set editing position #1"); @@ -193,7 +162,7 @@ public class WorldEditController { return sessions.get(player); } else { WorldEditSession session = new WorldEditSession(); - session.setBlockChangeLimit(getDefaultChangeLimit()); + session.setBlockChangeLimit(defaultChangeLimit); sessions.put(player, session); return session; } @@ -1166,25 +1135,6 @@ public class WorldEditController { sessions.clear(); } - /** - * Get the list of commands. - * - * @return List - */ - public HashMap getCommands() { - return commands; - } - - /** - * Set the list of allowed blocks. Provided null to use the default list. - * - * @param allowedBlocks - */ - public void setAllowedBlocks(HashSet allowedBlocks) { - this.allowedBlocks = allowedBlocks != null ? allowedBlocks - : new HashSet(Arrays.asList(DEFAULT_ALLOWED_BLOCKS)); - } - /** * Get a comma-delimited list of the default allowed blocks. * @@ -1197,49 +1147,228 @@ public class WorldEditController { } return b.substring(0, b.length() - 1); } - /** - * @return the defaultChangeLimit + * + * @param player */ - public int getDefaultChangeLimit() { - return defaultChangeLimit; + @Override + public void onDisconnect(Player player) { + removeSession(new WorldEditPlayer(player)); } /** - * Set the default limit on the number of blocks that can be changed - * in one operation. - * - * @param defaultChangeLimit the defaultChangeLimit to set + * Called on right click. + * + * @param modPlayer + * @param blockPlaced + * @param blockClicked + * @param itemInHand + * @return false if you want the action to go through */ - public void setDefaultChangeLimit(int defaultChangeLimit) { - this.defaultChangeLimit = defaultChangeLimit; + @Override + public boolean onBlockCreate(Player modPlayer, Block blockPlaced, + Block blockClicked, int itemInHand) { + WorldEditPlayer player = new WorldEditPlayer(modPlayer); + + if (itemInHand != 271) { return false; } + if (!canUseCommand(modPlayer, "//pos2")) { return false; } + + WorldEditSession session = getSession(player); + + if (session.isToolControlEnabled()) { + Vector cur = Vector.toBlockPoint(blockClicked.getX(), + blockClicked.getY(), + blockClicked.getZ()); + + session.setPos2(cur); + player.print("Second edit position set."); + + return true; + } + + return false; } /** - * @return the snapshotRepo + * Called on left click. + * + * @param modPlayer + * @param blockClicked + * @param itemInHand + * @return false if you want the action to go through */ - public SnapshotRepository getSnapshotRepo() { - return snapshotRepo; + @Override + public boolean onBlockDestroy(Player modPlayer, Block blockClicked) { + if (!canUseCommand(modPlayer, "//pos1") + && !canUseCommand(modPlayer, "//")) { return false; } + + WorldEditPlayer player = new WorldEditPlayer(modPlayer); + WorldEditSession session = getSession(player); + + if (player.getItemInHand() == 271) { + if (session.isToolControlEnabled()) { + Vector cur = Vector.toBlockPoint(blockClicked.getX(), + blockClicked.getY(), + blockClicked.getZ()); + + // Bug workaround + if (cur.getBlockX() == 0 && cur.getBlockY() == 0 + && cur.getBlockZ() == 0) { + return false; + } + + try { + if (session.getPos1().equals(cur)) { + return false; + } + } catch (IncompleteRegionException e) { + } + + session.setPos1(cur); + player.print("First edit position set."); + + return true; + } + } else if (player.isHoldingPickAxe()) { + if (session.hasSuperPickAxe()) { + Vector pos = new Vector(blockClicked.getX(), + blockClicked.getY(), blockClicked.getZ()); + if (ServerInterface.getBlockType(pos) == 7 + && !canUseCommand(modPlayer, "/worldeditbedrock")) { + return true; + } else if (ServerInterface.getBlockType(pos) == 46) { + return false; + } + + ServerInterface.setBlockType(pos, 0); + + return true; + } + } + + return false; } /** - * @param snapshotRepo the snapshotRepo to set + * + * @param ply + * @param split + * @return whether the command was processed */ - public void setSnapshotRepository(SnapshotRepository snapshotRepo) { - this.snapshotRepo = snapshotRepo; + @Override + public boolean onCommand(Player ply, String[] split) { + try { + // Legacy /, command + if (split[0].equals("/,")) { + split[0] = "//"; + } + + if (commands.containsKey(split[0].toLowerCase())) { + if (canUseCommand(ply, split[0])) { + WorldEditPlayer player = new WorldEditPlayer(ply); + WorldEditSession session = getSession(player); + EditSession editSession = + new EditSession(session.getBlockChangeLimit()); + editSession.enableQueue(); + + try { + return performCommand(player, session, editSession, split); + } finally { + session.remember(editSession); + editSession.flushQueue(); + } + } + } + + return false; + } catch (NumberFormatException e) { + ply.sendMessage(Colors.Rose + "Number expected; string given."); + } catch (IncompleteRegionException e2) { + ply.sendMessage(Colors.Rose + "The edit region has not been fully defined."); + } catch (UnknownItemException e3) { + ply.sendMessage(Colors.Rose + "Block name was not recognized."); + } catch (DisallowedItemException e4) { + ply.sendMessage(Colors.Rose + "Block not allowed (see WorldEdit configuration)."); + } catch (MaxChangedBlocksException e5) { + ply.sendMessage(Colors.Rose + "The maximum number of blocks changed (" + + e5.getBlockLimit() + ") in an instance was reached."); + } catch (UnknownDirectionException ue) { + ply.sendMessage(Colors.Rose + "Unknown direction: " + ue.getDirection()); + } catch (InsufficientArgumentsException e6) { + ply.sendMessage(Colors.Rose + e6.getMessage()); + } catch (EmptyClipboardException ec) { + ply.sendMessage(Colors.Rose + "Your clipboard is empty."); + } catch (WorldEditException e7) { + ply.sendMessage(Colors.Rose + e7.getMessage()); + } catch (Throwable excp) { + ply.sendMessage(Colors.Rose + "Please report this error: [See console]"); + ply.sendMessage(excp.getClass().getName() + ": " + excp.getMessage()); + excp.printStackTrace(); + } + + return true; } /** - * @return the shellSaveType + * Checks to see if the player can use a command or /worldedit. + * + * @param player + * @param command + * @return */ - public String getShellSaveType() { - return shellSaveType; + private boolean canUseCommand(Player player, String command) { + return player.canUseCommand(command.replace("air", "")) + || player.canUseCommand("/worldedit"); } /** - * @param shellSaveType the shellSaveType to set + * Loads the configuration. */ - public void setShellSaveType(String shellSaveType) { - this.shellSaveType = shellSaveType; + public void loadConfiguration() { + if (properties == null) { + properties = new PropertiesFile("worldedit.properties"); + } else { + properties.load(); + } + + // Get allowed blocks + allowedBlocks = new HashSet(); + for (String b : properties.getString("allowed-blocks", + WorldEditListener.getDefaultAllowedBlocks()).split(",")) { + try { + allowedBlocks.add(Integer.parseInt(b)); + } catch (NumberFormatException e) { + } + } + + defaultChangeLimit = Math.max(-1, properties.getInt("max-blocks-changed", -1)); + + String snapshotsDir = properties.getString("snapshots-dir", ""); + if (!snapshotsDir.trim().equals("")) { + snapshotRepo = new SnapshotRepository(snapshotsDir); + } else { + snapshotRepo = null; + } + + String type = properties.getString("shell-save-type", "").trim(); + shellSaveType = type.equals("") ? null : type; + } + + /** + * Register commands with help. + */ + public void registerCommands() { + for (Map.Entry entry : commands.entrySet()) { + etc.getInstance().addCommand(entry.getKey(), entry.getValue()); + } + } + + /** + * De-register commands. + */ + public void deregisterCommands() { + for (String key : commands.keySet()) { + etc.getInstance().removeCommand(key); + } } } diff --git a/src/WorldEditPlayer.java b/src/WorldEditPlayer.java index f085e968b..80ef5eeb4 100644 --- a/src/WorldEditPlayer.java +++ b/src/WorldEditPlayer.java @@ -18,76 +18,27 @@ */ import com.sk89q.worldedit.Vector; -import com.sk89q.worldedit.ServerInterface; import com.sk89q.worldedit.blocks.BlockType; /** * * @author sk89q */ -public abstract class WorldEditPlayer { +public class WorldEditPlayer { /** - * Server interface. + * Stores the player. */ - protected ServerInterface server; + private Player player; /** - * Construct the player. + * Construct the object. + * + * @param player */ - public WorldEditPlayer() { - server = WorldEditController.getServer(); + public WorldEditPlayer(Player player) { + this.player = player; } - /** - * Get the name of the player. - * - * @return String - */ - public abstract String getName(); - /** - * Get the point of the block that is being stood upon. - * - * @return point - */ - public abstract Vector getBlockOn(); - /** - * Get the point of the block that is being stood in. - * - * @return point - */ - public abstract Vector getBlockIn(); - /** - * Get the point of the block being looked at. May return null. - * - * @param range - * @return point - */ - public abstract Vector getBlockTrace(int range); - /** - * Get the player's position. - * - * @return point - */ - public abstract Vector getPosition(); - /** - * Get the player's view pitch. - * - * @return pitch - */ - public abstract double getPitch(); - /** - * Get the player's view yaw. - * - * @return yaw - */ - public abstract double getYaw(); - /** - * Get the ID of the item that the player is holding. - * - * @return - */ - public abstract int getItemInHand(); - /** * Returns true if the player is holding a pick axe. * @@ -99,36 +50,6 @@ public abstract class WorldEditPlayer { || item == 285; } - /** - * Get the player's cardinal direction (N, W, NW, etc.). - * - * @return - */ - public abstract String getCardinalDirection(); - - /** - * Print a WorldEditLibrary message. - * - * @param msg - */ - public abstract void print(String msg); - - /** - * Print a WorldEditLibrary error. - * - * @param msg - */ - public abstract void printError(String msg); - - /** - * Move the player. - * - * @param pos - * @param pitch - * @param yaw - */ - public abstract void setPosition(Vector pos, float pitch, float yaw); - /** * Move the player. * @@ -182,14 +103,6 @@ public abstract class WorldEditPlayer { findFreePosition(getBlockIn()); } - /** - * Pass through the wall that you are looking at. - * - * @param range - * @return whether a wall was passed through - */ - public abstract boolean passThroughForwardWall(int range); - /** * Go up one level to the next free space above. * @@ -205,7 +118,7 @@ public abstract class WorldEditPlayer { byte spots = 0; while (y <= 129) { - if (server.getBlockType(new Vector(x, y, z)) == 0) { + if (ServerInterface.getBlockType(new Vector(x, y, z)) == 0) { free++; } else { free = 0; @@ -214,7 +127,7 @@ public abstract class WorldEditPlayer { if (free == 2) { spots++; if (spots == 2) { - int type = server.getBlockType(new Vector(x, y - 2, z)); + int type = ServerInterface.getBlockType(new Vector(x, y - 2, z)); // Don't get put in lava! if (type == 10 || type == 11) { @@ -246,7 +159,7 @@ public abstract class WorldEditPlayer { byte free = 0; while (y >= 1) { - if (server.getBlockType(new Vector(x, y, z)) == 0) { + if (ServerInterface.getBlockType(new Vector(x, y, z)) == 0) { free++; } else { free = 0; @@ -257,7 +170,7 @@ public abstract class WorldEditPlayer { // lightly and also check to see if there's something to // stand upon while (y >= 0) { - int type = server.getBlockType(new Vector(x, y, z)); + int type = ServerInterface.getBlockType(new Vector(x, y, z)); // Don't want to end up in lava if (type != 0 && type != 10 && type != 11) { @@ -292,15 +205,15 @@ public abstract class WorldEditPlayer { int z = pos.getBlockZ(); // No free space above - if (server.getBlockType(new Vector(x, y, z)) != 0) { + if (ServerInterface.getBlockType(new Vector(x, y, z)) != 0) { return false; } while (y <= 127) { // Found a ceiling! - if (server.getBlockType(new Vector(x, y, z)) != 0) { + if (ServerInterface.getBlockType(new Vector(x, y, z)) != 0) { int platformY = Math.max(initialY, y - 3 - clearance); - server.setBlockType(new Vector(x, platformY, z), + ServerInterface.setBlockType(new Vector(x, platformY, z), BlockType.GLASS.getID()); setPosition(new Vector(x + 0.5, platformY + 1, z + 0.5)); return true; @@ -312,14 +225,6 @@ public abstract class WorldEditPlayer { return false; } - /** - * Gives the player an item. - * - * @param type - * @param amt - */ - public abstract void giveItem(int type, int amt); - /** * Returns true if equal. * @@ -344,4 +249,193 @@ public abstract class WorldEditPlayer { public int hashCode() { return getName().hashCode(); } + + /** + * Get the point of the block that is being stood in. + * + * @return point + */ + public Vector getBlockIn() { + return Vector.toBlockPoint(player.getX(), player.getY(), player.getZ()); + } + + /** + * Get the point of the block that is being stood upon. + * + * @return point + */ + public Vector getBlockOn() { + return Vector.toBlockPoint(player.getX(), player.getY() - 1, player.getZ()); + } + + /** + * Get the point of the block being looked at. May return null. + * + * @param range + * @return point + */ + public Vector getBlockTrace(int range) { + HitBlox hitBlox = new HitBlox(player, range, 0.2); + Block block = hitBlox.getTargetBlock(); + if (block == null) { + return null; + } + return new Vector(block.getX(), block.getY(), block.getZ()); + } + + /** + * Get the player's cardinal direction (N, W, NW, etc.). + * + * @return + */ + public String getCardinalDirection() { + // From hey0's code + double rot = (getYaw() - 90) % 360; + if (rot < 0) { + rot += 360.0; + } + return etc.getCompassPointForDirection(rot).toLowerCase(); + } + + /** + * Get the ID of the item that the player is holding. + * + * @return + */ + /** + * Get the ID of the item that the player is holding. + * + * @return + */ + public int getItemInHand() { + return player.getItemInHand(); + } + + /** + * Get the name of the player. + * + * @return String + */ + public String getName() { + return player.getName(); + } + + /** + * Get the player's view pitch. + * + * @return pitch + */ + /** + * Get the player's view pitch. + * + * @return pitch + */ + public double getPitch() { + return player.getPitch(); + } + + /** + * Get the player's position. + * + * @return point + */ + public Vector getPosition() { + return new Vector(player.getX(), player.getY(), player.getZ()); + } + + /** + * Get the player's view yaw. + * + * @return yaw + */ + /** + * Get the player's view yaw. + * + * @return yaw + */ + public double getYaw() { + return player.getRotation(); + } + + /** + * Gives the player an item. + * + * @param type + * @param amt + */ + /** + * Gives the player an item. + * + * @param type + * @param amt + */ + public void giveItem(int type, int amt) { + player.giveItem(type, amt); + } + + /** + * Pass through the wall that you are looking at. + * + * @param range + * @return whether the player was pass through + */ + public boolean passThroughForwardWall(int range) { + boolean foundNext = false; + int searchDist = 0; + HitBlox hitBlox = new HitBlox(player, range, 0.2); + Block block; + while ((block = hitBlox.getNextBlock()) != null) { + searchDist++; + if (searchDist > 20) { + return false; + } + if (block.getType() == 0) { + if (foundNext) { + Vector v = new Vector(block.getX(), block.getY() - 1, block.getZ()); + if (ServerInterface.getBlockType(v) == 0) { + setPosition(v.add(0.5, 0, 0.5)); + return true; + } + } + } else { + foundNext = true; + } + } + return false; + } + + /** + * Print a WorldEdit message. + * + * @param msg + */ + public void print(String msg) { + player.sendMessage(Colors.LightPurple + msg); + } + + /** + * Print a WorldEdit error. + * + * @param msg + */ + public void printError(String msg) { + player.sendMessage(Colors.Rose + msg); + } + + /** + * Move the player. + * + * @param pos + * @param pitch + * @param yaw + */ + public void setPosition(Vector pos, float pitch, float yaw) { + Location loc = new Location(); + loc.x = pos.getX(); + loc.y = pos.getY(); + loc.z = pos.getZ(); + loc.rotX = (float) yaw; + loc.rotY = (float) pitch; + player.teleportTo(loc); + } } diff --git a/src/com/sk89q/worldedit/ServerInterface.java b/src/com/sk89q/worldedit/ServerInterface.java deleted file mode 100644 index 63f48d6bf..000000000 --- a/src/com/sk89q/worldedit/ServerInterface.java +++ /dev/null @@ -1,92 +0,0 @@ -// $Id$ -/* - * WorldEdit - * Copyright (C) 2010 sk89q - * - * 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 . -*/ - -package com.sk89q.worldedit; - -import java.util.Map; -import com.sk89q.worldedit.*; -import com.sk89q.worldedit.blocks.BaseItem; - -/** - * - * @author sk89q - */ -public interface ServerInterface { - /** - * Set block type. - * - * @param pt - * @param type - * @return - */ - public boolean setBlockType(Vector pt, int type); - /** - * Get block type. - * - * @param pt - * @return - */ - public int getBlockType(Vector pt); - /** - * Set block data. - * - * @param pt - * @param data - * @return - */ - public void setBlockData(Vector pt, int data); - /** - * Get block data. - * - * @param pt - * @return - */ - public int getBlockData(Vector pt); - /** - * Set sign text. - * - * @param pt - * @param text - */ - public void setSignText(Vector pt, String[] text); - /** - * Get sign text. - * - * @param pt - * @return - */ - public String[] getSignText(Vector pt); - /** - * Gets the contents of chests. - * - * @param pt - * @return - */ - public Map> getChestContents(Vector pt); - /** - * Sets a chest slot. - * - * @param pt - * @param slot - * @param item - * @param amount - * @return - */ - public boolean setChestSlot(Vector pt, byte slot, BaseItem item, int amount); -}