Begun adding support for data/damage values to the blockbag.

This commit is contained in:
TomyLobo 2011-11-03 15:48:41 +01:00
parent 429bfe9faa
commit ff39121fc8
3 changed files with 72 additions and 39 deletions

View File

@ -209,7 +209,7 @@ public class EditSession {
if (existing > 0) { if (existing > 0) {
try { try {
blockBag.storeDroppedBlock(existing); blockBag.storeDroppedBlock(existing, world.getBlockData(pt));
} catch (BlockBagException e) { } catch (BlockBagException e) {
} }
} }

View File

@ -32,13 +32,15 @@ public abstract class BlockBag {
* Stores a block as if it was mined. * Stores a block as if it was mined.
* *
* @param id * @param id
* @param data
* @throws BlockBagException * @throws BlockBagException
*/ */
public void storeDroppedBlock(int id) throws BlockBagException { public void storeDroppedBlock(int id, int data) throws BlockBagException {
int dropped = BlockType.getDroppedBlock(id); BaseItem dropped = BlockType.getDroppedBlock(id, data);
if (dropped > 0) { if (dropped == null) return;
storeBlock(dropped); if (dropped.getType() == BlockID.AIR) return;
}
storeItem(dropped);
} }
/** /**
@ -122,7 +124,19 @@ public abstract class BlockBag {
* @param id * @param id
* @throws BlockBagException * @throws BlockBagException
*/ */
public abstract void storeBlock(int id) throws BlockBagException; public void storeBlock(int id) throws BlockBagException {
storeItem(new BaseItem(id));
}
/**
* Store a block.
*
* @param item
* @throws BlockBagException
*/
public void storeItem(BaseItem item) throws BlockBagException {
storeBlock(item.getType());
}
/** /**
* Checks to see if a block exists without removing it. * Checks to see if a block exists without removing it.

View File

@ -23,6 +23,8 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.bags.*; import com.sk89q.worldedit.bags.*;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.blocks.BlockID;
public class BukkitPlayerBlockBag extends BlockBag { public class BukkitPlayerBlockBag extends BlockBag {
@ -114,53 +116,70 @@ public class BukkitPlayerBlockBag extends BlockBag {
* @param id * @param id
*/ */
@Override @Override
public void storeBlock(int id) throws BlockBagException { public void storeItem(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 <= 64);
boolean usesDamageValue = false; // TODO: Use ItemType.usesDamageValue once it's fixed.
if (id == BlockID.AIR) { if (id == BlockID.AIR) {
throw new IllegalArgumentException("Can't store air block"); throw new IllegalArgumentException("Can't store air block");
} }
loadInventory(); loadInventory();
boolean found = false;
int freeSlot = -1; int freeSlot = -1;
for (int slot = 0; slot < items.length; ++slot) { for (int slot = 0; slot < items.length; ++slot) {
ItemStack item = items[slot]; ItemStack bukkitItem = items[slot];
// Delay using up a free slot until we know there are no stacks if (bukkitItem == null) {
// of this item to merge into // Delay using up a free slot until we know there are no stacks
if (item == null) { // of this item to merge into
if (freeSlot == -1) { if (freeSlot == -1) {
freeSlot = slot; freeSlot = slot;
} }
continue; continue;
} }
if (item.getTypeId() == id) { if (bukkitItem.getTypeId() != id) {
int amount = item.getAmount(); // Type id doesn't fit
continue;
// Unlimited
if (amount < 0) {
return;
}
if (amount < 64) {
item.setAmount(amount + 1);
found = true;
break;
}
} }
if (usesDamageValue && bukkitItem.getDurability() != damage) {
// Damage value doesn't fit.
continue;
}
int currentAmount = bukkitItem.getAmount();
if (currentAmount < 0) {
// Unlimited
return;
}
if (currentAmount >= 64) {
// Full stack
continue;
}
int spaceLeft = 64 - currentAmount;
if (spaceLeft >= amount) {
bukkitItem.setAmount(currentAmount + amount);
return;
}
bukkitItem.setAmount(64);
amount -= spaceLeft;
} }
if (!found && freeSlot > -1) { if (freeSlot > -1) {
items[freeSlot] = new ItemStack(id, 1); items[freeSlot] = new ItemStack(id, amount);
found = true; return;
}
if (found) {
} else {
throw new OutOfSpaceException(id);
} }
throw new OutOfSpaceException(id);
} }
/** /**