From 7d661844ad06c10c7340c5dc7c85ab42f5c4740a Mon Sep 17 00:00:00 2001 From: sk89q Date: Fri, 18 Feb 2011 20:50:40 -0800 Subject: [PATCH] Improved messages for tools. --- .../worldedit/InvalidToolBindException.java | 35 +++++++++++++++++++ src/com/sk89q/worldedit/LocalSession.java | 14 ++++++-- src/com/sk89q/worldedit/WorldEdit.java | 3 ++ src/com/sk89q/worldedit/blocks/ItemType.java | 33 +++++++++++++++++ .../worldedit/commands/ToolCommands.java | 15 +++++--- 5 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 src/com/sk89q/worldedit/InvalidToolBindException.java diff --git a/src/com/sk89q/worldedit/InvalidToolBindException.java b/src/com/sk89q/worldedit/InvalidToolBindException.java new file mode 100644 index 000000000..f2b958bf3 --- /dev/null +++ b/src/com/sk89q/worldedit/InvalidToolBindException.java @@ -0,0 +1,35 @@ +// $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; + +public class InvalidToolBindException extends WorldEditException { + private static final long serialVersionUID = -1865311004052447699L; + + private int itemId; + + public InvalidToolBindException(int itemId, String msg) { + super(msg); + this.itemId = itemId; + } + + public int getItemId() { + return itemId; + } +} diff --git a/src/com/sk89q/worldedit/LocalSession.java b/src/com/sk89q/worldedit/LocalSession.java index 337a392fe..75f754689 100644 --- a/src/com/sk89q/worldedit/LocalSession.java +++ b/src/com/sk89q/worldedit/LocalSession.java @@ -431,8 +431,9 @@ public class LocalSession { * * @param item * @return the tool + * @throws InvalidToolBindException */ - public Brush getBrushTool(int item) { + public Brush getBrushTool(int item) throws InvalidToolBindException { Tool tool = getTool(item); if (tool == null || !(tool instanceof Brush)) { @@ -448,8 +449,17 @@ public class LocalSession { * * @param item * @param tool the tool to set + * @throws InvalidToolBindException */ - public void setTool(int item, Tool tool) { + public void setTool(int item, Tool tool) throws InvalidToolBindException { + if (item > 0 && item < 255) { + throw new InvalidToolBindException(item, "Blocks can't be used"); + } else if (item == 263 || item == 348) { + throw new InvalidToolBindException(item, "Item is not usuable"); + } else if (item == config.wandItem) { + throw new InvalidToolBindException(item, "Already used for the wand"); + } + this.tools.put(item, tool); } diff --git a/src/com/sk89q/worldedit/WorldEdit.java b/src/com/sk89q/worldedit/WorldEdit.java index 85cf0d0b8..dafc042d7 100644 --- a/src/com/sk89q/worldedit/WorldEdit.java +++ b/src/com/sk89q/worldedit/WorldEdit.java @@ -946,6 +946,9 @@ public class WorldEdit { } catch (FilenameResolutionException e) { player.printError("File '" + e.getFilename() + "' resolution error: " + e.getMessage()); + } catch (InvalidToolBindException e) { + player.printError("Can't bind tool to " + + ItemType.toHeldName(e.getItemId()) + ": " + e.getMessage()); } catch (FileSelectionAbortedException e) { player.printError("File selection aborted."); } catch (WorldEditException e) { diff --git a/src/com/sk89q/worldedit/blocks/ItemType.java b/src/com/sk89q/worldedit/blocks/ItemType.java index 49c5a8b42..04f8a649d 100644 --- a/src/com/sk89q/worldedit/blocks/ItemType.java +++ b/src/com/sk89q/worldedit/blocks/ItemType.java @@ -291,6 +291,39 @@ public enum ItemType { return ids.get(id); } + /** + * Get a name for the item. + * + * @param id + * @return + */ + public static String toName(int id) { + ItemType type = ids.get(id); + if (type != null) { + return type.getName(); + } else { + return "#" + id; + } + } + + /** + * Get a name for a held item. + * + * @param id + * @return + */ + public static String toHeldName(int id) { + if (id == 0) { + return "Hand"; + } + ItemType type = ids.get(id); + if (type != null) { + return type.getName(); + } else { + return "#" + id; + } + } + /** * Return type from name. May return null. * diff --git a/src/com/sk89q/worldedit/commands/ToolCommands.java b/src/com/sk89q/worldedit/commands/ToolCommands.java index ad19c4064..26b4f595c 100644 --- a/src/com/sk89q/worldedit/commands/ToolCommands.java +++ b/src/com/sk89q/worldedit/commands/ToolCommands.java @@ -25,6 +25,7 @@ import com.sk89q.minecraft.util.commands.CommandPermissions; import com.sk89q.minecraft.util.commands.NestedCommand; import com.sk89q.worldedit.*; import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.blocks.ItemType; import com.sk89q.worldedit.tools.*; import com.sk89q.worldedit.util.TreeGenerator; @@ -41,7 +42,7 @@ public class ToolCommands { throws WorldEditException { session.setTool(player.getItemInHand(), null); - player.print("Now no longer equipping a tool."); + player.print("Tool unbound from your current item."); } @Command( @@ -57,7 +58,8 @@ public class ToolCommands { throws WorldEditException { session.setTool(player.getItemInHand(), new QueryTool()); - player.print("Info tool equipped. Right click with a pickaxe."); + player.print("Info tool bound to " + + ItemType.toHeldName(player.getItemInHand()) + "."); } @Command( @@ -82,7 +84,8 @@ public class ToolCommands { } session.setTool(player.getItemInHand(), new TreePlanter(new TreeGenerator(type))); - player.print("Tree tool equipped. Right click grass with a pickaxe."); + player.print("Tree tool bound to " + + ItemType.toHeldName(player.getItemInHand()) + "."); } @Command( @@ -99,7 +102,8 @@ public class ToolCommands { BaseBlock targetBlock = we.getBlock(player, args.getString(0)); session.setTool(player.getItemInHand(), new BlockReplacer(targetBlock)); - player.print("Block replacer tool equipped. Right click with a pickaxe."); + player.print("Block replacer tool bound to " + + ItemType.toHeldName(player.getItemInHand()) + "."); } @Command( @@ -115,7 +119,8 @@ public class ToolCommands { throws WorldEditException { session.setTool(player.getItemInHand(), new BlockDataCyler()); - player.print("Block cycler tool equipped. Right click with a pickaxe."); + player.print("Block data cycler tool bound to " + + ItemType.toHeldName(player.getItemInHand()) + "."); } @Command(