Search in sources :

Example 11 with ICraftingBuildingModule

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

the class RecipeAnalyzer method findRecipes.

/**
 * Find all recipes for a given crafter.
 *
 * @param vanilla vanilla recipes map.
 * @param crafting crafting module.
 * @return list of recipes
 */
@NotNull
public static List<IGenericRecipe> findRecipes(@NotNull final Map<CraftingType, List<IGenericRecipe>> vanilla, @NotNull final ICraftingBuildingModule crafting) {
    final List<IGenericRecipe> recipes = new ArrayList<>();
    // all vanilla teachable recipes
    for (final Map.Entry<CraftingType, List<IGenericRecipe>> entry : vanilla.entrySet()) {
        if (crafting.canLearn(entry.getKey())) {
            for (final IGenericRecipe recipe : entry.getValue()) {
                final IGenericRecipe safeRecipe = GenericRecipeUtils.filterInputs(recipe, crafting.getIngredientValidator());
                if (crafting.isRecipeCompatible(safeRecipe)) {
                    recipes.add(safeRecipe);
                }
            }
        }
    }
    // custom MineColonies additional recipes
    for (final CustomRecipe customRecipe : CustomRecipeManager.getInstance().getRecipes(crafting.getCustomRecipeKey())) {
        final IRecipeStorage recipeStorage = customRecipe.getRecipeStorage();
        if (!recipeStorage.getAlternateOutputs().isEmpty()) {
            // this is a multi-output recipe; assume it replaces a bunch of vanilla
            // recipes we already added above
            recipes.removeIf(r -> ItemStackUtils.compareItemStacksIgnoreStackSize(recipeStorage.getPrimaryOutput(), r.getPrimaryOutput()));
            recipes.removeIf(r -> recipeStorage.getAlternateOutputs().stream().anyMatch(s -> ItemStackUtils.compareItemStacksIgnoreStackSize(s, r.getPrimaryOutput())));
        }
        recipes.add(GenericRecipeUtils.create(customRecipe, recipeStorage));
    }
    // and even more recipes that can't be taught, but are just inherent in the worker AI
    recipes.addAll(crafting.getAdditionalRecipesForDisplayPurposesOnly());
    return recipes;
}
Also used : ImmutableMap(com.google.common.collect.ImmutableMap) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) World(net.minecraft.world.World) ICraftingBuildingModule(com.minecolonies.api.colony.buildings.modules.ICraftingBuildingModule) IRecipeStorage(com.minecolonies.api.crafting.IRecipeStorage) IGenericRecipe(com.minecolonies.api.crafting.IGenericRecipe) ArrayList(java.util.ArrayList) MinecoloniesAPIProxy(com.minecolonies.api.MinecoloniesAPIProxy) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) RecipeManager(net.minecraft.item.crafting.RecipeManager) Map(java.util.Map) CraftingType(com.minecolonies.api.crafting.registry.CraftingType) NotNull(org.jetbrains.annotations.NotNull) CraftingType(com.minecolonies.api.crafting.registry.CraftingType) ArrayList(java.util.ArrayList) IRecipeStorage(com.minecolonies.api.crafting.IRecipeStorage) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) IGenericRecipe(com.minecolonies.api.crafting.IGenericRecipe) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with ICraftingBuildingModule

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

the class EntityAIWorkNether method decide.

