net.minecraft.inventory.IInventory Java Examples

The following examples show how to use net.minecraft.inventory.IInventory. 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: BlockTrackEntryInventory.java    From PneumaticCraft with GNU General Public License v3.0 7 votes vote down vote up
@Override
public void addInformation(World world, int x, int y, int z, TileEntity te, List<String> infoList){
    try {
        IInventory inventory = IOHelper.getInventoryForTE(te);
        if(inventory != null) {
            boolean empty = true;
            ItemStack[] inventoryStacks = new ItemStack[inventory.getSizeInventory()];
            for(int i = 0; i < inventory.getSizeInventory(); i++) {
                ItemStack iStack = inventory.getStackInSlot(i);
                if(iStack != null) empty = false;
                inventoryStacks[i] = iStack;
            }
            if(empty) {
                infoList.add("Contents: Empty");
            } else {
                infoList.add("Contents:");
                PneumaticCraftUtils.sortCombineItemStacksAndToString(infoList, inventoryStacks);
            }
        }
    } catch(Throwable e) {
        addTileEntityToBlackList(te, e);
    }
}
 
Example #2
Source File: BlockTrackEntryInventory.java    From PneumaticCraft with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean shouldTrackWithThisEntry(IBlockAccess world, int x, int y, int z, Block block, TileEntity te){
    if(tileEntityClassToNameMapping == null) {
        try {
            tileEntityClassToNameMapping = (Map)ReflectionHelper.findField(TileEntity.class, "field_145853_j", "classToNameMap").get(null);
        } catch(Exception e) {
            Log.error("[BlockTrackEntryInventory.class] Uhm reflection failed here!");
            e.printStackTrace();
        }
    }
    if(te instanceof TileEntityChest) {
        TileEntityChest chest = (TileEntityChest)te;
        if(chest.adjacentChestXNeg != null || chest.adjacentChestZNeg != null) return false;
    }
    return te != null && !invBlackList.contains(tileEntityClassToNameMapping.get(te.getClass())) && te instanceof IInventory && ((IInventory)te).getSizeInventory() > 0 && !MinecraftForge.EVENT_BUS.post(new InventoryTrackEvent(te));
}
 
Example #3
Source File: ContainerBusFluidStorage.java    From ExtraCells1 with MIT License 6 votes vote down vote up
public ContainerBusFluidStorage(IInventory inventoryPlayer, IInventory inventoryTileEntity)
{
	super(inventoryTileEntity.getSizeInventory());

	tileentity = inventoryTileEntity;

	for (int i = 0; i < 6; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			addSlotToContainer(new SlotFake(inventoryTileEntity, j + i * 9, 8 + j * 18, i * 18 - 10)
			{
				public boolean isItemValid(ItemStack itemstack)
				{
					return tileentity.isItemValidForSlot(0, itemstack);
				}
			});
		}
	}

	bindPlayerInventory(inventoryPlayer);
}
 
Example #4
Source File: AdapterInventory.java    From OpenPeripheral-Integration with MIT License 6 votes vote down vote up
@Asynchronous(false)
@ScriptCallable(description = "Swap two slots in the inventory")
public void swapStacks(IInventory target,
		@Arg(name = "from", description = "The first slot") Index fromSlot,
		@Arg(name = "to", description = "The other slot") Index intoSlot,
		@Optionals @Arg(name = "fromDirection") ForgeDirection fromDirection,
		@Arg(name = "fromDirection") ForgeDirection toDirection) {
	IInventory inventory = InventoryUtils.getInventory(target);
	Preconditions.checkNotNull(inventory, "Invalid target!");
	final int size = inventory.getSizeInventory();
	fromSlot.checkElementIndex("first slot id", size);
	intoSlot.checkElementIndex("second slot id", size);
	if (inventory instanceof ISidedInventory) {
		InventoryUtils.swapStacks((ISidedInventory)inventory,
				fromSlot.value, Objects.firstNonNull(fromDirection, ForgeDirection.UNKNOWN),
				intoSlot.value, Objects.firstNonNull(toDirection, ForgeDirection.UNKNOWN));
	} else InventoryUtils.swapStacks(inventory, fromSlot.value, intoSlot.value);

	inventory.markDirty();
}
 
