Java Code Examples for org.bukkit.inventory.Inventory#getContents()

The following examples show how to use org.bukkit.inventory.Inventory#getContents() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActiveMiner.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This consumes fuel from the given {@link Chest}.
 * 
 * @return The gained fuel value
 */
private int consumeFuel() {
    if (chest.getType() == Material.CHEST) {
        Inventory inv = ((Chest) chest.getState()).getBlockInventory();

        for (int i = 0; i < inv.getSize(); i++) {
            for (MachineFuel fuelType : miner.fuelTypes) {
                ItemStack item = inv.getContents()[i];

                if (fuelType.test(item)) {
                    ItemUtils.consumeItem(item, false);

                    if (miner instanceof AdvancedIndustrialMiner) {
                        inv.addItem(new ItemStack(Material.BUCKET));
                    }

                    return fuelType.getTicks();
                }
            }
        }
    }

    return 0;
}
 
Example 2
Source File: ItemKeep.java    From CardinalPGM with MIT License 6 votes vote down vote up
@EventHandler
public void onPlayerDeath(CardinalDeathEvent event) {
    Player player = event.getPlayer();
    Inventory inventory = player.getInventory();
    Map<Integer, ItemStack> itemsToKeep = new HashMap<>();
    if (inventory.getContents() != null) {
        for (int i = 0; i < inventory.getSize(); i++) {
            if (inventory.getItem(i) != null) {
                ItemStack item = inventory.getItem(i);
                if (item.getType().equals(type) && item.getDurability() == damageValue) {
                    itemsToKeep.put(i, item);
                    inventory.clear(i);
                }
            }
        }
    }
    items.put(player, itemsToKeep);
}
 
Example 3
Source File: PlayerShopkeeper.java    From Shopkeepers with GNU General Public License v3.0 6 votes vote down vote up
protected int getCurrencyInChest() {
	int total = 0;
	Block chest = this.getChest();
	if (Utils.isChest(chest.getType())) {
		Inventory inv = ((Chest) chest.getState()).getInventory();
		ItemStack[] contents = inv.getContents();
		for (ItemStack item : contents) {
			if (Settings.isCurrencyItem(item)) {
				total += item.getAmount();
			} else if (Settings.isHighCurrencyItem(item)) {
				total += item.getAmount() * Settings.highCurrencyValue;
			}
		}
	}
	return total;
}
 
Example 4
Source File: FlagTrackingSpectate.java    From HeavySpleef with GNU General Public License v3.0 6 votes vote down vote up
@Subscribe
public void onSpectateLeave(FlagSpectate.SpectateLeaveEvent event) {
	Player player = event.getPlayer().getBukkitPlayer();
	Inventory inventory = player.getInventory();
	
	for (ItemStack stack : inventory.getContents()) {
		if (stack == null) {
			continue;
		}
		
		MetadatableItemStack metadatable = new MetadatableItemStack(stack);
		if (!metadatable.hasItemMeta() || !metadatable.getItemMeta().hasLore() || !metadatable.hasMetadata(TRACKER_KEY)) {
			continue;
		}
		
		inventory.remove(stack);
	}
	
	player.updateInventory();
	tracking.remove(player);
}
 