@Override
protected IAIState decide() {
    if (job.isInNether()) {
        if (!worker.isInvisible()) {
            goToVault();
        }
        return NETHER_AWAY;
    }
    if (worker.isInvisible()) {
        returnFromVault(true);
    }
    IAIState crafterState = super.decide();
    if (crafterState != IDLE && crafterState != START_WORKING) {
        return crafterState;
    }
    // Get Armor if available.
    // This is async, so we could go to the nether without it.
    checkAndRequestArmor();
    // Check for materials needed to go to the Nether:
    IRecipeStorage rs = building.getFirstModuleOccurance(BuildingNetherWorker.CraftingModule.class).getFirstRecipe(ItemStack::isEmpty);
    boolean hasItemsAvailable = true;
    if (rs != null) {
        for (ItemStorage item : rs.getInput()) {
            if (!checkIfRequestForItemExistOrCreateAsync(new ItemStack(item.getItem(), 1), item.getAmount(), item.getAmount())) {
                hasItemsAvailable = false;
            }
        }
    }
    if (!hasItemsAvailable) {
        return IDLE;
    }
    final BlockPos portal = building.getPortalLocation();
    if (portal == null) {
        Log.getLogger().warn("--- Missing Portal Tag In Nether Worker Building! Aborting Operation! ---");
        return IDLE;
    }
    // Make sure we have a stash of some food
    checkIfRequestForItemExistOrCreate(new StackList(getEdiblesList(), "Edible Food", 16));
    // Get other adventuring supplies. These are required.
    // Done this way to get all the requests in parallel
    boolean missingAxe = checkForToolOrWeapon(ToolType.AXE);
    boolean missingPick = checkForToolOrWeapon(ToolType.PICKAXE);
    boolean missingShovel = checkForToolOrWeapon(ToolType.SHOVEL);
    boolean missingSword = checkForToolOrWeapon(ToolType.SWORD);
    boolean missingLighter = checkForToolOrWeapon(ToolType.FLINT_N_STEEL);
    if (missingAxe || missingPick || missingShovel || missingSword || missingLighter) {
        worker.getCitizenData().setIdleAtJob(true);
        return IDLE;
    }
    if (currentRecipeStorage == null) {
        final ICraftingBuildingModule module = building.getFirstModuleOccurance(BuildingNetherWorker.CraftingModule.class);
        currentRecipeStorage = module.getFirstFulfillableRecipe(ItemStackUtils::isEmpty, 1, false);
        if (building.isReadyForTrip()) {
            worker.getCitizenData().setIdleAtJob(true);
        }
        if (currentRecipeStorage == null && building.shallClosePortalOnReturn()) {
            final BlockState block = world.getBlockState(portal);
            if (block.is(Blocks.NETHER_PORTAL)) {
                return NETHER_CLOSEPORTAL;
            }
        }
        return getState();
    } else {
        if (!building.isReadyForTrip()) {
            worker.getCitizenData().setIdleAtJob(false);
            return IDLE;
        }
        if (walkTo != null || walkToBuilding()) {
            return getState();
        }
        if (InventoryUtils.isItemHandlerFull(worker.getInventoryCitizen())) {
            return INVENTORY_FULL;
        }
        IAIState checkResult = checkForItems(currentRecipeStorage);
        if (checkResult == GET_RECIPE) {
            currentRecipeStorage = null;
            worker.getCitizenData().setIdleAtJob(true);
            return IDLE;
        }
        if (checkResult != CRAFT) {
            return checkResult;
        }
    }
    return NETHER_LEAVE;
}
Also used : BuildingNetherWorker(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingNetherWorker) BlockState(net.minecraft.block.BlockState) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) IRecipeStorage(com.minecolonies.api.crafting.IRecipeStorage) BlockPos(net.minecraft.util.math.BlockPos) StackList(com.minecolonies.api.colony.requestsystem.requestable.StackList) ICraftingBuildingModule(com.minecolonies.api.colony.buildings.modules.ICraftingBuildingModule) ItemStorage(com.minecolonies.api.crafting.ItemStorage)

Example 13 with ICraftingBuildingModule

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

the class EntityAIWorkSifter method sift.

/**
 * The sifting process.
 *
 * @return the next AiState to go to.
 */
