Search in sources :

Example 6 with ICraftingBuildingModule

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

the class EntityAIWorkSifter method sift.

/**
 * The sifting process.
 *
 * @return the next AiState to go to.
 */
protected IAIState sift() {
    final BuildingSifter sifterBuilding = getOwnBuilding();
    // Go idle if we can't do any more today
    if (sifterBuilding.getCurrentDailyQuantity() >= sifterBuilding.getMaxDailyQuantity()) {
        return IDLE;
    }
    if (walkToBuilding()) {
        return getState();
    }
    if (InventoryUtils.isItemHandlerFull(worker.getInventoryCitizen())) {
        return INVENTORY_FULL;
    }
    if (currentRecipeStorage == null) {
        final ICraftingBuildingModule module = getOwnBuilding().getFirstModuleOccurance(BuildingSifter.CraftingModule.class);
        currentRecipeStorage = module.getFirstFulfillableRecipe(ItemStackUtils::isEmpty, 1, false);
    }
    if (currentRecipeStorage == null) {
        if (InventoryUtils.getCountFromBuilding(sifterBuilding, i -> ModTags.meshes.contains(i.getItem())) == 0) {
            if (InventoryUtils.getItemCountInProvider(worker, i -> ModTags.meshes.contains(i.getItem())) > 0) {
                // We don't want the mesh in our inventory, we 'craft' out of the building
                incrementActionsDone();
                return INVENTORY_FULL;
            }
            if (worker.getCitizenData() != null) {
                worker.getCitizenData().triggerInteraction(new StandardInteraction(new TranslationTextComponent(SIFTER_NO_MESH), ChatPriority.IMPORTANT));
                setDelay(NO_MESH_DELAY);
            }
        }
        if (!ItemStackUtils.isEmpty(worker.getMainHandItem())) {
            worker.setItemInHand(Hand.MAIN_HAND, ItemStack.EMPTY);
        }
        if (!ItemStackUtils.isEmpty(worker.getOffhandItem())) {
            worker.setItemInHand(Hand.OFF_HAND, ItemStack.EMPTY);
        }
        progress = 0;
        return START_WORKING;
    }
    final ItemStack meshItem = currentRecipeStorage.getCraftingTools().get(0);
    final ItemStack inputItem = currentRecipeStorage.getCleanedInput().stream().map(ItemStorage::getItemStack).filter(item -> !ItemStackUtils.compareItemStacksIgnoreStackSize(item, meshItem, false, true)).findFirst().orElse(ItemStack.EMPTY);
    if (meshItem.isEmpty() || inputItem.isEmpty()) {
        currentRecipeStorage = null;
        return getState();
    }
    if (!inputItem.isEmpty() && (ItemStackUtils.isEmpty(worker.getMainHandItem()) || ItemStackUtils.compareItemStacksIgnoreStackSize(worker.getMainHandItem(), inputItem))) {
        worker.setItemInHand(Hand.MAIN_HAND, inputItem);
    }
    if (!meshItem.isEmpty() && (ItemStackUtils.isEmpty(worker.getOffhandItem()) || ItemStackUtils.compareItemStacksIgnoreStackSize(worker.getOffhandItem(), meshItem, false, true))) {
        worker.setItemInHand(Hand.OFF_HAND, meshItem);
    }
    WorkerUtil.faceBlock(getOwnBuilding().getPosition(), worker);
    progress++;
    if (progress > MAX_LEVEL - (getEffectiveSkillLevel(getSecondarySkillLevel()) / 2)) {
        progress = 0;
        sifterBuilding.setCurrentDailyQuantity(sifterBuilding.getCurrentDailyQuantity() + 1);
        if (sifterBuilding.getCurrentDailyQuantity() >= sifterBuilding.getMaxDailyQuantity() || worker.getRandom().nextInt(ONE_HUNDRED_PERCENT) < CHANCE_TO_DUMP_INV) {
            incrementActionsDoneAndDecSaturation();
        }
        if (!currentRecipeStorage.fullfillRecipe(getLootContext(), sifterBuilding.getHandlers())) {
            currentRecipeStorage = null;
            return getState();
        }
        worker.decreaseSaturationForContinuousAction();
        worker.getCitizenExperienceHandler().addExperience(0.2);
    }
    Network.getNetwork().sendToTrackingEntity(new LocalizedParticleEffectMessage(meshItem, sifterBuilding.getID()), worker);
    Network.getNetwork().sendToTrackingEntity(new LocalizedParticleEffectMessage(inputItem, sifterBuilding.getID().below()), worker);
    worker.swing(Hand.MAIN_HAND);
    SoundUtils.playSoundAtCitizen(world, getOwnBuilding().getID(), SoundEvents.LEASH_KNOT_BREAK);
    return getState();
}
Also used : ModTags(com.minecolonies.api.items.ModTags) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) ICraftingBuildingModule(com.minecolonies.api.colony.buildings.modules.ICraftingBuildingModule) ChatPriority(com.minecolonies.api.colony.interactionhandling.ChatPriority) BuildingSifter(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingSifter) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) SIFTER_NO_MESH(com.minecolonies.api.util.constant.TranslationConstants.SIFTER_NO_MESH) SoundUtils(com.minecolonies.api.util.SoundUtils) LocalizedParticleEffectMessage(com.minecolonies.coremod.network.messages.client.LocalizedParticleEffectMessage) AIWorkerState(com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState) SoundEvents(net.minecraft.util.SoundEvents) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ItemStack(net.minecraft.item.ItemStack) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) WorkerUtil(com.minecolonies.coremod.util.WorkerUtil) InventoryUtils(com.minecolonies.api.util.InventoryUtils) AbstractEntityAICrafting(com.minecolonies.coremod.entity.ai.basic.AbstractEntityAICrafting) ONE_HUNDRED_PERCENT(com.minecolonies.api.util.constant.Constants.ONE_HUNDRED_PERCENT) Hand(net.minecraft.util.Hand) AITarget(com.minecolonies.api.entity.ai.statemachine.AITarget) Network(com.minecolonies.coremod.Network) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) JobSifter(com.minecolonies.coremod.colony.jobs.JobSifter) BuildingSifter(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingSifter) StandardInteraction(com.minecolonies.coremod.colony.interactionhandling.StandardInteraction) LocalizedParticleEffectMessage(com.minecolonies.coremod.network.messages.client.LocalizedParticleEffectMessage) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ICraftingBuildingModule(com.minecolonies.api.colony.buildings.modules.ICraftingBuildingModule) ItemStack(net.minecraft.item.ItemStack)

