Search in sources :

Example 21 with IAIState

use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by ldtteam.

the class EntityAIWorkHealer method decide.

/**
 * Decide what to do next. Check if all patients are up date, else update their states. Then check if there is any patient we can cure or request things for.
 *
 * @return the next state to go to.
 */
private IAIState decide() {
    if (walkToBuilding()) {
        return DECIDE;
    }
    final BuildingHospital hospital = getOwnBuilding();
    for (final AbstractEntityCitizen citizen : WorldUtil.getEntitiesWithinBuilding(world, AbstractEntityCitizen.class, getOwnBuilding(), cit -> cit.getCitizenDiseaseHandler().isSick())) {
        hospital.checkOrCreatePatientFile(citizen.getCivilianID());
    }
    for (final Patient patient : hospital.getPatients()) {
        final ICitizenData data = hospital.getColony().getCitizenManager().getCivilian(patient.getId());
        if (data == null || !data.getEntity().isPresent() || (data.getEntity().isPresent() && !data.getEntity().get().getCitizenDiseaseHandler().isSick())) {
            hospital.removePatientFile(patient);
            continue;
        }
        final EntityCitizen citizen = (EntityCitizen) data.getEntity().get();
        final String diseaseName = citizen.getCitizenDiseaseHandler().getDisease();
        @Nullable final Disease disease = diseaseName.isEmpty() ? null : IColonyManager.getInstance().getCompatibilityManager().getDisease(diseaseName);
        if (patient.getState() == Patient.PatientState.NEW) {
            this.currentPatient = patient;
            return REQUEST_CURE;
        }
        if (patient.getState() == Patient.PatientState.REQUESTED) {
            if (disease == null) {
                this.currentPatient = patient;
                return CURE;
            }
            if (testRandomCureChance()) {
                this.currentPatient = patient;
                return FREE_CURE;
            }
            if (!InventoryUtils.isItemHandlerFull(citizen.getInventoryCitizen())) {
                if (hasCureInInventory(disease, worker.getInventoryCitizen()) || hasCureInInventory(disease, getOwnBuilding().getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElseGet(null))) {
                    this.currentPatient = patient;
                    return CURE;
                }
                final ImmutableList<IRequest<? extends Stack>> list = getOwnBuilding().getOpenRequestsOfType(worker.getCitizenData().getId(), TypeToken.of(Stack.class));
                final ImmutableList<IRequest<? extends Stack>> completed = getOwnBuilding().getCompletedRequestsOfType(worker.getCitizenData(), TypeToken.of(Stack.class));
                for (final ItemStack cure : IColonyManager.getInstance().getCompatibilityManager().getDisease(diseaseName).getCure()) {
                    if (!InventoryUtils.hasItemInItemHandler(worker.getInventoryCitizen(), cure::sameItem)) {
                        if (InventoryUtils.getItemCountInItemHandler(getOwnBuilding().getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElseGet(null), stack -> stack.sameItem(cure)) >= cure.getCount()) {
                            needsCurrently = new Tuple<>(stack -> stack.sameItem(cure), cure.getCount());
                            return GATHERING_REQUIRED_MATERIALS;
                        }
                        boolean hasCureRequested = false;
                        for (final IRequest<? extends Stack> request : list) {
                            if (request.getRequest().getStack().sameItem(cure)) {
                                hasCureRequested = true;
                                break;
                            }
                        }
                        for (final IRequest<? extends Stack> request : completed) {
                            if (request.getRequest().getStack().sameItem(cure)) {
                                hasCureRequested = true;
                                break;
                            }
                        }
                        if (!hasCureRequested) {
                            patient.setState(Patient.PatientState.NEW);
                            break;
                        }
                    }
                }
            } else {
                data.triggerInteraction(new StandardInteraction(new TranslationTextComponent(PATIENT_FULL_INVENTORY), ChatPriority.BLOCKING));
            }
        }
        if (patient.getState() == Patient.PatientState.TREATED) {
            if (disease == null) {
                this.currentPatient = patient;
                return CURE;
            }
            if (!hasCureInInventory(disease, citizen.getInventoryCitizen())) {
                patient.setState(Patient.PatientState.NEW);
                return DECIDE;
            }
        }
    }
    for (final PlayerEntity player : WorldUtil.getEntitiesWithinBuilding(world, PlayerEntity.class, getOwnBuilding(), player -> player.getHealth() < player.getMaxHealth() - 10 - (2 * getOwnBuilding().getBuildingLevel()))) {
        playerToHeal = player;
        return CURE_PLAYER;
    }
    final ICitizenData data = getOwnBuilding().getColony().getCitizenManager().getRandomCitizen();
    if (data.getEntity().isPresent() && data.getEntity().get().getHealth() < 10.0 && BlockPosUtil.getDistance2D(data.getEntity().get().blockPosition(), getOwnBuilding().getPosition()) < getOwnBuilding().getBuildingLevel() * 40) {
        remotePatient = data;
        return WANDER;
    }
    return DECIDE;
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) ICitizenData(com.minecolonies.api.colony.ICitizenData) TypeToken(com.google.common.reflect.TypeToken) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ItemStack(net.minecraft.item.ItemStack) ImmutableList(com.google.common.collect.ImmutableList) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) JobHealer(com.minecolonies.coremod.colony.jobs.JobHealer) AITarget(com.minecolonies.api.entity.ai.statemachine.AITarget) com.minecolonies.api.util(com.minecolonies.api.util) Network(com.minecolonies.coremod.Network) PATIENT_FULL_INVENTORY(com.minecolonies.api.util.constant.TranslationConstants.PATIENT_FULL_INVENTORY) StreamParticleEffectMessage(com.minecolonies.coremod.network.messages.client.StreamParticleEffectMessage) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack) AbstractEntityAIInteract(com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIInteract) PlayerEntity(net.minecraft.entity.player.PlayerEntity) CircleParticleEffectMessage(com.minecolonies.coremod.network.messages.client.CircleParticleEffectMessage) IColonyManager(com.minecolonies.api.colony.IColonyManager) ChatPriority(com.minecolonies.api.colony.interactionhandling.ChatPriority) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) BlockPos(net.minecraft.util.math.BlockPos) AIWorkerState(com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState) ParticleTypes(net.minecraft.particles.ParticleTypes) Nullable(org.jetbrains.annotations.Nullable) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) BuildingHospital(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital) EntityCitizen(com.minecolonies.coremod.entity.citizen.EntityCitizen) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) NotNull(org.jetbrains.annotations.NotNull) BuildingHospital(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingHospital) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) ICitizenData(com.minecolonies.api.colony.ICitizenData) ItemStack(net.minecraft.item.ItemStack) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) EntityCitizen(com.minecolonies.coremod.entity.citizen.EntityCitizen) ItemStack(net.minecraft.item.ItemStack) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with IAIState

