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

The following examples show how to use net.minecraft.item.ItemStack#copy() . 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: TileEntityEtherealMacerator.java    From Electro-Magic-Tools with GNU General Public License v3.0 6 votes vote down vote up
public void smeltItem() {
    if (this.canSmelt()) {
        ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.slots[0]);

        if (this.slots[2] == null) {
            this.slots[2] = itemstack.copy();
            this.slots[2].stackSize *= 2;
        } else if (this.slots[2].isItemEqual(itemstack)) {
            slots[2].stackSize += (itemstack.stackSize * 2);
        }

        --this.slots[0].stackSize;

        if (this.slots[0].stackSize <= 0) {
            this.slots[0] = null;
        }
        this.addBonus();
    }
}
 
Example 2
Source File: RecipeCrudeHaloInfusion.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Nonnull
@Override
public NonNullList<ItemStack> getRemainingItems(@Nonnull InventoryCrafting inv) {
	NonNullList<ItemStack> remainingItems = ForgeHooks.defaultRecipeGetRemainingItems(inv);

	ItemStack gluestick;
	for (int i = 0; i < inv.getSizeInventory(); i++) {
		ItemStack stack = inv.getStackInSlot(i);
		if (stack.getItem() == Items.SLIME_BALL) {
			gluestick = stack.copy();
			remainingItems.set(i, gluestick);
			break;
		}
	}

	return remainingItems;
}
 
Example 3
Source File: ChoppingBlockTileEntity.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void spawnItemStack(World worldIn, double x, double y, double z, ItemStack stack)
{
    while (stack.getCount() > 0)
    {
        int i = /*RANDOM.nextInt(3) +*/ 1;

        if (i > stack.getCount())
        {
            i = stack.getCount();
        }

        ItemStack copy = stack.copy();
        copy.setCount(i);
        stack.grow(-i);

        Block.spawnAsEntity(worldIn, new BlockPos(x, y, z), stack);
    }
}
 
Example 4
Source File: ContainerProgrammableController.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2){

    ItemStack var3 = null;
    Slot var4 = (Slot)inventorySlots.get(par2);

    if(var4 != null && var4.getHasStack()) {
        ItemStack var5 = var4.getStack();
        var3 = var5.copy();

        if(par2 < 5) {
            if(!mergeItemStack(var5, 5, 40, false)) return null;
            var4.onSlotChange(var5, var3);
        } else if(te.isItemValidForSlot(0, var3)) {
            if(!mergeItemStack(var5, 0, 1, false)) return null;
            var4.onSlotChange(var5, var3);
        } else if(var3.getItem() == Itemss.machineUpgrade) {
            if(!mergeItemStack(var5, 1, 5, false)) return null;
            var4.onSlotChange(var5, var3);
        }
        if(var5.stackSize == 0) {
            var4.putStack((ItemStack)null);
        } else {
            var4.onSlotChanged();
        }

        if(var5.stackSize == var3.stackSize) return null;

        var4.onPickupFromSlot(par1EntityPlayer, var5);
    }

    return var3;
}
 
Example 5
Source File: AdapterWritingDesk.java    From OpenPeripheral-Integration with MIT License 5 votes vote down vote up
@ScriptCallable(
		returnTypes = ReturnType.NUMBER,
		description = "Push a page from the notebook into a specific slot in external inventory. Returns the amount of items moved")
public int pushNotebookPage(
		TileEntity desk,
		@Arg(name = "deskSlot") Index deskSlot,
		@Arg(name = "direction", description = "The direction of the other inventory. (north, south, east, west, up or down)") ForgeDirection direction,
		@Arg(name = "fromSlot", description = "The page slot in inventory that you're pushing from") Index fromSlot,
		@Optionals @Arg(name = "intoSlot", description = "The slot in the other inventory that you want to push into") Index intoSlot) {
	final NotebookWrapper wrapper = createInventoryWrapper(desk, deskSlot);

	ItemStack page = wrapper.getPageFromSlot(fromSlot);

	ItemStack removedPage = wrapper.removePage(page);
	Preconditions.checkNotNull(removedPage, "No page in notebook");

	GenericInventory tmp = new GenericInventory("tmp", false, 1);
	tmp.setInventorySlotContents(0, removedPage.copy());

	final IInventory target = getTargetTile(desk, direction);

	if (intoSlot == null) intoSlot = Index.fromJava(-1, 0);

	int result = ItemDistribution.moveItemInto(tmp, 0, target, intoSlot.value, removedPage.stackSize, direction, true);

	int remains = removedPage.stackSize - result;
	if (remains >= 0) {
		ItemStack returns = removedPage.copy();
		returns.stackSize = remains;
		wrapper.addPage(returns);
	}

	return result;
}
 
