Add recipe types for faster runtime iteration. (#5312)

This commit is contained in:
Brennan Ward authored and LexManos committed 2019-01-25 10:22:41 -08:00
1 parent 7c11fe1022
commit a15c4dc9b0
15 files changed
+390 -13

No files matched your search

@@ -0,0 +1,79 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2018.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.common.crafting;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import org.apache.logging.log4j.LogManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.util.ResourceLocation;
public class RecipeType<T extends IRecipe>
{
private static final Map<ResourceLocation, RecipeType<?>> TYPES = new HashMap<>();
protected final ResourceLocation id;
protected final Class<? extends T> baseClass;
private RecipeType(ResourceLocation id, Class<? extends T> baseClass) {
this.id = id;
this.baseClass = baseClass;
}
public ResourceLocation getId()
{
return id;
}
/**
* @return The class that all recipes of this type must extend.
*/
public Class<? extends T> getBaseClass()
{
return baseClass;
}
/**
* @param id The name of the recipe type.
* @param baseClass The base class of all recipes using this type.
* @return A recipe type, with the provided ID and base class, or null, if the entry exists and the base class is incorrect.
*/
@SuppressWarnings("unchecked")
@Nullable
public static <T extends IRecipe> RecipeType<T> get(ResourceLocation id, Class<? extends T> baseClass)
{
RecipeType<?> type = TYPES.get(id);
if(type == null)
{
type = new RecipeType<>(id, baseClass);
TYPES.put(id, type);
}
else if(type.getBaseClass() != baseClass)
{
LogManager.getLogger().error("Attempted to access RecipeType {} with the wrong base class. Provided {}, expected {}.");
return null;
}
return (RecipeType<T>) type;
}
}
@@ -0,0 +1,38 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2018.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.common.crafting;
import net.minecraft.item.crafting.FurnaceRecipe;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.util.ResourceLocation;
public final class VanillaRecipeTypes
{
/**
* Used for normal crafting.
*/
public static final RecipeType<IRecipe> CRAFTING = RecipeType.get(new ResourceLocation("crafting"), IRecipe.class);
/**
* Used for furnace recipes.
*/
public static final RecipeType<FurnaceRecipe> SMELTING = RecipeType.get(new ResourceLocation("smelting"), FurnaceRecipe.class);
}
@@ -0,0 +1,88 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2018.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.common.extensions;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import com.google.common.collect.Maps;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.resources.IResourceManager;
import net.minecraft.resources.IResourceManagerReloadListener;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.common.crafting.RecipeType;
@SuppressWarnings("deprecation")
public abstract class ForgeRecipeManager implements IResourceManagerReloadListener
{
protected final Map<RecipeType<? extends IRecipe>, List<? extends IRecipe>> sortedRecipes = Maps.newHashMap();
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
this.sortedRecipes.clear();
CraftingHelper.reloadConstants(resourceManager);
}
@SuppressWarnings("unchecked")
public <T extends IRecipe> List<T> getRecipes(RecipeType<T> type)
{
return (List<T>) this.sortedRecipes.computeIfAbsent(type, t -> new ArrayList<>());
}
public ItemStack getResult(IInventory input, World world, RecipeType<?> type)
{
for(IRecipe irecipe : getRecipes(type))
if (irecipe.matches(input, world)) return irecipe.getCraftingResult(input);
return ItemStack.EMPTY;
}
@Nullable
public <T extends IRecipe> T getRecipe(IInventory input, World world, RecipeType<T> type)
{
for(T irecipe : getRecipes(type))
if (irecipe.matches(input, world)) return irecipe;
return null;
}
/**
* 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.
*/
public NonNullList<ItemStack> getRemainingItems(IInventory input, World world, RecipeType<?> type)
{
for(IRecipe irecipe : getRecipes(type))
if (irecipe.matches(input, world)) return irecipe.getRemainingItems(input);
NonNullList<ItemStack> nonnulllist = NonNullList.withSize(input.getSizeInventory(), ItemStack.EMPTY);
for(int i = 0; i < nonnulllist.size(); ++i)
nonnulllist.set(i, input.getStackInSlot(i));
return nonnulllist;
}
}
@@ -0,0 +1,39 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2018.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
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;
public interface IForgeRecipe {
/**
* Used when sorting this recipe into it's category during load, and in the default type matcher below.
* @return The type of this recipe.
*/
default RecipeType<? extends IRecipe> getType()
{
return VanillaRecipeTypes.CRAFTING;
}
}