Java Code Examples for net.minecraft.item.ItemStack#splitStack()

The following examples show how to use net.minecraft.item.ItemStack#splitStack() . 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: ExtendedCreativeInv.java    From NotEnoughItems with MIT License 6 votes vote down vote up
@Override
public ItemStack decrStackSize(int slot, int size) {
    ItemStack item = getStackInSlot(slot);

    if (item != null) {
        if (item.stackSize <= size) {
            setInventorySlotContents(slot, null);
            markDirty();
            return item;
        }
        ItemStack itemstack1 = item.splitStack(size);
        if (item.stackSize == 0)
            setInventorySlotContents(slot, null);

        markDirty();
        return itemstack1;
    }
    return null;
}
 
Example 2
Source File: InventoryUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Static default implementation for IInventory method
 */
public static ItemStack decrStackSize(IInventory inv, int slot, int size) {
    ItemStack item = inv.getStackInSlot(slot);

    if (item != null) {
        if (item.stackSize <= size) {
            inv.setInventorySlotContents(slot, null);
            inv.markDirty();
            return item;
        }
        ItemStack itemstack1 = item.splitStack(size);
        if (item.stackSize == 0)
            inv.setInventorySlotContents(slot, null);
        else
            inv.setInventorySlotContents(slot, item);

        inv.markDirty();
        return itemstack1;
    }
    return null;
}
 
Example 3
Source File: ItemBackpack.java    From WearableBackpacks with MIT License 6 votes vote down vote up
@Override
public boolean itemInteractionForEntity(ItemStack stack, EntityPlayer playerIn,
                                        EntityLivingBase target, EnumHand hand) {
	// When right clicking a non-player entity with a backpack in
	// creative, make the target entity equip the held backpack.
	if (playerIn.world.isRemote || !playerIn.isCreative() ||
	    !BackpackRegistry.canEntityWearBackpacks(target) ||
	    (target instanceof EntityPlayer)) return false;
	
	// If the target entity is already wearing a backpack, call
	// onFaultyRemoval, which may for example drop the backpack's items.
	IBackpack backpack = BackpackHelper.getBackpack(target);
	if (backpack != null) backpack.getType().onFaultyRemoval(target, backpack);
	
	stack = stack.splitStack(1); // This reduces the held stack's size while
	                             // giving us a copy with a stack size of 1.
	// (Not necessary since in creative, but this is the right way to do things!)
	IBackpackData data = BackpackHelper.getBackpackType(stack).createBackpackData(stack);
	BackpackHelper.setEquippedBackpack(target, stack, data);
	
	return true;
}
 
Example 4
Source File: TileEntityGarden.java    From GardenCollection with MIT License 6 votes vote down vote up
@Override
public ItemStack decrStackSize (int slot, int count) {
    ItemStack stack = getStackInSlot(slot);
    if (stack != null) {
        if (stack.stackSize <= count) {
            setInventorySlotContents(slot, null);
            return stack;
        }
        else {
            ItemStack split = stack.splitStack(count);
            if (stack.stackSize == 0)
                setInventorySlotContents(slot, null);

            markDirty();
            return split;
        }
    }

    return null;
}
 
Example 5
Source File: StaticUtils.java    From BigReactors with MIT License 6 votes vote down vote up
/**
 * Consume some amount of items from a stack of items. Assumes you've already validated
 * the consumption. If you try to consume more than the stack has, it will simply destroy
 * the stack, as if you'd consumed all of it.
 * @param stack The stack from which to consume
 * @return The remainder of the stack, or null if the stack was fully consumed.
 */
public static ItemStack consumeItem(ItemStack stack, int amount)
{
	if(stack == null) { return null; }

	if(stack.stackSize <= amount)
	{
		if(stack.getItem().hasContainerItem(stack))
		{
			return stack.getItem().getContainerItem(stack);
		}
		else
		{
			return null;
		}
	}
	else
	{
		stack.splitStack(amount);
		return stack;
	}	
}
 
Example 6
Source File: TileEntityPressureChamberValve.java    From PneumaticCraft with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ItemStack decrStackSize(int slot, int amount){

    ItemStack itemStack = getStackInSlot(slot);
    if(itemStack != null) {
        if(itemStack.stackSize <= amount) {
            setInventorySlotContents(slot, null);
        } else {
            itemStack = itemStack.splitStack(amount);
            if(itemStack.stackSize == 0) {
                setInventorySlotContents(slot, null);
            }
        }
    }

    return itemStack;
}
 