Example #5
Source File: ContainerMinecoprocessor.java    From Minecoprocessors with GNU General Public License v3.0 6 votes vote down vote up
public ContainerMinecoprocessor(IInventory playerInventory, IInventory te) {
  this.te = te;

  this.codeBookSlot = this.addSlotToContainer(new CodeBookSlot(te, 0, 80, 35));

  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 9; ++j) {
      this.addSlotToContainer(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
    }
  }

  for (int k = 0; k < 9; ++k) {
    this.addSlotToContainer(new Slot(playerInventory, k, 8 + k * 18, 142));
  }

}
 
Example #6
Source File: LogisticsManager.java    From PneumaticCraft with GNU General Public License v3.0 6 votes vote down vote up
public static int getRequestedAmount(SemiBlockLogistics requester, ItemStack providingStack){
    TileEntity te = requester.getTileEntity();
    if(!(te instanceof IInventory)) return 0;
    int requestedAmount = requester instanceof ISpecificRequester ? ((ISpecificRequester)requester).amountRequested(providingStack) : providingStack.stackSize;
    if(requestedAmount == 0) return 0;
    providingStack = providingStack.copy();
    providingStack.stackSize = requestedAmount;
    ItemStack remainder = providingStack.copy();
    remainder.stackSize += requester.getIncomingItems(providingStack);
    for(ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) {
        remainder = IOHelper.insert(te, remainder, d, true);
        if(remainder == null) break;
    }
    if(remainder != null) providingStack.stackSize -= remainder.stackSize;
    if(providingStack.stackSize <= 0) return 0;
    return providingStack.stackSize;
}
 
Example #7
Source File: TileEntityAirCannon.java    From PneumaticCraft with GNU General Public License v3.0 6 votes vote down vote up
private boolean inventoryCanCarry(){
    insertingInventoryHasSpace = true;
    if(lastInsertingInventory == null) return true;
    if(inventory[0] == null) return true;
    TileEntity te = worldObj.getTileEntity(lastInsertingInventory.chunkPosX, lastInsertingInventory.chunkPosY, lastInsertingInventory.chunkPosZ);
    IInventory inv = IOHelper.getInventoryForTE(te);
    if(inv != null) {
        ItemStack remainder = IOHelper.insert(inv, inventory[0].copy(), lastInsertingInventorySide.ordinal(), true);
        insertingInventoryHasSpace = remainder == null;
        return insertingInventoryHasSpace;
    } else {
        lastInsertingInventory = null;
        lastInsertingInventorySide = null;
        return true;
    }
}
 
Example #8
Source File: IOHelper.java    From PneumaticCraft with GNU General Public License v3.0 6 votes vote down vote up
public static ItemStack insert(IInventory inventory, ItemStack itemStack, int side, boolean simulate){

        if(inventory instanceof ISidedInventory && side > -1) {
            ISidedInventory isidedinventory = (ISidedInventory)inventory;
            int[] aint = isidedinventory.getAccessibleSlotsFromSide(side);

            for(int j = 0; j < aint.length && itemStack != null && itemStack.stackSize > 0; ++j) {
                itemStack = insert(inventory, itemStack, aint[j], side, simulate);
            }
        } else if(inventory != null) {
            int k = inventory.getSizeInventory();

            for(int l = 0; l < k && itemStack != null && itemStack.stackSize > 0; ++l) {
                itemStack = insert(inventory, itemStack, l, side, simulate);
            }
        }

        if(itemStack != null && itemStack.stackSize == 0) {
            itemStack = null;
        }

        return itemStack;
    }
 
