Added LocalPlayer.getBlockInHand().

Also added an appropriate overload to BukkitPlayer.
This commit is contained in:
TomyLobo 2013-11-10 13:53:17 +01:00
parent cd0bc62931
commit 1a96847f88
4 changed files with 67 additions and 0 deletions

View File

@ -456,6 +456,19 @@ public abstract class LocalPlayer {
*/ */
public abstract int getItemInHand(); 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. * Get the name of the player.
* *

View File

@ -0,0 +1,34 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com> 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 <http://www.gnu.org/licenses/>.
*/
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.");
}
}

View File

@ -19,6 +19,8 @@
package com.sk89q.worldedit.bukkit; package com.sk89q.worldedit.bukkit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -49,6 +51,11 @@ public class BukkitPlayer extends LocalPlayer {
return itemStack != null ? itemStack.getTypeId() : 0; return itemStack != null ? itemStack.getTypeId() : 0;
} }
public BaseBlock getBlockInHand() throws WorldEditException {
ItemStack itemStack = player.getItemInHand();
return BukkitUtil.toBlock(getWorld(), itemStack);
}
@Override @Override
public String getName() { public String getName() {
return player.getName(); return player.getName();

View File

@ -21,6 +21,9 @@ package com.sk89q.worldedit.bukkit;
import java.util.List; 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.Server;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; 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.BukkitExpOrb;
import com.sk89q.worldedit.bukkit.entity.BukkitItem; import com.sk89q.worldedit.bukkit.entity.BukkitItem;
import com.sk89q.worldedit.bukkit.entity.BukkitPainting; import com.sk89q.worldedit.bukkit.entity.BukkitPainting;
import org.bukkit.inventory.ItemStack;
public class BukkitUtil { public class BukkitUtil {
private BukkitUtil() { private BukkitUtil() {
@ -153,4 +157,13 @@ public class BukkitUtil {
return new BukkitEntity(toLocation(e.getLocation()), e.getType(), e.getUniqueId()); 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);
}
} }