Search in sources :

Example 1 with InvWrapper

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

the class EntityAIWorkDeliveryman method gatherItems.

/**
     * Gather item from chest.
     * Gathers only one stack of the item.
     *
     * @param buildingToDeliver building to deliver to.
     */
private AIState gatherItems(@NotNull final AbstractBuilding buildingToDeliver, @NotNull final BlockPos position) {
    final TileEntity tileEntity = world.getTileEntity(position);
    if (tileEntity instanceof TileEntityChest) {
        if (!(tileEntity instanceof TileEntityColonyBuilding)) {
            if (((TileEntityChest) tileEntity).numPlayersUsing == 0) {
                this.world.addBlockEvent(tileEntity.getPos(), tileEntity.getBlockType(), 1, 1);
                this.world.notifyNeighborsOfStateChange(tileEntity.getPos(), tileEntity.getBlockType(), true);
                this.world.notifyNeighborsOfStateChange(tileEntity.getPos().down(), tileEntity.getBlockType(), true);
                setDelay(DUMP_AND_GATHER_DELAY);
                return GATHER_IN_WAREHOUSE;
            }
            this.world.addBlockEvent(tileEntity.getPos(), tileEntity.getBlockType(), 1, 0);
            this.world.notifyNeighborsOfStateChange(tileEntity.getPos(), tileEntity.getBlockType(), true);
            this.world.notifyNeighborsOfStateChange(tileEntity.getPos().down(), tileEntity.getBlockType(), true);
        }
        if (buildingToDeliver instanceof BuildingHome) {
            final int extraFood = worker.getCitizenData().getSaturation() < EntityCitizen.HIGH_SATURATION ? 1 : 0;
            //Tries to extract a certain amount of the item of the chest.
            if (InventoryUtils.transferXOfFirstSlotInProviderWithIntoNextFreeSlotInItemHandler(tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null), itemStack -> !InventoryUtils.isItemStackEmpty(itemStack) && itemStack.getItem() instanceof ItemFood, buildingToDeliver.getBuildingLevel() + extraFood, new InvWrapper(worker.getInventoryCitizen()))) {
                worker.setHeldItem(SLOT_HAND);
                setDelay(DUMP_AND_GATHER_DELAY);
                return DELIVERY;
            }
            ((BuildingDeliveryman) getOwnBuilding()).setBuildingToDeliver(null);
            itemsToDeliver.clear();
            return START_WORKING;
        } else if (itemsToDeliver.isEmpty() && !isToolInTileEntity((TileEntityChest) tileEntity, buildingToDeliver.getRequiredTool(), buildingToDeliver.getBuildingLevel())) {
            ((BuildingDeliveryman) getOwnBuilding()).setBuildingToDeliver(null);
            itemsToDeliver.clear();
            return START_WORKING;
        } else if (!itemsToDeliver.isEmpty()) {
            final ItemStack stack = itemsToDeliver.get(0);
            if (isInTileEntity((TileEntityChest) tileEntity, stack)) {
                itemsToDeliver.remove(0);
                worker.setHeldItem(SLOT_HAND);
                setDelay(DUMP_AND_GATHER_DELAY);
                return DELIVERY;
            }
            ((BuildingDeliveryman) getOwnBuilding()).setBuildingToDeliver(null);
            itemsToDeliver.clear();
            return START_WORKING;
        }
    }
    setDelay(DUMP_AND_GATHER_DELAY);
    return GATHER_IN_WAREHOUSE;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) TileEntityChest(net.minecraft.tileentity.TileEntityChest) ItemFood(net.minecraft.item.ItemFood) TileEntityColonyBuilding(com.minecolonies.coremod.tileentities.TileEntityColonyBuilding) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) ItemStack(net.minecraft.item.ItemStack)

Example 2 with InvWrapper

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

the class EntityAIWorkLumberjack method placeSaplings.

//todo: we need to use a different way to get Metadata
@SuppressWarnings(DEPRECATION)
private void placeSaplings(final int saplingSlot, @NotNull final ItemStack stack, @NotNull final Block block) {
    while (!job.tree.getStumpLocations().isEmpty()) {
        final BlockPos pos = job.tree.getStumpLocations().get(0);
        if ((BlockPosUtil.setBlock(world, pos, block.getStateFromMeta(stack.getMetadata()), 0x02) && getInventory().getStackInSlot(saplingSlot) != null) || Objects.equals(world.getBlockState(pos), block.getStateFromMeta(stack.getMetadata()))) {
            new InvWrapper(getInventory()).extractItem(saplingSlot, 1, false);
            job.tree.removeStump(pos);
        } else {
            return;
        }
    }
}
Also used : InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) BlockPos(net.minecraft.util.math.BlockPos)

Example 3 with InvWrapper

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

the class EntityAIWorkDeliveryman method gatherFromBuilding.

/**
     * Gather not needed Items from building.
     *
     * @param building building to gather it from.
     * @return true when finished.
     */
