Merge pull request #1881 from ganymedes01/master

Added a brewing recipe registry system
This commit is contained in:
LexManos committed 2015-06-09 12:05:40 -07:00
commit f62ee5c4f5
9 files changed
+439 -6

No files matched your search

@@ -0,0 +1,40 @@
package net.minecraftforge.common.brewing;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
public abstract class AbstractBrewingRecipe<T> implements IBrewingRecipe {
public final ItemStack input;
public final T ingredient;
public final ItemStack output;
protected AbstractBrewingRecipe(ItemStack input, T ingredient, ItemStack output)
{
this.input = input;
this.ingredient = ingredient;
this.output = output;
if (this.input == null || this.ingredient == null || this.output == null)
{
throw new IllegalArgumentException("A brewing recipe cannot have a null parameter.");
}
if (this.input.getMaxStackSize() != 1)
{
throw new IllegalArgumentException("Inputs must have a max size of 1 just like water bottles. Brewing Stands override the input with the output when the brewing is done, items that stack would end up getting lost.");
}
}
@Override
public boolean isInput(ItemStack stack)
{
return OreDictionary.itemMatches(this.input, stack, false);
}
@Override
public ItemStack getOutput(ItemStack input, ItemStack ingredient)
{
return isInput(input) && isIngredient(ingredient) ? ItemStack.copyItemStack(output) : null;
}
}
@@ -0,0 +1,32 @@
package net.minecraftforge.common.brewing;
import java.util.List;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
public class BrewingOreRecipe extends AbstractBrewingRecipe<List<ItemStack>> {
public BrewingOreRecipe(ItemStack input, String ingredient, ItemStack output)
{
super(input, OreDictionary.getOres(ingredient), output);
}
public BrewingOreRecipe(ItemStack input, List<ItemStack> ingredient, ItemStack output)
{
super(input, ingredient, output);
}
@Override
public boolean isIngredient(ItemStack stack)
{
for (ItemStack target : this.ingredient)
{
if (OreDictionary.itemMatches(target, stack, false))
{
return true;
}
}
return false;
}
}
@@ -0,0 +1,18 @@
package net.minecraftforge.common.brewing;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
public class BrewingRecipe extends AbstractBrewingRecipe<ItemStack> {
public BrewingRecipe(ItemStack input, ItemStack ingredient, ItemStack output)
{
super(input, ingredient, output);
}
@Override
public boolean isIngredient(ItemStack stack)
{
return OreDictionary.itemMatches(this.ingredient, stack, false);
}
}
@@ -0,0 +1,171 @@
package net.minecraftforge.common.brewing;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.minecraft.item.ItemStack;
public class BrewingRecipeRegistry {
private static List<IBrewingRecipe> recipes = new ArrayList<IBrewingRecipe>();
static
{
addRecipe(new VanillaBrewingRecipe());
}
/**
* Adds a recipe to the registry. Due to the nature of the brewing stand
* inputs that stack (a.k.a max stack size > 1) are not allowed.
*
* @param input
* The ItemStack that goes in same slots as the water bottles
* would.
* @param ingredient
* The ItemStack that goes in the same slot as nether wart would.
* @param output
* The ItemStack that will replace the input once the brewing is
* done.
* @return true if the recipe was added.
*/
public static boolean addRecipe(ItemStack input, ItemStack ingredient, ItemStack output)
{
return addRecipe(new BrewingRecipe(input, ingredient, output));
}
/**
* Adds a recipe to the registry. Due to the nature of the brewing stand
* inputs that stack (a.k.a max stack size > 1) are not allowed.
*
* @param input
* The ItemStack that goes in same slots as the water bottles
* would.
* @param ingredient
* The ItemStack that goes in the same slot as nether wart would.
* @param output
* The ItemStack that will replace the input once the brewing is
* done.
* @return true if the recipe was added.
*/
public static boolean addRecipe(ItemStack input, String ingredient, ItemStack output)
{
return addRecipe(new BrewingOreRecipe(input, ingredient, output));
}
/**
* Adds a recipe to the registry. Due to the nature of the brewing stand
* inputs that stack (a.k.a max stack size > 1) are not allowed.
*/
public static boolean addRecipe(IBrewingRecipe recipe)
{
return recipes.add(recipe);
}
/**
* Returns the output ItemStack obtained by brewing the passed input and
* ingredient. Null if no matches are found.
*/
public static ItemStack getOutput(ItemStack input, ItemStack ingredient)
{
if (input == null || input.getMaxStackSize() != 1 || input.stackSize != 1) return null;
if (ingredient == null || ingredient.stackSize <= 0) return null;
for (IBrewingRecipe recipe : recipes)
{
ItemStack output = recipe.getOutput(input, ingredient);
if (output != null)
{
return output;
}
}
return null;
}
/**
* Returns true if the passed input and ingredient have an output
*/
public static boolean hasOuput(ItemStack input, ItemStack ingredient)
{
return getOutput(input, ingredient) != null;
}
/**
* Used by the brewing stand to determine if its contents can be brewed.
* Extra parameters exist to allow modders to create bigger brewing stands
* without much hassle
*/
public static boolean canBrew(ItemStack[] inputs, ItemStack ingredient, int[] inputIndexes)
{
if (ingredient == null || ingredient.stackSize <= 0) return false;
for (int i : inputIndexes)
{
if (hasOuput(inputs[i], ingredient))
{
return true;
}
}
return false;
}
/**
* Used by the brewing stand to brew its inventory Extra parameters exist to
* allow modders to create bigger brewing stands without much hassle
*/
public static void brewPotions(ItemStack[] inputs, ItemStack ingredient, int[] inputIndexes)
{
for (int i : inputIndexes)
{
ItemStack output = getOutput(inputs[i], ingredient);
if (output != null)
{
inputs[i] = output;
}
}
}
/**
* Returns true if the passed ItemStack is a valid ingredient for any of the
* recipes in the registry.
*/
public static boolean isValidIngredient(ItemStack stack)
{
if (stack == null || stack.stackSize <= 0) return false;
for (IBrewingRecipe recipe : recipes)
{
if (recipe.isIngredient(stack))
{
return true;
}
}
return false;
}
/**
* Returns true if the passed ItemStack is a valid input for any of the
* recipes in the registry.
*/
public static boolean isValidInput(ItemStack stack)
{
if (stack == null || stack.getMaxStackSize() != 1 || stack.stackSize != 1) return false;
for (IBrewingRecipe recipe : recipes)
{
if (recipe.isInput(stack))
{
return true;
}
}
return false;
}
/**
* Returns an unmodifiable list containing all the recipes in the registry
*/
public static List<IBrewingRecipe> getRecipes()
{
return Collections.unmodifiableList(recipes);
}
}
@@ -0,0 +1,26 @@
package net.minecraftforge.common.brewing;
import net.minecraft.item.ItemStack;
public interface IBrewingRecipe {
/**
* Returns true is the passed ItemStack is an input for this recipe. "Input"
* being the item that goes in one of the three bottom slots of the brewing
* stand (e.g: water bottle)
*/
public boolean isInput(ItemStack input);
/**
* Returns true if the passed ItemStack is an ingredient for this recipe.
* "Ingredient" being the item that goes in the top slot of the brewing
* stand (e.g: nether wart)
*/
public boolean isIngredient(ItemStack ingredient);
/**
* Returns the output when the passed input is brewed with the passed
* ingredient. Null if invalid input or ingredient.
*/
public ItemStack getOutput(ItemStack input, ItemStack ingredient);
}
@@ -0,0 +1,75 @@
package net.minecraftforge.common.brewing;
import java.util.List;
import net.minecraft.init.Items;
import net.minecraft.item.ItemPotion;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionHelper;
/**
* Used in BrewingRecipeRegistry to maintain the vanilla behaviour.
*
* Most of the code was simply adapted from net.minecraft.tileentity.TileEntityBrewingStand
*/
public class VanillaBrewingRecipe implements IBrewingRecipe {
/**
* Code adapted from TileEntityBrewingStand.isItemValidForSlot(int index, ItemStack stack)
*/
@Override
public boolean isInput(ItemStack stack)
{
return stack.getItem() instanceof ItemPotion || stack.getItem() == Items.glass_bottle;
}
/**
* Code adapted from TileEntityBrewingStand.isItemValidForSlot(int index, ItemStack stack)
*/
@Override
public boolean isIngredient(ItemStack stack)
{
return stack.getItem().isPotionIngredient(stack);
}
/**
* Code copied from TileEntityBrewingStand.brewPotions()
* It brews the potion by doing the bit-shifting magic and then checking if the new PotionEffect list is different to the old one,
* or if the new potion is a splash potion when the old one wasn't.
*/
@Override
public ItemStack getOutput(ItemStack input, ItemStack ingredient)
{
if (ingredient != null && input != null && input.getItem() instanceof ItemPotion)
{
int inputMeta = input.getMetadata();
int outputMeta = PotionHelper.applyIngredient(inputMeta, ingredient.getItem().getPotionEffect(ingredient));
if (inputMeta == outputMeta)
{
return null;
}
List oldEffects = Items.potionitem.getEffects(inputMeta);
List newEffects = Items.potionitem.getEffects(outputMeta);
boolean hasResult = false;
if ((inputMeta <= 0 || oldEffects != newEffects) && (oldEffects == null || !oldEffects.equals(newEffects) && newEffects != null))
{
hasResult = true;
}
else if (!ItemPotion.isSplash(inputMeta) && ItemPotion.isSplash(outputMeta))
{
hasResult = true;
}
if (hasResult)
{
ItemStack output = input.copy();
output.setItemDamage(outputMeta);
return output;
}
}
return null;
}
}