Search in sources :

Example 6 with StandardInteraction

use of com.minecolonies.coremod.colony.interactionhandling.StandardInteraction in project minecolonies by ldtteam.

the class AbstractEntityAIRequestSmelter method fillUpFurnace.

/**
 * Smelt the smeltable after the required items are in the inv.
 *
 * @return the next state to go to.
 */
private IAIState fillUpFurnace() {
    final FurnaceUserModule module = getOwnBuilding().getFirstModuleOccurance(FurnaceUserModule.class);
    if (module.getFurnaces().isEmpty()) {
        if (worker.getCitizenData() != null) {
            worker.getCitizenData().triggerInteraction(new StandardInteraction(new TranslationTextComponent(BAKER_HAS_NO_FURNACES_MESSAGE), ChatPriority.BLOCKING));
        }
        setDelay(STANDARD_DELAY);
        return START_WORKING;
    }
    if (walkTo == null || world.getBlockState(walkTo).getBlock() != Blocks.FURNACE) {
        walkTo = null;
        setDelay(STANDARD_DELAY);
        return START_WORKING;
    }
    final int burningCount = countOfBurningFurnaces();
    final TileEntity entity = world.getBlockEntity(walkTo);
    if (entity instanceof FurnaceTileEntity && currentRecipeStorage != null) {
        final FurnaceTileEntity furnace = (FurnaceTileEntity) entity;
        final int maxFurnaces = getMaxUsableFurnaces();
        final Predicate<ItemStack> smeltable = stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(currentRecipeStorage.getCleanedInput().get(0).getItemStack(), stack);
        final int smeltableInFurnaces = getExtendedCount(currentRecipeStorage.getCleanedInput().get(0).getItemStack());
        final int resultInFurnaces = getExtendedCount(currentRecipeStorage.getPrimaryOutput());
        final int resultInCitizenInv = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, currentRecipeStorage.getPrimaryOutput()));
        final int targetCount = currentRequest.getRequest().getCount() - smeltableInFurnaces - resultInFurnaces - resultInCitizenInv;
        if (targetCount <= 0) {
            return START_WORKING;
        }
        final int amountOfSmeltableInBuilding = InventoryUtils.getCountFromBuilding(getOwnBuilding(), smeltable);
        final int amountOfSmeltableInInv = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), smeltable);
        if (worker.getItemInHand(Hand.MAIN_HAND).isEmpty()) {
            worker.setItemInHand(Hand.MAIN_HAND, currentRecipeStorage.getCleanedInput().get(0).getItemStack().copy());
        }
        if (amountOfSmeltableInInv > 0) {
            if (hasFuelInFurnaceAndNoSmeltable(furnace) || hasNeitherFuelNorSmeltAble(furnace)) {
                int toTransfer = 0;
                if (burningCount < maxFurnaces) {
                    final int availableFurnaces = maxFurnaces - burningCount;
                    if (targetCount > STACKSIZE * availableFurnaces) {
                        toTransfer = STACKSIZE;
                    } else {
                        // We need to split stacks and spread them across furnaces for best performance
                        // We will front-load the remainder
                        toTransfer = Math.min((targetCount / availableFurnaces) + (targetCount % availableFurnaces), STACKSIZE);
                    }
                }
                if (toTransfer > 0) {
                    if (walkToBlock(walkTo)) {
                        return getState();
                    }
                    worker.getCitizenItemHandler().hitBlockWithToolInHand(walkTo);
                    InventoryUtils.transferXInItemHandlerIntoSlotInItemHandler(worker.getInventoryCitizen(), smeltable, toTransfer, new InvWrapper(furnace), SMELTABLE_SLOT);
                }
            }
        } else if (amountOfSmeltableInBuilding >= targetCount - amountOfSmeltableInInv && currentRecipeStorage.getIntermediate() == Blocks.FURNACE) {
            needsCurrently = new Tuple<>(smeltable, targetCount);
            return GATHERING_REQUIRED_MATERIALS;
        } else {
            // This is a safety net for the AI getting way out of sync with it's tracking. It shouldn't happen.
            job.finishRequest(false);
            resetValues();
            walkTo = null;
            return IDLE;
        }
    } else if (!(world.getBlockState(walkTo).getBlock() instanceof FurnaceBlock)) {
        module.removeFromFurnaces(walkTo);
    }
    walkTo = null;
    setDelay(STANDARD_DELAY);
    return START_WORKING;
}
Also used : FurnaceTileEntity(net.minecraft.tileentity.FurnaceTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) TICKS_20(com.minecolonies.api.util.constant.CitizenConstants.TICKS_20) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) StackList(com.minecolonies.api.colony.requestsystem.requestable.StackList) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) IRecipeStorage(com.minecolonies.api.crafting.IRecipeStorage) TypeToken(com.google.common.reflect.TypeToken) FurnaceUserModule(com.minecolonies.coremod.colony.buildings.modules.FurnaceUserModule) AbstractJobCrafter(com.minecolonies.coremod.colony.jobs.AbstractJobCrafter) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) Tuple(com.minecolonies.api.util.Tuple) ImmutableList(com.google.common.collect.ImmutableList) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) Hand(net.minecraft.util.Hand) AITarget(com.minecolonies.api.entity.ai.statemachine.AITarget) FUEL_LIST(com.minecolonies.api.util.constant.BuildingConstants.FUEL_LIST) ItemListModule(com.minecolonies.coremod.colony.buildings.modules.ItemListModule) Constants(com.minecolonies.api.util.constant.Constants) TranslationConstants(com.minecolonies.api.util.constant.TranslationConstants) AIEventTarget(com.minecolonies.api.entity.ai.statemachine.AIEventTarget) World(net.minecraft.world.World) Predicate(java.util.function.Predicate) RequestState(com.minecolonies.api.colony.requestsystem.request.RequestState) ChatPriority(com.minecolonies.api.colony.interactionhandling.ChatPriority) AIBlockingEventType(com.minecolonies.api.entity.ai.statemachine.states.AIBlockingEventType) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) BlockPos(net.minecraft.util.math.BlockPos) FurnaceTileEntity(net.minecraft.tileentity.FurnaceTileEntity) AIWorkerState(com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState) Blocks(net.minecraft.block.Blocks) List(java.util.List) PublicCrafting(com.minecolonies.api.colony.requestsystem.requestable.crafting.PublicCrafting) InventoryUtils(com.minecolonies.api.util.InventoryUtils) TileEntity(net.minecraft.tileentity.TileEntity) ItemStorage(com.minecolonies.api.crafting.ItemStorage) WorldUtil(com.minecolonies.api.util.WorldUtil) FurnaceBlock(net.minecraft.block.FurnaceBlock) NotNull(org.jetbrains.annotations.NotNull) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) FurnaceTileEntity(net.minecraft.tileentity.FurnaceTileEntity) FurnaceBlock(net.minecraft.block.FurnaceBlock) ItemStack(net.minecraft.item.ItemStack) Tuple(com.minecolonies.api.util.Tuple) FurnaceUserModule(com.minecolonies.coremod.colony.buildings.modules.FurnaceUserModule)