private boolean gatherFromBuilding(@NotNull final AbstractBuilding building) {
    if (currentSlot >= building.getTileEntity().getSizeInventory()) {
        return true;
    }
    final ItemStack stack = building.getTileEntity().getStackInSlot(currentSlot);
    if (workerRequiresItem(building, stack, alreadyKept) || (building instanceof BuildingHome && stack.getItem() instanceof ItemFood)) {
        return false;
    }
    InventoryUtils.transferItemStackIntoNextFreeSlotInItemHandlers(building.getTileEntity().getSingleChestHandler(), currentSlot, new InvWrapper(worker.getInventoryCitizen()));
    building.markDirty();
    setDelay(DUMP_AND_GATHER_DELAY);
    worker.setHeldItem(SLOT_HAND);
    return false;
}
Also used : ItemFood(net.minecraft.item.ItemFood) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) ItemStack(net.minecraft.item.ItemStack)

Example 4 with InvWrapper

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

the class BuildingBuilder method updateAvailableResources.

/**
     * Update the available resources.
     * <p>
     * which are needed for the build and in the builder's chest or inventory
     */
private void updateAvailableResources() {
    final EntityCitizen builder = getWorkerEntity();
    InventoryCitizen builderInventory = null;
    if (builder != null) {
        builderInventory = builder.getInventoryCitizen();
    }
    for (@NotNull final Map.Entry<String, BuildingBuilderResource> entry : neededResources.entrySet()) {
        final BuildingBuilderResource resource = entry.getValue();
        resource.setAvailable(0);
        if (builderInventory != null) {
            resource.addAvailable(InventoryUtils.getItemCountInItemHandler(new InvWrapper(builderInventory), resource.getItem(), resource.getDamageValue()));
        }
        final TileEntity chestInventory = this.getTileEntity();
        if (chestInventory != null) {
            resource.addAvailable(InventoryUtils.getItemCountInProvider(chestInventory, resource.getItem(), resource.getDamageValue()));
        }
        //Count in the additional chests as well
        if (builder != null) {
            for (final BlockPos pos : getAdditionalCountainers()) {
                final TileEntity entity = builder.world.getTileEntity(pos);
                if (entity instanceof TileEntityChest) {
                    resource.addAvailable(InventoryUtils.getItemCountInProvider(entity, resource.getItem(), resource.getDamageValue()));
                }
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) TileEntityChest(net.minecraft.tileentity.TileEntityChest) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) InventoryCitizen(com.minecolonies.coremod.inventory.InventoryCitizen) BlockPos(net.minecraft.util.math.BlockPos) EntityCitizen(com.minecolonies.coremod.entity.EntityCitizen) BuildingBuilderResource(com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource) NotNull(org.jetbrains.annotations.NotNull) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with InvWrapper

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

the class TransferItemsRequestMessage method messageOnServerThread.

@Override
public void messageOnServerThread(final TransferItemsRequestMessage message, final EntityPlayerMP player) {
    final Colony colony = ColonyManager.getColony(message.colonyId);
    if (colony == null) {
        Log.getLogger().warn("TransferItemsRequestMessage colony is null");
        return;
    }
    final AbstractBuilding building = colony.getBuilding(message.buildingId);
    if (building == null) {
        Log.getLogger().warn("TransferItemsRequestMessage building is null");
        return;
    }
    if (message.quantity <= 0) {
        Log.getLogger().warn("TransferItemsRequestMessage quantity below 0");
        return;
    }
    final Item item = message.itemStack.getItem();
    final int amountToTake = Math.min(message.quantity, InventoryUtils.getItemCountInItemHandler(new InvWrapper(player.inventory), item, message.itemStack.getItemDamage()));
    final ItemStack itemStackToTake = new ItemStack(item, amountToTake, message.itemStack.getItemDamage());
    ItemStack remainingItemStack = InventoryUtils.addItemStackToProviderWithResult(building.getTileEntity(), itemStackToTake);
    if (!InventoryUtils.isItemStackEmpty(remainingItemStack)) {
        //If we still have some to drop, let's try the additional chests now
        if (InventoryUtils.getItemStackSize(remainingItemStack) > 0) {
            final World world = colony.getWorld();
            for (final BlockPos pos : building.getAdditionalCountainers()) {
                final TileEntity entity = world.getTileEntity(pos);
                remainingItemStack = InventoryUtils.addItemStackToProviderWithResult(entity, remainingItemStack);
                if (InventoryUtils.isItemStackEmpty(remainingItemStack)) {
                    break;
                }
            }
        }
    }
    if (InventoryUtils.isItemStackEmpty(remainingItemStack) || InventoryUtils.getItemStackSize(remainingItemStack) != InventoryUtils.getItemStackSize(itemStackToTake)) {
        //Only doing this at the moment as the additional chest do not detect new content
        building.getTileEntity().markDirty();
    }
    int amountToRemoveFromPlayer = amountToTake - InventoryUtils.getItemStackSize(remainingItemStack);
    while (amountToRemoveFromPlayer > 0) {
        final int slot = InventoryUtils.findFirstSlotInItemHandlerWith(new InvWrapper(player.inventory), item, message.itemStack.getItemDamage());
        final ItemStack itemsTaken = player.inventory.decrStackSize(slot, amountToRemoveFromPlayer);
        amountToRemoveFromPlayer -= InventoryUtils.getItemStackSize(itemsTaken);
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) Item(net.minecraft.item.Item) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) Colony(com.minecolonies.coremod.colony.Colony) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) World(net.minecraft.world.World) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding)

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