protected IAIState sift() {
    final BuildingSifter sifterBuilding = building;
    // 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 = building.getFirstModuleOccurance(BuildingSifter.CraftingModule.class);
        currentRecipeStorage = module.getFirstFulfillableRecipe(ItemStackUtils::isEmpty, 1, false);
    }
    if (currentRecipeStorage == null) {
        if (InventoryUtils.hasBuildingEnoughElseCount(sifterBuilding, i -> ModTags.meshes.contains(i.getItem()), 1) == 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(building.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, building.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 14 with ICraftingBuildingModule

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

the class RecipeSetting method getValue.

@Override
public IRecipeStorage getValue(final IBuilding building) {
    final ICraftingBuildingModule craftingModule = building.getModuleMatching(ICraftingBuildingModule.class, m -> m.getId().equals(craftingModuleId));
    for (final IToken<?> token : craftingModule.getRecipes()) {
        if (token.equals(currentIndex)) {
            return IColonyManager.getInstance().getRecipeManager().getRecipe(currentIndex);
        }
    }
    currentIndex = building.getFirstModuleOccurance(ICraftingBuildingModule.class).getRecipes().get(0);
    return IColonyManager.getInstance().getRecipeManager().getRecipe(currentIndex);
}
Also used : ICraftingBuildingModule(com.minecolonies.api.colony.buildings.modules.ICraftingBuildingModule)

Example 15 with ICraftingBuildingModule

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

the class AbstractEntityAICrafting method getRecipe.

/**
 * Query the IRecipeStorage of the first request in the queue.
 *
 * @return the next state to go to.
 */
protected IAIState getRecipe() {
    final IRequest<? extends PublicCrafting> currentTask = job.getCurrentTask();
    if (currentTask == null) {
        return START_WORKING;
    }
    final ICraftingBuildingModule module = building.getCraftingModuleForRecipe(currentTask.getRequest().getRecipeID());
    if (module == null) {
        job.finishRequest(false);
        incrementActionsDone(getActionRewardForCraftingSuccess());
        return START_WORKING;
    }
    currentRecipeStorage = module.getFirstFulfillableRecipe(stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, currentTask.getRequest().getStack()), 1, false);
    if (currentRecipeStorage == null) {
        job.finishRequest(false);
        incrementActionsDone(getActionRewardForCraftingSuccess());
        return START_WORKING;
    }
    currentRequest = currentTask;
    job.setMaxCraftingCount(currentRequest.getRequest().getCount());
    final int currentCount = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, currentRecipeStorage.getPrimaryOutput()));
    final int inProgressCount = getExtendedCount(currentRecipeStorage.getPrimaryOutput());
    final int countPerIteration = currentRecipeStorage.getPrimaryOutput().getCount();
    final int doneOpsCount = currentCount / countPerIteration;
    final int progressOpsCount = inProgressCount / countPerIteration;
    final int remainingOpsCount = currentRequest.getRequest().getCount() - doneOpsCount - progressOpsCount;
    final List<ItemStorage> input = currentRecipeStorage.getCleanedInput();
    for (final ItemStorage inputStorage : input) {
        final ItemStack container = inputStorage.getItem().getContainerItem(inputStorage.getItemStack());
        final int remaining;
        if (!currentRecipeStorage.getCraftingToolsAndSecondaryOutputs().isEmpty() && ItemStackUtils.compareItemStackListIgnoreStackSize(currentRecipeStorage.getCraftingToolsAndSecondaryOutputs(), inputStorage.getItemStack(), false, true)) {
            remaining = inputStorage.getAmount();
        } else if (!ItemStackUtils.isEmpty(container) && ItemStackUtils.compareItemStacksIgnoreStackSize(inputStorage.getItemStack(), container, false, true)) {
            remaining = inputStorage.getAmount();
        } else {
            remaining = inputStorage.getAmount() * remainingOpsCount;
        }
        if (InventoryUtils.getCountFromBuilding(building, itemStack -> ItemStackUtils.compareItemStacksIgnoreStackSize(itemStack, inputStorage.getItemStack(), false, true)) + InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), itemStack -> ItemStackUtils.compareItemStacksIgnoreStackSize(itemStack, inputStorage.getItemStack(), false, true)) + getExtendedCount(inputStorage.getItemStack()) < remaining) {
            job.finishRequest(false);
            incrementActionsDone(getActionRewardForCraftingSuccess());
            return START_WORKING;
        }
    }
    job.setCraftCounter(doneOpsCount);
    return QUERY_ITEMS;
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) IRecipeStorage(com.minecolonies.api.crafting.IRecipeStorage) AbstractJobCrafter(com.minecolonies.coremod.colony.jobs.AbstractJobCrafter) ItemStack(net.minecraft.item.ItemStack) AbstractAdvancedPathNavigate(com.minecolonies.api.entity.pathfinding.AbstractAdvancedPathNavigate) LootContext(net.minecraft.loot.LootContext) Tuple(com.minecolonies.api.util.Tuple) ImmutableList(com.google.common.collect.ImmutableList) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) CraftingWorkerBuildingModule(com.minecolonies.coremod.colony.buildings.modules.CraftingWorkerBuildingModule) Hand(net.minecraft.util.Hand) AITarget(com.minecolonies.api.entity.ai.statemachine.AITarget) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack) RecipeStorage(com.minecolonies.api.crafting.RecipeStorage) Predicate(java.util.function.Predicate) ICraftingBuildingModule(com.minecolonies.api.colony.buildings.modules.ICraftingBuildingModule) RequestState(com.minecolonies.api.colony.requestsystem.request.RequestState) VisibleCitizenStatus(com.minecolonies.api.entity.citizen.VisibleCitizenStatus) LootParameters(net.minecraft.loot.LootParameters) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding) AIWorkerState(com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState) DamageSource(net.minecraft.util.DamageSource) FakePlayerFactory(net.minecraftforge.common.util.FakePlayerFactory) DEFAULT_SPEED(com.minecolonies.api.util.constant.Constants.DEFAULT_SPEED) List(java.util.List) PublicCrafting(com.minecolonies.api.colony.requestsystem.requestable.crafting.PublicCrafting) InventoryUtils(com.minecolonies.api.util.InventoryUtils) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) FakePlayer(net.minecraftforge.common.util.FakePlayer) ICraftingBuildingModule(com.minecolonies.api.colony.buildings.modules.ICraftingBuildingModule) ItemStack(net.minecraft.item.ItemStack) ItemStorage(com.minecolonies.api.crafting.ItemStorage)

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