Example 7 with StandardInteraction

use of com.minecolonies.coremod.colony.interactionhandling.StandardInteraction in project minecolonies by ldtteam.

the class EntityAIWorkFlorist method decide.

/**
 * Main decision method of florist to decide what to do.
 *
 * @return the next AI state to go to.
 */
private IAIState decide() {
    worker.getCitizenData().setVisibleStatus(VisibleCitizenStatus.WORKING);
    if (getOwnBuilding().getPlantGround().isEmpty()) {
        worker.getCitizenData().triggerInteraction(new StandardInteraction(new TranslationTextComponent(NO_PLANT_GROUND_FLORIST), ChatPriority.BLOCKING));
        return IDLE;
    }
    worker.setItemInHand(Hand.MAIN_HAND, ItemStack.EMPTY);
    final long distance = BlockPosUtil.getDistance2D(worker.blockPosition(), getOwnBuilding().getPosition());
    if (distance > MAX_DISTANCE && walkToBuilding()) {
        return DECIDE;
    }
    final int amountOfCompostInInv = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), IS_COMPOST);
    if (amountOfCompostInInv <= 0) {
        final int amountOfCompostInBuilding = InventoryUtils.getCountFromBuilding(getOwnBuilding(), IS_COMPOST);
        if (amountOfCompostInBuilding > 0) {
            needsCurrently = new Tuple<>(IS_COMPOST, STACKSIZE);
            return GATHERING_REQUIRED_MATERIALS;
        } else {
            checkIfRequestForItemExistOrCreateAsynch(new ItemStack(ModItems.compost, COMPOST_REQUEST_QTY));
        }
    }
    harvestPosition = areThereFlowersToGather();
    if (harvestPosition != null) {
        return FLORIST_HARVEST;
    }
    if (amountOfCompostInInv <= 0) {
        if (!isThereCompostedLand(getOwnBuilding(), world)) {
            worker.getCitizenData().triggerInteraction(new StandardInteraction(new TranslationTextComponent(NO_COMPOST), ChatPriority.BLOCKING));
            return START_WORKING;
        }
        return DECIDE;
    } else {
        compostPosition = getFirstNotCompostedLand();
        return FLORIST_COMPOST;
    }
}
Also used : StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ItemStack(net.minecraft.item.ItemStack)

