From 1a96847f880481e391a1647e29c4bc462a92d2f5 Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Sun, 10 Nov 2013 13:53:17 +0100 Subject: [PATCH] Added LocalPlayer.getBlockInHand(). Also added an appropriate overload to BukkitPlayer. --- .../java/com/sk89q/worldedit/LocalPlayer.java | 13 +++++++ .../sk89q/worldedit/NotABlockException.java | 34 +++++++++++++++++++ .../sk89q/worldedit/bukkit/BukkitPlayer.java | 7 ++++ .../sk89q/worldedit/bukkit/BukkitUtil.java | 13 +++++++ 4 files changed, 67 insertions(+) create mode 100644 src/main/java/com/sk89q/worldedit/NotABlockException.java diff --git a/src/main/java/com/sk89q/worldedit/LocalPlayer.java b/src/main/java/com/sk89q/worldedit/LocalPlayer.java index dbcfaa9a1..4fa98c7aa 100644 --- a/src/main/java/com/sk89q/worldedit/LocalPlayer.java +++ b/src/main/java/com/sk89q/worldedit/LocalPlayer.java @@ -456,6 +456,19 @@ public abstract class LocalPlayer { */ public abstract int getItemInHand(); + /** + * Get the Block that the player is holding. + * + * @return the item id of the item the player is holding + */ + public BaseBlock getBlockInHand() throws WorldEditException { + final int typeId = getItemInHand(); + if (!getWorld().isValidBlockType(typeId)) { + throw new NotABlockException(typeId); + } + return new BaseBlock(typeId); + } + /** * Get the name of the player. * diff --git a/src/main/java/com/sk89q/worldedit/NotABlockException.java b/src/main/java/com/sk89q/worldedit/NotABlockException.java new file mode 100644 index 000000000..06d045109 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/NotABlockException.java @@ -0,0 +1,34 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010, 2011 sk89q and contributors + * + * 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 NotABlockException extends WorldEditException { + public NotABlockException() { + super("This item is not a block."); + } + + public NotABlockException(String type) { + super("The item '"+type+"' is not a block."); + } + + public NotABlockException(int typeId) { + super("The item with the ID "+typeId+" is not a block."); + } +} diff --git a/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java b/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java index b137e6441..7374105e7 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java @@ -19,6 +19,8 @@ package com.sk89q.worldedit.bukkit; +import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.blocks.BaseBlock; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -49,6 +51,11 @@ public class BukkitPlayer extends LocalPlayer { return itemStack != null ? itemStack.getTypeId() : 0; } + public BaseBlock getBlockInHand() throws WorldEditException { + ItemStack itemStack = player.getItemInHand(); + return BukkitUtil.toBlock(getWorld(), itemStack); + } + @Override public String getName() { return player.getName(); diff --git a/src/main/java/com/sk89q/worldedit/bukkit/BukkitUtil.java b/src/main/java/com/sk89q/worldedit/bukkit/BukkitUtil.java index 975906041..483198211 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/BukkitUtil.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/BukkitUtil.java @@ -21,6 +21,9 @@ package com.sk89q.worldedit.bukkit; import java.util.List; +import com.sk89q.worldedit.NotABlockException; +import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.blocks.BaseBlock; import org.bukkit.Server; import org.bukkit.World; import org.bukkit.block.Block; @@ -41,6 +44,7 @@ import com.sk89q.worldedit.bukkit.entity.BukkitEntity; import com.sk89q.worldedit.bukkit.entity.BukkitExpOrb; import com.sk89q.worldedit.bukkit.entity.BukkitItem; import com.sk89q.worldedit.bukkit.entity.BukkitPainting; +import org.bukkit.inventory.ItemStack; public class BukkitUtil { private BukkitUtil() { @@ -153,4 +157,13 @@ public class BukkitUtil { return new BukkitEntity(toLocation(e.getLocation()), e.getType(), e.getUniqueId()); } } + + public static BaseBlock toBlock(LocalWorld world, ItemStack itemStack) throws WorldEditException { + final int typeId = itemStack.getTypeId(); + if (world.isValidBlockType(typeId)) { + return new BaseBlock(typeId, itemStack.getDurability()); + } + + throw new NotABlockException(typeId); + } }