Add class to wrap IItemHandler as IInventory for easier recipe interaction. (#5522)

This commit is contained in:
Brennan Ward authored and LexManos committed 2019-03-25 12:31:54 -07:00
1 parent 5fa732d223
commit 36470f69ad
3 files changed
+190 -5

No files matched your search

@@ -28,6 +28,7 @@ import javax.annotation.Nullable;
import com.google.common.collect.Maps;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.resources.IResourceManager;
@@ -36,24 +37,42 @@ import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.common.crafting.RecipeType;
import net.minecraftforge.items.wrapper.RecipeWrapper;
@SuppressWarnings("deprecation")
public abstract class ForgeRecipeManager implements IResourceManagerReloadListener
{
/**
* Master list of sorted recipes.
*/
protected final Map<RecipeType<? extends IRecipe>, List<? extends IRecipe>> sortedRecipes = Maps.newHashMap();
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
public void onResourceManagerReload(IResourceManager resourceManager)
{
this.sortedRecipes.clear();
CraftingHelper.reloadConstants(resourceManager);
}
/**
* Returns all the recipes that match this type.
* @param type A recipe type.
* @return All recipes that are of the passed type. This list may be empty.
*/
@SuppressWarnings("unchecked")
public <T extends IRecipe> List<T> getRecipes(RecipeType<T> type)
{
return (List<T>) this.sortedRecipes.computeIfAbsent(type, t -> new ArrayList<>());
}
/**
* Automatically returns a result from the first recipe that matches this inv and type.
* Modders should probably not use this, and instead use {@link #getRecipe(IInventory, World, RecipeType)} since you get the recipe object itself.
* @param input An inventory.
* @param world The world.
* @param type The type of recipe to match against.
* @return The output of the first matching recipe, or {@link ItemStack#EMPTY} if no recipes match.
*/
public ItemStack getResult(IInventory input, World world, RecipeType<?> type)
{
for(IRecipe irecipe : getRecipes(type))
@@ -61,7 +80,16 @@ public abstract class ForgeRecipeManager implements IResourceManagerReloadListen
return ItemStack.EMPTY;
}
@Nullable
/**
* Used to find a matching recipe for the given inv, world, and type.
* Since this requires an IInventory, modders can use {@link RecipeWrapper} to avoid direct implementation.
* For crafting recipes, it is advised to still use {@link InventoryCrafting}. The dynamic vanilla recipes require they be crafted in one.
* @param input An inventory.
* @param world The world.
* @param type The type of recipe to match against.
* @return The first matching recipe.
*/
@Nullable
public <T extends IRecipe> T getRecipe(IInventory input, World world, RecipeType<T> type)
{
for(T irecipe : getRecipes(type))
@@ -71,7 +99,9 @@ public abstract class ForgeRecipeManager implements IResourceManagerReloadListen
/**
* Modders should not use this if possible. In the context that this is used, you should already have the recipe. Do not run extra lookups using this method.
* Use {@link IRecipe#getRemainingItems(IInventory)}.
*/
@Deprecated
public NonNullList<ItemStack> getRemainingItems(IInventory input, World world, RecipeType<?> type)
{
for(IRecipe irecipe : getRecipes(type))
@@ -19,8 +19,6 @@
package net.minecraftforge.common.extensions;
import java.util.Collection;
import net.minecraft.item.crafting.IRecipe;
import net.minecraftforge.common.crafting.RecipeType;
import net.minecraftforge.common.crafting.VanillaRecipeTypes;
@@ -28,7 +26,7 @@ import net.minecraftforge.common.crafting.VanillaRecipeTypes;
public interface IForgeRecipe {
/**
* Used when sorting this recipe into it's category during load, and in the default type matcher below.
* Used when sorting this recipe into it's category during load. This type determines what class the recipe must subclass.
* @return The type of this recipe.
*/
default RecipeType<? extends IRecipe> getType()