Example 7 with ICraftingBuildingModule

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

the class AbstractCraftingProductionResolver method resolveForBuilding.

/**
 * Resolve the request in a building.
 *
 * @param manager  the request manager.
 * @param request  the request.
 * @param building the building.
 */
public void resolveForBuilding(@NotNull final IRequestManager manager, @NotNull final IRequest<? extends C> request, @NotNull final AbstractBuilding building) {
    final AbstractBuilding buildingWorker = (AbstractBuilding) building;
    final ICraftingBuildingModule module = buildingWorker.getCraftingModuleForRecipe(request.getId());
    if (module == null) {
        manager.updateRequestState(request.getId(), RequestState.FAILED);
        return;
    }
    final IRecipeStorage storage = module.getFirstFulfillableRecipe(stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, request.getRequest().getStack()), request.getRequest().getCount(), false);
    if (storage == null) {
        manager.updateRequestState(request.getId(), RequestState.FAILED);
        return;
    }
    final int craftingCount = CraftingUtils.calculateMaxCraftingCount(request.getRequest().getCount(), storage);
    for (int i = 0; i < craftingCount; i++) {
        module.fullFillRecipe(storage);
    }
    manager.updateRequestState(request.getId(), RequestState.RESOLVED);
}
Also used : IRecipeStorage(com.minecolonies.api.crafting.IRecipeStorage) ICraftingBuildingModule(com.minecolonies.api.colony.buildings.modules.ICraftingBuildingModule) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding)

Example 8 with ICraftingBuildingModule

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

the class PrivateWorkerCraftingProductionResolver method resolveForBuilding.

@Override
public void resolveForBuilding(@NotNull final IRequestManager manager, @NotNull final IRequest<? extends PrivateCrafting> request, @NotNull final AbstractBuilding building) {
    manager.updateRequestState(request.getId(), RequestState.FINALIZING);
    final IRecipeStorage storage = IColonyManager.getInstance().getRecipeManager().getRecipes().get(request.getRequest().getRecipeID());
    if (storage == null) {
        manager.updateRequestState(request.getId(), RequestState.FAILED);
        return;
    }
    final ICraftingBuildingModule module = building.getCraftingModuleForRecipe(request.getRequest().getRecipeID());
    if (module == null) {
        manager.updateRequestState(request.getId(), RequestState.FAILED);
        return;
    }
    for (int i = 0; i < request.getRequest().getCount(); i++) {
        module.fullFillRecipe(storage);
    }
    manager.updateRequestState(request.getId(), RequestState.RESOLVED);
}
Also used : IRecipeStorage(com.minecolonies.api.crafting.IRecipeStorage) ICraftingBuildingModule(com.minecolonies.api.colony.buildings.modules.ICraftingBuildingModule)

Example 9 with ICraftingBuildingModule

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

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(building.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 = building.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)

Example 10 with ICraftingBuildingModule

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

the class CustomRecipe method isPrecursorRecipeMissing.

/**
 * Check if a precursor recipe is missing from the building.
 * @param building      The building which would contain the precursor recipe.
 * @return              True if a precursor recipe was required and not present.
 */
private boolean isPrecursorRecipeMissing(IBuilding building) {
    if (mustExist) {
        final IRecipeStorage compareStorage = this.getRecipeStorage();
        final ResourceLocation recipeSource = this.getRecipeId();
        for (final ICraftingBuildingModule module : building.getModules(ICraftingBuildingModule.class)) {
            for (IToken<?> recipeToken : module.getRecipes()) {
                final IRecipeStorage storage = IColonyManager.getInstance().getRecipeManager().getRecipes().get(recipeToken);
                if ((storage.getRecipeSource() != null && storage.getRecipeSource().equals(recipeSource)) || (ItemStackUtils.compareItemStacksIgnoreStackSize(storage.getPrimaryOutput(), compareStorage.getPrimaryOutput(), false, true) && storage.getCleanedInput().containsAll(compareStorage.getCleanedInput()) && compareStorage.getCleanedInput().containsAll(storage.getCleanedInput()))) {
                    return false;
                }
            }
        }
        return true;
    }
    // if no precursor needed.
    return false;
}
Also used : ResourceLocation(net.minecraft.util.ResourceLocation) 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