Example #9
Source File: IOHelper.java    From PneumaticCraft with GNU General Public License v3.0 6 votes vote down vote up
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 #10
Source File: DroneAIExternalProgram.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected boolean isValidPosition(ChunkPosition pos){
    if(traversedPositions.add(pos)) {
        curSlot = 0;
        TileEntity te = drone.getWorld().getTileEntity(pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ);
        return te instanceof IInventory;
    }
    return false;
}
 
Example #11
Source File: TileGuidanceComputerHatch.java    From AdvancedRocketry with MIT License 5 votes vote down vote up
private void ejectChipFrom(IInventory guidanceComputer) {
	for(EnumFacing dir : EnumFacing.VALUES) {
		TileEntity tile = world.getTileEntity(getPos().offset(dir));
		if(tile instanceof IInventory) {
			if(ZUtils.doesInvHaveRoom(guidanceComputer.getStackInSlot(0), (IInventory)tile)) {
				ZUtils.mergeInventory(guidanceComputer.getStackInSlot(0), (IInventory)tile);
				guidanceComputer.removeStackFromSlot(0);
			}
		}
	}
}
 
Example #12
Source File: ContainerCart.java    From TFC2 with GNU General Public License v3.0 5 votes vote down vote up
protected void layoutContainer(IInventory playerInventory, IInventory chestInventory, int xSize, int ySize)
{
	for(int y = 0; y < 3; y++)
	{
		for(int x = 0; x < 9; x++)
		{
			this.addSlotToContainer(new Slot(chestInventory, x+y*9, 8+x*18, 18+y*18));
		}
	}
}
 
Example #13
Source File: InventoryConverter.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Inventory toNova(IInventory nativeObj) {
	if (nativeObj instanceof FWInventory) {
		return ((FWInventory) nativeObj).wrapped;
	}

	return new BWInventory(nativeObj);
}
 
Example #14
Source File: BlockHardMEDrive.java    From ExtraCells1 with 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 #15
Source File: EnchantmentTableTweaks.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static int getEnchantmentSlotIndex(ContainerEnchantment container) {
    IInventory inventory = container.tableInventory;
    for (int i = 0; i < container.inventorySlots.size(); i++) {
        Slot slot = container.inventorySlots.get(i);
        if (slot.isHere(inventory, EnchantmentLapisSlot.ENCHANTMENT_LAPIS_SLOT_INDEX)) return i;
    }
    return -1;
}
 
Example #16
Source File: GuiContainerBase.java    From Signals with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void drawGuiContainerForegroundLayer(int x, int y){
    if(getInvNameOffset() != null && te instanceof IInventory) {
        IInventory inv = (IInventory)te;
        String containerName = inv.hasCustomName() ? inv.getName() : I18n.format(inv.getName() + ".name");
        fontRenderer.drawString(containerName, xSize / 2 - fontRenderer.getStringWidth(containerName) / 2 + getInvNameOffset().x, 6 + getInvNameOffset().y, 4210752);
    }
    if(getInvTextOffset() != null) fontRenderer.drawString(I18n.format("container.inventory"), 8 + getInvTextOffset().x, ySize - 94 + getInvTextOffset().y, 4210752);
}
 
