Search in sources :

Example 1 with OutOfSpaceException

use of com.sk89q.worldedit.extent.inventory.OutOfSpaceException in project FastAsyncWorldEdit by IntellectualSites.

the class BukkitPlayerBlockBag method storeBlock.

@Override
public void storeBlock(BlockState blockState, int amount) throws BlockBagException {
    if (blockState.getBlockType().getMaterial().isAir()) {
        throw new IllegalArgumentException("Can't store air block");
    }
    if (!blockState.getBlockType().hasItemType()) {
        throw new IllegalArgumentException("This block cannot be stored");
    }
    loadInventory();
    int freeSlot = -1;
    for (int slot = 0; slot < items.length; ++slot) {
        ItemStack bukkitItem = items[slot];
        if (bukkitItem == null) {
            if (freeSlot == -1) {
                freeSlot = slot;
            }
            continue;
        }
        if (!BukkitAdapter.equals(blockState.getBlockType(), bukkitItem.getType())) {
            // Type id 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 (freeSlot > -1) {
        items[freeSlot] = BukkitAdapter.adapt(new BaseItemStack(blockState.getBlockType().getItemType(), amount));
        return;
    }
    throw new OutOfSpaceException(blockState.getBlockType());
}
Also used : BaseItemStack(com.sk89q.worldedit.blocks.BaseItemStack) BaseItemStack(com.sk89q.worldedit.blocks.BaseItemStack) ItemStack(org.bukkit.inventory.ItemStack) OutOfSpaceException(com.sk89q.worldedit.extent.inventory.OutOfSpaceException)

Aggregations

BaseItemStack (com.sk89q.worldedit.blocks.BaseItemStack)1 OutOfSpaceException (com.sk89q.worldedit.extent.inventory.OutOfSpaceException)1 ItemStack (org.bukkit.inventory.ItemStack)1