Java Code Examples for net.minecraft.item.crafting.IRecipe#matches()

The following examples show how to use net.minecraft.item.crafting.IRecipe#matches() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModHandler.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static Pair<IRecipe, ItemStack> getRecipeOutput(World world, ItemStack... recipe) {
    if (recipe == null || recipe.length == 0)
        return ImmutablePair.of(null, ItemStack.EMPTY);

    if (world == null) world = DummyWorld.INSTANCE;

    InventoryCrafting craftingGrid = new InventoryCrafting(new DummyContainer(), 3, 3);

    for (int i = 0; i < 9 && i < recipe.length; i++) {
        ItemStack recipeStack = recipe[i];
        if (recipeStack != null && !recipeStack.isEmpty()) {
            craftingGrid.setInventorySlotContents(i, recipeStack);
        }
    }

    for (IRecipe tmpRecipe : CraftingManager.REGISTRY) {
        if (tmpRecipe.matches(craftingGrid, world)) {
            ItemStack itemStack = tmpRecipe.getCraftingResult(craftingGrid);
            return ImmutablePair.of(tmpRecipe, itemStack);
        }
    }

    return ImmutablePair.of(null, ItemStack.EMPTY);
}
 
Example 2
Source File: GTContainerAutocrafter.java    From GT-Classic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void checkForMatchingRecipes() {
	for (IRecipe recipe : ForgeRegistries.RECIPES) {
		ItemStack craftingOutput = recipe.getRecipeOutput().copy();
		// iterates the tiles ghost slots to the fake crafting inventory for matching
		for (int i = 18; i < 27; ++i) {
			this.fakeMatrix.setInventorySlotContents(i - 18, this.block.inventory.get(i).copy());
		}
		// if recipe matches set the output ghost slot to the recipe output
		if (recipe.matches(fakeMatrix, this.block.getWorld())) {
			this.block.currentRecipe.clear();
			List<ItemStack> tempList = new ArrayList<>();
			// condense stacks and remove empty stacks in raw resource demands
			for (int j = 0; j < fakeMatrix.getSizeInventory(); ++j) {
				tempList.add((fakeMatrix.getStackInSlot(j).copy()));
			}
			this.block.setStackInSlot(27, craftingOutput);
			GTHelperStack.mergeItems(this.block.currentRecipe, tempList);
			return;
			// else then set the output slot to air
		} else {
			this.block.setStackInSlot(27, ItemStack.EMPTY);
			this.block.currentRecipe.clear();
		}
	}
}
 
Example 3
Source File: CraftingManagerTFC.java    From TFC2 with GNU General Public License v3.0 6 votes vote down vote up
public NonNullList<ItemStack> getRemainingItems(InventoryCrafting craftMatrix, World worldIn)
{
	for (IRecipe irecipe : this.recipes)
	{
		if (irecipe.matches(craftMatrix, worldIn))
		{
			return irecipe.getRemainingItems(craftMatrix);
		}
	}

	NonNullList<ItemStack> nonnulllist = NonNullList.<ItemStack>withSize(craftMatrix.getSizeInventory(), ItemStack.EMPTY);

	/*for (int i = 0; i < nonnulllist.size(); ++i)
	{
		nonnulllist.set(i, craftMatrix.getStackInSlot(i));
	}*/

	return nonnulllist;
}
 
Example 4
Source File: TofuEventLoader.java    From TofuCraftReload with MIT License 5 votes vote down vote up
@SubscribeEvent
public void onCrafting(PlayerEvent.ItemCraftedEvent event) {
       EntityPlayer player = event.player;
       ItemStack item = event.crafting;
	IInventory craftMatrix = event.craftMatrix;
	
	if(craftMatrix instanceof InventoryCrafting){
	InventoryCrafting craftMatrix1 = (InventoryCrafting) craftMatrix;
	IRecipe recipe = ForgeRegistries.RECIPES.getValue(new ResourceLocation(TofuMain.MODID, "soymilk_cloth"));
	if(recipe!=null){
		if(!item.isEmpty()&&recipe.matches(craftMatrix1, player.world))
			player.inventory.addItemStackToInventory(new ItemStack(ItemLoader.material,1,11));
		}
	}
}
 
Example 5
Source File: CraftingManagerCS4.java    From customstuff4 with GNU General Public License v3.0 5 votes vote down vote up
public static ItemStack findMatchingRecipe(Iterable<IRecipe> recipes, InventoryCrafting craftMatrix, World worldIn)
{
    for (IRecipe irecipe : recipes)
    {
        if (irecipe.matches(craftMatrix, worldIn))
        {
            return irecipe.getCraftingResult(craftMatrix);
        }
    }

    return ItemStack.EMPTY;
}
 
Example 6
Source File: ProgWidgetCrafting.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
public static IRecipe getRecipe(World world, ICraftingWidget widget){
    InventoryCrafting craftingGrid = widget.getCraftingGrid();
    for(IRecipe recipe : (List<IRecipe>)CraftingManager.getInstance().getRecipeList()) {
        if(recipe.matches(craftingGrid, world)) {
            return recipe;
        }
    }
    return null;
}
 