Example 8 with StandardInteraction

use of com.minecolonies.coremod.colony.interactionhandling.StandardInteraction in project minecolonies by ldtteam.

the class EntityAIWorkFlorist method compost.

/**
 * Walk to the block to compost and apply compost to it.
 *
 * @return the next state to go to.
 */
private IAIState compost() {
    if (compostPosition == null) {
        return START_WORKING;
    }
    worker.getCitizenData().setVisibleStatus(GARDENING);
    if (walkToBlock(compostPosition)) {
        return getState();
    }
    final TileEntity entity = world.getBlockEntity(compostPosition);
    if (entity instanceof TileEntityCompostedDirt) {
        @Nullable final ItemStack stack = getOwnBuilding().getFlowerToGrow();
        if (stack != null) {
            if (worker.getRandom().nextInt(200 - getPrimarySkillLevel()) < 0 || InventoryUtils.shrinkItemCountInItemHandler(worker.getInventoryCitizen(), IS_COMPOST)) {
                ((TileEntityCompostedDirt) entity).compost(PERCENT_CHANGE_FOR_GROWTH - (getOwnBuilding().getBuildingLevel() * 0.01), getOwnBuilding().getFlowerToGrow());
            }
        } else {
            worker.getCitizenData().triggerInteraction(new StandardInteraction(new TranslationTextComponent(NO_FLOWERS_IN_CONFIG), ChatPriority.BLOCKING));
        }
    }
    incrementActionsDone();
    worker.decreaseSaturationForContinuousAction();
    compostPosition = null;
    return START_WORKING;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) TileEntityCompostedDirt(com.minecolonies.coremod.tileentities.TileEntityCompostedDirt) ItemStack(net.minecraft.item.ItemStack) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with StandardInteraction

use of com.minecolonies.coremod.colony.interactionhandling.StandardInteraction in project minecolonies by ldtteam.

the class CitizenSleepHandler method trySleep.

/**
 * Attempts a sleep interaction with the citizen and the given bed.
 *
 * @param bedLocation The possible location to sleep.
 */
@Override
public boolean trySleep(final BlockPos bedLocation) {
    final BlockState state = WorldUtil.isEntityBlockLoaded(citizen.level, bedLocation) ? citizen.level.getBlockState(bedLocation) : null;
    final boolean isBed = state != null && state.getBlock().isBed(state, citizen.level, bedLocation, citizen);
    if (!isBed) {
        return false;
    }
    citizen.updatePose(Pose.SLEEPING);
    citizen.getNavigation().stop();
    citizen.setPos(((float) bedLocation.getX() + HALF_BLOCK), (float) bedLocation.getY() + 0.8F, ((float) bedLocation.getZ() + HALF_BLOCK));
    citizen.setSleepingPos(bedLocation);
    citizen.setDeltaMovement(Vector3d.ZERO);
    citizen.hasImpulse = true;
    // Remove item while citizen is asleep.
    citizen.getCitizenItemHandler().removeHeldItem();
    setIsAsleep(true);
    citizen.getCitizenData().triggerInteraction(new StandardInteraction(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_ENTITY_CITIZEN_SLEEPING), ChatPriority.HIDDEN));
    if (citizen.getCitizenData() != null) {
        citizen.getCitizenData().setBedPos(bedLocation);
    }
    citizen.getEntityData().set(DATA_BED_POS, bedLocation);
    citizen.getCitizenData().getColony().getCitizenManager().onCitizenSleep();
    return true;
}
Also used : BlockState(net.minecraft.block.BlockState) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent)

Example 10 with StandardInteraction

use of com.minecolonies.coremod.colony.interactionhandling.StandardInteraction in project minecolonies by ldtteam.

the class EntityCitizen method determineDesiredActivity.

/**
 * Determines the desired activity
 */
