Search in sources :

Example 71 with IAIState

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

the class EntityAIWorkCrusher method craft.

/**
 * The actual crafting logic.
 *
 * @return the next state to go to.
 */
@Override
protected IAIState craft() {
    if (currentRecipeStorage == null) {
        return START_WORKING;
    }
    if (currentRequest == null && job.getCurrentTask() != null) {
        return GET_RECIPE;
    }
    if (walkToBuilding()) {
        return getState();
    }
    job.setProgress(job.getProgress() + 1);
    worker.setItemInHand(Hand.MAIN_HAND, currentRecipeStorage.getCleanedInput().get(worker.getRandom().nextInt(currentRecipeStorage.getCleanedInput().size())).getItemStack().copy());
    worker.setItemInHand(Hand.OFF_HAND, currentRecipeStorage.getPrimaryOutput().copy());
    worker.getCitizenItemHandler().hitBlockWithToolInHand(getOwnBuilding().getPosition());
    currentRequest = job.getCurrentTask();
    if (currentRequest != null && (currentRequest.getState() == RequestState.CANCELLED || currentRequest.getState() == RequestState.FAILED)) {
        currentRequest = null;
        incrementActionsDone(getActionRewardForCraftingSuccess());
        currentRecipeStorage = null;
        return START_WORKING;
    }
    final IAIState check = crush();
    if (check == getState()) {
        if (job.getCraftCounter() >= job.getMaxCraftingCount()) {
            incrementActionsDone(getActionRewardForCraftingSuccess());
            currentRecipeStorage = null;
            resetValues();
            if (inventoryNeedsDump()) {
                if (job.getMaxCraftingCount() == 0 && job.getProgress() == 0 && job.getCraftCounter() == 0 && currentRequest != null) {
                    job.finishRequest(true);
                }
            }
        }
    } else {
        currentRequest = null;
        job.finishRequest(false);
        incrementActionsDoneAndDecSaturation();
        resetValues();
    }
    return getState();
}
Also used : IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState)

Example 72 with IAIState

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

the class EntityAIWorkComposter method getMaterials.

/**
 * Method for the AI to try to get the materials needed for the task he's doing. Will request if there are no materials
 *
 * @return the new IAIState after doing this
 */
private IAIState getMaterials() {
    if (walkToBuilding()) {
        setDelay(2);
        return getState();
    }
    final List<ItemStorage> list = getOwnBuilding().getModuleMatching(ItemListModule.class, m -> m.getId().equals(COMPOSTABLE_LIST)).getList();
    if (list.isEmpty()) {
        complain();
        return getState();
    }
    if (InventoryUtils.hasItemInProvider(getOwnBuilding(), stack -> list.contains(new ItemStorage(stack)))) {
        InventoryUtils.transferItemStackIntoNextFreeSlotFromProvider(getOwnBuilding(), InventoryUtils.findFirstSlotInProviderNotEmptyWith(getOwnBuilding(), stack -> list.contains(new ItemStorage(stack))), worker.getInventoryCitizen());
    }
    final int slot = InventoryUtils.findFirstSlotInItemHandlerWith(worker.getInventoryCitizen(), stack -> list.contains(new ItemStorage(stack)));
    if (slot >= 0) {
        worker.setItemInHand(Hand.MAIN_HAND, worker.getInventoryCitizen().getStackInSlot(slot));
        return START_WORKING;
    }
    worker.setItemInHand(Hand.MAIN_HAND, ItemStack.EMPTY);
    if (!getOwnBuilding().hasWorkerOpenRequests(worker.getCitizenData().getId())) {
        final ArrayList<ItemStack> itemList = new ArrayList<>();
        for (final ItemStorage item : list) {
            final ItemStack itemStack = item.getItemStack();
            itemStack.setCount(itemStack.getMaxStackSize());
            itemList.add(itemStack);
        }
        if (!itemList.isEmpty()) {
            worker.getCitizenData().createRequestAsync(new StackList(itemList, COM_MINECOLONIES_REQUESTS_COMPOSTABLE, Constants.STACKSIZE * getOwnBuilding().getBarrels().size(), 1, getOwnBuilding().getSetting(BuildingComposter.MIN).getValue()));
        }
    }
    setDelay(2);
    return START_WORKING;
}
Also used : ItemListModule(com.minecolonies.coremod.colony.buildings.modules.ItemListModule) StackList(com.minecolonies.api.colony.requestsystem.requestable.StackList) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) Random(java.util.Random) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) JobComposter(com.minecolonies.coremod.colony.jobs.JobComposter) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) Hand(net.minecraft.util.Hand) AITarget(com.minecolonies.api.entity.ai.statemachine.AITarget) TICKS_SECOND(com.minecolonies.api.util.constant.Constants.TICKS_SECOND) Constants(com.minecolonies.api.util.constant.Constants) ItemListModule(com.minecolonies.coremod.colony.buildings.modules.ItemListModule) BuildingComposter(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingComposter) AbstractEntityAIInteract(com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIInteract) TranslationConstants(com.minecolonies.api.util.constant.TranslationConstants) AIEventTarget(com.minecolonies.api.entity.ai.statemachine.AIEventTarget) PlayerEntity(net.minecraft.entity.player.PlayerEntity) World(net.minecraft.world.World) VisibleCitizenStatus(com.minecolonies.api.entity.citizen.VisibleCitizenStatus) AIBlockingEventType(com.minecolonies.api.entity.ai.statemachine.states.AIBlockingEventType) BlockPos(net.minecraft.util.math.BlockPos) DOUBLE(com.minecolonies.api.util.constant.Constants.DOUBLE) AIWorkerState(com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState) Blocks(net.minecraft.block.Blocks) List(java.util.List) MineColonies(com.minecolonies.coremod.MineColonies) InventoryUtils(com.minecolonies.api.util.InventoryUtils) ResourceLocation(net.minecraft.util.ResourceLocation) TileEntityBarrel(com.minecolonies.coremod.tileentities.TileEntityBarrel) TileEntity(net.minecraft.tileentity.TileEntity) ItemStorage(com.minecolonies.api.crafting.ItemStorage) WorldUtil(com.minecolonies.api.util.WorldUtil) NotNull(org.jetbrains.annotations.NotNull) PODZOL_CHANCE(com.minecolonies.api.research.util.ResearchConstants.PODZOL_CHANCE) ArrayList(java.util.ArrayList) StackList(com.minecolonies.api.colony.requestsystem.requestable.StackList) ItemStack(net.minecraft.item.ItemStack) ItemStorage(com.minecolonies.api.crafting.ItemStorage)