Example 5
Source File: Items.java    From TabooLib with MIT License 6 votes vote down vote up
public static boolean takeItem(Inventory inventory, Matcher matcher, int amount) {
    int takeAmount = amount;
    ItemStack[] contents = inventory.getContents();
    for (int i = 0; i < contents.length; i++) {
        ItemStack itemStack = contents[i];
        if (!isNull(itemStack) && matcher.match(itemStack)) {
            takeAmount -= itemStack.getAmount();
            if (takeAmount < 0) {
                itemStack.setAmount(itemStack.getAmount() - (takeAmount + itemStack.getAmount()));
                return true;
            } else {
                inventory.setItem(i, null);
                if (takeAmount == 0) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 6
Source File: EnhancedCraftingTable.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
private void craft(Inventory inv, Block dispenser, Player p, Block b, ItemStack output) {
    Inventory fakeInv = createVirtualInventory(inv);
    Inventory outputInv = findOutputInventory(output, dispenser, inv, fakeInv);

    if (outputInv != null) {
        SlimefunItem sfItem = SlimefunItem.getByItem(output);

        if (sfItem instanceof SlimefunBackpack) {
            upgradeBackpack(p, inv, (SlimefunBackpack) sfItem, output);
        }

        for (int j = 0; j < 9; j++) {
            ItemStack item = inv.getContents()[j];

            if (item != null && item.getType() != Material.AIR) {
                ItemUtils.consumeItem(item, true);
            }
        }
        p.getWorld().playSound(b.getLocation(), Sound.BLOCK_WOODEN_BUTTON_CLICK_ON, 1, 1);

        outputInv.addItem(output);

    }
    else SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);
}
 
Example 7
Source File: ExprAmountOfItems.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Integer[] get(final Event e) {
	int r = 0;
	final ItemType[] types = items.getArray(e);
	for (final Inventory invi : invis.getArray(e)) {
		itemsLoop: for (final ItemStack i : invi.getContents()) {
			for (final ItemType t : types) {
				if (t.isOfType(i)) {
					r += i == null ? 1 : i.getAmount();
					continue itemsLoop;
				}
			}
		}
	}
	return new Integer[] {r};
}
 
Example 8
Source File: OreCrusher.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onInteract(Player p, Block b) {
    Block dispBlock = b.getRelative(BlockFace.DOWN);
    Dispenser disp = (Dispenser) dispBlock.getState();
    Inventory inv = disp.getInventory();

    for (ItemStack current : inv.getContents()) {
        for (ItemStack convert : RecipeType.getRecipeInputs(this)) {
            if (convert != null && SlimefunUtils.isItemSimilar(current, convert, true)) {
                ItemStack adding = RecipeType.getRecipeOutput(this, convert);
                Inventory outputInv = findOutputInventory(adding, dispBlock, inv);
                if (outputInv != null) {
                    ItemStack removing = current.clone();
                    removing.setAmount(convert.getAmount());
                    inv.removeItem(removing);
                    outputInv.addItem(adding);
                    p.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, 1);
                }
                else SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);

                return;
            }
        }
    }

    SlimefunPlugin.getLocalization().sendMessage(p, "machines.unknown-material", true);
}
 
Example 9
Source File: FlagItemReward.java    From HeavySpleef with GNU General Public License v3.0 5 votes vote down vote up
private static boolean isInventoryFull(Inventory inventory) {
	ItemStack[] content = inventory.getContents();
	
	for (ItemStack stack : content) {
		if (stack != null && stack.getType() != Material.AIR) {
			continue;
		}
		
		return false;
	}
	
	return true;
}
 
Example 10
Source File: CargoUtils.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
static ItemStack withdrawFromVanillaInventory(Block node, ItemStack template, Inventory inv) {
    ItemStack[] contents = inv.getContents();
    int minSlot = 0;
    int maxSlot = contents.length;

    if (inv instanceof FurnaceInventory) {
        minSlot = 2;
        maxSlot = 3;
    }
    else if (inv instanceof BrewerInventory) {
        maxSlot = 3;
    }

    ItemStackWrapper wrapper = new ItemStackWrapper(template);

    for (int slot = minSlot; slot < maxSlot; slot++) {
        // Changes to this ItemStack are synchronized with the Item in the Inventory
        ItemStack itemInSlot = contents[slot];

        if (SlimefunUtils.isItemSimilar(itemInSlot, wrapper, true) && matchesFilter(node, itemInSlot)) {
            if (itemInSlot.getAmount() > template.getAmount()) {
                itemInSlot.setAmount(itemInSlot.getAmount() - template.getAmount());
                return template;
            }
            else {
                ItemStack clone = itemInSlot.clone();
                itemInSlot.setAmount(0);
                return clone;
            }
        }
    }

    return null;
}
 
Example 11
Source File: PressureChamber.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onInteract(Player p, Block b) {
    Block dispBlock = b.getRelative(BlockFace.UP).getRelative(BlockFace.UP);
    Dispenser disp = (Dispenser) dispBlock.getState();
    Inventory inv = disp.getInventory();

    for (ItemStack current : inv.getContents()) {
        for (ItemStack convert : RecipeType.getRecipeInputs(this)) {
            if (convert != null && SlimefunUtils.isItemSimilar(current, convert, true)) {
                ItemStack output = RecipeType.getRecipeOutput(this, convert);
                Inventory outputInv = findOutputInventory(output, dispBlock, inv);

                if (outputInv != null) {
                    ItemStack removing = current.clone();
                    removing.setAmount(convert.getAmount());
                    inv.removeItem(removing);

                    craft(p, b, output, outputInv);
                }
                else SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);

                return;
            }
        }
    }
    SlimefunPlugin.getLocalization().sendMessage(p, "machines.unknown-material", true);
}
 