Example 7
Source File: DispenserBehaviorFireball.java    From Artifacts with MIT License 6 votes vote down vote up
/**
 * Dispense the specified stack, play the dispense sound and spawn particles.
 */
public ItemStack dispenseStack(IBlockSource par1IBlockSource, ItemStack par2ItemStack)
{
    EnumFacing enumfacing = BlockTrap.getFacing(par1IBlockSource.getBlockMetadata());
    IPosition iposition = BlockTrap.getIPositionFromBlockSource(par1IBlockSource);
    double d0 = iposition.getX() + (double)((float)enumfacing.getFrontOffsetX() * 0.3F);
    double d1 = iposition.getY() + (double)((float)enumfacing.getFrontOffsetX() * 0.3F);
    double d2 = iposition.getZ() + (double)((float)enumfacing.getFrontOffsetZ() * 0.3F);
    World world = par1IBlockSource.getWorld();
    Random random = world.rand;
    double d3 = random.nextGaussian() * 0.05D + (double)enumfacing.getFrontOffsetX();
    double d4 = random.nextGaussian() * 0.05D + (double)enumfacing.getFrontOffsetY();
    double d5 = random.nextGaussian() * 0.05D + (double)enumfacing.getFrontOffsetZ();
    world.spawnEntityInWorld(new EntitySmallFireball(world, d0, d1, d2, d3, d4, d5));
    par2ItemStack.splitStack(1);
    return par2ItemStack;
}
 
Example 8
Source File: TileEntityElectricCompressor.java    From PneumaticCraft with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ItemStack decrStackSize(int slot, int amount){

    ItemStack itemStack = getStackInSlot(slot);
    if(itemStack != null) {
        if(itemStack.stackSize <= amount) {
            setInventorySlotContents(slot, null);
        } else {
            itemStack = itemStack.splitStack(amount);
            if(itemStack.stackSize == 0) {
                setInventorySlotContents(slot, null);
            }
        }
    }

    return itemStack;
}
 
Example 9
Source File: TileEntityFluxCompressor.java    From PneumaticCraft with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ItemStack decrStackSize(int slot, int amount){

    ItemStack itemStack = getStackInSlot(slot);
    if(itemStack != null) {
        if(itemStack.stackSize <= amount) {
            setInventorySlotContents(slot, null);
        } else {
            itemStack = itemStack.splitStack(amount);
            if(itemStack.stackSize == 0) {
                setInventorySlotContents(slot, null);
            }
        }
    }

    return itemStack;
}
 
Example 10
Source File: TileEntityPneumaticGenerator.java    From PneumaticCraft with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ItemStack decrStackSize(int slot, int amount){

    ItemStack itemStack = getStackInSlot(slot);
    if(itemStack != null) {
        if(itemStack.stackSize <= amount) {
            setInventorySlotContents(slot, null);
        } else {
            itemStack = itemStack.splitStack(amount);
            if(itemStack.stackSize == 0) {
                setInventorySlotContents(slot, null);
            }
        }
    }

    return itemStack;
}
 
Example 11
Source File: TileEntityVacuumPump.java    From PneumaticCraft with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ItemStack decrStackSize(int slot, int amount){

    ItemStack itemStack = getStackInSlot(slot);
    if(itemStack != null) {
        if(itemStack.stackSize <= amount) {
            setInventorySlotContents(slot, null);
        } else {
            itemStack = itemStack.splitStack(amount);
            if(itemStack.stackSize == 0) {
                setInventorySlotContents(slot, null);
            }
        }
    }

    return itemStack;
}
 
Example 12
Source File: ExtendedCreativeInv.java    From NotEnoughItems with MIT License 6 votes vote down vote up
@Override
public ItemStack decrStackSize(int slot, int size) {
    ItemStack item = getStackInSlot(slot);

    if (item != null) {
        if (item.getCount() <= size) {
            setInventorySlotContents(slot, ItemStack.EMPTY);
            markDirty();
            return item;
        }
        ItemStack itemstack1 = item.splitStack(size);
        if (item.getCount() == 0) {
            setInventorySlotContents(slot, ItemStack.EMPTY);
        }

        markDirty();
        return itemstack1;
    }
    return null;
}
 
