Search in sources :

Example 1 with IArtisanRecipe

use of com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe in project artisan-worktables by codetaylor.

the class RecipeRegistry method findRecipe.

@Nullable
public IArtisanRecipe findRecipe(int playerExperience, int playerLevels, boolean isPlayerCreative, ItemStack[] tools, ICraftingMatrixStackHandler craftingMatrix, @Nullable FluidStack fluidStack, ISecondaryIngredientMatcher secondaryIngredientMatcher, EnumTier tier, Map<ResourceLocation, IRequirementContext> requirementContextMap) {
    // If the recipe list is empty, short-circuit.
    if (this.recipeList.isEmpty()) {
        return null;
    }
    // Next, check the last recipe first.
    IArtisanRecipe lastRecipe = this.recipeList.get(this.recipeList.size() - 1);
    boolean lastRecipeMatches = lastRecipe.matches(requirementContextMap, playerExperience, playerLevels, isPlayerCreative, tools, craftingMatrix, fluidStack, secondaryIngredientMatcher, tier);
    if (lastRecipeMatches) {
        return lastRecipe;
    }
    // Next, loop through the remaining recipes in reverse.
    for (int i = this.recipeList.size() - 2; i >= 0; i--) {
        IArtisanRecipe recipe = this.recipeList.get(i);
        boolean matches = recipe.matches(requirementContextMap, playerExperience, playerLevels, isPlayerCreative, tools, craftingMatrix, fluidStack, secondaryIngredientMatcher, tier);
        if (matches) {
            // If the recipe matches, move it to the end of the list. This ensures that the
            // recipe will be checked faster next time, increasing performance for shift +
            // click crafting operations.
            // 
            // Worst case remove: O(n) for re-indexing.
            // Worst case add: O(1) because we're adding to the end of the list.
            this.recipeList.remove(i);
            this.recipeList.add(recipe);
            return recipe;
        }
    }
    // Finally, if no recipe was matched, return null.
    return null;
}
Also used : IArtisanRecipe(com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe) Nullable(javax.annotation.Nullable)

Example 2 with IArtisanRecipe

use of com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe in project artisan-worktables by codetaylor.

the class TileEntityBase method onTakeResult.

public void onTakeResult(EntityPlayer player) {
    IArtisanRecipe recipe = this.getRecipe(player);
    if (recipe == null) {
        return;
    }
    recipe.doCraft(this.getCraftingContext(player));
    this.markDirty();
    if (!this.world.isRemote) {
        this.notifyBlockUpdate();
    }
}
Also used : IArtisanRecipe(com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe)

Example 3 with IArtisanRecipe

use of com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe in project artisan-worktables by codetaylor.

the class Container method transferStackInSlot.

