Java Code Examples for org.spongepowered.api.item.inventory.ItemStack#setQuantity()

The following examples show how to use org.spongepowered.api.item.inventory.ItemStack#setQuantity() . 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: ItemquantityCommand.java    From UltimateCore with MIT License 6 votes vote down vote up
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    checkIfPlayer(sender);
    checkPermission(sender, ItemPermissions.UC_ITEM_ITEMDURABILITY_BASE);
    Player p = (Player) sender;

    if (p.getItemInHand(HandTypes.MAIN_HAND).getType().equals(ItemTypes.NONE)) {
        throw new ErrorMessageException(Messages.getFormatted(p, "item.noiteminhand"));
    }
    ItemStack stack = p.getItemInHand(HandTypes.MAIN_HAND);
    int quantity = args.<Integer>getOne("quantity").get();

    if (quantity > stack.getMaxStackQuantity()) {
        throw new ErrorMessageException(Messages.getFormatted(sender, "item.numberinvalid", "%number%", quantity));
    }

    stack.setQuantity(quantity);
    p.setItemInHand(HandTypes.MAIN_HAND, stack);
    Messages.send(sender, "item.command.itemquantity.success", "%arg%", quantity);
    return CommandResult.success();
}
 
Example 2
Source File: HatExecutor.java    From EssentialCmds with MIT License 5 votes vote down vote up
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException
{
	if (src instanceof Player)
	{
		Player player = (Player) src;
		Optional<ItemStack> itemInHand = player.getItemInHand(HandTypes.MAIN_HAND);

		if (itemInHand.isPresent())
		{
			if (itemInHand.get().getQuantity() > 1)
			{
				ItemStack stack = itemInHand.get();
				stack.setQuantity(itemInHand.get().getQuantity() - 1);
				player.setItemInHand(HandTypes.MAIN_HAND, stack);
				stack.setQuantity(1);
				player.setHelmet(stack);
			}
			else
			{
				player.setHelmet(itemInHand.get());
				player.setItemInHand(HandTypes.MAIN_HAND, null);
			}
		}
		else
		{
			player.sendMessage(Text.of("No item selected in hotbar."));
		}
	}

	return CommandResult.success();
}
 
Example 3
Source File: MoreExecutor.java    From EssentialCmds with MIT License 5 votes vote down vote up
public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
{
	if (src instanceof Player)
	{
		Player player = (Player) src;

		if (player.getItemInHand(HandTypes.MAIN_HAND).isPresent())
		{
			ItemStack stack = player.getItemInHand(HandTypes.MAIN_HAND).get();
			stack.setQuantity(64);
			player.setItemInHand(HandTypes.MAIN_HAND, stack);
			src.sendMessage(Text.of(TextColors.GREEN, "Success! ", TextColors.YELLOW, "Stacked the item in your hand!"));
		}
		else
		{
			src.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "You're not holding anything to stack!"));
		}
	}
	else if (src instanceof ConsoleSource)
	{
		src.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "Must be an in-game player to use /more!"));
	}
	else if (src instanceof CommandBlockSource)
	{
		src.sendMessage(Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "Must be an in-game player to use /more!"));
	}
	return CommandResult.success();
}
 
Example 4
Source File: MoreCommand.java    From UltimateCore with MIT License 5 votes vote down vote up
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
    checkIfPlayer(sender);
    checkPermission(sender, ItemPermissions.UC_ITEM_MORE_BASE);
    Player p = (Player) sender;
    if (p.getItemInHand(HandTypes.MAIN_HAND).getType().equals(ItemTypes.NONE)) {
        throw new ErrorMessageException(Messages.getFormatted(p, "item.noiteminhand"));
    }
    ItemStack stack = p.getItemInHand(HandTypes.MAIN_HAND);
    stack.setQuantity(stack.getMaxStackQuantity());
    p.setItemInHand(HandTypes.MAIN_HAND, stack);
    Messages.send(p, "item.command.more.success", "%amount%", stack.getMaxStackQuantity());
    return CommandResult.success();
}
 
Example 5
Source File: ProtectionManagerImpl.java    From EagleFactions with MIT License 4 votes vote down vote up
private void removeEagleFeather(final User user)
{
    final ItemStack feather = user.getItemInHand(HandTypes.MAIN_HAND).get();
    feather.setQuantity(feather.getQuantity() - 1);
    user.getPlayer().ifPresent(x->x.sendMessage(Text.of(PluginInfo.PLUGIN_PREFIX, TextColors.DARK_PURPLE, "You have used eagle's feather!")));
}
 
Example 6
Source File: PlayerInteractListener.java    From EagleFactions with MIT License 4 votes vote down vote up
private void removeEagleFeather(final Player player)
{
    final ItemStack feather = player.getItemInHand(HandTypes.MAIN_HAND).get();
    feather.setQuantity(feather.getQuantity() - 1);
    player.sendMessage(Text.of(PluginInfo.PLUGIN_PREFIX, TextColors.DARK_PURPLE, "You used Eagle's Feather!"));
}
 
Example 7
Source File: VirtualChestActions.java    From VirtualChest with GNU Lesser General Public License v3.0 4 votes vote down vote up
private CompletableFuture<CommandResult> processCostItem(CommandResult parent, String command,
                                                         ClassToInstanceMap<Context> contextMap)
{
    int count = Integer.parseInt(command.replaceFirst("\\s++$", ""));
    HandheldItemContext itemTemplate = contextMap.getInstance(Context.HANDHELD_ITEM);
    Optional<Player> playerOptional = contextMap.getInstance(Context.PLAYER).getPlayer();
    if (Objects.isNull(itemTemplate) || !playerOptional.isPresent())
    {
        return CompletableFuture.completedFuture(CommandResult.empty());
    }
    Player player = playerOptional.get();
    ItemStackSnapshot itemHeldByMouse = SpongeUnimplemented.getItemHeldByMouse(player);
    int stackUsedQuantity = SpongeUnimplemented.getCount(itemHeldByMouse);
    if (itemTemplate.matchItem(itemHeldByMouse))
    {
        count -= stackUsedQuantity;
        if (count < 0)
        {
            ItemStack stackUsed = itemHeldByMouse.createStack();
            stackUsed.setQuantity(-count);
            SpongeUnimplemented.setItemHeldByMouse(player, stackUsed.createSnapshot());
        }
        else
        {
            SpongeUnimplemented.setItemHeldByMouse(player, ItemStackSnapshot.NONE);
        }
    }
    if (count <= 0)
    {
        return CompletableFuture.completedFuture(CommandResult.success());
    }
    else
    {
        for (Slot slot : player.getInventory().<Slot>slots())
        {
            Optional<ItemStack> stackOptional = slot.peek();
            if (stackOptional.isPresent())
            {
                ItemStackSnapshot slotItem = stackOptional.get().createSnapshot();
                int slotItemSize = SpongeUnimplemented.getCount(slotItem);
                if (itemTemplate.matchItem(slotItem))
                {
                    count -= slotItemSize;
                    if (count < 0)
                    {
                        ItemStack slotItemStack = slotItem.createStack();
                        slotItemStack.setQuantity(-count);
                        slot.set(slotItemStack);
                    }
                    else
                    {
                        slot.clear();
                    }
                }
            }
            if (count <= 0)
            {
                return CompletableFuture.completedFuture(CommandResult.success());
            }
        }
        return CompletableFuture.completedFuture(CommandResult.empty());
    }
}