use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by ldtteam.

the class EntityAIConcreteMixer method mixConcrete.

/**
 * Mix the concrete and mine it.
 *
 * @return next state.
 */
private IAIState mixConcrete() {
    int slot = -1;
    if (currentRequest != null && currentRecipeStorage != null) {
        ItemStack inputStack = currentRecipeStorage.getCleanedInput().get(0).getItemStack();
        if (CONCRETE.test(inputStack)) {
            slot = InventoryUtils.findFirstSlotInItemHandlerWith(worker.getInventoryCitizen(), s -> ItemStackUtils.compareItemStacksIgnoreStackSize(s, inputStack));
        } else {
            return START_WORKING;
        }
    } else {
        slot = InventoryUtils.findFirstSlotInItemHandlerWith(worker.getInventoryCitizen(), CONCRETE);
    }
    if (slot != -1) {
        final ItemStack stack = worker.getInventoryCitizen().getStackInSlot(slot);
        final Block block = ((BlockItem) stack.getItem()).getBlock();
        final BlockPos posToPlace = getOwnBuilding().getBlockToPlace();
        if (posToPlace != null) {
            if (walkToBlock(posToPlace)) {
                walkTo = posToPlace;
                return START_WORKING;
            }
            walkTo = null;
            if (InventoryUtils.attemptReduceStackInItemHandler(worker.getInventoryCitizen(), stack, 1)) {
                world.setBlock(posToPlace, block.defaultBlockState().updateShape(Direction.DOWN, block.defaultBlockState(), world, posToPlace, posToPlace), 0x03);
            }
            return START_WORKING;
        }
    }
    final BlockPos pos = getOwnBuilding().getBlockToMine();
    if (pos != null) {
        if (walkToBlock(pos)) {
            walkTo = pos;
            return START_WORKING;
        }
        walkTo = null;
        if (mineBlock(pos)) {
            this.resetActionsDone();
            return CRAFT;
        }
        return START_WORKING;
    }
    if (InventoryUtils.hasItemInItemHandler(getOwnBuilding().getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElseGet(null), CONCRETE)) {
        needsCurrently = new Tuple<>(CONCRETE, STACKSIZE);
        return GATHERING_REQUIRED_MATERIALS;
    } else {
        incrementActionsDone();
    }
    return START_WORKING;
}
Also used : ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) STACKSIZE(com.minecolonies.api.util.constant.Constants.STACKSIZE) Predicate(java.util.function.Predicate) RequestState(com.minecolonies.api.colony.requestsystem.request.RequestState) BlockPos(net.minecraft.util.math.BlockPos) Direction(net.minecraft.util.Direction) AIWorkerState(com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState) ItemStack(net.minecraft.item.ItemStack) Tuple(com.minecolonies.api.util.Tuple) JobConcreteMixer(com.minecolonies.coremod.colony.jobs.JobConcreteMixer) Block(net.minecraft.block.Block) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) BlockItem(net.minecraft.item.BlockItem) InventoryUtils(com.minecolonies.api.util.InventoryUtils) ConcretePowderBlock(net.minecraft.block.ConcretePowderBlock) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) AbstractEntityAICrafting(com.minecolonies.coremod.entity.ai.basic.AbstractEntityAICrafting) BuildingConcreteMixer(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingConcreteMixer) NotNull(org.jetbrains.annotations.NotNull) Block(net.minecraft.block.Block) ConcretePowderBlock(net.minecraft.block.ConcretePowderBlock) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) BlockItem(net.minecraft.item.BlockItem)