Example #17
Source File: TileEntityPressureChamberValve.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
private void dropInventory(World world, double x, double y, double z){

        IInventory inventory = this;
        Random rand = new Random();
        for(int i = 0; i < inventory.getSizeInventory(); i++) {

            ItemStack itemStack = inventory.getStackInSlot(i);

            if(itemStack != null && itemStack.stackSize > 0) {
                float dX = rand.nextFloat() * 0.8F - 0.4F;
                float dY = rand.nextFloat() * 0.8F - 0.4F;
                float dZ = rand.nextFloat() * 0.8F - 0.4F;

                EntityItem entityItem = new EntityItem(world, x + dX, y + dY, z + dZ, new ItemStack(itemStack.getItem(), itemStack.stackSize, itemStack.getItemDamage()));

                if(itemStack.hasTagCompound()) {
                    entityItem.getEntityItem().setTagCompound((NBTTagCompound)itemStack.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);
                // itemStack.stackSize = 0;
            }
        }
        this.inventory = new ItemStack[4];
    }
 
Example #18
Source File: InventoryRange.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
public InventoryRange(IInventory inv, int fslot, int lslot)
{
    this.inv = inv;
    slots = new int[lslot-fslot];
    for(int i = 0; i < slots.length; i++)
        slots[i] = fslot+i;
}
 
Example #19
Source File: ObservationFromFullInventoryImplementation.java    From malmo with MIT License 5 votes vote down vote up
public static String getInventoryName(IInventory inv)
{
    String invName = inv.getName();
    String prefix = "container.";
    if (invName.startsWith(prefix))
        invName = invName.substring(prefix.length());
    return invName;
}
 
Example #20
Source File: InventoryUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Consumes one item from slot in inv with support for containers.
 */
public static void consumeItem(IInventory inv, int slot) {
    ItemStack stack = inv.getStackInSlot(slot);
    Item item = stack.getItem();
    if (item.hasContainerItem(stack)) {
        ItemStack container = item.getContainerItem(stack);
        inv.setInventorySlotContents(slot, container);
    } else {
        inv.decrStackSize(slot, 1);
    }
}
 
Example #21
Source File: ContainerBusFluidImport.java    From ExtraCells1 with MIT License 5 votes vote down vote up
protected void bindPlayerInventory(IInventory inventoryPlayer)
{
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, i * 18 + 79));
		}
	}

	for (int i = 0; i < 9; i++)
	{
		addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 137));
	}
}
 
Example #22
Source File: ItemSubCollar.java    From ForbiddenMagic with Do What The F*ck You Want To Public License 5 votes vote down vote up
/**
 * Returns true if the item can be used on the given entity, e.g. shears on sheep.
 */
@Override
public boolean itemInteractionForEntity(ItemStack itemstack, EntityPlayer player, EntityLivingBase entity)
{
    if (entity.worldObj.isRemote)
    {
        return false;
    }
    if (entity instanceof EntityPlayer)
    {
        EntityPlayer sub = (EntityPlayer)entity;
        IInventory baubles = BaublesApi.getBaubles(sub);
        if(baubles.getStackInSlot(0) == null) {
            if(!itemstack.hasTagCompound()){
                NBTTagCompound tag = new NBTTagCompound();
                itemstack.setTagCompound(tag);
            }
            itemstack.stackTagCompound.setString("owner", player.getDisplayName());
            baubles.setInventorySlotContents(0, itemstack.copy());
            itemstack.stackSize = 0;
            sub.addChatMessage(new ChatComponentText(StatCollector.translateToLocal("message.collar.placescollar").replace("%s", player.getDisplayName())));
            player.addChatMessage(new ChatComponentText(StatCollector.translateToLocal("message.collar.youplacecollar").replace("%s", sub.getDisplayName())));
            return true;
        }
        else
            player.addChatMessage(new ChatComponentText(StatCollector.translateToLocal("message.collar.alreadywearing").replace("%s", sub.getDisplayName())));
    }
    return false;
}
 
