2014-02-10 04:16:36 +01:00
|
|
|
package appeng.recipes;
|
|
|
|
|
2014-02-11 06:51:25 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2014-02-10 04:16:36 +01:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2014-07-29 04:52:17 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2014-02-10 04:16:36 +01:00
|
|
|
import net.minecraftforge.oredict.OreDictionary;
|
2014-02-25 06:33:31 +01:00
|
|
|
import appeng.api.AEApi;
|
2014-02-20 00:33:36 +01:00
|
|
|
import appeng.api.exceptions.MissingIngredientError;
|
|
|
|
import appeng.api.exceptions.RecipeError;
|
|
|
|
import appeng.api.exceptions.RegistrationError;
|
|
|
|
import appeng.api.recipes.IIngredient;
|
2014-03-10 03:11:29 +01:00
|
|
|
import appeng.api.recipes.ResolverResult;
|
|
|
|
import appeng.api.recipes.ResolverResultSet;
|
2014-02-11 06:51:25 +01:00
|
|
|
import cpw.mods.fml.common.registry.GameRegistry;
|
2014-02-10 04:16:36 +01:00
|
|
|
|
2014-02-20 00:33:36 +01:00
|
|
|
public class Ingredient implements IIngredient
|
2014-02-10 04:16:36 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
final public boolean isAir;
|
|
|
|
|
|
|
|
final public String nameSpace;
|
|
|
|
final public String itemName;
|
|
|
|
final public int meta;
|
2014-07-29 04:52:17 +02:00
|
|
|
NBTTagCompound nbt = null;
|
2014-02-10 04:16:36 +01:00
|
|
|
|
2014-02-11 06:51:25 +01:00
|
|
|
final public int qty;
|
|
|
|
|
2014-07-26 00:49:49 +02:00
|
|
|
ItemStack[] baked;
|
2014-07-26 02:31:12 +02:00
|
|
|
|
2014-03-10 03:11:29 +01:00
|
|
|
public Ingredient(RecipeHandler handler, String input, int qty) throws RecipeError, MissedIngredientSet {
|
2014-02-11 06:51:25 +01:00
|
|
|
|
|
|
|
// works no matter wat!
|
|
|
|
this.qty = qty;
|
2014-02-10 04:16:36 +01:00
|
|
|
|
|
|
|
if ( input.equals( "_" ) )
|
|
|
|
{
|
|
|
|
isAir = true;
|
|
|
|
nameSpace = "";
|
|
|
|
itemName = "";
|
|
|
|
meta = OreDictionary.WILDCARD_VALUE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isAir = false;
|
|
|
|
String[] parts = input.split( ":" );
|
|
|
|
if ( parts.length >= 2 )
|
|
|
|
{
|
|
|
|
nameSpace = handler.alias( parts[0] );
|
|
|
|
String tmpName = handler.alias( parts[1] );
|
|
|
|
|
|
|
|
if ( parts.length != 3 )
|
|
|
|
{
|
|
|
|
int sel = 0;
|
|
|
|
|
|
|
|
if ( nameSpace.equals( "oreDictionary" ) )
|
|
|
|
{
|
|
|
|
if ( parts.length == 3 )
|
|
|
|
throw new RecipeError( "Cannot specify meta when using ore dictionary." );
|
|
|
|
sel = OreDictionary.WILDCARD_VALUE;
|
|
|
|
}
|
2014-02-25 06:33:31 +01:00
|
|
|
else
|
2014-02-10 04:16:36 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-03-10 03:11:29 +01:00
|
|
|
Object ro = AEApi.instance().registries().recipes().resolveItem( nameSpace, tmpName );
|
|
|
|
if ( ro instanceof ResolverResult )
|
2014-02-10 04:16:36 +01:00
|
|
|
{
|
2014-03-10 03:11:29 +01:00
|
|
|
ResolverResult rr = (ResolverResult) ro;
|
2014-02-25 06:33:31 +01:00
|
|
|
tmpName = rr.itemName;
|
|
|
|
sel = rr.damageValue;
|
2014-07-29 04:52:17 +02:00
|
|
|
nbt = rr.compound;
|
2014-02-10 04:16:36 +01:00
|
|
|
}
|
2014-03-10 03:11:29 +01:00
|
|
|
else if ( ro instanceof ResolverResultSet )
|
|
|
|
{
|
|
|
|
throw new MissedIngredientSet( (ResolverResultSet) ro );
|
|
|
|
}
|
2014-02-10 04:16:36 +01:00
|
|
|
}
|
|
|
|
catch (IllegalArgumentException e)
|
|
|
|
{
|
2014-09-21 02:13:15 +02:00
|
|
|
throw new RecipeError( tmpName + " is not a valid ae2 item definition." );
|
2014-02-10 04:16:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
meta = sel;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( parts[2].equals( "*" ) )
|
|
|
|
{
|
|
|
|
meta = OreDictionary.WILDCARD_VALUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
meta = Integer.parseInt( parts[2] );
|
|
|
|
}
|
|
|
|
catch (NumberFormatException e)
|
|
|
|
{
|
|
|
|
throw new RecipeError( "Invalid Metadata." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
itemName = tmpName;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new RecipeError( input + " : Needs at least Namespace and Name." );
|
2014-07-26 02:31:12 +02:00
|
|
|
|
|
|
|
handler.data.knownItem.add( toString() );
|
2014-02-10 04:16:36 +01:00
|
|
|
}
|
|
|
|
|
2014-02-20 00:33:36 +01:00
|
|
|
@Override
|
2014-02-11 00:15:43 +01:00
|
|
|
public ItemStack getItemStack() throws RegistrationError, MissingIngredientError
|
2014-02-10 04:16:36 +01:00
|
|
|
{
|
|
|
|
if ( isAir )
|
|
|
|
throw new RegistrationError( "Found blank item and expected a real item." );
|
|
|
|
|
2014-02-11 06:51:25 +01:00
|
|
|
if ( nameSpace.equalsIgnoreCase( "oreDictionary" ) )
|
|
|
|
throw new RegistrationError( "Recipe format expected a single item, but got a set of items." );
|
|
|
|
|
|
|
|
Block blk = GameRegistry.findBlock( nameSpace, itemName );
|
|
|
|
if ( blk == null )
|
|
|
|
blk = GameRegistry.findBlock( nameSpace, "tile." + itemName );
|
2014-02-10 04:16:36 +01:00
|
|
|
|
2014-02-11 06:51:25 +01:00
|
|
|
if ( blk != null )
|
2014-03-18 04:58:20 +01:00
|
|
|
{
|
|
|
|
Item it = Item.getItemFromBlock( blk );
|
|
|
|
if ( it != null )
|
2014-07-29 04:52:17 +02:00
|
|
|
return MakeItemStack( it, qty, meta, nbt );
|
2014-03-18 04:58:20 +01:00
|
|
|
}
|
2014-02-10 04:16:36 +01:00
|
|
|
|
2014-02-11 06:51:25 +01:00
|
|
|
Item it = GameRegistry.findItem( nameSpace, itemName );
|
|
|
|
if ( it == null )
|
|
|
|
it = GameRegistry.findItem( nameSpace, "item." + itemName );
|
|
|
|
|
|
|
|
if ( it != null )
|
2014-07-29 04:52:17 +02:00
|
|
|
return MakeItemStack( it, qty, meta, nbt );
|
2014-02-11 06:51:25 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Object o = Item.itemRegistry.getObject( nameSpace + ":" + itemName ); if ( o instanceof Item ) return new
|
|
|
|
* ItemStack( (Item) o, qty, meta );
|
|
|
|
*
|
|
|
|
* if ( o instanceof Block ) return new ItemStack( (Block) o, qty, meta );
|
|
|
|
*
|
|
|
|
* o = Item.itemRegistry.getObject( nameSpace + ":item." + itemName ); if ( o instanceof Item ) return new
|
|
|
|
* ItemStack( (Item) o, qty, meta );
|
|
|
|
*
|
|
|
|
* o = Block.blockRegistry.getObject( nameSpace + ":tile." + itemName ); if ( o instanceof Block && (!(o
|
|
|
|
* instanceof BlockAir)) ) return new ItemStack( (Block) o, qty, meta );
|
|
|
|
*/
|
2014-02-10 04:16:36 +01:00
|
|
|
|
2014-02-11 00:15:43 +01:00
|
|
|
throw new MissingIngredientError( "Unable to find item: " + toString() );
|
2014-02-10 04:16:36 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 04:52:17 +02:00
|
|
|
private ItemStack MakeItemStack(Item it, int quantity, int damageValue, NBTTagCompound compound)
|
|
|
|
{
|
|
|
|
ItemStack is = new ItemStack( it, quantity, damageValue );
|
|
|
|
is.setTagCompound( compound );
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
|
2014-02-10 04:16:36 +01:00
|
|
|
@Override
|
|
|
|
public String toString()
|
|
|
|
{
|
|
|
|
return nameSpace + ":" + itemName + ":" + meta;
|
|
|
|
}
|
|
|
|
|
2014-02-20 00:33:36 +01:00
|
|
|
@Override
|
|
|
|
public ItemStack[] getItemStackSet() throws RegistrationError, MissingIngredientError
|
2014-02-11 06:51:25 +01:00
|
|
|
{
|
2014-07-26 00:49:49 +02:00
|
|
|
if ( baked != null )
|
|
|
|
return baked;
|
2014-07-26 02:31:12 +02:00
|
|
|
|
2014-02-11 06:51:25 +01:00
|
|
|
if ( nameSpace.equalsIgnoreCase( "oreDictionary" ) )
|
|
|
|
{
|
|
|
|
List<ItemStack> ores = OreDictionary.getOres( itemName );
|
|
|
|
ItemStack[] set = ores.toArray( new ItemStack[ores.size()] );
|
|
|
|
|
|
|
|
// clone and set qty.
|
|
|
|
for (int x = 0; x < set.length; x++)
|
|
|
|
{
|
|
|
|
ItemStack is = set[x].copy();
|
|
|
|
is.stackSize = qty;
|
|
|
|
set[x] = is;
|
|
|
|
}
|
|
|
|
|
2014-02-25 06:33:31 +01:00
|
|
|
if ( set.length == 0 )
|
2014-07-15 19:02:12 +02:00
|
|
|
throw new MissingIngredientError( getItemName() + " - ore dictionary could not be resolved to any items." );
|
2014-02-25 06:33:31 +01:00
|
|
|
|
2014-02-11 06:51:25 +01:00
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ItemStack[] { getItemStack() };
|
|
|
|
}
|
|
|
|
|
2014-02-20 00:33:36 +01:00
|
|
|
@Override
|
2014-02-25 06:33:31 +01:00
|
|
|
public String getNameSpace()
|
|
|
|
{
|
2014-02-20 00:33:36 +01:00
|
|
|
return nameSpace;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-25 06:33:31 +01:00
|
|
|
public String getItemName()
|
|
|
|
{
|
2014-02-20 00:33:36 +01:00
|
|
|
return itemName;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-25 06:33:31 +01:00
|
|
|
public int getDamageValue()
|
|
|
|
{
|
2014-02-20 00:33:36 +01:00
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-25 06:33:31 +01:00
|
|
|
public int getQty()
|
|
|
|
{
|
2014-02-20 00:33:36 +01:00
|
|
|
return qty;
|
|
|
|
}
|
2014-02-25 06:33:31 +01:00
|
|
|
|
2014-02-20 00:33:36 +01:00
|
|
|
@Override
|
2014-02-25 06:33:31 +01:00
|
|
|
public boolean isAir()
|
|
|
|
{
|
2014-02-20 00:33:36 +01:00
|
|
|
return isAir;
|
|
|
|
}
|
|
|
|
|
2014-07-26 00:49:49 +02:00
|
|
|
@Override
|
|
|
|
public void bake() throws RegistrationError, MissingIngredientError
|
|
|
|
{
|
2014-07-26 02:31:12 +02:00
|
|
|
baked = null;
|
2014-07-26 00:49:49 +02:00
|
|
|
baked = getItemStackSet();
|
|
|
|
}
|
2014-07-26 02:31:12 +02:00
|
|
|
|
2014-02-10 04:16:36 +01:00
|
|
|
}
|