Example 23 with IAIState

use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by ldtteam.

the class EntityAIWorkCook method serveFoodToCitizen.

/**
 * Serve food to customer
 * <p>
 * If no customer, transition to START_WORKING. If we need to walk to the customer, repeat this state with tiny delay. If the customer has a full inventory, report and remove
 * customer, delay and repeat this state. If we have food, then COOK_SERVE. If no food in the building, transition to START_WORKING. If we were able to get the stored food,
 * then COOK_SERVE. If food is no longer available, delay and transition to START_WORKING. Otherwise, give the customer some food, then delay and repeat this state.
 *
 * @return next IAIState
 */
private IAIState serveFoodToCitizen() {
    worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent(TranslationConstants.COM_MINECOLONIES_COREMOD_STATUS_SERVING));
    if (citizenToServe.isEmpty() && playerToServe.isEmpty()) {
        return START_WORKING;
    }
    worker.getCitizenData().setVisibleStatus(COOK);
    final Entity living = citizenToServe.isEmpty() ? playerToServe.get(0) : citizenToServe.get(0);
    if (!getOwnBuilding().isInBuilding(living.blockPosition())) {
        worker.getNavigation().stop();
        removeFromQueue();
        return START_WORKING;
    }
    if (walkToBlock(new BlockPos(living.position()))) {
        return getState();
    }
    final IItemHandler handler = citizenToServe.isEmpty() ? new InvWrapper(playerToServe.get(0).inventory) : citizenToServe.get(0).getInventoryCitizen();
    if (InventoryUtils.isItemHandlerFull(handler)) {
        if (!citizenToServe.isEmpty()) {
            final int foodSlot = InventoryUtils.findFirstSlotInItemHandlerWith(worker.getInventoryCitizen(), stack -> ItemStackUtils.CAN_EAT.test(stack) && canEat(stack, citizenToServe.isEmpty() ? null : citizenToServe.get(0)));
            if (foodSlot != -1) {
                final ItemStack stack = worker.getInventoryCitizen().extractItem(foodSlot, 1, false);
                if (stack.getItem().isEdible()) {
                    citizenToServe.get(0).getCitizenData().increaseSaturation(stack.getItem().getFoodProperties().getNutrition() / 2.0);
                }
            }
        }
        removeFromQueue();
        return getState();
    } else if (InventoryUtils.hasItemInItemHandler(handler, stack -> CAN_EAT.test(stack) && canEat(stack, citizenToServe.isEmpty() ? null : citizenToServe.get(0)))) {
        removeFromQueue();
        return getState();
    }
    if (!InventoryUtils.transferFoodUpToSaturation(worker, handler, getOwnBuilding().getBuildingLevel() * SATURATION_TO_SERVE, stack -> CAN_EAT.test(stack) && canEat(stack, citizenToServe.isEmpty() ? null : citizenToServe.get(0)))) {
        removeFromQueue();
        return getState();
    }
    if (!citizenToServe.isEmpty() && citizenToServe.get(0).getCitizenData() != null) {
        citizenToServe.get(0).getCitizenData().setJustAte(true);
    }
    if (citizenToServe.isEmpty() && living instanceof PlayerEntity) {
        LanguageHandler.sendPlayerMessage((PlayerEntity) living, "com.minecolonies.coremod.cook.serve.player", worker.getName().getString());
    }
    removeFromQueue();
    worker.getCitizenExperienceHandler().addExperience(BASE_XP_GAIN);
    this.incrementActionsDoneAndDecSaturation();
    return START_WORKING;
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) java.util(java.util) LanguageHandler(com.ldtteam.structurize.util.LanguageHandler) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) BuildingCook(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingCook) JobCook(com.minecolonies.coremod.colony.jobs.JobCook) TypeToken(com.google.common.reflect.TypeToken) Action(com.minecolonies.api.colony.permissions.Action) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) FOOD_EXCLUSION_LIST(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingCook.FOOD_EXCLUSION_LIST) MinecoloniesAPIProxy(com.minecolonies.api.MinecoloniesAPIProxy) ItemStack(net.minecraft.item.ItemStack) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) Tuple(com.minecolonies.api.util.Tuple) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) CAN_EAT(com.minecolonies.api.util.ItemStackUtils.CAN_EAT) AITarget(com.minecolonies.api.entity.ai.statemachine.AITarget) Constants(com.minecolonies.api.util.constant.Constants) ItemListModule(com.minecolonies.coremod.colony.buildings.modules.ItemListModule) TranslationConstants(com.minecolonies.api.util.constant.TranslationConstants) Entity(net.minecraft.entity.Entity) Food(com.minecolonies.api.colony.requestsystem.requestable.Food) PlayerEntity(net.minecraft.entity.player.PlayerEntity) IColonyManager(com.minecolonies.api.colony.IColonyManager) VisibleCitizenStatus(com.minecolonies.api.entity.citizen.VisibleCitizenStatus) ChatPriority(com.minecolonies.api.colony.interactionhandling.ChatPriority) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) BlockPos(net.minecraft.util.math.BlockPos) FurnaceTileEntity(net.minecraft.tileentity.FurnaceTileEntity) IRequestable(com.minecolonies.api.colony.requestsystem.requestable.IRequestable) AIWorkerState(com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState) Collectors(java.util.stream.Collectors) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) InventoryUtils(com.minecolonies.api.util.InventoryUtils) ResourceLocation(net.minecraft.util.ResourceLocation) ItemStorage(com.minecolonies.api.crafting.ItemStorage) WorldUtil(com.minecolonies.api.util.WorldUtil) AbstractEntityAIUsesFurnace(com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIUsesFurnace) NotNull(org.jetbrains.annotations.NotNull) Entity(net.minecraft.entity.Entity) PlayerEntity(net.minecraft.entity.player.PlayerEntity) FurnaceTileEntity(net.minecraft.tileentity.FurnaceTileEntity) IItemHandler(net.minecraftforge.items.IItemHandler) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity)