Example #23
Source File: AmadronOfferCustom.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
public void returnStock(){
    TileEntity provider = getProvidingTileEntity();
    TileEntity returning = getReturningTileEntity();
    invert();
    while(inStock > 0) {
        int stock = Math.min(inStock, 50);
        stock = ContainerAmadron.capShoppingAmount(this, stock, returning instanceof IInventory ? (IInventory)returning : null, provider instanceof IInventory ? (IInventory)provider : null, returning instanceof IFluidHandler ? (IFluidHandler)returning : null, provider instanceof IFluidHandler ? (IFluidHandler)provider : null, null);
        if(stock > 0) {
            inStock -= stock;
            if(getInput() instanceof ItemStack) {
                ItemStack deliveringItems = (ItemStack)getInput();
                int amount = deliveringItems.stackSize * stock;
                List<ItemStack> stacks = new ArrayList<ItemStack>();
                while(amount > 0) {
                    ItemStack stack = deliveringItems.copy();
                    stack.stackSize = Math.min(amount, stack.getMaxStackSize());
                    stacks.add(stack);
                    amount -= stack.stackSize;
                }
                PneumaticRegistry.getInstance().deliverItemsAmazonStyle(provider.getWorldObj(), provider.xCoord, provider.yCoord, provider.zCoord, stacks.toArray(new ItemStack[stacks.size()]));
            } else {
                FluidStack deliveringFluid = ((FluidStack)getInput()).copy();
                deliveringFluid.amount *= stock;
                PneumaticRegistry.getInstance().deliverFluidAmazonStyle(provider.getWorldObj(), provider.xCoord, provider.yCoord, provider.zCoord, deliveringFluid);
            }
        } else {
            break;
        }
    }
}
 
Example #24
Source File: InventoryUtils.java    From OpenModsLib with MIT License 5 votes vote down vote up
public static Iterable<ItemStack> asIterable(final IInventory inv) {
	return () -> new AbstractIterator<ItemStack>() {
		final int size = inv.getSizeInventory();
		int slot = 0;

		@Override
		protected ItemStack computeNext() {
			if (slot >= size) return endOfData();
			return inv.getStackInSlot(slot++);
		}
	};
}
 
Example #25
Source File: GraveyardGenerator.java    From ToroQuest with GNU General Public License v3.0 5 votes vote down vote up
protected void addLootToChest(IInventory chest) {
	for (int i = 0; i < chest.getSizeInventory(); i++) {
		int roll = rand.nextInt(8);

		if (roll == 0) {
			chest.setInventorySlotContents(i, new ItemStack(Items.BONE, rand.nextInt(5) + 1));
		} else if (roll == 1) {
			chest.setInventorySlotContents(i, new ItemStack(Items.ROTTEN_FLESH, rand.nextInt(5) + 1));
		} else if (roll == 2) {
			if (rand.nextInt(20) == 0) {
				chest.setInventorySlotContents(i, new ItemStack(NICE_STUFF[rand.nextInt(NICE_STUFF.length)]));
			}
		}
	}
}
 
Example #26
Source File: IOHelper.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
public static IInventory getInventoryForTE(TileEntity te){

        if(te instanceof IInventory) {
            IInventory inv = (IInventory)te;
            Block block = te.getBlockType();
            if(block instanceof BlockChest) {
                inv = ((BlockChest)block).func_149951_m(te.getWorldObj(), te.xCoord, te.yCoord, te.zCoord);
            }
            return inv;
        } else {
            return null;
        }
    }
 
Example #27
Source File: ArcanePackagerGui.java    From Gadomancy with GNU Lesser General Public License v3.0 5 votes vote down vote up
public ArcanePackagerGui(InventoryPlayer playerInv, IInventory inventory) {
    super(new ContainerArcanePackager(playerInv, inventory));

    this.tile = (TileArcanePackager) inventory;
    this.playerInv = playerInv;

    this.ySize = 234;
    this.xSize = 190;
}
 
Example #28
Source File: Inventories.java    From Production-Line with MIT License 4 votes vote down vote up
public static void spill(World world, double x, double y, double z, IInventory inventory) {
    for (int i = 0; i < inventory.getSizeInventory(); i++) {
        spill(world, x, y, z, inventory.removeStackFromSlot(i));
    }
}
 
Example #29
Source File: AdapterInventory.java    From OpenPeripheral-Integration with MIT License 4 votes vote down vote up
@Override
public Class<?> getTargetClass() {
	return IInventory.class;
}
 
Example #30
Source File: InventoryRange.java    From CodeChickenLib with GNU Lesser General Public License v2.1 4 votes vote down vote up
public InventoryRange(IInventory inv) {
    this(inv, Direction.DOWN);
}