Example 13
Source File: TileEntityThermopneumaticProcessingPlant.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ItemStack decrStackSize(int slot, int amount){
    ItemStack itemStack = getStackInSlot(slot);
    if(itemStack != null) {
        if(itemStack.stackSize <= amount) {
            setInventorySlotContents(slot, null);
        } else {
            itemStack = itemStack.splitStack(amount);
            if(itemStack.stackSize == 0) {
                setInventorySlotContents(slot, null);
            }
        }
    }
    return itemStack;
}
 
Example 14
Source File: NEIServerUtils.java    From NotEnoughItems with MIT License 5 votes vote down vote up
public static ItemStack copyStack(ItemStack itemstack, int i) {
    if (itemstack == null)
        return null;

    itemstack.stackSize += i;
    return itemstack.splitStack(i);
}
 
Example 15
Source File: DispenserBehaviorFireworks.java    From Artifacts with MIT License 5 votes vote down vote up
/**
 * Dispense the specified stack, play the dispense sound and spawn particles.
 */
public ItemStack dispenseStack(IBlockSource par1IBlockSource, ItemStack par2ItemStack)
{
    EnumFacing enumfacing = BlockDispenser.func_149937_b/*getFacing*/(par1IBlockSource.getBlockMetadata());
    double d0 = par1IBlockSource.getX() + (double)enumfacing.getFrontOffsetX();
    double d1 = (double)((float)par1IBlockSource.getYInt() + 0.2F);
    double d2 = par1IBlockSource.getZ() + (double)enumfacing.getFrontOffsetZ();
    EntityFireworkRocket entityfireworkrocket = new EntityFireworkRocket(par1IBlockSource.getWorld(), d0, d1, d2, par2ItemStack);
    par1IBlockSource.getWorld().spawnEntityInWorld(entityfireworkrocket);
    par2ItemStack.splitStack(1);
    return par2ItemStack;
}
 
Example 16
Source File: TileEntityOmnidirectionalHopper.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ItemStack decrStackSize(int slot, int amount){
    ItemStack itemStack = getStackInSlot(slot);
    if(itemStack != null) {
        if(itemStack.stackSize <= amount) {
            setInventorySlotContents(slot, null);
        } else {
            itemStack = itemStack.splitStack(amount);
            if(itemStack.stackSize == 0) {
                setInventorySlotContents(slot, null);
            }
        }
    }
    return itemStack;
}
 
Example 17
Source File: NEIServerUtils.java    From NotEnoughItems with MIT License 5 votes vote down vote up
public static ItemStack copyStack(ItemStack itemstack, int i) {
    if (itemstack.isEmpty()) {
        return ItemStack.EMPTY;
    }

    itemstack.grow(i);
    return itemstack.splitStack(i);
}
 
Example 18
Source File: TileEntityGasLift.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ItemStack decrStackSize(int slot, int amount){
    ItemStack itemStack = getStackInSlot(slot);
    if(itemStack != null) {
        if(itemStack.stackSize <= amount) {
            setInventorySlotContents(slot, null);
        } else {
            itemStack = itemStack.splitStack(amount);
            if(itemStack.stackSize == 0) {
                setInventorySlotContents(slot, null);
            }
        }
    }
    return itemStack;
}
 