Example 6
Source File: RecipeHandlerCyaniteReprocessor.java    From NEI-Integration with MIT License 5 votes vote down vote up
public CachedCyaniteReprocessorRecipe(ItemStack input, ItemStack output) {
    input = input.copy();
    input.stackSize = 2;
    this.input = new PositionedStack(input, 36, 25);
    this.output = new PositionedStack(output, 108, 25);
    this.water = new PositionedFluidTank(new FluidStack(FluidRegistry.WATER, 1000), 5000, new Rectangle(1, 1, 16, 62), "neiintegration:textures/overlays.png", new Point(18, 97));
}
 
Example 7
Source File: InventoryUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param simulate If set to true, no items will actually be inserted
 * @return The number of items unable to be inserted
 */
public static int insertItem(InventoryRange inv, @Nonnull ItemStack stack, boolean simulate) {
    stack = stack.copy();
    for (int pass = 0; pass < 2; pass++) {
        for (int slot : inv.slots) {
            ItemStack base = inv.inv.getStackInSlot(slot);
            if ((pass == 0) == (base.isEmpty())) {
                continue;
            }
            int fit = fitStackInSlot(inv, slot, stack);
            if (fit == 0) {
                continue;
            }

            if (!base.isEmpty()) {
                stack.shrink(fit);
                if (!simulate) {
                    base.grow(fit);
                    inv.inv.setInventorySlotContents(slot, base);
                }
            } else {
                if (!simulate) {
                    inv.inv.setInventorySlotContents(slot, ItemUtils.copyStack(stack, fit));
                }
                stack.shrink(fit);
            }
            if (stack.getCount() == 0) {
                return 0;
            }
        }
    }
    return stack.getCount();
}
 
Example 8
Source File: MachineScreenHandler.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public ItemStack transferSlot(PlayerEntity playerEntity, int slotId) {
    ItemStack itemStack = ItemStack.EMPTY;
    Slot slot = this.slots.get(slotId);

    if (slot != null && slot.hasStack()) {
        ItemStack itemStack1 = slot.getStack();
        itemStack = itemStack1.copy();

        if (itemStack.isEmpty()) {
            return itemStack;
        }

        if (slotId < this.blockEntity.getInventory().getSize()) {
            if (!this.insertItem(itemStack1, this.blockEntity.getInventory().getSize(), this.slots.size(), true)) {
                return ItemStack.EMPTY;
            }
        } else if (!this.insertItem(itemStack1, 0, this.blockEntity.getInventory().getSize(), false)) {
            return ItemStack.EMPTY;
        }
        if (itemStack1.getCount() == 0) {
            slot.setStack(ItemStack.EMPTY);
        } else {
            slot.markDirty();
        }
    }
    return itemStack;
}
 
Example 9
Source File: Container4UpgradeSlots.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param itemStack
 *            ItemStack to merge into inventory
 * @param start
 *            minimum slot to attempt fill
 * @param end
 *            maximum slot to attempt fill
 * @param backwards
 *            go backwards
 * @return true if stacks merged successfully public boolean
 *         mergeItemStack(itemStack, start, end, backwards)
 */

@Override
public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2){

    ItemStack var3 = null;
    Slot var4 = (Slot)inventorySlots.get(par2);

    if(var4 != null && var4.getHasStack()) {
        ItemStack var5 = var4.getStack();
        var3 = var5.copy();

        if(par2 < 4) {
            if(!mergeItemStack(var5, 4, 39, false)) return null;
            var4.onSlotChange(var5, var3);
        } else if(var3.getItem() == Itemss.machineUpgrade) {
            if(!mergeItemStack(var5, 0, 4, false)) return null;
            var4.onSlotChange(var5, var3);
        }

        if(var5.stackSize == 0) {
            var4.putStack((ItemStack)null);
        } else {
            var4.onSlotChanged();
        }

        if(var5.stackSize == var3.stackSize) return null;

        var4.onPickupFromSlot(par1EntityPlayer, var5);
    }

    return var3;
}
 
