Java Code Examples for net.minecraft.inventory.IInventory#getStackInSlot()

The following examples show how to use net.minecraft.inventory.IInventory#getStackInSlot() . These examples are extracted from open source projects. 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
/**
 * Static default implementation for IInventory method
 */
@Nonnull
public static ItemStack decrStackSize(IInventory inv, int slot, int size) {
    ItemStack item = inv.getStackInSlot(slot);

    if (!item.isEmpty()) {
        if (item.getCount() <= size) {
            inv.setInventorySlotContents(slot, ItemStack.EMPTY);
            inv.markDirty();
            return item;
        }
        ItemStack itemstack1 = item.split(size);
        if (item.getCount() == 0) {
            inv.setInventorySlotContents(slot, ItemStack.EMPTY);
        } else {
            inv.setInventorySlotContents(slot, item);
        }

        inv.markDirty();
        return itemstack1;
    }
    return ItemStack.EMPTY;
}
 
Example 2
private boolean tryCookSlot(IInventory inv, int slot){
    ItemStack stack = inv.getStackInSlot(slot);
    if(stack != null) {
        ItemStack result = FurnaceRecipes.smelting().getSmeltingResult(stack);
        if(result != null) {
            ItemStack remainder = IOHelper.insert(getTileEntity(), result, true);
            if(remainder == null) {
                IOHelper.insert(getTileEntity(), result, false);
                inv.decrStackSize(slot, 1);
                lastValidSlot = slot;
                return true;
            }
        }
    }
    return false;
}
 
Example 3
@Override
public boolean linkRocket(EntityRocketBase rocket) {
	this.rocket = (EntityRocket) rocket;
	IInventory guidanceComputer;

	if(!chipEjected && buttonState[buttonAutoEject] && (guidanceComputer = this.rocket.storage.getGuidanceComputer()) != null ) {
		ItemStack stack = guidanceComputer.getStackInSlot(0);
		if(!stack.isEmpty()) {
			if(		(stack.getItem() instanceof ItemSatelliteIdentificationChip && buttonState[buttonSatellite]) ||
					(stack.getItem() instanceof ItemPlanetIdentificationChip && buttonState[buttonPlanet]) ||
					(stack.getItem() instanceof ItemStationChip && buttonState[buttonStation])) {
				ejectChipFrom(guidanceComputer);
				chipEjected = true;
			}
		}
		else
			chipEjected = true;
	}

	return true;
}
 
Example 4
public void equipTick(World world, EntityPlayer player, Aspect aspect) {
    if(world.isRemote) return;

    FamiliarData data = new FamiliarData(player.getCommandSenderName(), aspect.getTag());

    IInventory baublesInv = BaublesApi.getBaubles(player);
    if(baublesInv.getStackInSlot(0) == null) {
        handleUnequip(world, player, aspect);
        return;
    }

    if(familiarControllers.get(player) == null || !playersWithFamiliar.contains(data)) {
        handleEquip(world, player, aspect);
    }

    familiarControllers.get(player).tick();
}
 
Example 5
Source Project: AdvancedRocketry   File: TileMicrowaveReciever.java    License: MIT License 6 votes vote down vote up
@Override
public void onInventoryUpdated() {
	super.onInventoryUpdated();

	List list = new LinkedList<Long>();

	for(IInventory inv : itemInPorts) {
		for(int i = 0; i < inv.getSizeInventory(); i++) {
			ItemStack stack = inv.getStackInSlot(i);
			if(stack != null && stack.getItem() instanceof ItemSatelliteIdentificationChip) {
				ItemSatelliteIdentificationChip item = (ItemSatelliteIdentificationChip)stack.getItem();
				list.add(item.getSatelliteId(stack));
			}
		}
	}


	connectedSatellites = list;

}
 
Example 6
public static void dropInventory(World world, int x, int y, int z){

        TileEntity tileEntity = world.getTileEntity(x, y, z);

        if(!(tileEntity instanceof IInventory)) {
            return;
        }

        IInventory inventory = (IInventory)tileEntity;

        for(int i = 0; i < inventory.getSizeInventory(); i++) {
            ItemStack itemStack = inventory.getStackInSlot(i);

            if(itemStack != null && itemStack.stackSize > 0) {
                spawnItemInWorld(world, itemStack, x, y, z);
            }
        }
    }
 