Example 12
Source File: JoinItems.java    From HubBasics with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("ConstantConditions")
private void removeItem(CustomItem item, Inventory inventory) {
    for (ItemStack content : inventory.getContents()) {
        if (content == null || (content.getType() != item.getMaterial())) {
            continue;
        }
        NBTItem nbtItem = new NBTItem(content);
        if (nbtItem.hasKey("HubBasics")
                && item.getId().equalsIgnoreCase(nbtItem.getString("HubBasics"))) {
            inventory.remove(content);
        }
    }
}
 
Example 13
Source File: Recipes.java    From ProRecipes with GNU General Public License v2.0 5 votes vote down vote up
public void updateInventory(Player p, Inventory i){
	ItemStack[] contents = i.getContents();
	i.clear();
	//p.closeInventory();
	i.setContents(contents);
	//p.openInventory(i);
}
 
Example 14
Source File: RestorableBlock.java    From Modern-LWC with MIT License 5 votes vote down vote up
/**
 * Wrap a block in a restorableblock object
 *
 * @param block
 * @return
 */
public static RestorableBlock wrapBlock(Block block) {
    if (block == null) {
        return null;
    }

    BlockCache blockCache = BlockCache.getInstance();
    RestorableBlock rblock = new RestorableBlock();
    rblock.id = blockCache.getBlockId(block);
    rblock.world = block.getWorld().getName();
    rblock.x = block.getX();
    rblock.y = block.getY();
    rblock.z = block.getZ();
    rblock.data = block.getData();

    BlockState state = block.getState();

    // Does it have an inventory? ^^
    if (state instanceof InventoryHolder) {
        Inventory inventory = ((InventoryHolder) state).getInventory();
        ItemStack[] stacks = inventory.getContents();

        for (int slot = 0; slot < stacks.length; slot++) {
            ItemStack stack = stacks[slot];

            if (stack == null) {
                continue; // don't waste space!
            }

            rblock.setSlot(slot, stack);
        }
    }

    return rblock;
}
 
Example 15
Source File: Items.java    From TabooLib with MIT License 5 votes vote down vote up
public static boolean hasItem(Inventory inventory, Matcher matcher, int amount) {
    int checkAmount = amount;
    for (ItemStack itemStack : inventory.getContents()) {
        if (!isNull(itemStack) && matcher.match(itemStack)) {
            checkAmount -= itemStack.getAmount();
            if (checkAmount <= 0) {
                return true;
            }
        }
    }
    return false;
}
 
Example 16
Source File: Compressor.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onInteract(Player p, Block b) {
	Block dispBlock = b.getRelative(BlockFace.DOWN);
	Dispenser disp = (Dispenser) dispBlock.getState();
	Inventory inv = disp.getInventory();
	
	for (ItemStack item : inv.getContents()) {
		for (ItemStack recipeInput : RecipeType.getRecipeInputs(this)) {
			if (recipeInput != null && SlimefunUtils.isItemSimilar(item, recipeInput, true)) {
				ItemStack output = RecipeType.getRecipeOutput(this, recipeInput);
				Inventory outputInv = findOutputInventory(output, dispBlock, inv);
				
				if (outputInv != null) {
				    ItemStack removing = item.clone();
			        removing.setAmount(recipeInput.getAmount());
			        inv.removeItem(removing);

					craft(p, output, outputInv);
				}
				else {
				    SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);
				}
				
				return;
			}
		}
	}
	
	SlimefunPlugin.getLocalization().sendMessage(p, "machines.unknown-material", true);
}
 
Example 17
Source File: BuyingPlayerShopkeeper.java    From Shopkeepers with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void onPurchaseClick(InventoryClickEvent event, Player player, ItemStack[] usedRecipe, ItemStack offered1, ItemStack offered2) {
	super.onPurchaseClick(event, player, usedRecipe, offered1, offered2);
	if (event.isCancelled()) return;
	final BuyingPlayerShopkeeper shopkeeper = this.getShopkeeper();

	// get offer for this bought item:
	ItemStack requestedItem = usedRecipe[0];
	PriceOffer offer = shopkeeper.getOffer(requestedItem);
	if (offer == null) {
		// this should not happen.. because the recipes were created based on the shopkeeper's offers
		event.setCancelled(true);
		return;
	}

	int tradedItemAmount = offer.getItem().getAmount();
	if (tradedItemAmount > requestedItem.getAmount()) {
		// this shouldn't happen .. because the recipe was created based on this offer
		event.setCancelled(true);
		return;
	}

	// get chest:
	Block chest = shopkeeper.getChest();
	if (!Utils.isChest(chest.getType())) {
		event.setCancelled(true);
		return;
	}

	// remove currency from chest:
	Inventory inventory = ((Chest) chest.getState()).getInventory();
	ItemStack[] contents = inventory.getContents();
	boolean removed = this.removeCurrencyFromChest(offer.getPrice(), contents);
	if (!removed) {
		event.setCancelled(true);
		return;
	}

	// add items to chest:
	int amount = this.getAmountAfterTaxes(tradedItemAmount);
	if (amount > 0) {
		// the item the trading player gave might slightly differ from the required item,
		// but is still accepted, depending on item comparison and settings:
		ItemStack receivedItem = offered1.clone(); // create a copy, just in case
		receivedItem.setAmount(amount);
		if (Utils.addItems(contents, receivedItem) != 0) {
			event.setCancelled(true);
			return;
		}
	}

	// save chest contents:
	inventory.setContents(contents);
}
 
