Search in sources :

Example 1 with InventoryCitizen

use of com.minecolonies.api.inventory.InventoryCitizen in project minecolonies by Minecolonies.

the class BuildingResourcesModule method updateAvailableResources.

/**
 * Update the available resources.
 * <p>
 * which are needed for the build and in the structureBuilder's chest or inventory
 */
private void updateAvailableResources() {
    final Set<ICitizenData> set = building.getAllAssignedCitizen();
    final ICitizenData data = set.isEmpty() ? null : set.iterator().next();
    if (data == null) {
        return;
    }
    data.getEntity().ifPresent(structureBuilder -> {
        final InventoryCitizen structureBuilderInventory = data.getInventory();
        if (structureBuilderInventory == null) {
            return;
        }
        for (@NotNull final Map.Entry<String, BuildingBuilderResource> entry : neededResources.entrySet()) {
            final BuildingBuilderResource resource = entry.getValue();
            resource.setAvailable(0);
            if (structureBuilderInventory != null) {
                resource.addAvailable(InventoryUtils.getItemCountInItemHandler(structureBuilderInventory, stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, resource.getItemStack(), true, true)));
            }
            if (building.getTileEntity() != null) {
                resource.addAvailable(InventoryUtils.getItemCountInItemHandler(building.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, resource.getItemStack(), true, true)));
            }
            if (data.getJob() instanceof IJobWithExternalWorkStations) {
                for (final IBuilding station : ((IJobWithExternalWorkStations) data.getJob()).getWorkStations()) {
                    resource.addAvailable(InventoryUtils.getItemCountInItemHandler(station.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, resource.getItemStack(), true, true)));
                }
            }
        }
    });
}
Also used : AbstractBuildingModule(com.minecolonies.api.colony.buildings.modules.AbstractBuildingModule) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) BuilderBucket(com.minecolonies.coremod.colony.buildings.utils.BuilderBucket) java.util(java.util) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) CompoundNBT(net.minecraft.nbt.CompoundNBT) ICitizenData(com.minecolonies.api.colony.ICitizenData) TypeToken(com.google.common.reflect.TypeToken) ItemStack(net.minecraft.item.ItemStack) BuildingBuilderResource(com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource) ImmutableList(com.google.common.collect.ImmutableList) TAG_CURR_STAGE(com.minecolonies.api.util.constant.NbtTagConstants.TAG_CURR_STAGE) AbstractBuildingStructureBuilder(com.minecolonies.coremod.colony.buildings.AbstractBuildingStructureBuilder) ITEM_HANDLER_CAPABILITY(net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) TAG_TOTAL_STAGES(com.minecolonies.api.util.constant.NbtTagConstants.TAG_TOTAL_STAGES) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack) BuildingStructureHandler(com.minecolonies.coremod.entity.ai.util.BuildingStructureHandler) IPersistentModule(com.minecolonies.api.colony.buildings.modules.IPersistentModule) Nullable(org.jetbrains.annotations.Nullable) IJobWithExternalWorkStations(com.minecolonies.api.colony.jobs.IJobWithExternalWorkStations) InventoryCitizen(com.minecolonies.api.inventory.InventoryCitizen) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) InventoryUtils(com.minecolonies.api.util.InventoryUtils) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) WorkOrderBuildDecoration(com.minecolonies.coremod.colony.workorders.WorkOrderBuildDecoration) AbstractJobStructure(com.minecolonies.coremod.colony.jobs.AbstractJobStructure) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) PacketBuffer(net.minecraft.network.PacketBuffer) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) InventoryCitizen(com.minecolonies.api.inventory.InventoryCitizen) ICitizenData(com.minecolonies.api.colony.ICitizenData) IJobWithExternalWorkStations(com.minecolonies.api.colony.jobs.IJobWithExternalWorkStations) BuildingBuilderResource(com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with InventoryCitizen

use of com.minecolonies.api.inventory.InventoryCitizen in project minecolonies by Minecolonies.

the class AbstractEntityAIBasic method checkForNeededTool.

/**
 * Check if we need a tool.
 * <p>
 * Do not use it to find a pickaxe as it need a minimum level.
 *
 * @param toolType     tool required for block.
 * @param minimalLevel the minimal level.
 * @return true if we need a tool.
 */
private boolean checkForNeededTool(@NotNull final IToolType toolType, final int minimalLevel) {
    final int maxToolLevel = worker.getCitizenColonyHandler().getWorkBuilding().getMaxToolLevel();
    final InventoryCitizen inventory = worker.getInventoryCitizen();
    if (InventoryUtils.isToolInItemHandler(inventory, toolType, minimalLevel, maxToolLevel)) {
        return false;
    }
    delay += DELAY_RECHECK;
    return walkToBuilding() || !retrieveToolInHut(toolType, minimalLevel);
}
Also used : InventoryCitizen(com.minecolonies.api.inventory.InventoryCitizen)

Example 3 with InventoryCitizen

use of com.minecolonies.api.inventory.InventoryCitizen in project minecolonies by Minecolonies.

the class AbstractEntityAIBasic method getMostEfficientTool.

/**
 * Calculates the most efficient tool to use on that block.
 *
 * @param target the BlockState to mine
 * @param pos    the pos it is at.
 * @return the slot with the best tool
 */
protected int getMostEfficientTool(@NotNull final BlockState target, final BlockPos pos) {
    final IToolType toolType = WorkerUtil.getBestToolForBlock(target, target.getDestroySpeed(world, pos));
    final int required = WorkerUtil.getCorrectHarvestLevelForBlock(target);
    if (toolType == ToolType.NONE) {
        final int heldSlot = worker.getInventoryCitizen().getHeldItemSlot(Hand.MAIN_HAND);
        return heldSlot >= 0 ? heldSlot : 0;
    }
    int bestSlot = -1;
    int bestLevel = Integer.MAX_VALUE;
    @NotNull final InventoryCitizen inventory = worker.getInventoryCitizen();
    final int maxToolLevel = worker.getCitizenColonyHandler().getWorkBuilding().getMaxToolLevel();
    for (int i = 0; i < worker.getInventoryCitizen().getSlots(); i++) {
        final ItemStack item = inventory.getStackInSlot(i);
        final int level = ItemStackUtils.getMiningLevel(item, toolType);
        if (level > -1 && level >= required && level < bestLevel && ItemStackUtils.verifyToolLevel(item, level, required, maxToolLevel)) {
            bestSlot = i;
            bestLevel = level;
        }
    }
    return bestSlot;
}
Also used : IToolType(com.minecolonies.api.util.constant.IToolType) InventoryCitizen(com.minecolonies.api.inventory.InventoryCitizen) ItemStack(net.minecraft.item.ItemStack) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with InventoryCitizen

use of com.minecolonies.api.inventory.InventoryCitizen in project minecolonies by ldtteam.

the class BuildingResourcesModule method updateAvailableResources.

/**
 * Update the available resources.
 * <p>
 * which are needed for the build and in the structureBuilder's chest or inventory
 */
private void updateAvailableResources() {
    final Set<ICitizenData> set = building.getAllAssignedCitizen();
    final ICitizenData data = set.isEmpty() ? null : set.iterator().next();
    if (data == null) {
        return;
    }
    data.getEntity().ifPresent(structureBuilder -> {
        final InventoryCitizen structureBuilderInventory = data.getInventory();
        if (structureBuilderInventory == null) {
            return;
        }
        for (@NotNull final Map.Entry<String, BuildingBuilderResource> entry : neededResources.entrySet()) {
            final BuildingBuilderResource resource = entry.getValue();
            resource.setAvailable(0);
            if (structureBuilderInventory != null) {
                resource.addAvailable(InventoryUtils.getItemCountInItemHandler(structureBuilderInventory, stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, resource.getItemStack(), true, true)));
            }
            if (building.getTileEntity() != null) {
                resource.addAvailable(InventoryUtils.getItemCountInItemHandler(building.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, resource.getItemStack(), true, true)));
            }
            if (data.getJob() instanceof IJobWithExternalWorkStations) {
                for (final IBuilding station : ((IJobWithExternalWorkStations) data.getJob()).getWorkStations()) {
                    resource.addAvailable(InventoryUtils.getItemCountInItemHandler(station.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, resource.getItemStack(), true, true)));
                }
            }
        }
    });
}
Also used : AbstractBuildingModule(com.minecolonies.api.colony.buildings.modules.AbstractBuildingModule) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) BuilderBucket(com.minecolonies.coremod.colony.buildings.utils.BuilderBucket) java.util(java.util) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) CompoundNBT(net.minecraft.nbt.CompoundNBT) ICitizenData(com.minecolonies.api.colony.ICitizenData) TypeToken(com.google.common.reflect.TypeToken) ItemStack(net.minecraft.item.ItemStack) BuildingBuilderResource(com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource) ImmutableList(com.google.common.collect.ImmutableList) TAG_CURR_STAGE(com.minecolonies.api.util.constant.NbtTagConstants.TAG_CURR_STAGE) AbstractBuildingStructureBuilder(com.minecolonies.coremod.colony.buildings.AbstractBuildingStructureBuilder) ITEM_HANDLER_CAPABILITY(net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) TAG_TOTAL_STAGES(com.minecolonies.api.util.constant.NbtTagConstants.TAG_TOTAL_STAGES) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack) BuildingStructureHandler(com.minecolonies.coremod.entity.ai.util.BuildingStructureHandler) IPersistentModule(com.minecolonies.api.colony.buildings.modules.IPersistentModule) Nullable(org.jetbrains.annotations.Nullable) IJobWithExternalWorkStations(com.minecolonies.api.colony.jobs.IJobWithExternalWorkStations) InventoryCitizen(com.minecolonies.api.inventory.InventoryCitizen) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) InventoryUtils(com.minecolonies.api.util.InventoryUtils) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) WorkOrderBuildDecoration(com.minecolonies.coremod.colony.workorders.WorkOrderBuildDecoration) AbstractJobStructure(com.minecolonies.coremod.colony.jobs.AbstractJobStructure) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) PacketBuffer(net.minecraft.network.PacketBuffer) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) InventoryCitizen(com.minecolonies.api.inventory.InventoryCitizen) ICitizenData(com.minecolonies.api.colony.ICitizenData) IJobWithExternalWorkStations(com.minecolonies.api.colony.jobs.IJobWithExternalWorkStations) BuildingBuilderResource(com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with InventoryCitizen

use of com.minecolonies.api.inventory.InventoryCitizen in project minecolonies by ldtteam.

the class CitizenDataView method deserialize.

@Override
public void deserialize(@NotNull final PacketBuffer buf) {
    name = buf.readUtf(32767);
    female = buf.readBoolean();
    entityId = buf.readInt();
    paused = buf.readBoolean();
    isChild = buf.readBoolean();
    homeBuilding = buf.readBoolean() ? buf.readBlockPos() : null;
    workBuilding = buf.readBoolean() ? buf.readBlockPos() : null;
    // Attributes
    health = buf.readFloat();
    maxHealth = buf.readFloat();
    saturation = buf.readDouble();
    happiness = buf.readDouble();
    citizenSkillHandler.read(buf.readNbt());
    job = buf.readUtf(32767);
    colonyId = buf.readInt();
    final CompoundNBT compound = buf.readNbt();
    inventory = new InventoryCitizen(this.name, true);
    final ListNBT ListNBT = compound.getList("inventory", 10);
    this.inventory.read(ListNBT);
    this.inventory.setHeldItem(Hand.MAIN_HAND, compound.getInt(TAG_HELD_ITEM_SLOT));
    this.inventory.setHeldItem(Hand.OFF_HAND, compound.getInt(TAG_OFFHAND_HELD_ITEM_SLOT));
    position = buf.readBlockPos();
    citizenChatOptions.clear();
    final int size = buf.readInt();
    for (int i = 0; i < size; i++) {
        final CompoundNBT compoundNBT = buf.readNbt();
        final ServerCitizenInteraction handler = (ServerCitizenInteraction) MinecoloniesAPIProxy.getInstance().getInteractionResponseHandlerDataManager().createFrom(this, compoundNBT);
        citizenChatOptions.put(handler.getInquiry(), handler);
    }
    sortedInteractions = citizenChatOptions.values().stream().sorted(Comparator.comparingInt(e -> e.getPriority().getPriority())).collect(Collectors.toList());
    citizenHappinessHandler.read(buf.readNbt());
    int statusindex = buf.readInt();
    statusIcon = statusindex >= 0 ? VisibleCitizenStatus.getForId(statusindex) : null;
    if (buf.readBoolean()) {
        final IColonyView colonyView = IColonyManager.getInstance().getColonyView(colonyId, Minecraft.getInstance().level.dimension());
        jobView = IJobDataManager.getInstance().createViewFrom(colonyView, this, buf);
    } else {
        jobView = null;
    }
    children.clear();
    siblings.clear();
    partner = buf.readInt();
    final int siblingsSize = buf.readInt();
    for (int i = 0; i < siblingsSize; i++) {
        siblings.add(buf.readInt());
    }
    final int childrenSize = buf.readInt();
    for (int i = 0; i < childrenSize; i++) {
        children.add(buf.readInt());
    }
    final String parentA = buf.readUtf();
    final String parentB = buf.readUtf();
    parents = new Tuple<>(parentA, parentB);
}
Also used : java.util(java.util) Suppression(com.minecolonies.api.util.constant.Suppression) CompoundNBT(net.minecraft.nbt.CompoundNBT) ITextComponent(net.minecraft.util.text.ITextComponent) MinecoloniesAPIProxy(com.minecolonies.api.MinecoloniesAPIProxy) ICitizenSkillHandler(com.minecolonies.api.entity.citizen.citizenhandlers.ICitizenSkillHandler) Tuple(com.minecolonies.api.util.Tuple) TAG_OFFHAND_HELD_ITEM_SLOT(com.minecolonies.api.util.constant.NbtTagConstants.TAG_OFFHAND_HELD_ITEM_SLOT) Minecraft(net.minecraft.client.Minecraft) IJobDataManager(com.minecolonies.api.colony.jobs.registry.IJobDataManager) IInteractionResponseHandler(com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler) Hand(net.minecraft.util.Hand) CitizenSkillHandler(com.minecolonies.coremod.entity.citizen.citizenhandlers.CitizenSkillHandler) Constants(com.minecolonies.api.util.constant.Constants) ListNBT(net.minecraft.nbt.ListNBT) IColonyView(com.minecolonies.api.colony.IColonyView) ICitizenHappinessHandler(com.minecolonies.api.entity.citizen.citizenhandlers.ICitizenHappinessHandler) IColonyManager(com.minecolonies.api.colony.IColonyManager) VisibleCitizenStatus(com.minecolonies.api.entity.citizen.VisibleCitizenStatus) ICitizenDataView(com.minecolonies.api.colony.ICitizenDataView) ChatPriority(com.minecolonies.api.colony.interactionhandling.ChatPriority) BlockPos(net.minecraft.util.math.BlockPos) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) InventoryCitizen(com.minecolonies.api.inventory.InventoryCitizen) IJobView(com.minecolonies.api.colony.jobs.IJobView) ResourceLocation(net.minecraft.util.ResourceLocation) ServerCitizenInteraction(com.minecolonies.coremod.colony.interactionhandling.ServerCitizenInteraction) NotNull(org.jetbrains.annotations.NotNull) CitizenHappinessHandler(com.minecolonies.coremod.entity.citizen.citizenhandlers.CitizenHappinessHandler) PacketBuffer(net.minecraft.network.PacketBuffer) ListNBT(net.minecraft.nbt.ListNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) ServerCitizenInteraction(com.minecolonies.coremod.colony.interactionhandling.ServerCitizenInteraction) InventoryCitizen(com.minecolonies.api.inventory.InventoryCitizen) IColonyView(com.minecolonies.api.colony.IColonyView)

