Search in sources :

Example 11 with InvWrapper

use of net.minecraftforge.items.wrapper.InvWrapper in project MinecraftForge by MinecraftForge.

the class FluidUtil method interactWithFluidHandler.

/**
     * Used to handle the common case of a player holding a fluid item and right-clicking on a fluid handler.
     * First it tries to fill the container item from the fluid handler,
     * if that action fails then it tries to drain the container item into the fluid handler.
     *
     * @param stack        The filled or empty fluid container.
     *                     Will not be modified directly, if modifications are necessary a modified copy is returned in the result.
     * @param fluidHandler The fluid handler to interact with.
     * @param player       The player doing the interaction between the item and fluid handler.
     * @return a {@link FluidActionResult} holding the result and resulting container.
     */
@Nonnull
public static FluidActionResult interactWithFluidHandler(@Nonnull ItemStack stack, IFluidHandler fluidHandler, EntityPlayer player) {
    if (stack.isEmpty() || fluidHandler == null || player == null) {
        return FluidActionResult.FAILURE;
    }
    IItemHandler playerInventory = new InvWrapper(player.inventory);
    FluidActionResult fillResult = tryFillContainerAndStow(stack, fluidHandler, playerInventory, Integer.MAX_VALUE, player);
    if (fillResult.isSuccess()) {
        return fillResult;
    } else {
        return tryEmptyContainerAndStow(stack, fluidHandler, playerInventory, Integer.MAX_VALUE, player);
    }
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) Nonnull(javax.annotation.Nonnull)

Example 12 with InvWrapper

use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.

the class WindowHutBuilder method pullResourcesFromHut.

/**
     * Retrieve resources from the building to display in GUI.
     */
private void pullResourcesFromHut() {
    final AbstractBuilding.View newView = builder.getColony().getBuilding(builder.getID());
    if (newView instanceof BuildingBuilderView) {
        final BuildingBuilderView updatedView = (BuildingBuilderView) newView;
        final InventoryPlayer inventory = this.mc.player.inventory;
        resources.clear();
        resources.addAll(updatedView.getResources().values());
        for (final BuildingBuilderResource resource : resources) {
            resource.setPlayerAmount(InventoryUtils.getItemCountInItemHandler(new InvWrapper(inventory), resource.getItem(), resource.getDamageValue()));
        }
        resources.sort(new BuildingBuilderResource.ResourceComparator());
    }
}
Also used : InventoryPlayer(net.minecraft.entity.player.InventoryPlayer) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) BuildingBuilderResource(com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource) BuildingBuilderView(com.minecolonies.coremod.colony.buildings.views.BuildingBuilderView) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding)

Example 13 with InvWrapper

use of net.minecraftforge.items.wrapper.InvWrapper 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 14 with InvWrapper

use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.

the class EntityAIGoHome method handleSaturation.

/**
     * Handle the saturation of the citizen.
     *
     * @param pos the position.
     */
private void handleSaturation(@NotNull final BlockPos pos) {
    if (citizen.isWorkerAtSiteWithMove(pos, 2) && citizen.getColony() != null && citizen.getCitizenData() != null && citizen.getCitizenData().getSaturation() < EntityCitizen.HIGH_SATURATION) {
        final double currentSaturation = citizen.getCitizenData().getSaturation();
        boolean tookFood = false;
        final AbstractBuilding home = citizen.getColony().getBuilding(pos);
        if (home instanceof BuildingHome && currentSaturation < EntityCitizen.FULL_SATURATION) {
            final int slot = InventoryUtils.findFirstSlotInProviderNotEmptyWith(home.getTileEntity(), itemStack -> itemStack.getItem() instanceof ItemFood);
            if (slot != -1) {
                final ItemStack stack = home.getTileEntity().getStackInSlot(slot);
                if (!InventoryUtils.isItemStackEmpty(stack)) {
                    final int slotToSet = InventoryUtils.getFirstOpenSlotFromItemHandler(new InvWrapper(citizen.getInventoryCitizen()));
                    if (slotToSet == -1) {
                        InventoryUtils.forceItemStackToItemHandler(new InvWrapper(citizen.getInventoryCitizen()), new ItemStack(stack.getItem(), 1), stack1 -> citizen.getWorkBuilding() == null || !citizen.getWorkBuilding().neededForWorker(stack1));
                    } else {
                        citizen.getInventoryCitizen().setInventorySlotContents(slotToSet, new ItemStack(stack.getItem(), 1));
                    }
                    tookFood = true;
                    stack.setCount(stack.getCount() - 1);
                }
                ((BuildingHome) home).setFoodNeeded(false);
            }
        }
        if (!tookFood) {
            requestFoodIfRequired(currentSaturation, home);
        }
    }
}
Also used : ItemFood(net.minecraft.item.ItemFood) BuildingHome(com.minecolonies.coremod.colony.buildings.BuildingHome) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) ItemStack(net.minecraft.item.ItemStack) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding)

Example 15 with InvWrapper

use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.

the class EntityAIStructureMiner method setBlockFromInventory.

private void setBlockFromInventory(@NotNull final BlockPos location, final Block block, final IBlockState metadata) {
    final int slot;
    if (block instanceof BlockLadder) {
        slot = worker.findFirstSlotInInventoryWith(block, -1);
    } else {
        slot = worker.findFirstSlotInInventoryWith(block, block.getMetaFromState(metadata));
    }
    if (slot != -1) {
        new InvWrapper(getInventory()).extractItem(slot, 1, false);
        //Flag 1+2 is needed for updates
        world.setBlockState(location, metadata, 0x03);
    }
}
Also used : InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) BlockLadder(net.minecraft.block.BlockLadder)

Aggregations

InvWrapper (net.minecraftforge.items.wrapper.InvWrapper)22 ItemStack (net.minecraft.item.ItemStack)15 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)5 InventoryCitizen (com.minecolonies.coremod.inventory.InventoryCitizen)4 TileEntity (net.minecraft.tileentity.TileEntity)4 TileEntityChest (net.minecraft.tileentity.TileEntityChest)4 BlockPos (net.minecraft.util.math.BlockPos)4 AbstractBuilding (com.minecolonies.coremod.colony.buildings.AbstractBuilding)3 Block (net.minecraft.block.Block)3 ItemFood (net.minecraft.item.ItemFood)3 AbstractBuildingWorker (com.minecolonies.coremod.colony.buildings.AbstractBuildingWorker)2 BuildingBuilderResource (com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource)2 ItemStorage (com.minecolonies.coremod.entity.ai.item.handling.ItemStorage)2 TileEntityColonyBuilding (com.minecolonies.coremod.tileentities.TileEntityColonyBuilding)2 ArrayList (java.util.ArrayList)2 IBlockState (net.minecraft.block.state.IBlockState)2 ItemArmor (net.minecraft.item.ItemArmor)2 com.minecolonies.api.util (com.minecolonies.api.util)1 InventoryUtils (com.minecolonies.api.util.InventoryUtils)1