The block bag fetch/place mapping is now taken from BlockType.getBlockBagItem.

This commit is contained in:
TomyLobo 2011-11-04 16:56:53 +01:00
parent 7beac92232
commit 3ed5841863
3 changed files with 79 additions and 57 deletions

View File

@ -198,7 +198,7 @@ public class EditSession {
if (blockBag != null) { if (blockBag != null) {
if (type > 0) { if (type > 0) {
try { try {
blockBag.fetchPlacedBlock(type); blockBag.fetchPlacedBlock(type, 0);
} catch (UnplaceableBlockException e) { } catch (UnplaceableBlockException e) {
return false; return false;
} catch (BlockBagException e) { } catch (BlockBagException e) {

View File

@ -34,7 +34,7 @@ public abstract class BlockBag {
* @param id * @param id
* @param data * @param data
* @throws BlockBagException * @throws BlockBagException
* @deprecated Use {@link BlockBag#storeDroppedBlock(int, int)} * @deprecated Use {@link BlockBag#storeDroppedBlock(int, int)} instead
*/ */
@Deprecated @Deprecated
public void storeDroppedBlock(int id) throws BlockBagException { public void storeDroppedBlock(int id) throws BlockBagException {
@ -61,8 +61,20 @@ public abstract class BlockBag {
* *
* @param id * @param id
* @throws BlockBagException * @throws BlockBagException
* @deprecated Use {@link #fetchPlacedBlock(int,int)} instead
*/ */
public void fetchPlacedBlock(int id) throws BlockBagException { 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 { try {
// Blocks that can't be fetched... // Blocks that can't be fetched...
switch (id) { switch (id) {
@ -95,41 +107,37 @@ public abstract class BlockBag {
} }
} catch (OutOfBlocksException e) { } catch (OutOfBlocksException e) {
switch (id) { BaseItem placed = BlockType.getBlockBagItem(id, data);
case BlockID.STONE: if (placed == null) throw e; // TODO: check
fetchBlock(BlockID.COBBLESTONE); if (placed.getType() == BlockID.AIR) throw e; // TODO: check
break;
case BlockID.GRASS: fetchItem(placed);
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;
}
} }
} }
/** /**
* Get a block. * Get a block.
* *
* Either this method or fetchItem needs to be overridden
*
* @param id * @param id
* @throws BlockBagException * @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. * Store a block.

View File

@ -71,7 +71,13 @@ public class BukkitPlayerBlockBag extends BlockBag {
* @param id * @param id
*/ */
@Override @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) { if (id == BlockID.AIR) {
throw new IllegalArgumentException("Can't fetch air block"); throw new IllegalArgumentException("Can't fetch air block");
} }
@ -81,20 +87,30 @@ public class BukkitPlayerBlockBag extends BlockBag {
boolean found = false; boolean found = false;
for (int slot = 0; slot < items.length; ++slot) { for (int slot = 0; slot < items.length; ++slot) {
ItemStack item = items[slot]; ItemStack bukkitItem = items[slot];
if (item == null) continue; if (bukkitItem == null) {
continue;
}
if (item.getTypeId() == id) { if (bukkitItem.getTypeId() != id) {
int amount = item.getAmount(); // 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 // Unlimited
if (amount < 0) {
return; return;
} }
if (amount > 1) { if (currentAmount > 1) {
item.setAmount(amount - 1); bukkitItem.setAmount(currentAmount - 1);
found = true; found = true;
} else { } else {
items[slot] = null; items[slot] = null;
@ -103,10 +119,8 @@ public class BukkitPlayerBlockBag extends BlockBag {
break; break;
} }
}
if (found) { if (!found) {
} else {
throw new OutOfBlocksException(); throw new OutOfBlocksException();
} }
} }