Example 7
Source File: BWCoreStaticReplacementMethodes.java    From bartworks with MIT License 4 votes vote down vote up
@SuppressWarnings("ALL")
public static ItemStack findCachedMatchingRecipe(InventoryCrafting inventoryCrafting, World world) {
    int i = 0;
    ItemStack itemstack = null;
    ItemStack itemstack1 = null;
    int j;

    for (j = 0; j < inventoryCrafting.getSizeInventory(); ++j)
    {
        ItemStack itemstack2 = inventoryCrafting.getStackInSlot(j);

        if (itemstack2 != null)
        {
            if (i == 0)
            {
                itemstack = itemstack2;
            }

            if (i == 1)
            {
                itemstack1 = itemstack2;
            }

            ++i;
        }
    }

    if (i == 2 && itemstack.getItem() == itemstack1.getItem() && itemstack.stackSize == 1 && itemstack1.stackSize == 1 && itemstack.getItem().isRepairable())
    {
        Item item = itemstack.getItem();
        int j1 = item.getMaxDamage() - itemstack.getItemDamageForDisplay();
        int k = item.getMaxDamage() - itemstack1.getItemDamageForDisplay();
        int l = j1 + k + item.getMaxDamage() * 5 / 100;
        int i1 = item.getMaxDamage() - l;

        if (i1 < 0)
        {
            i1 = 0;
        }

        return new ItemStack(itemstack.getItem(), 1, i1);
    } else {
        IRecipe iPossibleRecipe = null;
        int index = 0;
        for (Iterator<IRecipe> it = RECENTLYUSEDRECIPES.iterator(); it.hasNext(); ++index) {
            IRecipe RECENTLYUSEDRECIPE = it.next();
            if (RECENTLYUSEDRECIPE.matches(inventoryCrafting, world)) {
                iPossibleRecipe = RECENTLYUSEDRECIPE;
                break;
            }
        }

        if (iPossibleRecipe != null) {
            RECENTLYUSEDRECIPES.addPrioToNode(index);
            return iPossibleRecipe.getCraftingResult(inventoryCrafting);
        }

        ItemStack stack = null;

        HashSet<IRecipe> recipeSet = new NonNullWrappedHashSet<>();
        List recipeList = CraftingManager.getInstance().getRecipeList();

        for (int k = 0; k < recipeList.size(); k++) {
            recipeSet.add((IRecipe) recipeList.get(k));
        }

        Object[] arr = recipeSet.parallelStream().filter(r -> r.matches(inventoryCrafting, world)).toArray();

        if (arr.length == 0)
            return null;

        IRecipe recipe = (IRecipe) arr[0];
        stack = recipe.getCraftingResult(inventoryCrafting);

        if (arr.length != 1)
            return stack;

        if (stack != null)
            RECENTLYUSEDRECIPES.addLast(recipe);

        return stack;
    }
}
 
Example 8
Source File: SawmillContainer.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean matches(IRecipe<? super ChoppingContext> recipeIn)
{
    return recipeIn.matches(this.wrappedInventory, this.world);
}
 
Example 9
Source File: ProgWidgetCrafting.java    From PneumaticCraft with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean shouldExecute(){
    IRecipe recipe = ProgWidgetCrafting.getRecipe(drone.getWorld(), widget);
    if(recipe == null) return false;
    InventoryCrafting craftingGrid = widget.getCraftingGrid();
    for(int crafted = 0; !((ICountWidget)widget).useCount() || crafted < ((ICountWidget)widget).getCount(); crafted++) {
        List<ItemStack>[] equivalentsList = new List[9];
        for(int i = 0; i < equivalentsList.length; i++) {
            ItemStack originalStack = craftingGrid.getStackInSlot(i);
            if(originalStack != null) {
                List<ItemStack> equivalents = new ArrayList<ItemStack>();
                for(int j = 0; j < drone.getInventory().getSizeInventory(); j++) {
                    ItemStack droneStack = drone.getInventory().getStackInSlot(j);
                    if(droneStack != null && (droneStack.getItem() == originalStack.getItem() || PneumaticCraftUtils.isSameOreDictStack(droneStack, originalStack))) {
                        equivalents.add(droneStack);
                    }
                }
                if(equivalents.isEmpty()) return false;
                equivalentsList[i] = equivalents;
            }
        }

        int[] curIndexes = new int[9];
        boolean first = true;
        boolean hasCrafted = false;
        while(first || count(curIndexes, equivalentsList)) {
            first = false;
            InventoryCrafting craftMatrix = new InventoryCrafting(new Container(){
                @Override
                public boolean canInteractWith(EntityPlayer p_75145_1_){
                    return false;
                }

            }, 3, 3);
            for(int i = 0; i < 9; i++) {
                ItemStack stack = equivalentsList[i] == null ? null : equivalentsList[i].get(curIndexes[i]);
                craftMatrix.setInventorySlotContents(i, stack);
            }
            if(recipe.matches(craftMatrix, drone.getWorld())) {
                if(craft(recipe.getCraftingResult(craftMatrix), craftMatrix)) {
                    hasCrafted = true;
                    break;
                }
            }
        }
        if(!hasCrafted) return false;
    }
    return false;
}