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 (type > 0) {
try {
blockBag.fetchPlacedBlock(type);
blockBag.fetchPlacedBlock(type, 0);
} catch (UnplaceableBlockException e) {
return false;
} catch (BlockBagException e) {

View File

@ -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,41 +107,37 @@ 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.

View File

@ -71,7 +71,13 @@ 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");
}
@ -81,32 +87,40 @@ public class BukkitPlayerBlockBag extends BlockBag {
boolean found = false;
for (int slot = 0; slot < items.length; ++slot) {
ItemStack item = items[slot];
ItemStack bukkitItem = 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;
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();
}
}