Search in sources :

Example 21 with TileEntityChest

use of net.minecraft.tileentity.TileEntityChest in project minecolonies by Minecolonies.

the class TileEntityWareHouse method dumpInventoryIntoWareHouse.

/**
     * Dump the inventory of a citizen into the warehouse.
     * Go through all items and search the right chest to dump it in.
     * @param inventoryCitizen the inventory of the citizen
     */
public void dumpInventoryIntoWareHouse(@NotNull final InventoryCitizen inventoryCitizen) {
    for (int i = 0; i < new InvWrapper(inventoryCitizen).getSlots(); i++) {
        final ItemStack stack = inventoryCitizen.getStackInSlot(i);
        if (InventoryUtils.isItemStackEmpty(stack)) {
            continue;
        }
        @Nullable final TileEntityChest chest = searchRightChestForStack(stack);
        if (chest == null) {
            LanguageHandler.sendPlayersMessage(getColony().getMessageEntityPlayers(), COM_MINECOLONIES_COREMOD_WAREHOUSE_FULL);
            return;
        }
        InventoryUtils.transferItemStackIntoNextFreeSlotInProvider(new InvWrapper(inventoryCitizen), i, chest);
    }
}
Also used : TileEntityChest(net.minecraft.tileentity.TileEntityChest) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) ItemStack(net.minecraft.item.ItemStack) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with TileEntityChest

use of net.minecraft.tileentity.TileEntityChest in project ArsMagica2 by Mithion.

the class FlickerOperatorItemTransport method FindOutput.

/**
	 * Will try to find a place to move the item stack
	 *
	 * @param stack The item stack to move
	 * @return Returns true if the item can be moved, returns false otherwise
	 */