Example 24 with IAIState

use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by ldtteam.

the class EntityAIWorkCrusher method crush.

/**
 * The crushing process.
 *
 * @return the next AiState to go to.
 */
protected IAIState crush() {
    if (walkToBuilding()) {
        return getState();
    }
    worker.getCitizenData().setVisibleStatus(CRUSHING);
    job.setProgress(job.getProgress() + TICK_DELAY);
    final BuildingCrusher crusherBuilding = getOwnBuilding();
    WorkerUtil.faceBlock(crusherBuilding.getPosition(), worker);
    if (currentRecipeStorage == null) {
        currentRecipeStorage = crusherBuilding.getCurrentRecipe();
    }
    if ((getState() != CRAFT && crusherBuilding.getCurrentDailyQuantity() >= crusherBuilding.getCrusherMode().getB()) || currentRecipeStorage == null) {
        return START_WORKING;
    }
    final IAIState check = checkForItems(currentRecipeStorage);
    if (job.getProgress() > MAX_LEVEL - Math.min((getSecondarySkillLevel() / 2) + 1, MAX_LEVEL)) {
        job.setProgress(0);
        if (check == CRAFT) {
            if (getState() != CRAFT) {
                crusherBuilding.setCurrentDailyQuantity(crusherBuilding.getCurrentDailyQuantity() + 1);
                if (crusherBuilding.getCurrentDailyQuantity() >= crusherBuilding.getCrusherMode().getB()) {
                    incrementActionsDoneAndDecSaturation();
                }
            }
            if (currentRequest != null) {
                currentRequest.addDelivery(currentRecipeStorage.getPrimaryOutput());
            }
            worker.swing(Hand.MAIN_HAND);
            job.setCraftCounter(job.getCraftCounter() + 1);
            currentRecipeStorage.fullfillRecipe(getLootContext(), ImmutableList.of(worker.getItemHandlerCitizen()));
            worker.decreaseSaturationForContinuousAction();
            worker.getCitizenExperienceHandler().addExperience(0.1);
        } else if (getState() != CRAFT) {
            currentRecipeStorage = crusherBuilding.getCurrentRecipe();
            final int requestQty = Math.min((crusherBuilding.getCrusherMode().getB() - crusherBuilding.getCurrentDailyQuantity()) * 2, STACKSIZE);
            if (requestQty <= 0) {
                return START_WORKING;
            }
            final ItemStack stack = currentRecipeStorage.getInput().get(0).getItemStack().copy();
            stack.setCount(requestQty);
            checkIfRequestForItemExistOrCreateAsynch(stack);
            return START_WORKING;
        } else {
            return check;
        }
    }
    if (check == CRAFT) {
        Network.getNetwork().sendToTrackingEntity(new LocalizedParticleEffectMessage(currentRecipeStorage.getInput().get(0).getItemStack().copy(), crusherBuilding.getID()), worker);
        Network.getNetwork().sendToTrackingEntity(new LocalizedParticleEffectMessage(currentRecipeStorage.getPrimaryOutput().copy(), crusherBuilding.getID().below()), worker);
        SoundUtils.playSoundAtCitizen(world, getOwnBuilding().getID(), SoundEvents.STONE_BREAK);
    }
    return getState();
}
Also used : BuildingCrusher(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingCrusher) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) LocalizedParticleEffectMessage(com.minecolonies.coremod.network.messages.client.LocalizedParticleEffectMessage) ItemStack(net.minecraft.item.ItemStack)