@Override
public ItemStack transferStackInSlot(EntityPlayer playerIn, int slotIndex) {
    ItemStack itemStackCopy = ItemStack.EMPTY;
    Slot slot = this.inventorySlots.get(slotIndex);
    if (slot != null && slot.getHasStack()) {
        ItemStack itemStack = slot.getStack();
        itemStackCopy = itemStack.copy();
        if (this.isSlotIndexResult(slotIndex)) {
            // Result
            // This is executed on both the client and server for each craft. If the crafting
            // grid has multiple, complete recipes, this will be executed for each complete
            // recipe.
            IArtisanRecipe recipe = this.tile.getRecipe(playerIn);
            if (recipe == null) {
                return ItemStack.EMPTY;
            }
            if (recipe.hasMultipleWeightedOutputs()) {
                // it has been retrieved.
                return ItemStack.EMPTY;
            }
            if (!this.mergeInventory(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
                return ItemStack.EMPTY;
            }
            itemStack.getItem().onCreated(itemStack, this.world, playerIn);
            slot.onSlotChange(itemStack, itemStackCopy);
        } else if (this.isSlotIndexInventory(slotIndex)) {
            if (this.swapTools(slotIndex)) {
                // swapped tools
                return ItemStack.EMPTY;
            }
            if (!this.mergeCraftingMatrix(itemStack, false) && !this.mergeSecondaryInput(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
                return ItemStack.EMPTY;
            }
        } else if (this.isSlotIndexHotbar(slotIndex)) {
            if (this.swapTools(slotIndex)) {
                // swapped tools
                return ItemStack.EMPTY;
            }
            if (!this.mergeCraftingMatrix(itemStack, false) && !this.mergeSecondaryInput(itemStack, false) && !this.mergeInventory(itemStack, false)) {
                return ItemStack.EMPTY;
            }
        } else if (this.isSlotIndexToolbox(slotIndex)) {
            if (this.swapTools(slotIndex)) {
                // swapped tools
                return ItemStack.EMPTY;
            }
            if (!this.mergeCraftingMatrix(itemStack, false) && !this.mergeSecondaryInput(itemStack, false) && !this.mergeInventory(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
                return ItemStack.EMPTY;
            }
        } else if (this.isSlotIndexTool(slotIndex)) {
            if (this.canPlayerUseToolbox()) {
                if (!this.mergeToolbox(itemStack, false) && !this.mergeInventory(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
                    return ItemStack.EMPTY;
                }
            } else if (!this.mergeInventory(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
                return ItemStack.EMPTY;
            }
        } else if (!this.mergeInventory(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
            // All others: crafting matrix, secondary output
            return ItemStack.EMPTY;
        }
        if (itemStack.isEmpty()) {
            slot.putStack(ItemStack.EMPTY);
        } else {
            slot.onSlotChanged();
        }
        if (itemStack.getCount() == itemStackCopy.getCount()) {
            return ItemStack.EMPTY;
        }
        ItemStack itemStack2 = slot.onTake(playerIn, itemStack);
        if (slotIndex == 0) {
            playerIn.dropItem(itemStack2, false);
        }
    }
    return itemStackCopy;
}
Also used : IArtisanRecipe(com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe) Slot(net.minecraft.inventory.Slot) ItemStack(net.minecraft.item.ItemStack)

Example 4 with IArtisanRecipe

use of com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe in project artisan-worktables by codetaylor.

the class Container method updateRecipeOutput.

public void updateRecipeOutput() {
    if (this.tile == null) {
        return;
    }
    IArtisanRecipe recipe = this.tile.getRecipe(this.player);
    if (recipe != null) {
        ICraftingContext context = this.tile.getCraftingContext(this.player);
        this.resultHandler.setStackInSlot(0, recipe.getBaseOutput(context).toItemStack());
    } else {
        this.resultHandler.setStackInSlot(0, ItemStack.EMPTY);
    }
}
Also used : ICraftingContext(com.codetaylor.mc.artisanworktables.api.internal.recipe.ICraftingContext) IArtisanRecipe(com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe)

Example 5 with IArtisanRecipe

use of com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe in project artisan-worktables by codetaylor.

the class PluginJEI method register.

@Override
public void register(IModRegistry registry) {
    for (EnumTier tier : EnumTier.values()) {
        if (!ModuleWorktablesConfig.isTierEnabled(tier)) {
            continue;
        }
        for (String name : ArtisanAPI.getWorktableNames()) {
            registry.addRecipeCatalyst(this.getWorktableAsItemStack(name, tier), PluginJEI.createUID(name, tier));
        }
        for (String name : ArtisanAPI.getWorktableNames()) {
            registry.handleRecipes(ArtisanRecipe.class, JEIRecipeWrapper::new, PluginJEI.createUID(name, tier));
        }
        IRecipeTransferRegistry transferRegistry = registry.getRecipeTransferRegistry();
        for (String name : ArtisanAPI.getWorktableNames()) {
            transferRegistry.addRecipeTransferHandler(new JEIRecipeTransferInfoWorktable(name, PluginJEI.createUID(name, tier), tier));
        }
        for (String name : ArtisanAPI.getWorktableNames()) {
            List<IArtisanRecipe> recipeList = new ArrayList<>();
            RecipeRegistry recipeRegistry = ArtisanAPI.getWorktableRecipeRegistry(name);
            recipeList = recipeRegistry.getRecipeListByTier(recipeList, tier);
            registry.addRecipes(recipeList, PluginJEI.createUID(name, tier));
        }
    }
}
Also used : IArtisanRecipe(com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe) ArrayList(java.util.ArrayList) EnumTier(com.codetaylor.mc.artisanworktables.api.internal.reference.EnumTier) IRecipeTransferRegistry(mezz.jei.api.recipe.transfer.IRecipeTransferRegistry) IRecipeRegistry(mezz.jei.api.IRecipeRegistry) RecipeRegistry(com.codetaylor.mc.artisanworktables.api.internal.recipe.RecipeRegistry)

Aggregations

IArtisanRecipe (com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe)6 RecipeRegistry (com.codetaylor.mc.artisanworktables.api.internal.recipe.RecipeRegistry)2 EnumTier (com.codetaylor.mc.artisanworktables.api.internal.reference.EnumTier)2 ICraftingContext (com.codetaylor.mc.artisanworktables.api.internal.recipe.ICraftingContext)1 IRequirement (com.codetaylor.mc.artisanworktables.api.recipe.requirement.IRequirement)1 GameStagesRequirementContext (com.codetaylor.mc.artisanworktables.modules.requirement.gamestages.requirement.GameStagesRequirementContext)1 ArrayList (java.util.ArrayList)1 Nullable (javax.annotation.Nullable)1 IRecipeRegistry (mezz.jei.api.IRecipeRegistry)1 IRecipeWrapper (mezz.jei.api.recipe.IRecipeWrapper)1 IRecipeTransferRegistry (mezz.jei.api.recipe.transfer.IRecipeTransferRegistry)1 EntityPlayerSP (net.minecraft.client.entity.EntityPlayerSP)1 Slot (net.minecraft.inventory.Slot)1 ItemStack (net.minecraft.item.ItemStack)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)1