From 3ed58418638833f24cdb3bffca743746590bcc57 Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Fri, 4 Nov 2011 16:56:53 +0100 Subject: [PATCH] The block bag fetch/place mapping is now taken from BlockType.getBlockBagItem. --- .../java/com/sk89q/worldedit/EditSession.java | 2 +- .../com/sk89q/worldedit/bags/BlockBag.java | 62 +++++++++------- .../bukkit/BukkitPlayerBlockBag.java | 72 +++++++++++-------- 3 files changed, 79 insertions(+), 57 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/EditSession.java b/src/main/java/com/sk89q/worldedit/EditSession.java index e7630da40..7de40b5ee 100644 --- a/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/src/main/java/com/sk89q/worldedit/EditSession.java @@ -198,7 +198,7 @@ public class EditSession { if (blockBag != null) { if (type > 0) { try { - blockBag.fetchPlacedBlock(type); + blockBag.fetchPlacedBlock(type, 0); } catch (UnplaceableBlockException e) { return false; } catch (BlockBagException e) { diff --git a/src/main/java/com/sk89q/worldedit/bags/BlockBag.java b/src/main/java/com/sk89q/worldedit/bags/BlockBag.java index 0910dab17..bb59ffb84 100644 --- a/src/main/java/com/sk89q/worldedit/bags/BlockBag.java +++ b/src/main/java/com/sk89q/worldedit/bags/BlockBag.java @@ -34,7 +34,7 @@ public abstract class BlockBag { * @param id * @param data * @throws BlockBagException - * @deprecated Use {@link BlockBag#storeDroppedBlock(int, int)} + * @deprecated Use {@link BlockBag#storeDroppedBlock(int, int)} instead */ @Deprecated public void storeDroppedBlock(int id) throws BlockBagException { @@ -61,8 +61,20 @@ public abstract class BlockBag { * * @param id * @throws BlockBagException + * @deprecated Use {@link #fetchPlacedBlock(int,int)} instead */ public void fetchPlacedBlock(int id) throws BlockBagException { + fetchPlacedBlock(id, 0); + } + + /** + * Sets a block as if it was placed by hand. + * + * @param id + * @param data TODO + * @throws BlockBagException + */ + public void fetchPlacedBlock(int id, int data) throws BlockBagException { try { // Blocks that can't be fetched... switch (id) { @@ -95,42 +107,38 @@ public abstract class BlockBag { } } catch (OutOfBlocksException e) { - switch (id) { - case BlockID.STONE: - fetchBlock(BlockID.COBBLESTONE); - break; + BaseItem placed = BlockType.getBlockBagItem(id, data); + if (placed == null) throw e; // TODO: check + if (placed.getType() == BlockID.AIR) throw e; // TODO: check - case BlockID.GRASS: - fetchBlock(BlockID.DIRT); - break; - - case BlockID.REDSTONE_WIRE: - fetchBlock(ItemID.REDSTONE_DUST); - break; - - case BlockID.REDSTONE_TORCH_OFF: - fetchBlock(BlockID.REDSTONE_TORCH_ON); - break; - - case BlockID.WALL_SIGN: - case BlockID.SIGN_POST: - fetchBlock(ItemID.SIGN); - break; - - default: - throw e; - } + fetchItem(placed); } } /** * Get a block. * + * Either this method or fetchItem needs to be overridden + * * @param id * @throws BlockBagException */ - public abstract void fetchBlock(int id) throws BlockBagException; - + public void fetchBlock(int id) throws BlockBagException { + fetchItem(new BaseItem(id)); + } + + /** + * Get a block. + * + * Either this method or fetchBlock needs to be overridden + * + * @param item + * @throws BlockBagException + */ + public void fetchItem(BaseItem item) throws BlockBagException { + fetchBlock(item.getType()); + } + /** * Store a block. * diff --git a/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java b/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java index aa76db272..10aea6eb8 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayerBlockBag.java @@ -71,46 +71,60 @@ public class BukkitPlayerBlockBag extends BlockBag { * @param id */ @Override - public void fetchBlock(int id) throws BlockBagException { + public void fetchItem(BaseItem item) throws BlockBagException { + final int id = item.getType(); + final int damage = item.getDamage(); + int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1; + assert(amount == 1); + boolean usesDamageValue = ItemType.usesDamageValue(id); + if (id == BlockID.AIR) { throw new IllegalArgumentException("Can't fetch air block"); } - + loadInventory(); - + boolean found = false; - + for (int slot = 0; slot < items.length; ++slot) { - ItemStack item = items[slot]; - - if (item == null) continue; - - if (item.getTypeId() == id) { - int amount = item.getAmount(); - - // Unlimited - if (amount < 0) { - return; - } - - if (amount > 1) { - item.setAmount(amount - 1); - found = true; - } else { - items[slot] = null; - found = true; - } - - break; + ItemStack bukkitItem = items[slot]; + + if (bukkitItem == null) { + continue; } + + if (bukkitItem.getTypeId() != id) { + // Type id doesn't fit + continue; + } + + if (usesDamageValue && bukkitItem.getDurability() != damage) { + // Damage value doesn't fit. + continue; + } + + int currentAmount = bukkitItem.getAmount(); + if (currentAmount < 0) { + // Unlimited + return; + } + + if (currentAmount > 1) { + bukkitItem.setAmount(currentAmount - 1); + found = true; + } else { + items[slot] = null; + found = true; + } + + break; } - - if (found) { - } else { + + if (!found) { throw new OutOfBlocksException(); } } - + /** * Store a block. *