Example 7
Source Project: AdvancedRocketry   File: TileWarpCore.java    License: MIT License 5 votes vote down vote up
@Override
public void onInventoryUpdated() {
	//Needs completion
	if(itemInPorts.isEmpty() /*&& !worldObj.isRemote*/) {
		attemptCompleteStructure(world.getBlockState(pos));
	}
	
	if(getSpaceObject() == null || getSpaceObject().getFuelAmount() == getSpaceObject().getMaxFuelAmount())
		return;
	for(IInventory inv : itemInPorts) {
		for(int i = 0; i < inv.getSizeInventory(); i++) {
			ItemStack stack = inv.getStackInSlot(i);
			int amt = 0;
			if(stack != null && OreDictionary.itemMatches(MaterialRegistry.getItemStackFromMaterialAndType("Dilithium", AllowedProducts.getProductByName("CRYSTAL")), stack, false)) {
				int stackSize = stack.getCount();
				if(!world.isRemote)
					amt = getSpaceObject().addFuel(Configuration.fuelPointsPerDilithium*stack.getCount());
				else
					amt = Math.min(getSpaceObject().getFuelAmount() + 10*stack.getCount(), getSpaceObject().getMaxFuelAmount()) - getSpaceObject().getFuelAmount();//
				inv.decrStackSize(i, amt/10);
				inv.markDirty();
				
				//If full
				if(stackSize/10 != amt)
					return;
			}
		}
	}
}
 
Example 8
Source Project: ExtraCells1   File: BlockHardMEDrive.java    License: MIT License 5 votes vote down vote up
private void dropItems(World world, int x, int y, int z)
{
	Random rand = new Random();

	TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
	if (!(tileEntity instanceof IInventory))
	{
		return;
	}
	IInventory inventory = (IInventory) tileEntity;

	for (int i = 0; i < inventory.getSizeInventory(); i++)
	{
		ItemStack item = inventory.getStackInSlot(i);

		if (item != null && item.stackSize > 0)
		{
			float rx = rand.nextFloat() * 0.8F + 0.1F;
			float ry = rand.nextFloat() * 0.8F + 0.1F;
			float rz = rand.nextFloat() * 0.8F + 0.1F;

			EntityItem entityItem = new EntityItem(world, x + rx, y + ry, z + rz, new ItemStack(item.itemID, item.stackSize, item.getItemDamage()));

			if (item.hasTagCompound())
			{
				entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy());
			}

			float factor = 0.05F;
			entityItem.motionX = rand.nextGaussian() * factor;
			entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
			entityItem.motionZ = rand.nextGaussian() * factor;
			world.spawnEntityInWorld(entityItem);
			item.stackSize = 0;
		}
	}
}
 
Example 9
@Override
public void onInventoryChanged(IInventory inventory, int slotNumber) {
	if (worldObj.isRemote) return;
	boolean nowHasTicket = inventory.getStackInSlot(SLOT_OUTPUT) != null;
	if (nowHasTicket != hasTicket.getValue()) hasTicket.set(nowHasTicket);
	markUpdated();
}
 
Example 10
@ScriptCallable(returnTypes = ReturnType.BOOLEAN, description = "Has the Table any items")
public boolean hasItem(Object target) {
	if (target instanceof IInventory) {
		IInventory inv = (IInventory)target;
		return inv.getStackInSlot(0) != null;
	}
	return false;
}
 
Example 11
/**
 * This method will be called by the BlockTrackUpgradeHandler when it finds inventories while scanning blocks.
 * @param te TileEntity that already has been checked on if it implements IInventory, so it's save to cast it to IInventory.
 */
public void checkInventoryForItems(TileEntity te){
    try {
        ItemStack searchStack = ItemPneumaticArmor.getSearchedStack(FMLClientHandler.instance().getClient().thePlayer.getCurrentArmor(3));
        IInventory inventory = (IInventory)te;
        boolean hasFoundItem = false;
        if(searchStack != null) {
            for(int l = 0; l < inventory.getSizeInventory(); l++) {
                if(inventory.getStackInSlot(l) != null) {
                    int items = RenderSearchItemBlock.getSearchedItemCount(inventory.getStackInSlot(l), searchStack);
                    if(items > 0) {
                        hasFoundItem = true;
                        searchedItemCounter += items;
                    }
                }
            }
        }
        if(hasFoundItem) {
            boolean inList = false;
            for(RenderSearchItemBlock trackedBlock : searchedBlocks) {
                if(trackedBlock.isAlreadyTrackingCoord(te.xCoord, te.yCoord, te.zCoord)) {
                    inList = true;
                    break;
                }
            }
            if(!inList) searchedBlocks.add(new RenderSearchItemBlock(te.getWorldObj(), te.xCoord, te.yCoord, te.zCoord));
        }
    } catch(Throwable e) {

    }
}
 
Example 12
public void checkPlayerEquipment(EntityPlayer p) {
    IInventory baublesInv = BaublesApi.getBaubles(p);
    if(baublesInv.getStackInSlot(0) != null) {
        ItemStack amulet = baublesInv.getStackInSlot(0);
        if(amulet.getItem() != null && amulet.getItem() instanceof ItemEtherealFamiliar) {
            Aspect a = ItemEtherealFamiliar.getFamiliarAspect(amulet);
            if(a != null) {
                handleEquip(p.worldObj, p, a);
            }
        }
    }
}
 