private boolean FindOutput(World worldObj, TileEntityFlickerHabitat attuner, ItemStack stack, IInventory source) {
    HashMap<Integer, ArrayList<AMVector3>> removeFromOutList = new HashMap<Integer, ArrayList<AMVector3>>();
    boolean itemMoved = false;
    for (int priority = 0; priority <= TileEntityFlickerHabitat.PRIORITY_FINAL; ++priority) {
        if (attuner.getOutListPosition(priority) >= attuner.getOutListSize(priority)) {
            //if the out list position has gone outside the list size reset it to 0
            attuner.setOutListPosition(priority, 0);
        }
        int start = attuner.getOutListPosition(priority);
        int pos = start;
        boolean fullLoop = false;
        while (!fullLoop) {
            //get the crystal marker tile entity for the specified position
            AMVector3 vector = attuner.getOutListAt(priority, pos);
            TileEntity te = null;
            TileEntityCrystalMarker crystalMarkerTE = GetCrystalMarkerTileEntity(worldObj, (int) vector.x, (int) vector.y, (int) vector.z);
            if (crystalMarkerTE == null) {
                //crystal marker no longer exists, remove it from the list
                if (!removeFromOutList.containsKey(priority))
                    removeFromOutList.put(priority, new ArrayList<AMVector3>());
                removeFromOutList.get(priority).add(vector);
                break;
            }
            te = GetAttachedCrystalMarkerTileEntity(worldObj, crystalMarkerTE, vector);
            int markerType = worldObj.getBlockMetadata((int) vector.x, (int) vector.y, (int) vector.z);
            if (te != null && te instanceof IInventory) {
                IInventory inventory = (IInventory) te;
                itemMoved = outputItem(markerType, new IInventory[] { inventory }, stack, crystalMarkerTE, new IInventory[] { source });
                if (itemMoved) {
                    attuner.setOutListPosition(priority, pos + 1);
                }
            }
            if (!itemMoved && te instanceof TileEntityChest) {
                //This handles the special case of double chests
                TileEntityChest adjacent = InventoryUtilities.getAdjacentChest((TileEntityChest) te);
                if (adjacent != null) {
                    IInventory inventory = adjacent;
                    itemMoved = outputItem(markerType, new IInventory[] { inventory, (IInventory) te }, stack, crystalMarkerTE, new IInventory[] { source });
                    if (itemMoved) {
                        attuner.setOutListPosition(priority, pos + 1);
                    }
                }
            }
            if (itemMoved)
                break;
            pos++;
            pos %= attuner.getOutListSize(priority);
            if (pos == start)
                fullLoop = true;
        }
        for (int i : removeFromOutList.keySet()) {
            for (AMVector3 vector : removeFromOutList.get(i)) {
                attuner.removeOutListAt(i, vector);
            }
        }
        if (!itemMoved) {
            attuner.setOutListPosition(priority, 0);
        } else {
            break;
        }
    }
    return itemMoved;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IInventory(net.minecraft.inventory.IInventory) AMVector3(am2.api.math.AMVector3) TileEntityChest(net.minecraft.tileentity.TileEntityChest) TileEntityCrystalMarker(am2.blocks.tileentities.TileEntityCrystalMarker) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 23 with TileEntityChest

use of net.minecraft.tileentity.TileEntityChest in project minecolonies by Minecolonies.

the class AbstractEntityAIBasic method isInHut.

/**
     * Check all chests in the worker hut for a required item.
     *
     * @param is the type of item requested (amount is ignored)
     * @return true if a stack of that type was found
     */
public boolean isInHut(@Nullable final ItemStack is) {
    @Nullable final AbstractBuildingWorker building = getOwnBuilding();
    boolean hasItem;
    if (building != null) {
        hasItem = isInTileEntity(building.getTileEntity(), is);
        if (hasItem) {
            return true;
        }
        for (final BlockPos pos : building.getAdditionalCountainers()) {
            final TileEntity entity = world.getTileEntity(pos);
            if (entity instanceof TileEntityChest) {
                hasItem = isInTileEntity((TileEntityChest) entity, is);
                if (hasItem) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) TileEntityChest(net.minecraft.tileentity.TileEntityChest) AbstractBuildingWorker(com.minecolonies.coremod.colony.buildings.AbstractBuildingWorker) BlockPos(net.minecraft.util.math.BlockPos) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with TileEntityChest

use of net.minecraft.tileentity.TileEntityChest in project minecolonies by Minecolonies.

the class AbstractBuilding method transferStack.

/**
     * Try to transfer a stack to one of the inventories of the building.
     *
     * @param stack the stack to transfer.
     * @param world the world to do it in.
     * @return The {@link ItemStack} as that is left over, might be {@link InventoryUtils#EMPTY} if the stack was completely accepted
     */
public ItemStack transferStack(@NotNull final ItemStack stack, @NotNull final World world) {
    if (tileEntity == null || InventoryUtils.isProviderFull(tileEntity)) {
        final Iterator<BlockPos> posIterator = containerList.iterator();
        @NotNull ItemStack resultStack = stack.copy();
        while (posIterator.hasNext() && !InventoryUtils.isItemStackEmpty(resultStack)) {
            final BlockPos pos = posIterator.next();
            final TileEntity tempTileEntity = world.getTileEntity(pos);
            if (tempTileEntity instanceof TileEntityChest && !InventoryUtils.isProviderFull(tempTileEntity)) {
                resultStack = InventoryUtils.addItemStackToProviderWithResult(tempTileEntity, stack);
            }
        }
        return resultStack;
    } else {
        return InventoryUtils.addItemStackToProviderWithResult(tileEntity, stack);
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) TileEntityChest(net.minecraft.tileentity.TileEntityChest) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

TileEntityChest (net.minecraft.tileentity.TileEntityChest)24 TileEntity (net.minecraft.tileentity.TileEntity)15 BlockPos (net.minecraft.util.math.BlockPos)9 ItemStack (net.minecraft.item.ItemStack)7 Nullable (org.jetbrains.annotations.Nullable)5 IInventory (net.minecraft.inventory.IInventory)4 AMVector3 (am2.api.math.AMVector3)3 HashMap (java.util.HashMap)3 Nonnull (javax.annotation.Nonnull)3 Block (net.minecraft.block.Block)3 NotNull (org.jetbrains.annotations.NotNull)3 TileEntityCrystalMarker (am2.blocks.tileentities.TileEntityCrystalMarker)2 AbstractBuildingWorker (com.minecolonies.coremod.colony.buildings.AbstractBuildingWorker)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 InvWrapper (net.minecraftforge.items.wrapper.InvWrapper)2 TileEntityFlickerHabitat (am2.blocks.tileentities.TileEntityFlickerHabitat)1 AbstractBuilding (com.minecolonies.coremod.colony.buildings.AbstractBuilding)1 BuildingBuilderResource (com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource)1