Example 10
Source File: BiomassRecipes.java    From EmergingTechnology with MIT License 5 votes vote down vote up
public static ItemStack getOutputByItemStack(ItemStack itemStack) {
    ItemStack stack = RecipeBuilder.getOutputForItemStackFromRecipes(itemStack, getRecipes());

    if (stack == null) {
        return ItemStack.EMPTY;
    }

    return stack.copy();
}
 
Example 11
Source File: InfusionDisguiseArmor.java    From Gadomancy with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static ItemStack disguiseStack(ItemStack stack, ItemStack disguise) {
    ItemStack out = stack.copy();
    NBTTagCompound compound = NBTHelper.getPersistentData(out);
    if(disguise.getItem() == Items.potionitem) {
        compound.setBoolean("disguise", true);
    } else {
        compound.setTag("disguise", disguise.writeToNBT(new NBTTagCompound()));
    }
    return out;
}
 
Example 12
Source File: ContainerLiquidHopper.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param itemStack
 *            ItemStack to merge into inventory
 * @param start
 *            minimum slot to attempt fill
 * @param end
 *            maximum slot to attempt fill
 * @param backwards
 *            go backwards
 * @return true if stacks merged successfully public boolean
 *         mergeItemStack(itemStack, start, end, backwards)
 */

@Override
public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2){

    ItemStack var3 = null;
    Slot var4 = (Slot)inventorySlots.get(par2);

    if(var4 != null && var4.getHasStack()) {
        ItemStack var5 = var4.getStack();
        var3 = var5.copy();

        if(par2 < 4) {
            if(!mergeItemStack(var5, 4, 40, false)) return null;

            var4.onSlotChange(var5, var3);
        } else {

            if(var5.getItem() == Itemss.machineUpgrade) {
                if(!mergeItemStack(var5, 0, 4, false)) return null;
            }
            var4.onSlotChange(var5, var3);
        }

        if(var5.stackSize == 0) {
            var4.putStack((ItemStack)null);
        } else {
            var4.onSlotChanged();
        }

        if(var5.stackSize == var3.stackSize) return null;

        var4.onPickupFromSlot(par1EntityPlayer, var5);
    }

    return var3;
}
 
Example 13
Source File: CraftingHelper.java    From malmo with MIT License 5 votes vote down vote up
/**
 * Manually attempt to remove ingredients from the player's inventory.<br>
 *
 * @param player
 * @param ingredients
 */
public static void removeIngredientsFromPlayer(EntityPlayerMP player, List<ItemStack> ingredients) {
    NonNullList<ItemStack> main = player.inventory.mainInventory;
    NonNullList<ItemStack> arm = player.inventory.armorInventory;

    for (ItemStack isIngredient : ingredients) {
        int target = isIngredient.getCount();
        for (int i = 0; i < main.size() + arm.size() && target > 0; i++) {
            ItemStack isPlayer = (i >= main.size()) ? arm.get(i - main.size()) : main.get(i);
            if (itemStackIngredientsMatch(isPlayer, isIngredient)) {
                if (target >= isPlayer.getCount()) {
                    // Consume this stack:
                    target -= isPlayer.getCount();
                    if (i >= main.size())
                        arm.get(i - main.size()).setCount(0);
                    else
                        main.get(i).setCount(0);
                } else {
                    isPlayer.setCount(isPlayer.getCount() - target);
                    target = 0;
                }
            }
        }
        ItemStack resultForReward = isIngredient.copy();
        RewardForDiscardingItemImplementation.LoseItemEvent event = new RewardForDiscardingItemImplementation.LoseItemEvent(resultForReward);
        MinecraftForge.EVENT_BUS.post(event);
    }
}
 
Example 14
Source File: ModuleOreMapper.java    From AdvancedRocketry with MIT License 4 votes vote down vote up
ItemMapper(ItemStack block) {
	//Copy so we dont have any possible CME or oddness due to that
	myBlock = block.copy();
}
 