Example 18
Source File: BookPlayerShopkeeper.java    From Shopkeepers with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void onPurchaseClick(InventoryClickEvent event, Player player, ItemStack[] usedRecipe, ItemStack offered1, ItemStack offered2) {
	super.onPurchaseClick(event, player, usedRecipe, offered1, offered2);
	if (event.isCancelled()) return;
	final BookPlayerShopkeeper shopkeeper = this.getShopkeeper();

	ItemStack book = usedRecipe[2];
	String bookTitle = getTitleOfBook(book);
	if (bookTitle == null) {
		// this should not happen.. because the recipes were created based on the shopkeeper's offers
		event.setCancelled(true);
		return;
	}

	// get chest:
	Block chest = shopkeeper.getChest();
	if (!Utils.isChest(chest.getType())) {
		event.setCancelled(true);
		return;
	}

	// remove blank book from chest:
	boolean removed = false;
	Inventory inv = ((Chest) chest.getState()).getInventory();
	ItemStack[] contents = inv.getContents();
	for (int i = 0; i < contents.length; i++) {
		if (contents[i] != null && contents[i].getType() == Material.BOOK_AND_QUILL) {
			if (contents[i].getAmount() == 1) {
				contents[i] = null;
			} else {
				contents[i].setAmount(contents[i].getAmount() - 1);
			}
			removed = true;
			break;
		}
	}
	if (!removed) {
		event.setCancelled(true);
		return;
	}

	// get price:
	BookOffer offer = shopkeeper.getOffer(bookTitle);
	if (offer == null) {
		event.setCancelled(true);
		return;
	}
	int price = this.getAmountAfterTaxes(offer.getPrice());

	// add earnings to chest:
	if (price > 0) {
		int highCost = price / Settings.highCurrencyValue;
		int lowCost = price % Settings.highCurrencyValue;
		if (highCost > 0) {
			if (Utils.addItems(contents, Settings.createHighCurrencyItem(highCost)) != 0) {
				event.setCancelled(true);
				return;
			}
		}
		if (lowCost > 0) {
			if (Utils.addItems(contents, Settings.createCurrencyItem(lowCost)) != 0) {
				event.setCancelled(true);
				return;
			}
		}
	}

	// set chest contents:
	inv.setContents(contents);
}
 
Example 19
Source File: MultiInventory.java    From civcraft with GNU General Public License v2.0 4 votes vote down vote up
public boolean contains(String mid, int type, short data, int amount) {
	
	int count = 0;
	for (Inventory inv : invs) {
		for (ItemStack item : inv.getContents()) {		
			if (item == null) {
				continue;
			}
			
			if (mid != null) {
				LoreCraftableMaterial craftMat = LoreCraftableMaterial.getCraftMaterial(item);
				if (craftMat == null) {
					continue;
				}
				
				if (!craftMat.getConfigId().equals(mid)) {
					continue;
				}
			} else {
				/* Vanilla item. */
				if (ItemManager.getId(item) != type) {
					continue;
				}
				
				/* Only check the data if this item doesnt use durability. */
				if (ItemManager.getMaterial(type).getMaxDurability() == 0) {
					if (ItemManager.getData(item) != data) {
						continue;
					}
				}
			}
			
			count += item.getAmount();
			if (count >= amount) {
				break;
			}
		}
	}
		
	if (count >= amount) 
		return true;
	return false;
}
 
Example 20
Source File: ItemMods.java    From CardinalPGM with MIT License 4 votes vote down vote up
private void applyRules(Inventory inventory) {
    for (ItemStack itemStack : inventory.getContents()) {
        applyRules(itemStack);
    }
}