Aggregations

InventoryCitizen (com.minecolonies.api.inventory.InventoryCitizen)10 NotNull (org.jetbrains.annotations.NotNull)8 java.util (java.util)4 ItemStack (net.minecraft.item.ItemStack)4 CompoundNBT (net.minecraft.nbt.CompoundNBT)4 PacketBuffer (net.minecraft.network.PacketBuffer)4 Nullable (org.jetbrains.annotations.Nullable)4 ImmutableList (com.google.common.collect.ImmutableList)2 TypeToken (com.google.common.reflect.TypeToken)2 MinecoloniesAPIProxy (com.minecolonies.api.MinecoloniesAPIProxy)2 ICitizenData (com.minecolonies.api.colony.ICitizenData)2 ICitizenDataView (com.minecolonies.api.colony.ICitizenDataView)2 IColonyManager (com.minecolonies.api.colony.IColonyManager)2 IColonyView (com.minecolonies.api.colony.IColonyView)2 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)2 AbstractBuildingModule (com.minecolonies.api.colony.buildings.modules.AbstractBuildingModule)2 IPersistentModule (com.minecolonies.api.colony.buildings.modules.IPersistentModule)2 ChatPriority (com.minecolonies.api.colony.interactionhandling.ChatPriority)2 IInteractionResponseHandler (com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler)2 IJobView (com.minecolonies.api.colony.jobs.IJobView)2