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

The following examples show how to use org.bukkit.inventory.Inventory#removeItem() . 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: InvUtils.java    From CS-CoreLib with GNU General Public License v3.0 6 votes vote down vote up
public static void removeItem(Inventory inv, ItemStack item, int Amount) {
	ItemStack[] contents = inv.getContents();

	 for (int i = 0; i < Amount; i++) {
		 for (int j = 0; j < contents.length; j++) {
			 if (contents[j] != null) {
				 if (contents[j].getType() == item.getType() && contents[j].getDurability() == item.getDurability()) {
					 if (contents[j].getAmount() > 1) {
						 contents[j].setAmount(contents[j].getAmount() - 1);
					 }
					 else {
						 inv.removeItem(contents[j]);
					 }
					 break;
				 }
			 }
		 }
	 }
}
 
Example 2
Source File: ItemCost.java    From GiantTrees with GNU General Public License v3.0 6 votes vote down vote up
private int takeFromOneInventory(Inventory inventory, int quantity) {
    HashMap<Integer, ? extends ItemStack> matchingInvSlots = inventory.all(getMaterial());

    for (Entry<Integer, ? extends ItemStack> entry : matchingInvSlots.entrySet()) {
        if (matches(entry.getValue())) {
            quantity -= entry.getValue().getAmount();
            if (quantity < 0) {
                entry.getValue().setAmount(-quantity);
                taken.add(entry.getValue().clone());
                break;
            } else {
                inventory.removeItem(entry.getValue());
                taken.add(entry.getValue().clone());
            }
            if (quantity == 0) {
                break;
            }
        }
    }
    return quantity;
}
 
Example 3
Source File: Resident.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public boolean takeItemInHand(int itemId, int itemData, int amount) throws CivException {
	Player player = CivGlobal.getPlayer(this);
	Inventory inv = player.getInventory();

	if (!inv.contains(itemId)) {
		return false;
	}

	if ((player.getItemInHand().getTypeId() != itemId) &&
			(player.getItemInHand().getTypeId() != itemData)) {
		return false;
	}
	
	ItemStack stack = player.getItemInHand();
	
	if (stack.getAmount() < amount) {
		return false;
	} else if (stack.getAmount() == amount) {
		inv.removeItem(stack);
	} else {
		stack.setAmount(stack.getAmount() - amount);
	}
	
	player.updateInventory();
	return true;
}
 
Example 4
Source File: Resident.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public boolean takeItem(int itemId, int itemData, int amount) throws CivException {
	Player player = CivGlobal.getPlayer(this);
	Inventory inv = player.getInventory();

	if (!inv.contains(itemId)) {
		return false;
	}
	
	HashMap<Integer, ? extends ItemStack> stacks;
	stacks = inv.all(itemId);
	
	for (ItemStack stack : stacks.values()) {
		if (stack.getData().getData() != (byte)itemData) {
			continue;
		}
		
		if (stack.getAmount() <= 0)
			continue;
		
		if (stack.getAmount() < amount) {
			amount -= stack.getAmount();
			stack.setAmount(0);
			inv.removeItem(stack);
			continue;
		}
		else {			
			stack.setAmount(stack.getAmount()-amount);
			break;
		}
	}
	
	player.updateInventory();
	return true;
}
 
Example 5
Source File: GrindStone.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 output = RecipeType.getRecipeOutput(this, convert);
				Inventory outputInv = findOutputInventory(output, dispBlock, inv);
				
				if (outputInv != null) {
					ItemStack removing = current.clone();
					removing.setAmount(1);
					inv.removeItem(removing);
					outputInv.addItem(output);
					p.getWorld().playSound(p.getLocation(), Sound.BLOCK_WOODEN_BUTTON_CLICK_ON, 1, 1);
				}
				else {
					SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);
				}
				
				return;
			}
		}
	}
	SlimefunPlugin.getLocalization().sendMessage(p, "machines.unknown-material", true);
}
 
Example 6
Source File: OreWasher.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
private void removeItem(Player p, Block b, Inventory inputInv, Inventory outputInv, ItemStack input, ItemStack output, int amount) {
    if (outputInv != null) {
        ItemStack removing = input.clone();
        removing.setAmount(amount);
        inputInv.removeItem(removing);
        outputInv.addItem(output.clone());

        b.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, Material.WATER);
        b.getWorld().playSound(b.getLocation(), Sound.ENTITY_PLAYER_SPLASH, 1, 1);
    }
    else {
        SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);
    }
}
 
Example 7
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 8
Source File: Juicer.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(1);
					inv.removeItem(removing);
					outputInv.addItem(adding);
					p.getWorld().playSound(b.getLocation(), Sound.ENTITY_PLAYER_SPLASH, 1F, 1F);
					p.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, Material.HAY_BLOCK);
				}
				else SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);
				
				return;
			}
		}
	}
	
	SlimefunPlugin.getLocalization().sendMessage(p, "machines.unknown-material", true);
}
 
Example 9
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 10
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 11
Source File: Talisman.java    From Slimefun4 with GNU General Public License v3.0 4 votes vote down vote up
private static void consumeItem(Inventory inv, Talisman talisman, ItemStack talismanItem) {
    if (talisman.isConsumable()) {
        inv.removeItem(talismanItem);
    }
}