Example 15
Source File: ContainerTFAdvancedAggregator.java    From TofuCraftReload with MIT License 4 votes vote down vote up
/**
 * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
 */
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) {
    ItemStack itemstack = ItemStack.EMPTY;
    Slot slot = inventorySlots.get(slotIndex);

    if (slot != null && slot.getHasStack()) {
        ItemStack itemstack1 = slot.getStack();
        itemstack = itemstack1.copy();

        switch (slotIndex) {
case 0:
            if (!this.mergeItemStack(itemstack1, 5, 41, true)) 
            return ItemStack.EMPTY;
case 1:
            if (!this.mergeItemStack(itemstack1, 5, 41, true)) 
            return ItemStack.EMPTY;
case 2:
            if (!this.mergeItemStack(itemstack1, 5, 41, true)) 
            return ItemStack.EMPTY;
case 3:
            if (!this.mergeItemStack(itemstack1, 5, 41, true)) 
            return ItemStack.EMPTY;
case 4:
            if (!this.mergeItemStack(itemstack1, 5, 41, true)) 
            return ItemStack.EMPTY;
case 5:
            if (!this.mergeItemStack(itemstack1, 5, 41, true)) 
            return ItemStack.EMPTY;
default:
            if (!this.mergeItemStack(itemstack1, 0, 4, false)) {
                return ItemStack.EMPTY;
}

            slot.onSlotChange(itemstack1, itemstack);
        }
        if (itemstack1.getCount() == 0)
            slot.putStack(ItemStack.EMPTY);
        else
            slot.onSlotChanged();
        if (itemstack1.getCount() == itemstack.getCount())
            return ItemStack.EMPTY;

        slot.onTake(player, itemstack1);
    }
    return itemstack;
}
 
Example 16
Source File: SyncedGuiDescription.java    From LibGui with MIT License 4 votes vote down vote up
@Override
public ItemStack onSlotClick(int slotNumber, int button, SlotActionType action, PlayerEntity player) {
	if (action==SlotActionType.QUICK_MOVE) {
		
		if (slotNumber < 0) {
			return ItemStack.EMPTY;
		}
		
		if (slotNumber>=this.slots.size()) return ItemStack.EMPTY;
		Slot slot = this.slots.get(slotNumber);
		if (slot == null || !slot.canTakeItems(player)) {
			return ItemStack.EMPTY;
		}
		
		ItemStack remaining = ItemStack.EMPTY;
		if (slot != null && slot.hasStack()) {
			ItemStack toTransfer = slot.getStack();
			remaining = toTransfer.copy();
			//if (slot.inventory==blockInventory) {
			if (blockInventory!=null) {
				if (slot.inventory==blockInventory) {
					//Try to transfer the item from the block into the player's inventory
					if (!this.insertItem(toTransfer, this.playerInventory, true, player)) {
						return ItemStack.EMPTY;
					}
				} else if (!this.insertItem(toTransfer, this.blockInventory, false, player)) { //Try to transfer the item from the player to the block
					return ItemStack.EMPTY;
				}
			} else {
				//There's no block, just swap between the player's storage and their hotbar
				if (!swapHotbar(toTransfer, slotNumber, this.playerInventory, player)) {
					return ItemStack.EMPTY;
				}
			}
			
			if (toTransfer.isEmpty()) {
				slot.setStack(ItemStack.EMPTY);
			} else {
				slot.markDirty();
			}
		}
		
		return remaining;
	} else {
		return super.onSlotClick(slotNumber, button, action, player);
	}
}
 
Example 17
Source File: ECContainer.java    From ExtraCells1 with MIT License 4 votes vote down vote up
private ItemStack slotClickPhantom(Slot slot, int mouseButton, int modifier, EntityPlayer player)
{
	ItemStack stack = null;

	if (mouseButton == 2)
	{
		if (((SlotFake) slot).canAdjust())
		{
			slot.putStack(null);
		}
	} else if (mouseButton == 0 || mouseButton == 1)
	{
		InventoryPlayer playerInv = player.inventory;
		slot.onSlotChanged();
		ItemStack stackSlot = slot.getStack();
		ItemStack stackHeld = playerInv.getItemStack();

		if (stackSlot != null)
		{
			stack = stackSlot.copy();
		}

		if (stackSlot == null)
		{
			if (stackHeld != null && slot.isItemValid(stackHeld))
			{
				fillPhantomSlot(slot, stackHeld, mouseButton, modifier);
			}
		} else if (stackHeld == null)
		{
			adjustPhantomSlot(slot, mouseButton, modifier);
			slot.onPickupFromSlot(player, playerInv.getItemStack());
		} else if (slot.isItemValid(stackHeld))
		{
			if (canStacksMerge(stackSlot, stackHeld))
			{
				adjustPhantomSlot(slot, mouseButton, modifier);
			} else
			{
				fillPhantomSlot(slot, stackHeld, mouseButton, modifier);
			}
		}
	}
	return stack;
}
 
