Search in sources :

Example 21 with ICraftingBuildingModule

use of com.minecolonies.api.colony.buildings.modules.ICraftingBuildingModule in project minecolonies by ldtteam.

the class AbstractEntityAICrafting method craft.

/**
 * The actual crafting logic.
 *
 * @return the next state to go to.
 */
protected IAIState craft() {
    if (currentRecipeStorage == null || job.getCurrentTask() == 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;
    }
    if (job.getProgress() >= getRequiredProgressForMakingRawMaterial()) {
        final IAIState check = checkForItems(currentRecipeStorage);
        if (check == CRAFT) {
            if (!currentRecipeStorage.fullfillRecipe(getLootContext(), ImmutableList.of(worker.getItemHandlerCitizen()))) {
                currentRequest = null;
                incrementActionsDone(getActionRewardForCraftingSuccess());
                job.finishRequest(false);
                resetValues();
                return START_WORKING;
            }
            currentRequest.addDelivery(currentRecipeStorage.getPrimaryOutput());
            job.setCraftCounter(job.getCraftCounter() + 1);
            if (job.getCraftCounter() >= job.getMaxCraftingCount()) {
                incrementActionsDone(getActionRewardForCraftingSuccess());
                final ICraftingBuildingModule module = getOwnBuilding().getCraftingModuleForRecipe(currentRecipeStorage.getToken());
                if (module != null) {
                    module.improveRecipe(currentRecipeStorage, job.getCraftCounter(), worker.getCitizenData());
                }
                currentRecipeStorage = null;
                resetValues();
                if (inventoryNeedsDump()) {
                    if (job.getMaxCraftingCount() == 0 && job.getProgress() == 0 && job.getCraftCounter() == 0 && currentRequest != null) {
                        job.finishRequest(true);
                        worker.getCitizenExperienceHandler().addExperience(currentRequest.getRequest().getCount() / 2.0);
                    }
                }
            } else {
                job.setProgress(0);
                return GET_RECIPE;
            }
        } else {
            currentRequest = null;
            job.finishRequest(false);
            incrementActionsDoneAndDecSaturation();
            resetValues();
        }
        return START_WORKING;
    }
    return getState();
}
Also used : IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) ICraftingBuildingModule(com.minecolonies.api.colony.buildings.modules.ICraftingBuildingModule)

Aggregations

ICraftingBuildingModule (com.minecolonies.api.colony.buildings.modules.ICraftingBuildingModule)21 IRecipeStorage (com.minecolonies.api.crafting.IRecipeStorage)11 IAIState (com.minecolonies.api.entity.ai.statemachine.states.IAIState)8 ItemStorage (com.minecolonies.api.crafting.ItemStorage)7 NotNull (org.jetbrains.annotations.NotNull)6 ItemStackUtils (com.minecolonies.api.util.ItemStackUtils)5 ItemStack (net.minecraft.item.ItemStack)5 AITarget (com.minecolonies.api.entity.ai.statemachine.AITarget)4 AIWorkerState (com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState)4 InventoryUtils (com.minecolonies.api.util.InventoryUtils)4 AbstractBuilding (com.minecolonies.coremod.colony.buildings.AbstractBuilding)4 Hand (net.minecraft.util.Hand)4 IBuildingModule (com.minecolonies.api.colony.buildings.modules.IBuildingModule)3 BuildingEntry (com.minecolonies.api.colony.buildings.registry.BuildingEntry)3 Nullable (org.jetbrains.annotations.Nullable)3 ImmutableList (com.google.common.collect.ImmutableList)2 ChatPriority (com.minecolonies.api.colony.interactionhandling.ChatPriority)2 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)2 RequestState (com.minecolonies.api.colony.requestsystem.request.RequestState)2 Stack (com.minecolonies.api.colony.requestsystem.requestable.Stack)2