From bb06f9daedbcbbfc1b7f08a5c05bc49fbf536eeb Mon Sep 17 00:00:00 2001 From: sk89q Date: Sun, 3 Oct 2010 16:45:54 -0700 Subject: [PATCH] Added region selection with a wooden axe (#271); --- src/WorldEdit.java | 54 ++++++++++++++++++++++++++ src/WorldEditSession.java | 81 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 132 insertions(+), 3 deletions(-) diff --git a/src/WorldEdit.java b/src/WorldEdit.java index 77ab9877d..36bbeba34 100644 --- a/src/WorldEdit.java +++ b/src/WorldEdit.java @@ -189,6 +189,60 @@ public class WorldEdit extends Plugin { sessions.clear(); } + /** + * Called on right click. + * + * @param player + * @param blockPlaced + * @param blockClicked + * @param itemInHand + * @return false if you want the action to go through + */ + @Override + public boolean onBlockCreate(Player player, Block blockPlaced, + Block blockClicked, int itemInHand) { + if (itemInHand == 271) { // Wooden axe + if (!etc.getInstance().canUseCommand(player.getName(), "/editpos1") + || !etc.getInstance().canUseCommand(player.getName(), "/editpos2")) { + return false; + } + + WorldEditSession session = getSession(player); + + int x = (int)Math.floor(blockClicked.getX()); + int y = (int)Math.floor(blockClicked.getY()); + int z = (int)Math.floor(blockClicked.getZ()); + + if (session.isToolControlEnabled()) { + try { + if (session.hasToolBeenDoubleClicked() + && x == session.getPos1()[0] + && y == session.getPos1()[1] + && z == session.getPos1()[2]) { // Pos 2 + session.setPos2(x, y, z); + session.setPos1(session.getLastToolPos1()); + player.sendMessage(Colors.LightPurple + "Second edit position set; first one restored."); + } else { + // Have to remember the original position because on + // double click, we are going to restore it + try { + session.setLastToolPos1(session.getPos1()); + } catch (IncompleteRegionException e) {} + + session.setPos1(x, y, z); + player.sendMessage(Colors.LightPurple + "First edit position set."); + } + } catch (IncompleteRegionException e) {} + + session.triggerToolClick(); + + return true; + } + } + + return false; + } + /** * * @param player diff --git a/src/WorldEditSession.java b/src/WorldEditSession.java index 68f94510f..3fe0d973b 100644 --- a/src/WorldEditSession.java +++ b/src/WorldEditSession.java @@ -33,6 +33,9 @@ public class WorldEditSession { private LinkedList history = new LinkedList(); private int historyPointer = 0; private RegionClipboard clipboard; + private boolean toolControl = true; + private int[] lastToolPos1 = new int[3]; + private long lastToolClick = 0; /** * Clear history. @@ -127,8 +130,8 @@ public class WorldEditSession { } /** - * Sets postiion 1. - * + * Sets position 1. + * * @param x * @param y * @param z @@ -138,6 +141,18 @@ public class WorldEditSession { pos1 = new int[]{x, y, z}; } + /** + * Sets position 1. + * + * @param x + * @param y + * @param z + */ + public void setPos1(int[] pos) { + hasSetPos1 = true; + pos1 = pos; + } + /** * Gets position 2. * @@ -151,7 +166,7 @@ public class WorldEditSession { /** * Sets position 2. - * + * * @param x * @param y * @param z @@ -161,6 +176,18 @@ public class WorldEditSession { pos2 = new int[]{x, y, z}; } + /** + * Sets position 2. + * + * @param x + * @param y + * @param z + */ + public void setPos2(int[] pos) { + hasSetPos2 = true; + pos2 = pos; + } + /** * Get lower X bound. * @@ -262,4 +289,52 @@ public class WorldEditSession { public void setClipboard(RegionClipboard clipboard) { this.clipboard = clipboard; } + + /** + * See if tool control is enabled. + * + * @return + */ + public boolean isToolControlEnabled() { + return toolControl; + } + + /** + * Change tool control setting. + * + * @param + */ + public void setToolControl(boolean toolControl) { + this.toolControl = toolControl; + } + + /** + * @return the lastToolPos1 + */ + public int[] getLastToolPos1() { + return lastToolPos1; + } + + /** + * @param lastToolPos1 the lastToolPos1 to set + */ + public void setLastToolPos1(int[] lastToolPos1) { + this.lastToolPos1 = lastToolPos1; + } + + /** + * Returns true if the tool has been double clicked. + * + * @return + */ + public boolean hasToolBeenDoubleClicked() { + return System.currentTimeMillis() - lastToolClick < 500; + } + + /** + * Triggers a click of the tool. + */ + public void triggerToolClick() { + lastToolClick = System.currentTimeMillis(); + } }