Search in sources :

Example 71 with ItemStack

use of org.bukkit.inventory.ItemStack in project BKCommonLib by bergerhealer.

the class InventoryTest method testItemStackConversion.

@Test
public void testItemStackConversion() {
    ItemStack item = new ItemStack(Material.WOOD, 1);
    Object nmsHandle = HandleConversion.toItemStackHandle(item);
    assertNotNull(nmsHandle);
    ItemStack itemBackConv = WrapperConversion.toItemStack(nmsHandle);
    assertItemEquals(item, itemBackConv);
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) Test(org.junit.Test)

Example 72 with ItemStack

use of org.bukkit.inventory.ItemStack in project BKCommonLib by bergerhealer.

the class ItemMaterialTest method testEmptyItem.

@Test
public void testEmptyItem() {
    ItemStack item = ItemUtil.emptyItem();
    assertNotNull(item);
    assertTrue(ItemUtil.isEmpty(item));
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) Test(org.junit.Test)

Example 73 with ItemStack

use of org.bukkit.inventory.ItemStack in project BKCommonLib by bergerhealer.

the class CraftRecipe method create.

/**
     * Creates a new Craft Recipe from an IRecipe instance. This method is not
     * recommended to be used.
     *
     * @param recipe to use
     * @return the CraftRecipe, or null on failure
     */
@Deprecated
public static CraftRecipe create(Object recipe) {
    final ItemStack output = NMSRecipe.getOutput(recipe);
    final List<ItemStack> inputs = NMSRecipe.getInputItems(recipe);
    if (inputs != null) {
        return create(inputs, output);
    } else {
        return null;
    }
}
Also used : ItemStack(org.bukkit.inventory.ItemStack)

Example 74 with ItemStack

use of org.bukkit.inventory.ItemStack in project Essentials by drtshock.

the class InventoryWorkaround method addOversizedItems.

// Returns what it couldn't store
// Set oversizedStack to below normal stack size to disable oversized stacks
public static Map<Integer, ItemStack> addOversizedItems(final Inventory inventory, final int oversizedStacks, final ItemStack... items) {
    if (isCombinedInventory(inventory)) {
        Inventory fakeInventory = makeTruncatedPlayerInventory((PlayerInventory) inventory);
        Map<Integer, ItemStack> overflow = addOversizedItems(fakeInventory, oversizedStacks, items);
        for (int i = 0; i < fakeInventory.getContents().length; i++) {
            inventory.setItem(i, fakeInventory.getContents()[i]);
        }
        return overflow;
    }
    final Map<Integer, ItemStack> leftover = new HashMap<>();
    /*
         * TODO: some optimization - Create a 'firstPartial' with a 'fromIndex' - Record the lastPartial per Material -
		 * Cache firstEmpty result
		 */
    // combine items
    final ItemStack[] combined = new ItemStack[items.length];
    for (ItemStack item : items) {
        if (item == null || item.getAmount() < 1) {
            continue;
        }
        for (int j = 0; j < combined.length; j++) {
            if (combined[j] == null) {
                combined[j] = item.clone();
                break;
            }
            if (combined[j].isSimilar(item)) {
                combined[j].setAmount(combined[j].getAmount() + item.getAmount());
                break;
            }
        }
    }
    for (int i = 0; i < combined.length; i++) {
        final ItemStack item = combined[i];
        if (item == null || item.getType() == Material.AIR) {
            continue;
        }
        while (true) {
            // Do we already have a stack of it?
            final int maxAmount = oversizedStacks > item.getType().getMaxStackSize() ? oversizedStacks : item.getType().getMaxStackSize();
            final int firstPartial = firstPartial(inventory, item, maxAmount);
            // Drat! no partial stack
            if (firstPartial == -1) {
                // Find a free spot!
                final int firstFree = inventory.firstEmpty();
                if (firstFree == -1) {
                    // No space at all!
                    leftover.put(i, item);
                    break;
                } else {
                    // More than a single stack!
                    if (item.getAmount() > maxAmount) {
                        final ItemStack stack = item.clone();
                        stack.setAmount(maxAmount);
                        inventory.setItem(firstFree, stack);
                        item.setAmount(item.getAmount() - maxAmount);
                    } else {
                        // Just store it
                        inventory.setItem(firstFree, item);
                        break;
                    }
                }
            } else {
                // So, apparently it might only partially fit, well lets do just that
                final ItemStack partialItem = inventory.getItem(firstPartial);
                final int amount = item.getAmount();
                final int partialAmount = partialItem.getAmount();
                // Check if it fully fits
                if (amount + partialAmount <= maxAmount) {
                    partialItem.setAmount(amount + partialAmount);
                    break;
                }
                // It fits partially
                partialItem.setAmount(maxAmount);
                item.setAmount(amount + partialAmount - maxAmount);
            }
        }
    }
    return leftover;
}
Also used : HashMap(java.util.HashMap) ItemStack(org.bukkit.inventory.ItemStack) PlayerInventory(org.bukkit.inventory.PlayerInventory) Inventory(org.bukkit.inventory.Inventory)

Example 75 with ItemStack

use of org.bukkit.inventory.ItemStack in project Essentials by drtshock.

the class SignProtection method checkIfSignsAreBroken.

private void checkIfSignsAreBroken(final Block block, final User player, final String username, final IEssentials ess) throws MaxMoneyException {
    final Map<Location, SignProtectionState> signs = getConnectedSigns(block, player, username, false);
    for (Map.Entry<Location, SignProtectionState> entry : signs.entrySet()) {
        if (entry.getValue() != SignProtectionState.NOSIGN) {
            final Block sign = entry.getKey().getBlock();
            if (!hasAdjacentBlock(sign, block)) {
                block.setType(Material.AIR);
                final Trade trade = new Trade(new ItemStack(Material.SIGN, 1), ess);
                trade.pay(player, OverflowType.DROP);
            }
        }
    }
}
Also used : Trade(com.earth2me.essentials.Trade) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) Location(org.bukkit.Location)

Aggregations

ItemStack (org.bukkit.inventory.ItemStack)1420 Player (org.bukkit.entity.Player)270 EventHandler (org.bukkit.event.EventHandler)197 ItemMeta (org.bukkit.inventory.meta.ItemMeta)192 ArrayList (java.util.ArrayList)151 Inventory (org.bukkit.inventory.Inventory)127 Material (org.bukkit.Material)103 Location (org.bukkit.Location)96 PlayerInventory (org.bukkit.inventory.PlayerInventory)84 Entity (org.bukkit.entity.Entity)67 Block (org.bukkit.block.Block)54 LivingEntity (org.bukkit.entity.LivingEntity)51 TagCompound (de.keyle.knbt.TagCompound)47 HashMap (java.util.HashMap)47 Map (java.util.Map)46 Vector (org.bukkit.util.Vector)44 Enchantment (org.bukkit.enchantments.Enchantment)41 SkullMeta (org.bukkit.inventory.meta.SkullMeta)41 Mage (com.elmakers.mine.bukkit.api.magic.Mage)39 Test (org.junit.Test)38