Example 19
Source File: ModularUIContainer.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int index) {
    Slot slot = inventorySlots.get(index);
    if (!slot.canTakeStack(player)) {
        return ItemStack.EMPTY;
    }
    if (!slot.getHasStack()) {
        //return empty if we can't transfer it
        return ItemStack.EMPTY;
    }
    ItemStack stackInSlot = slot.getStack();
    ItemStack stackToMerge = slotMap.get(slot).onItemTake(player, stackInSlot.copy(), true);
    boolean fromContainer = !slotMap.get(slot).getSlotLocationInfo().isPlayerInventory;
    if (!attemptMergeStack(stackToMerge, fromContainer, true)) {
        return ItemStack.EMPTY;
    }
    int itemsMerged;
    if (stackToMerge.isEmpty() || slotMap.get(slot).canMergeSlot(stackToMerge)) {
        itemsMerged = stackInSlot.getCount() - stackToMerge.getCount();
    } else {
        //if we can't have partial stack merge, we have to use all the stack
        itemsMerged = stackInSlot.getCount();
    }
    int itemsToExtract = itemsMerged;
    itemsMerged += transferredPerTick.get(player.world);
    if (itemsMerged > stackInSlot.getMaxStackSize()) {
        //we can merge at most one stack at a time
        return ItemStack.EMPTY;
    }
    transferredPerTick.increment(player.world, itemsToExtract);
    //otherwise, perform extraction and merge
    ItemStack extractedStack = stackInSlot.splitStack(itemsToExtract);
    if (stackInSlot.isEmpty()) {
        slot.putStack(ItemStack.EMPTY);
    } else {
        slot.onSlotChanged();
    }
    extractedStack = slotMap.get(slot).onItemTake(player, extractedStack, false);
    ItemStack resultStack = extractedStack.copy();
    if (!attemptMergeStack(extractedStack, fromContainer, false)) {
        resultStack = ItemStack.EMPTY;
    }
    if (!extractedStack.isEmpty()) {
        player.dropItem(extractedStack, false, false);
        resultStack = ItemStack.EMPTY;
    }
    return resultStack;
}
 
Example 20
Source File: ContainerTFC.java    From TFC2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
/**
 * Handles slot click.
 *  
 * @param mode 0 = basic click, 1 = shift click, 2 = hotbar, 3 = pick block, 4 = drop, 5 = ?, 6 = double click
 */
public ItemStack slotClick(int slotID, int dragType, ClickType clickTypeIn, EntityPlayer p)
{
	if (slotID >= 0 && slotID < this.inventorySlots.size())
	{
		Slot sourceSlot = (Slot) this.inventorySlots.get(slotID);
		ItemStack slotStack = sourceSlot.getStack();

		//This section is for merging foods with differing expirations.
		if(clickTypeIn == ClickType.SWAP && slotStack != null && p.inventory.getItemStack() != null)
		{
			ItemStack itemstack4 = p.inventory.getItemStack();
			if (slotStack.getItem() == itemstack4.getItem() && slotStack.getMetadata() == itemstack4.getMetadata() && ContainerTFC.areCompoundsEqual(slotStack, itemstack4))
			{
				if(slotStack.getItem() instanceof IFood && itemstack4.getItem() instanceof IFood)
				{
					long ex1 = Food.getDecayTimer(slotStack);
					long ex2 = Food.getDecayTimer(itemstack4);
					if(ex2 < ex1)
						Food.setDecayTimer(slotStack, ex2);
				}

				//int l1 = clickedButton == 0 ? itemstack4.getMaxStackSize() : 1;
				int l1 = itemstack4.getMaxStackSize();

				if (l1 > sourceSlot.getItemStackLimit(itemstack4) - slotStack.getMaxStackSize())
				{
					l1 = sourceSlot.getItemStackLimit(itemstack4) - slotStack.getMaxStackSize();
				}

				if (l1 > itemstack4.getMaxStackSize() - slotStack.getMaxStackSize())
				{
					l1 = itemstack4.getMaxStackSize() - slotStack.getMaxStackSize();
				}

				itemstack4.splitStack(l1);

				if (itemstack4.getMaxStackSize() == 0)
				{
					p.inventory.setItemStack(ItemStack.EMPTY);
				}

				slotStack.grow(l1);
				return ItemStack.EMPTY;
			}
			else if (itemstack4.getMaxStackSize() <= sourceSlot.getItemStackLimit(itemstack4))
			{
				sourceSlot.putStack(itemstack4);
				p.inventory.setItemStack(slotStack);
			}
		}

		// Hotbar press to remove from crafting output
		if (clickTypeIn == ClickType.QUICK_MOVE && slotID == 0 && slotStack != null)
		{
			//Removed During Port
			//CraftingHandler.preCraft(p, slotStack, craftMatrix);
		}
		// S and D hotkeys for trimming/combining food
		/*else if (mode == 7 && slotID >= 9 && slotID < 45)
		{
			if (sourceSlot.canTakeStack(p))
			{
				Slot destSlot = (Slot) this.inventorySlots.get(clickedButton);
				destSlot.putStack(slotStack);
				sourceSlot.putStack(null);
				return null;
			}
		}*/
	}

	ItemStack is = super.slotClick(slotID, dragType, clickTypeIn, p);
	//saveContents(is);
	return is;
}