From 47268bef2024d70d9668bf72c92a9fe708b1a0bf Mon Sep 17 00:00:00 2001 From: sk89q Date: Mon, 25 Apr 2011 01:52:34 -0700 Subject: [PATCH] Added secondary mode to tool framework. Updated /cycler to make use of this. --- src/com/sk89q/worldedit/WorldEdit.java | 11 ++- .../sk89q/worldedit/tools/AreaPickaxe.java | 2 +- .../sk89q/worldedit/tools/BlockDataCyler.java | 84 ++++++++++++++----- .../sk89q/worldedit/tools/BlockReplacer.java | 2 +- src/com/sk89q/worldedit/tools/BlockTool.java | 2 +- .../tools/DoubleActionBlockTool.java | 47 +++++++++++ src/com/sk89q/worldedit/tools/QueryTool.java | 2 +- .../worldedit/tools/RecursivePickaxe.java | 2 +- .../sk89q/worldedit/tools/SinglePickaxe.java | 2 +- .../sk89q/worldedit/tools/TreePlanter.java | 2 +- 10 files changed, 128 insertions(+), 28 deletions(-) create mode 100644 src/com/sk89q/worldedit/tools/DoubleActionBlockTool.java diff --git a/src/com/sk89q/worldedit/WorldEdit.java b/src/com/sk89q/worldedit/WorldEdit.java index 01972c776..6446740ca 100644 --- a/src/com/sk89q/worldedit/WorldEdit.java +++ b/src/com/sk89q/worldedit/WorldEdit.java @@ -866,7 +866,7 @@ public class WorldEdit { Tool tool = session.getTool(player.getItemInHand()); if (tool != null && tool instanceof BlockTool) { - ((BlockTool)tool).act(server, config, player, session, clicked); + ((BlockTool)tool).actPrimary(server, config, player, session, clicked); return true; } @@ -901,11 +901,18 @@ public class WorldEdit { } } else if (player.isHoldingPickAxe() && session.hasSuperPickAxe()) { if (session.getSuperPickaxe() != null) { - return session.getSuperPickaxe().act(server, config, + return session.getSuperPickaxe().actPrimary(server, config, player, session, clicked); } } + Tool tool = session.getTool(player.getItemInHand()); + + if (tool != null && tool instanceof DoubleActionBlockTool) { + ((DoubleActionBlockTool)tool).actSecondary(server, config, player, session, clicked); + return true; + } + return false; } diff --git a/src/com/sk89q/worldedit/tools/AreaPickaxe.java b/src/com/sk89q/worldedit/tools/AreaPickaxe.java index c51292a97..e8a8530d7 100644 --- a/src/com/sk89q/worldedit/tools/AreaPickaxe.java +++ b/src/com/sk89q/worldedit/tools/AreaPickaxe.java @@ -37,7 +37,7 @@ public class AreaPickaxe implements BlockTool { } @Override - public boolean act(ServerInterface server, LocalConfiguration config, + public boolean actPrimary(ServerInterface server, LocalConfiguration config, LocalPlayer player, LocalSession session, WorldVector clicked) { LocalWorld world = clicked.getWorld(); int ox = clicked.getBlockX(); diff --git a/src/com/sk89q/worldedit/tools/BlockDataCyler.java b/src/com/sk89q/worldedit/tools/BlockDataCyler.java index 35c839f8d..fdda86477 100644 --- a/src/com/sk89q/worldedit/tools/BlockDataCyler.java +++ b/src/com/sk89q/worldedit/tools/BlockDataCyler.java @@ -27,10 +27,10 @@ import com.sk89q.worldedit.blocks.BlockID; * * @author sk89q */ -public class BlockDataCyler implements BlockTool { - @Override - public boolean act(ServerInterface server, LocalConfiguration config, - LocalPlayer player, LocalSession session, WorldVector clicked) { +public class BlockDataCyler implements DoubleActionBlockTool { + + private boolean handleCycle(ServerInterface server, LocalConfiguration config, + LocalPlayer player, LocalSession session, WorldVector clicked, boolean forward) { LocalWorld world = clicked.getWorld(); @@ -44,40 +44,50 @@ public class BlockDataCyler implements BlockTool { return true; } + int increment = forward ? 1 : -1; + if (type == BlockID.LOG) { - data = (data + 1) % 3; + data = (data + increment) % 3; } else if (type == BlockID.LEAVES) { - data = (data + 1) % 3; + data = (data + increment) % 3; + } else if (type == BlockID.SAPLING) { + int saplingType = data & 0x03; + int age = data & 0x0c; + data = (saplingType + increment) % 4 | age; } else if (type == BlockID.CACTUS) { - data = (data + 1) % 16; + data = (data + increment) % 16; } else if (type == BlockID.SOIL) { - data = (data + 1) % 9; + data = (data + increment) % 9; } else if (type == BlockID.CROPS) { - data = (data + 1) % 6; + data = (data + increment) % 6; } else if (type == BlockID.MINECART_TRACKS) { if (data >= 6 && data <= 9) { - data = (data + 1) % 4 + 6; + data = (data + increment) % 4 + 6; } else { player.printError("This minecart track orientation is not supported."); return true; } } else if (type == BlockID.WOODEN_STAIRS || type == BlockID.COBBLESTONE_STAIRS) { - data = (data + 1) % 4; + data = (data + increment) % 4; } else if (type == BlockID.SIGN_POST) { - data = (data + 1) % 16; - } else if (type == BlockID.WALL_SIGN) { - if(data == 2) data = 5; else data--; + data = (data + increment) % 16; + } else if (type == BlockID.WALL_SIGN) { + data = ((data + increment) - 2) % 4 + 2; } else if (type == BlockID.STEP) { - data = (data + 1) % 3; + data = (data + increment) % 3; } else if (type == BlockID.DOUBLE_STEP) { - data = (data + 1) % 3; + data = (data + increment) % 3; } else if (type == BlockID.FURNACE || type == BlockID.BURNING_FURNACE || type == BlockID.DISPENSER) { - data = (data + 1) % 4 + 2; + data = (data + increment) % 4 + 2; } else if (type == BlockID.PUMPKIN || type == BlockID.JACKOLANTERN) { - data = (data + 1) % 4; + data = (data + increment) % 4; } else if (type == BlockID.CLOTH) { - data = nextClothColor(data); + if (forward) { + data = nextClothColor(data); + } else { + data = prevClothColor(data); + } } else { player.printError("That block's data cannot be cycled."); return true; @@ -88,6 +98,19 @@ public class BlockDataCyler implements BlockTool { return true; } + @Override + public boolean actPrimary(ServerInterface server, LocalConfiguration config, + LocalPlayer player, LocalSession session, WorldVector clicked) { + return handleCycle(server, config, player, session, clicked, true); + } + + @Override + public boolean actSecondary(ServerInterface server, + LocalConfiguration config, LocalPlayer player, + LocalSession session, WorldVector clicked) { + return handleCycle(server, config, player, session, clicked, false); + } + private static int nextClothColor(int data) { switch (data) { case 0: return 8; @@ -110,4 +133,27 @@ public class BlockDataCyler implements BlockTool { return 0; } + + private static int prevClothColor(int data) { + switch (data) { + case 8: return 0; + case 7: return 8; + case 15: return 7; + case 12: return 15; + case 14: return 12; + case 1: return 14; + case 4: return 1; + case 5: return 4; + case 13: return 5; + case 9: return 13; + case 3: return 9; + case 11: return 3; + case 10: return 11; + case 2: return 10; + case 6: return 2; + case 0: return 6; + } + + return 0; + } } diff --git a/src/com/sk89q/worldedit/tools/BlockReplacer.java b/src/com/sk89q/worldedit/tools/BlockReplacer.java index 4f00e71d5..1aba1c0a5 100644 --- a/src/com/sk89q/worldedit/tools/BlockReplacer.java +++ b/src/com/sk89q/worldedit/tools/BlockReplacer.java @@ -36,7 +36,7 @@ public class BlockReplacer implements BlockTool { } @Override - public boolean act(ServerInterface server, LocalConfiguration config, + public boolean actPrimary(ServerInterface server, LocalConfiguration config, LocalPlayer player, LocalSession session, WorldVector clicked) { BlockBag bag = session.getBlockBag(player); diff --git a/src/com/sk89q/worldedit/tools/BlockTool.java b/src/com/sk89q/worldedit/tools/BlockTool.java index 0af87a2c1..389acddf7 100644 --- a/src/com/sk89q/worldedit/tools/BlockTool.java +++ b/src/com/sk89q/worldedit/tools/BlockTool.java @@ -38,6 +38,6 @@ public interface BlockTool extends Tool { * @param clicked * @return true to deny */ - public boolean act(ServerInterface server, LocalConfiguration config, + public boolean actPrimary(ServerInterface server, LocalConfiguration config, LocalPlayer player, LocalSession session, WorldVector clicked); } diff --git a/src/com/sk89q/worldedit/tools/DoubleActionBlockTool.java b/src/com/sk89q/worldedit/tools/DoubleActionBlockTool.java new file mode 100644 index 000000000..07f8dff5b --- /dev/null +++ b/src/com/sk89q/worldedit/tools/DoubleActionBlockTool.java @@ -0,0 +1,47 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010, 2011 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.tools; + +import com.sk89q.worldedit.LocalConfiguration; +import com.sk89q.worldedit.LocalPlayer; +import com.sk89q.worldedit.LocalSession; +import com.sk89q.worldedit.ServerInterface; +import com.sk89q.worldedit.WorldVector; + +/** + * Represents a block tool that also has a secondary/primary function. + * + * @author sk89q + */ +public interface DoubleActionBlockTool extends BlockTool { + /** + * Perform the secondary action. Should return true to deny the default + * action. + * + * @param server + * @param config + * @param player + * @param session + * @param clicked + * @return true to deny + */ + public boolean actSecondary(ServerInterface server, LocalConfiguration config, + LocalPlayer player, LocalSession session, WorldVector clicked); +} diff --git a/src/com/sk89q/worldedit/tools/QueryTool.java b/src/com/sk89q/worldedit/tools/QueryTool.java index f8ad88313..fcd3e4dd8 100644 --- a/src/com/sk89q/worldedit/tools/QueryTool.java +++ b/src/com/sk89q/worldedit/tools/QueryTool.java @@ -30,7 +30,7 @@ import com.sk89q.worldedit.blocks.*; public class QueryTool implements BlockTool { @Override - public boolean act(ServerInterface server, LocalConfiguration config, + public boolean actPrimary(ServerInterface server, LocalConfiguration config, LocalPlayer player, LocalSession session, WorldVector clicked) { LocalWorld world = clicked.getWorld(); diff --git a/src/com/sk89q/worldedit/tools/RecursivePickaxe.java b/src/com/sk89q/worldedit/tools/RecursivePickaxe.java index 63db01620..0b6e2c9c5 100644 --- a/src/com/sk89q/worldedit/tools/RecursivePickaxe.java +++ b/src/com/sk89q/worldedit/tools/RecursivePickaxe.java @@ -40,7 +40,7 @@ public class RecursivePickaxe implements BlockTool { } @Override - public boolean act(ServerInterface server, LocalConfiguration config, + public boolean actPrimary(ServerInterface server, LocalConfiguration config, LocalPlayer player, LocalSession session, WorldVector clicked) { LocalWorld world = clicked.getWorld(); diff --git a/src/com/sk89q/worldedit/tools/SinglePickaxe.java b/src/com/sk89q/worldedit/tools/SinglePickaxe.java index 2ccacdb8e..d90f3a276 100644 --- a/src/com/sk89q/worldedit/tools/SinglePickaxe.java +++ b/src/com/sk89q/worldedit/tools/SinglePickaxe.java @@ -29,7 +29,7 @@ import com.sk89q.worldedit.blocks.BlockID; */ public class SinglePickaxe implements BlockTool { @Override - public boolean act(ServerInterface server, LocalConfiguration config, + public boolean actPrimary(ServerInterface server, LocalConfiguration config, LocalPlayer player, LocalSession session, WorldVector clicked) { LocalWorld world = clicked.getWorld(); diff --git a/src/com/sk89q/worldedit/tools/TreePlanter.java b/src/com/sk89q/worldedit/tools/TreePlanter.java index e36129078..239bc2449 100644 --- a/src/com/sk89q/worldedit/tools/TreePlanter.java +++ b/src/com/sk89q/worldedit/tools/TreePlanter.java @@ -35,7 +35,7 @@ public class TreePlanter implements BlockTool { } @Override - public boolean act(ServerInterface server, LocalConfiguration config, + public boolean actPrimary(ServerInterface server, LocalConfiguration config, LocalPlayer player, LocalSession session, WorldVector clicked) { LocalWorld world = clicked.getWorld();