Example 73 with IAIState

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

the class EntityAIWorkFisherman method doFishing.

/**
 * Main fishing methods, let's the fisherman gather xp orbs next to him, check if all requirements to fish are given. Actually fish, retrieve his rod if stuck or if a fish
 * bites.
 *
 * @return the next IAIState the fisherman should switch to, after executing this method.
 */
@Nullable
private IAIState doFishing() {
    worker.getCitizenStatusHandler().setLatestStatus(new TranslationTextComponent("com.minecolonies.coremod.status.fishing"));
    @Nullable final IAIState notReadyState = isReadyToFish();
    if (notReadyState != null) {
        return notReadyState;
    }
    if (caughtFish()) {
        playCaughtFishSound();
        this.incrementActionsDoneAndDecSaturation();
        if (worker.getRandom().nextDouble() < CHANCE_NEW_POND) {
            job.setWater(null);
            return FISHERMAN_SEARCHING_WATER;
        }
        return FISHERMAN_WALKING_TO_WATER;
    }
    return throwOrRetrieveHook();
}
Also used : IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) Nullable(org.jetbrains.annotations.Nullable) Nullable(org.jetbrains.annotations.Nullable)

Example 74 with IAIState

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

the class EntityAIWorkCowboy method decideWhatToDo.

@Override
public IAIState decideWhatToDo() {
    final IAIState result = super.decideWhatToDo();
    final BuildingCowboy building = getOwnBuilding();
    final boolean hasBucket = InventoryUtils.hasItemInItemHandler(worker.getInventoryCitizen(), Items.BUCKET);
    if (building != null && getOwnBuilding().getSetting(BuildingCowboy.MILKING).getValue() && result.equals(START_WORKING) && hasBucket) {
        return COWBOY_MILK;
    }
    return result;
}
Also used : BuildingCowboy(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingCowboy) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState)

Aggregations

IAIState (com.minecolonies.api.entity.ai.statemachine.states.IAIState)74 ItemStack (net.minecraft.item.ItemStack)58 BlockPos (net.minecraft.util.math.BlockPos)52 AIWorkerState (com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState)50 NotNull (org.jetbrains.annotations.NotNull)50 AITarget (com.minecolonies.api.entity.ai.statemachine.AITarget)48 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)48 ItemStorage (com.minecolonies.api.crafting.ItemStorage)42 StandardInteraction (com.minecolonies.coremod.colony.interactionhandling.StandardInteraction)38 Hand (net.minecraft.util.Hand)37 ChatPriority (com.minecolonies.api.colony.interactionhandling.ChatPriority)34 ItemStackUtils (com.minecolonies.api.util.ItemStackUtils)34 InventoryUtils (com.minecolonies.api.util.InventoryUtils)31 TranslationConstants (com.minecolonies.api.util.constant.TranslationConstants)30 TileEntity (net.minecraft.tileentity.TileEntity)28 List (java.util.List)27 TypeToken (com.google.common.reflect.TypeToken)26 StackList (com.minecolonies.api.colony.requestsystem.requestable.StackList)26 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)25 VisibleCitizenStatus (com.minecolonies.api.entity.citizen.VisibleCitizenStatus)25