Example 13
private void trySwitchTool(IInventory inv) {
    for(int slot = 0; slot < inv.getSizeInventory(); slot++) {
        ItemStack stack = inv.getStackInSlot(slot);

        ItemStack current = golem.getCarried();
        if(current != null) {
            current = InventoryUtils.insertStack(inv, current, golem.homeFacing, true);
            if(current != null) {
                return;
            }
            golem.setCarried(null);

            if(hasValidTool()) {
                hasValidTool = true;
                return;
            }
        }

        if(stack != null && isValidTool(stack)) {
            stack = stack.copy();

            stack.stackSize = 1;
            stack = InventoryUtils.extractStack(inv, stack, golem.homeFacing, false, false, false, true);

            if(stack != null) {
                golem.setCarried(stack);
                golem.updateCarried();
                player.setHeldItem(stack);

                hasValidTool = true;
                break;
            }
        }
    }
}
 
Example 14
Source Project: OpenModsLib   File: BlockUtils.java    License: MIT License 5 votes vote down vote up
public static void dropInventory(IInventory inventory, World world, double x, double y, double z) {
	if (inventory == null) { return; }
	for (int i = 0; i < inventory.getSizeInventory(); ++i) {
		ItemStack itemStack = inventory.getStackInSlot(i);
		if (!itemStack.isEmpty()) {
			dropItemStackInWorld(world, x, y, z, itemStack);
		}
	}
}
 
Example 15
@Override
public boolean matches(IInventory var1, World world, EntityPlayer player)
{
	if (research.length()>0 && !ThaumcraftApiHelper.isResearchComplete(player.getCommandSenderName(), research)) {
		return false;
	}
	
    ArrayList required = new ArrayList(input);
    
    for (int x = 0; x < 9; x++)
    {
        ItemStack slot = var1.getStackInSlot(x);

        if (slot != null)
        {
            boolean inRecipe = false;
            Iterator req = required.iterator();

            while (req.hasNext())
            {
                boolean match = false;

                Object next = req.next();

                if (next instanceof ItemStack)
                {
                    match = checkItemEquals((ItemStack)next, slot);
                }
                else if (next instanceof ArrayList)
                {
                    for (ItemStack item : (ArrayList<ItemStack>)next)
                    {
                        match = match || checkItemEquals(item, slot);
                    }
                }

                if (match)
                {
                    inRecipe = true;
                    required.remove(next);
                    break;
                }
            }

            if (!inRecipe)
            {
                return false;
            }
        }
    }
    
    return required.isEmpty();
}
 
Example 16
/**
 * Gets the size of the stack in a slot. Returns 0 on empty stacks
 */
public static int stackSize(IInventory inv, int slot) {
    ItemStack stack = inv.getStackInSlot(slot);
    return stack.isEmpty() ? 0 : stack.getCount();
}
 
Example 17
/**
 * Dispatches mana to items in a given player's inventory. Note that this method
 * does not automatically remove mana from the item which is exporting.
 * @param manaToSend How much mana is to be sent.
 * @param remove If true, the mana will be added from the target item. Set to false to just check.
 * @return The amount of mana actually sent.
 */
public static int dispatchMana(ItemStack stack, EntityPlayer player, int manaToSend, boolean add) {
	if(stack == null)
		return 0;

	IInventory mainInv = player.inventory;
	IInventory baublesInv = BotaniaAPI.internalHandler.getBaublesInventory(player);

	int invSize = mainInv.getSizeInventory();
	int size = invSize;
	if(baublesInv != null)
		size += baublesInv.getSizeInventory();

	for(int i = 0; i < size; i++) {
		boolean useBaubles = i >= invSize;
		IInventory inv = useBaubles ? baublesInv : mainInv;
		int slot = i - (useBaubles ? invSize : 0);
		ItemStack stackInSlot = inv.getStackInSlot(slot);
		if(stackInSlot == stack)
			continue;

		if(stackInSlot != null && stackInSlot.getItem() instanceof IManaItem) {
			IManaItem manaItemSlot = (IManaItem) stackInSlot.getItem();

			if(manaItemSlot.canReceiveManaFromItem(stackInSlot, stack)) {
				if(stack.getItem() instanceof IManaItem && !((IManaItem) stack.getItem()).canExportManaToItem(stack, stackInSlot))
					continue;

				int received = 0;
				if(manaItemSlot.getMana(stackInSlot) + manaToSend <= manaItemSlot.getMaxMana(stackInSlot))
					received = manaToSend;
				else received = manaToSend - (manaItemSlot.getMana(stackInSlot) + manaToSend - manaItemSlot.getMaxMana(stackInSlot));


				if(add)
					manaItemSlot.addMana(stackInSlot, manaToSend);
				if(useBaubles)
					BotaniaAPI.internalHandler.sendBaubleUpdatePacket(player, slot);

				return received;
			}
		}
	}

	return 0;
}
 