Example 18
Source File: SyncableItemStack.java    From OpenModsLib with MIT License 4 votes vote down vote up
public void set(@Nonnull ItemStack stack) {
	this.stack = stack.copy();
	markDirty();
}
 
Example 19
Source File: ContainerReactorAccessPort.java    From BigReactors with MIT License 4 votes vote down vote up
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slot)
{
	ItemStack stack = null;
	Slot slotObject = (Slot) inventorySlots.get(slot);
	int numSlots = _port.getSizeInventory();

	if(slotObject != null && slotObject.getHasStack())
	{
		ItemStack stackInSlot = slotObject.getStack();
		stack = stackInSlot.copy();

		if(slot < numSlots)
		{
			if(!mergeItemStack(stackInSlot, numSlots, inventorySlots.size(), true))
			{
				return null;
			}
		}
		else if(!mergeItemStack(stackInSlot, 0, numSlots, false))
		{
			return null;
		}

		if(stackInSlot.stackSize == 0)
		{
			slotObject.putStack(null);
		}
		else
		{
			slotObject.onSlotChanged();
		}

		if(stackInSlot.stackSize == stack.stackSize)
		{
			return null;
		}

		slotObject.onPickupFromSlot(player, stackInSlot);
	}

	return stack;
}
 
Example 20
Source File: ContainerPedestal.java    From Artifacts with MIT License 4 votes vote down vote up
@Override
protected boolean mergeItemStack(ItemStack itemstack, int i, int j, boolean flag) {
	// The default implementation in Slot doesn't take into account the Slot.isItemValid() and Slot.getSlotStackLimit() values.
	// So here is a modified implementation. I have only modified the parts with a comment.

	boolean flag1 = false;
	int k = i;
	if (flag) {
		k = j - 1;
	}
	if (itemstack.isStackable()) {
		while (itemstack.stackSize > 0 && (!flag && k < j || flag && k >= i)) {
			Slot slot = (Slot)inventorySlots.get(k);
			ItemStack itemstack1 = slot.getStack();

			if (flag) {
				k--;
			}
			else {
				k++;
			}

			// Check if item is valid:
			if (!slot.isItemValid(itemstack)) {
				continue;
			}

			if (itemstack1 != null && itemstack1.getItem() == itemstack.getItem() && (!itemstack.getHasSubtypes() || itemstack.getItemDamage() == itemstack1.getItemDamage())
					&& ItemStack.areItemStackTagsEqual(itemstack, itemstack1)) {
				//ItemStack.areItemStacksEqual(par0ItemStack, par1ItemStack)
				//ItemStack.areItemStackTagsEqual(par0ItemStack, par1ItemStack)
				int i1 = itemstack1.stackSize + itemstack.stackSize;

				// Don't put more items than the slot can take:
				int maxItemsInDest = Math.min(itemstack1.getMaxStackSize(), slot.getSlotStackLimit());

				if (i1 <= maxItemsInDest) {
					itemstack.stackSize = 0;
					itemstack1.stackSize = i1;
					slot.onSlotChanged();
					flag1 = true;
				}
				else if (itemstack1.stackSize < maxItemsInDest) {
					itemstack.stackSize -= maxItemsInDest - itemstack1.stackSize;
					itemstack1.stackSize = maxItemsInDest;
					slot.onSlotChanged();
					flag1 = true;
				}
			}

		}
	}
	if (itemstack.stackSize > 0) {
		int l;
		if (flag) {
			l = j - 1;
		}
		else {
			l = i;
		}
		do {
			if ((flag || l >= j) && (!flag || l < i)) {
				break;
			}
			Slot slot1 = (Slot)inventorySlots.get(l);
			ItemStack itemstack2 = slot1.getStack();

			if (flag) {
				l--;
			}
			else {
				l++;
			}

			// Check if item is valid:
			if (!slot1.isItemValid(itemstack)) {
				continue;
			}

			if (itemstack2 == null) {

				// Don't put more items than the slot can take:
				int nbItemsInDest = Math.min(itemstack.stackSize, slot1.getSlotStackLimit());
				ItemStack itemStack1 = itemstack.copy();
				itemstack.stackSize -= nbItemsInDest;
				itemStack1.stackSize = nbItemsInDest;

				slot1.putStack(itemStack1);
				slot1.onSlotChanged();
				// itemstack.stackSize = 0;
				flag1 = true;
				break;
			}
		} while (true);
	}
	return flag1;
}