Search in sources :

Example 61 with IInventory

use of net.minecraft.inventory.IInventory in project ArsMagica2 by Mithion.

the class EntityAIChestDeposit method updateTask.

@Override
public void updateTask() {
    AMVector3 iLoc = host.getChestLocation();
    if (iLoc == null)
        return;
    TileEntity te = host.worldObj.getTileEntity((int) iLoc.x, (int) iLoc.y, (int) iLoc.z);
    if (te == null || !(te instanceof IInventory))
        return;
    if (new AMVector3(host).distanceSqTo(iLoc) > 256) {
        host.setPosition(iLoc.x, iLoc.y, iLoc.z);
        return;
    }
    if (new AMVector3(host).distanceSqTo(iLoc) > 9) {
        host.getNavigator().tryMoveToXYZ(iLoc.x + 0.5, iLoc.y, iLoc.z + 0.5, 0.5f);
    } else {
        IInventory inventory = (IInventory) te;
        if (!isDepositing)
            inventory.openInventory();
        isDepositing = true;
        depositCounter++;
        if (depositCounter > 10) {
            ItemStack mergeStack = InventoryUtilities.getFirstStackInInventory(host.getBroomInventory()).copy();
            int originalSize = mergeStack.stackSize;
            if (!InventoryUtilities.mergeIntoInventory(inventory, mergeStack, 1)) {
                if (te instanceof TileEntityChest) {
                    TileEntityChest chest = (TileEntityChest) te;
                    TileEntityChest adjacent = null;
                    if (chest.adjacentChestXNeg != null)
                        adjacent = chest.adjacentChestXNeg;
                    else if (chest.adjacentChestXPos != null)
                        adjacent = chest.adjacentChestXPos;
                    else if (chest.adjacentChestZPos != null)
                        adjacent = chest.adjacentChestZPos;
                    else if (chest.adjacentChestZNeg != null)
                        adjacent = chest.adjacentChestZNeg;
                    if (adjacent != null) {
                        InventoryUtilities.mergeIntoInventory(adjacent, mergeStack, 1);
                    }
                }
            }
            InventoryUtilities.deductFromInventory(host.getBroomInventory(), mergeStack, originalSize - mergeStack.stackSize);
        }
        if (depositCounter > 10 && (InventoryUtilities.isInventoryEmpty(host.getBroomInventory()) || !InventoryUtilities.canMergeHappen(host.getBroomInventory(), inventory))) {
            inventory.closeInventory();
            resetTask();
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IInventory(net.minecraft.inventory.IInventory) AMVector3(am2.api.math.AMVector3) TileEntityChest(net.minecraft.tileentity.TileEntityChest) ItemStack(net.minecraft.item.ItemStack)

Example 62 with IInventory

use of net.minecraft.inventory.IInventory in project NewHorizonsCoreMod by GTNewHorizons.

the class GT_CustomMetaPipeEntity_Item method onPostTick.

@Override
public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
    if (aBaseMetaTileEntity.isServerSide() && aTick % 10 == 0) {
        mConnections = 0;
        if (aTick % mCustomTickTime == 0)
            mTransferredItems = 0;
        for (byte i = 0; i < 6; i++) {
            TileEntity tTileEntity = aBaseMetaTileEntity.getTileEntityAtSide(i);
            if (tTileEntity != null) {
                boolean temp = GT_Utility.isConnectableNonInventoryPipe(tTileEntity, GT_Utility.getOppositeSide(i));
                if (tTileEntity instanceof IGregTechTileEntity) {
                    temp = true;
                    if (((IGregTechTileEntity) tTileEntity).getMetaTileEntity() == null)
                        continue;
                    if (aBaseMetaTileEntity.getColorization() >= 0) {
                        byte tColor = ((IGregTechTileEntity) tTileEntity).getColorization();
                        if (tColor >= 0 && tColor != aBaseMetaTileEntity.getColorization()) {
                            continue;
                        }
                    }
                    if (((IGregTechTileEntity) tTileEntity).getMetaTileEntity().connectsToItemPipe(GT_Utility.getOppositeSide(i))) {
                        mConnections |= (1 << i);
                        continue;
                    }
                }
                if (tTileEntity instanceof IInventory) {
                    temp = true;
                    if (((IInventory) tTileEntity).getSizeInventory() <= 0) {
                        continue;
                    }
                }
                if (tTileEntity instanceof ISidedInventory) {
                    temp = true;
                    int[] tSlots = ((ISidedInventory) tTileEntity).getAccessibleSlotsFromSide(GT_Utility.getOppositeSide(i));
                    if (tSlots == null || tSlots.length <= 0) {
                        continue;
                    }
                }
                if (temp) {
                    if (tTileEntity instanceof ICoverable && ((ICoverable) tTileEntity).getCoverBehaviorAtSide(GT_Utility.getOppositeSide(i)).alwaysLookConnected(GT_Utility.getOppositeSide(i), ((ICoverable) tTileEntity).getCoverIDAtSide(GT_Utility.getOppositeSide(i)), ((ICoverable) tTileEntity).getCoverDataAtSide(GT_Utility.getOppositeSide(i)), ((ICoverable) tTileEntity))) {
                        mConnections |= (1 << i);
                        continue;
                    }
                    if (aBaseMetaTileEntity.getCoverBehaviorAtSide(i).alwaysLookConnected(i, aBaseMetaTileEntity.getCoverIDAtSide(i), aBaseMetaTileEntity.getCoverDataAtSide(i), aBaseMetaTileEntity)) {
                        mConnections |= (1 << i);
                        continue;
                    }
                    if (aBaseMetaTileEntity.getCoverBehaviorAtSide(i).letsItemsIn(i, aBaseMetaTileEntity.getCoverIDAtSide(i), aBaseMetaTileEntity.getCoverDataAtSide(i), -1, aBaseMetaTileEntity)) {
                        mConnections |= (1 << i);
                        continue;
                    }
                    if (aBaseMetaTileEntity.getCoverBehaviorAtSide(i).letsItemsOut(i, aBaseMetaTileEntity.getCoverIDAtSide(i), aBaseMetaTileEntity.getCoverDataAtSide(i), -1, aBaseMetaTileEntity)) {
                        mConnections |= (1 << i);
                        continue;
                    }
                }
            }
        }
        if (oLastReceivedFrom == mLastReceivedFrom) {
            doTickProfilingInThisTick = false;
            ArrayList<IMetaTileEntityItemPipe> tPipeList = new ArrayList<IMetaTileEntityItemPipe>();
            for (boolean temp = true; temp && !isInventoryEmpty() && pipeCapacityCheck(); ) {
                temp = false;
                tPipeList.clear();
                for (IMetaTileEntityItemPipe tTileEntity : GT_Utility.sortMapByValuesAcending(IMetaTileEntityItemPipe.Util.scanPipes(this, new HashMap<IMetaTileEntityItemPipe, Long>(), 0, false, false)).keySet()) {
                    if (temp)
                        break;
                    tPipeList.add(tTileEntity);
                    while (!temp && !isInventoryEmpty() && tTileEntity.sendItemStack(aBaseMetaTileEntity)) for (IMetaTileEntityItemPipe tPipe : tPipeList) if (!tPipe.incrementTransferCounter(1))
                        temp = true;
                }
            }
        }
        if (isInventoryEmpty())
            mLastReceivedFrom = 6;
        oLastReceivedFrom = mLastReceivedFrom;
    } else if (aBaseMetaTileEntity.isClientSide() && GT_Client.changeDetected == 4)
        aBaseMetaTileEntity.issueTextureUpdate();
}
Also used : IInventory(net.minecraft.inventory.IInventory) ICoverable(gregtech.api.interfaces.tileentity.ICoverable) IGregTechTileEntity(gregtech.api.interfaces.tileentity.IGregTechTileEntity) IMetaTileEntityItemPipe(gregtech.api.interfaces.metatileentity.IMetaTileEntityItemPipe) ArrayList(java.util.ArrayList) IGregTechTileEntity(gregtech.api.interfaces.tileentity.IGregTechTileEntity) IMetaTileEntity(gregtech.api.interfaces.metatileentity.IMetaTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) ISidedInventory(net.minecraft.inventory.ISidedInventory)

Example 63 with IInventory

use of net.minecraft.inventory.IInventory in project malmo by Microsoft.

the class InventoryCommandsImplementation method combineSlots.

static ItemStack[] combineSlots(EntityPlayerMP player, String invDst, int dst, String invAdd, int add, BlockPos containerPos) {
    IInventory container = null;
    String containerName = "";
    if (containerPos != null) {
        TileEntity te = player.world.getTileEntity(containerPos);
        if (te != null && te instanceof TileEntityLockableLoot) {
            containerName = ObservationFromFullInventoryImplementation.getInventoryName((IInventory) te);
            container = (IInventory) te;
        } else if (te != null && te instanceof TileEntityEnderChest) {
            containerName = ObservationFromFullInventoryImplementation.getInventoryName(player.getInventoryEnderChest());
            container = player.getInventoryEnderChest();
        }
    }
    IInventory dstInv = invDst.equals("inventory") ? player.inventory : (invDst.equals(containerName) ? container : null);
    IInventory addInv = invAdd.equals("inventory") ? player.inventory : (invAdd.equals(containerName) ? container : null);
    if (dstInv == null || addInv == null)
        // Source or dest container not available.
        return null;
    ItemStack dstStack = dstInv.getStackInSlot(dst);
    ItemStack addStack = addInv.getStackInSlot(add);
    if (addStack == null)
        // Combination is a no-op.
        return null;
    ItemStack[] returnStacks = null;
    if (// Do a straight move - nothing to combine with.
    dstStack == null) {
        if (dstInv != addInv) {
            // Items are moving between our inventory and the foreign inventory - may need to trigger
            // rewards for collecting / discarding.
            returnStacks = new ItemStack[2];
            ItemStack stackBeingLost = (addInv == player.inventory) ? addStack : null;
            ItemStack stackBeingGained = (dstInv == player.inventory) ? addStack : null;
            if (stackBeingGained != null)
                returnStacks[0] = stackBeingGained.copy();
            if (stackBeingLost != null)
                returnStacks[1] = stackBeingLost.copy();
        }
        dstInv.setInventorySlotContents(dst, addStack);
        addInv.setInventorySlotContents(add, null);
        return returnStacks;
    }
    // Check we can combine. This logic comes from InventoryPlayer.storeItemStack():
    boolean itemsMatch = dstStack.getItem() == addStack.getItem();
    boolean dstCanStack = dstStack.isStackable() && dstStack.getCount() < dstStack.getMaxStackSize() && dstStack.getCount() < dstInv.getInventoryStackLimit();
    boolean subTypesMatch = !dstStack.getHasSubtypes() || dstStack.getMetadata() == addStack.getMetadata();
    boolean tagsMatch = ItemStack.areItemStackTagsEqual(dstStack, addStack);
    if (itemsMatch && dstCanStack && subTypesMatch && tagsMatch) {
        // We can combine, so figure out how much we have room for:
        int limit = Math.min(dstStack.getMaxStackSize(), dstInv.getInventoryStackLimit());
        int room = limit - dstStack.getCount();
        ItemStack itemsTransferred = dstStack.copy();
        if (addStack.getCount() > room) {
            // Not room for all of it, so shift across as much as possible.
            addStack.shrink(room);
            dstStack.grow(room);
            itemsTransferred.setCount(room);
        } else {
            // Room for the whole lot, so empty out the add slot.
            dstStack.grow(addStack.getCount());
            itemsTransferred.setCount(addStack.getCount());
            // setInventorySlotContents(add, null);
            addInv.removeStackFromSlot(add);
        }
        if (dstInv != addInv) {
            // Items are moving between our inventory and the foreign inventory - may need to trigger
            // rewards for collecting / discarding.
            returnStacks = new ItemStack[2];
            if (dstInv == player.inventory)
                // We're gaining them
                returnStacks[0] = itemsTransferred;
            else
                // We're losing them
                returnStacks[1] = itemsTransferred;
        }
    }
    return returnStacks;
}
Also used : IInventory(net.minecraft.inventory.IInventory) TileEntity(net.minecraft.tileentity.TileEntity) TileEntityLockableLoot(net.minecraft.tileentity.TileEntityLockableLoot) TileEntityEnderChest(net.minecraft.tileentity.TileEntityEnderChest) ItemStack(net.minecraft.item.ItemStack)

Example 64 with IInventory

use of net.minecraft.inventory.IInventory in project MorePlanets by SteveKunG.

the class BlockAncientChestMP method breakBlock.

@Override
public void breakBlock(World world, BlockPos pos, IBlockState state) {
    TileEntity tileentity = world.getTileEntity(pos);
    if (tileentity instanceof IInventory) {
        InventoryHelper.dropInventoryItems(world, pos, (IInventory) tileentity);
        world.updateComparatorOutputLevel(pos, this);
    }
    super.breakBlock(world, pos, state);
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IInventory(net.minecraft.inventory.IInventory)

Example 65 with IInventory

use of net.minecraft.inventory.IInventory in project MorePlanets by SteveKunG.

the class BlockTreasureChestMP method breakBlock.

@Override
public void breakBlock(World world, BlockPos pos, IBlockState state) {
    TileEntity tileentity = world.getTileEntity(pos);
    if (tileentity instanceof IInventory) {
        InventoryHelper.dropInventoryItems(world, pos, (IInventory) tileentity);
        world.updateComparatorOutputLevel(pos, this);
    }
    super.breakBlock(world, pos, state);
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IInventory(net.minecraft.inventory.IInventory)

Aggregations

IInventory (net.minecraft.inventory.IInventory)129 ItemStack (net.minecraft.item.ItemStack)80 TileEntity (net.minecraft.tileentity.TileEntity)59 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)21 ArrayList (java.util.ArrayList)18 ISidedInventory (net.minecraft.inventory.ISidedInventory)13 EntityItem (net.minecraft.entity.item.EntityItem)12 EntityPlayer (net.minecraft.entity.player.EntityPlayer)12 IInventoryUtil (logisticspipes.interfaces.IInventoryUtil)10 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)9 WorldCoordinatesWrapper (network.rs485.logisticspipes.world.WorldCoordinatesWrapper)9 AMVector3 (am2.api.math.AMVector3)8 List (java.util.List)8 SidedInventoryMinecraftAdapter (logisticspipes.utils.SidedInventoryMinecraftAdapter)8 SinkReply (logisticspipes.utils.SinkReply)8 ItemIdentifier (logisticspipes.utils.item.ItemIdentifier)8 LogisticsModule (logisticspipes.modules.abstractmodules.LogisticsModule)7 CoreRoutedPipe (logisticspipes.pipes.basic.CoreRoutedPipe)7 SimpleServiceLocator (logisticspipes.proxy.SimpleServiceLocator)7 ConnectionPipeType (logisticspipes.routing.pathfinder.IPipeInformationProvider.ConnectionPipeType)7