Example 18
@Override
public boolean matches(IInventory var1, World world, EntityPlayer player)
{
	if (research.length()>0 && !ThaumcraftApiHelper.isResearchComplete(player.getCommandSenderName(), research)) {
		return false;
	}
	
    ArrayList required = new ArrayList(input);
    
    for (int x = 0; x < 9; x++)
    {
        ItemStack slot = var1.getStackInSlot(x);

        if (slot != null)
        {
            boolean inRecipe = false;
            Iterator req = required.iterator();

            while (req.hasNext())
            {
                boolean match = false;

                Object next = req.next();

                if (next instanceof ItemStack)
                {
                    match = checkItemEquals((ItemStack)next, slot);
                }
                else if (next instanceof ArrayList)
                {
                    for (ItemStack item : (ArrayList<ItemStack>)next)
                    {
                        match = match || checkItemEquals(item, slot);
                    }
                }

                if (match)
                {
                    inRecipe = true;
                    required.remove(next);
                    break;
                }
            }

            if (!inRecipe)
            {
                return false;
            }
        }
    }
    
    return required.isEmpty();
}
 
Example 19
@Override
public boolean matches(IInventory var1, World world, EntityPlayer player)
{
	if (research.length()>0 && !ThaumcraftApiHelper.isResearchComplete(player.getCommandSenderName(), research)) {
		return false;
	}
	
    ArrayList required = new ArrayList(input);
    
    for (int x = 0; x < 9; x++)
    {
        ItemStack slot = var1.getStackInSlot(x);

        if (slot != null)
        {
            boolean inRecipe = false;
            Iterator req = required.iterator();

            while (req.hasNext())
            {
                boolean match = false;

                Object next = req.next();

                if (next instanceof ItemStack)
                {
                    match = checkItemEquals((ItemStack)next, slot);
                }
                else if (next instanceof ArrayList)
                {
                    for (ItemStack item : (ArrayList<ItemStack>)next)
                    {
                        match = match || checkItemEquals(item, slot);
                    }
                }

                if (match)
                {
                    inRecipe = true;
                    required.remove(next);
                    break;
                }
            }

            if (!inRecipe)
            {
                return false;
            }
        }
    }
    
    return required.isEmpty();
}
 
Example 20
Source Project: Et-Futurum   File: ContainerEnchantment.java    License: The Unlicense 4 votes vote down vote up
/**
 * Callback for when the crafting matrix is changed.
 */
@Override
public void onCraftMatrixChanged(IInventory p_75130_1_) {
	if (p_75130_1_ == tableInventory) {
		ItemStack var2 = p_75130_1_.getStackInSlot(0);
		int power;

		if (var2 != null && var2.isItemEnchantable()) {
			if (!world.isRemote) {
				power = 0;
				int j;

				for (j = -1; j <= 1; ++j)
					for (int k = -1; k <= 1; ++k)
						if ((j != 0 || k != 0) && world.isAirBlock(posX + k, posY, posZ + j) && world.isAirBlock(posX + k, posY + 1, posZ + j)) {
							power += ForgeHooks.getEnchantPower(world, posX + k * 2, posY, posZ + j * 2);
							power += ForgeHooks.getEnchantPower(world, posX + k * 2, posY + 1, posZ + j * 2);

							if (k != 0 && j != 0) {
								power += ForgeHooks.getEnchantPower(world, posX + k * 2, posY, posZ + j);
								power += ForgeHooks.getEnchantPower(world, posX + k * 2, posY + 1, posZ + j);
								power += ForgeHooks.getEnchantPower(world, posX + k, posY, posZ + j * 2);
								power += ForgeHooks.getEnchantPower(world, posX + k, posY + 1, posZ + j * 2);
							}
						}

				rand.setSeed(enchantmentSeed);

				for (j = 0; j < 3; ++j) {
					enchantLevels[j] = EnchantmentHelper.calcItemStackEnchantability(rand, j, power, var2);
					field_178151_h[j] = -1;

					if (enchantLevels[j] < j + 1)
						enchantLevels[j] = 0;
				}

				for (j = 0; j < 3; ++j)
					if (enchantLevels[j] > 0) {
						List<EnchantmentData> var7 = func_178148_a(var2, j, enchantLevels[j]);

						if (var7 != null && !var7.isEmpty()) {
							EnchantmentData var6 = var7.get(rand.nextInt(var7.size()));
							field_178151_h[j] = var6.enchantmentobj.effectId | var6.enchantmentLevel << 8;
						}
					}

				detectAndSendChanges();
			}
		} else
			for (power = 0; power < 3; ++power) {
				enchantLevels[power] = 0;
				field_178151_h[power] = -1;
			}
	}
}