private boolean determineDesiredActivity() {
    if (citizenJobHandler.getColonyJob() instanceof AbstractJobGuard) {
        desiredActivity = DesiredActivity.WORK;
        return false;
    }
    if (getCitizenColonyHandler().getColony().getRaiderManager().isRaided()) {
        citizenData.triggerInteraction(new StandardInteraction(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_ENTITY_CITIZEN_RAID), ChatPriority.IMPORTANT));
        setVisibleStatusIfNone(RAIDED);
        desiredActivity = DesiredActivity.SLEEP;
        return false;
    }
    // Sleeping
    if (!WorldUtil.isPastTime(CompatibilityUtils.getWorldFromCitizen(this), NIGHT - 2000)) {
        if (desiredActivity == DesiredActivity.SLEEP) {
            setVisibleStatusIfNone(SLEEP);
            return false;
        }
        if (citizenSleepHandler.shouldGoSleep()) {
            citizenData.onGoSleep();
            citizenData.decreaseSaturation(citizenColonyHandler.getPerBuildingFoodCost() * 2);
            citizenData.markDirty();
            citizenStatusHandler.setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.sleeping"));
            desiredActivity = DesiredActivity.SLEEP;
            return false;
        }
    }
    // Mourning
    if (citizenData.getCitizenMournHandler().isMourning() && citizenData.getCitizenMournHandler().shouldMourn()) {
        if (!getCitizenColonyHandler().getColony().getRaiderManager().isRaided()) {
            citizenData.triggerInteraction(new StandardInteraction(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_ENTITY_CITIZEN_MOURNING, citizenData.getCitizenMournHandler().getDeceasedCitizens().iterator().next()), new TranslationTextComponent(COM_MINECOLONIES_COREMOD_ENTITY_CITIZEN_MOURNING), ChatPriority.IMPORTANT));
        }
        setVisibleStatusIfNone(MOURNING);
        desiredActivity = DesiredActivity.MOURN;
        return false;
    }
    if (citizenSleepHandler.isAsleep() && !citizenDiseaseHandler.isSick()) {
        citizenSleepHandler.onWakeUp();
    }
    // Raining
    if (CompatibilityUtils.getWorldFromCitizen(this).isRaining() && !shouldWorkWhileRaining() && !WorldUtil.isNetherType(level)) {
        citizenStatusHandler.setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.waiting"), new TranslationTextComponent("com.minecolonies.coremod.status.rainStop"));
        setVisibleStatusIfNone(BAD_WEATHER);
        if (!citizenData.getColony().getRaiderManager().isRaided() && !citizenData.getCitizenMournHandler().isMourning()) {
            citizenData.triggerInteraction(new StandardInteraction(new TranslationTextComponent(COM_MINECOLONIES_COREMOD_ENTITY_CITIZEN_RAINING), ChatPriority.HIDDEN));
        }
        desiredActivity = DesiredActivity.SLEEP;
        return false;
    }
    if (isBaby() && getCitizenJobHandler().getColonyJob() instanceof JobPupil && level.getDayTime() % 24000 > NOON) {
        setVisibleStatusIfNone(HOUSE);
        desiredActivity = DesiredActivity.IDLE;
        return false;
    }
    if (getCitizenJobHandler().getColonyJob() != null) {
        desiredActivity = DesiredActivity.WORK;
        return false;
    }
    setVisibleStatusIfNone(HOUSE);
    desiredActivity = DesiredActivity.IDLE;
    return false;
}
Also used : StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent)

Aggregations

StandardInteraction (com.minecolonies.coremod.colony.interactionhandling.StandardInteraction)54 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)53 BlockPos (net.minecraft.util.math.BlockPos)26 ItemStack (net.minecraft.item.ItemStack)25 NotNull (org.jetbrains.annotations.NotNull)21 IAIState (com.minecolonies.api.entity.ai.statemachine.states.IAIState)19 ChatPriority (com.minecolonies.api.colony.interactionhandling.ChatPriority)17 ItemStorage (com.minecolonies.api.crafting.ItemStorage)15 AITarget (com.minecolonies.api.entity.ai.statemachine.AITarget)15 AIWorkerState (com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState)15 InventoryUtils (com.minecolonies.api.util.InventoryUtils)12 TypeToken (com.google.common.reflect.TypeToken)11 StackList (com.minecolonies.api.colony.requestsystem.requestable.StackList)11 ItemStackUtils (com.minecolonies.api.util.ItemStackUtils)11 TranslationConstants (com.minecolonies.api.util.constant.TranslationConstants)11 Network (com.minecolonies.coremod.Network)11 Hand (net.minecraft.util.Hand)11 ICitizenData (com.minecolonies.api.colony.ICitizenData)10 ItemListModule (com.minecolonies.coremod.colony.buildings.modules.ItemListModule)10 TileEntity (net.minecraft.tileentity.TileEntity)9