Example 25 with IAIState

use of com.minecolonies.api.entity.ai.statemachine.states.IAIState in project minecolonies by ldtteam.

the class EntityAIWorkDeliveryman method deliver.

/**
 * Deliver the items to the hut. TODO: Current precondition: The dman's inventory may only consist of the requested itemstack.
 *
 * @return the next state.
 */
private IAIState deliver() {
    final IRequest<? extends IDeliverymanRequestable> currentTask = job.getCurrentTask();
    if (!(currentTask instanceof DeliveryRequest)) {
        // Since prepareDelivery() was called earlier, go dumping first and then restart.
        return DUMPING;
    }
    worker.getCitizenData().setVisibleStatus(DELIVERING);
    worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.delivering"));
    final ILocation targetBuildingLocation = ((Delivery) currentTask.getRequest()).getTarget();
    if (!targetBuildingLocation.isReachableFromLocation(worker.getLocation())) {
        Log.getLogger().info(worker.getCitizenColonyHandler().getColony().getName() + ": " + worker.getName() + ": Can't inter dimension yet: ");
        return START_WORKING;
    }
    if (!worker.isWorkerAtSiteWithMove(targetBuildingLocation.getInDimensionLocation(), MIN_DISTANCE_TO_WAREHOUSE)) {
        setDelay(WALK_DELAY);
        return DELIVERY;
    }
    final TileEntity tileEntity = world.getBlockEntity(targetBuildingLocation.getInDimensionLocation());
    if (!(tileEntity instanceof TileEntityColonyBuilding)) {
        // TODO: Non-Colony deliveries are unsupported yet. Fix that at some point in time.
        job.finishRequest(true);
        return START_WORKING;
    }
    final IBuilding targetBuilding = ((AbstractTileEntityColonyBuilding) tileEntity).getBuilding();
    boolean success = true;
    boolean extracted = false;
    final IItemHandler workerInventory = worker.getInventoryCitizen();
    for (int i = 0; i < workerInventory.getSlots(); i++) {
        if (workerInventory.getStackInSlot(i).isEmpty()) {
            continue;
        }
        final ItemStack stack = workerInventory.extractItem(i, Integer.MAX_VALUE, false);
        if (ItemStackUtils.isEmpty(stack)) {
            continue;
        }
        extracted = true;
        final ItemStack insertionResultStack;
        // TODO: Please only push items into the target that were actually requested.
        if (targetBuilding instanceof AbstractBuilding) {
            insertionResultStack = InventoryUtils.forceItemStackToItemHandler(targetBuilding.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack, ((IBuilding) targetBuilding)::isItemStackInRequest);
        } else {
            // Buildings that are not inherently part of the request system, but just receive a delivery, cannot have their items replaced.
            // Therefore, the keep-predicate always returns true.
            insertionResultStack = InventoryUtils.forceItemStackToItemHandler(targetBuilding.getCapability(ITEM_HANDLER_CAPABILITY, null).orElseGet(null), stack, itemStack -> true);
        }
        if (!ItemStackUtils.isEmpty(insertionResultStack)) {
            if (ItemStack.matches(insertionResultStack, stack) && worker.getCitizenData() != null) {
                // The replaced stack is the same as the one we tried to put into the inventory.
                // Meaning, replacing failed.
                success = false;
                if (targetBuilding.hasModule(WorkerBuildingModule.class)) {
                    worker.getCitizenData().triggerInteraction(new PosBasedInteraction(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_NAMEDCHESTFULL, targetBuilding.getFirstModuleOccurance(WorkerBuildingModule.class).getFirstCitizen().getName()), ChatPriority.IMPORTANT, new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL), targetBuilding.getID()));
                } else {
                    worker.getCitizenData().triggerInteraction(new PosBasedInteraction(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL, new StringTextComponent(" :" + targetBuilding.getSchematicName())), ChatPriority.IMPORTANT, new TranslationTextComponent(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL), targetBuildingLocation.getInDimensionLocation()));
                }
            }
            // Insert the result back into the inventory so we do not lose it.
            workerInventory.insertItem(i, insertionResultStack, false);
        }
    }
    if (!extracted) {
        // This can only happen if the dman's inventory was completely empty.
        // Let the retry-system handle this case.
        worker.decreaseSaturationForContinuousAction();
        worker.getCitizenItemHandler().setHeldItem(Hand.MAIN_HAND, SLOT_HAND);
        job.finishRequest(false);
        // No need to go dumping in this case.
        return START_WORKING;
    }
    worker.getCitizenExperienceHandler().addExperience(1.5D);
    worker.decreaseSaturationForContinuousAction();
    worker.getCitizenItemHandler().setHeldItem(Hand.MAIN_HAND, SLOT_HAND);
    job.finishRequest(true);
    return success ? START_WORKING : DUMPING;
}
Also used : TileEntityRack(com.minecolonies.api.tileentities.TileEntityRack) IItemHandler(net.minecraftforge.items.IItemHandler) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) AbstractTileEntityColonyBuilding(com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding) DeliveryRequest(com.minecolonies.coremod.colony.requestsystem.requests.StandardRequests.DeliveryRequest) IWareHouse(com.minecolonies.api.colony.buildings.workerbuildings.IWareHouse) PickupRequest(com.minecolonies.coremod.colony.requestsystem.requests.StandardRequests.PickupRequest) TileEntityColonyBuilding(com.minecolonies.api.tileentities.TileEntityColonyBuilding) PosBasedInteraction(com.minecolonies.coremod.colony.interactionhandling.PosBasedInteraction) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ArrayList(java.util.ArrayList) JobDeliveryman(com.minecolonies.coremod.colony.jobs.JobDeliveryman) ItemStack(net.minecraft.item.ItemStack) IDeliverymanRequestable(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.IDeliverymanRequestable) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) StringTextComponent(net.minecraft.util.text.StringTextComponent) Hand(net.minecraft.util.Hand) ITEM_HANDLER_CAPABILITY(net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) Delivery(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery) AITarget(com.minecolonies.api.entity.ai.statemachine.AITarget) Log(com.minecolonies.api.util.Log) TICKS_SECOND(com.minecolonies.api.util.constant.Constants.TICKS_SECOND) Constants(com.minecolonies.api.util.constant.Constants) AbstractEntityAIInteract(com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIInteract) TranslationConstants(com.minecolonies.api.util.constant.TranslationConstants) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) VisibleCitizenStatus(com.minecolonies.api.entity.citizen.VisibleCitizenStatus) ChatPriority(com.minecolonies.api.colony.interactionhandling.ChatPriority) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) BlockPos(net.minecraft.util.math.BlockPos) IRequestable(com.minecolonies.api.colony.requestsystem.requestable.IRequestable) AIWorkerState(com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) WorkerBuildingModule(com.minecolonies.coremod.colony.buildings.modules.WorkerBuildingModule) BuildingDeliveryman(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingDeliveryman) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) InventoryUtils(com.minecolonies.api.util.InventoryUtils) ResourceLocation(net.minecraft.util.ResourceLocation) TileEntity(net.minecraft.tileentity.TileEntity) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) DeliveryRequest(com.minecolonies.coremod.colony.requestsystem.requests.StandardRequests.DeliveryRequest) IItemHandler(net.minecraftforge.items.IItemHandler) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) AbstractTileEntityColonyBuilding(com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding) TileEntityColonyBuilding(com.minecolonies.api.tileentities.TileEntityColonyBuilding) WorkerBuildingModule(com.minecolonies.coremod.colony.buildings.modules.WorkerBuildingModule) PosBasedInteraction(com.minecolonies.coremod.colony.interactionhandling.PosBasedInteraction) TileEntity(net.minecraft.tileentity.TileEntity) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) AbstractTileEntityColonyBuilding(com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) StringTextComponent(net.minecraft.util.text.StringTextComponent) Delivery(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery) ItemStack(net.minecraft.item.ItemStack) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding)

Aggregations

IAIState (com.minecolonies.api.entity.ai.statemachine.states.IAIState)67 ItemStack (net.minecraft.item.ItemStack)52 BlockPos (net.minecraft.util.math.BlockPos)45 AIWorkerState (com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState)44 NotNull (org.jetbrains.annotations.NotNull)44 AITarget (com.minecolonies.api.entity.ai.statemachine.AITarget)42 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)42 ItemStorage (com.minecolonies.api.crafting.ItemStorage)35 InventoryUtils (com.minecolonies.api.util.InventoryUtils)32 StandardInteraction (com.minecolonies.coremod.colony.interactionhandling.StandardInteraction)32 Hand (net.minecraft.util.Hand)32 ItemStackUtils (com.minecolonies.api.util.ItemStackUtils)30 ChatPriority (com.minecolonies.api.colony.interactionhandling.ChatPriority)28 TranslationConstants (com.minecolonies.api.util.constant.TranslationConstants)24 ItemListModule (com.minecolonies.coremod.colony.buildings.modules.ItemListModule)24 TileEntity (net.minecraft.tileentity.TileEntity)24 Tuple (com.minecolonies.api.util.Tuple)22 List (java.util.List)22 TypeToken (com.google.common.reflect.TypeToken)20 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)20