2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
2015-05-17 23:52:15 +02:00
|
|
|
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
|
2014-11-14 12:02:52 +01:00
|
|
|
*
|
|
|
|
* Applied Energistics 2 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, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 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 Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
|
|
|
*/
|
|
|
|
|
2014-02-10 04:16:36 +01:00
|
|
|
package appeng.recipes;
|
|
|
|
|
2014-11-28 04:36:46 +01:00
|
|
|
|
2014-02-11 06:51:25 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2015-08-06 22:45:16 +02:00
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
|
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;
|
2015-06-16 02:44:59 +02:00
|
|
|
import net.minecraftforge.fml.common.registry.GameRegistry;
|
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-10 04:16:36 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-02-20 00:33:36 +01:00
|
|
|
public class Ingredient implements IIngredient
|
2014-02-10 04:16:36 +01:00
|
|
|
{
|
|
|
|
|
2015-08-06 22:45:16 +02:00
|
|
|
private final boolean isAir;
|
|
|
|
private final String nameSpace;
|
|
|
|
private final String itemName;
|
|
|
|
private final int meta;
|
|
|
|
private final int qty;
|
|
|
|
private NBTTagCompound nbt = null;
|
|
|
|
private ItemStack[] baked;
|
2014-07-26 02:31:12 +02:00
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
public Ingredient( final RecipeHandler handler, final String input, final int qty ) throws RecipeError, MissedIngredientSet
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
2015-08-06 22:45:16 +02:00
|
|
|
Preconditions.checkNotNull( handler );
|
|
|
|
Preconditions.checkNotNull( input );
|
|
|
|
Preconditions.checkState( qty > 0 );
|
2014-02-11 06:51:25 +01:00
|
|
|
|
|
|
|
// works no matter wat!
|
|
|
|
this.qty = qty;
|
2014-02-10 04:16:36 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( input.equals( "_" ) )
|
2014-02-10 04:16:36 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.isAir = true;
|
|
|
|
this.nameSpace = "";
|
|
|
|
this.itemName = "";
|
|
|
|
this.meta = OreDictionary.WILDCARD_VALUE;
|
2014-02-10 04:16:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.isAir = false;
|
2015-09-30 14:24:40 +02:00
|
|
|
final String[] parts = input.split( ":" );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( parts.length >= 2 )
|
2014-02-10 04:16:36 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.nameSpace = handler.alias( parts[0] );
|
2014-02-10 04:16:36 +01:00
|
|
|
String tmpName = handler.alias( parts[1] );
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( parts.length != 3 )
|
2014-02-10 04:16:36 +01:00
|
|
|
{
|
|
|
|
int sel = 0;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.nameSpace.equals( "oreDictionary" ) )
|
2014-02-10 04:16:36 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( parts.length == 3 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-02-10 04:16:36 +01:00
|
|
|
throw new RecipeError( "Cannot specify meta when using ore dictionary." );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-02-10 04:16:36 +01:00
|
|
|
sel = OreDictionary.WILDCARD_VALUE;
|
|
|
|
}
|
2014-02-25 06:33:31 +01:00
|
|
|
else
|
2014-02-10 04:16:36 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final Object ro = AEApi.instance().registries().recipes().resolveItem( this.nameSpace, tmpName );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( ro instanceof ResolverResult )
|
2014-02-10 04:16:36 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final ResolverResult rr = (ResolverResult) ro;
|
2014-02-25 06:33:31 +01:00
|
|
|
tmpName = rr.itemName;
|
|
|
|
sel = rr.damageValue;
|
2014-12-29 15:13:47 +01:00
|
|
|
this.nbt = rr.compound;
|
2014-02-10 04:16:36 +01:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
else if( ro instanceof ResolverResultSet )
|
2014-03-10 03:11:29 +01:00
|
|
|
{
|
|
|
|
throw new MissedIngredientSet( (ResolverResultSet) ro );
|
|
|
|
}
|
2014-02-10 04:16:36 +01:00
|
|
|
}
|
2015-09-30 14:24:40 +02:00
|
|
|
catch( final IllegalArgumentException e )
|
2014-02-10 04:16:36 +01:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.meta = sel;
|
2014-02-10 04:16:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( parts[2].equals( "*" ) )
|
2014-02-10 04:16:36 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.meta = OreDictionary.WILDCARD_VALUE;
|
2014-02-10 04:16:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.meta = Integer.parseInt( parts[2] );
|
2014-02-10 04:16:36 +01:00
|
|
|
}
|
2015-09-30 14:24:40 +02:00
|
|
|
catch( final NumberFormatException e )
|
2014-02-10 04:16:36 +01:00
|
|
|
{
|
|
|
|
throw new RecipeError( "Invalid Metadata." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-29 15:13:47 +01:00
|
|
|
this.itemName = tmpName;
|
2014-02-10 04:16:36 +01:00
|
|
|
}
|
|
|
|
else
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-02-10 04:16:36 +01:00
|
|
|
throw new RecipeError( input + " : Needs at least Namespace and Name." );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-07-26 02:31:12 +02:00
|
|
|
|
2015-08-06 22:45:16 +02:00
|
|
|
handler.getData().knownItem.add( this.toString() );
|
2014-02-10 04:16:36 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@Override
|
|
|
|
public String toString()
|
|
|
|
{
|
|
|
|
return this.nameSpace + ':' + this.itemName + ':' + this.meta;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.isAir )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-02-10 04:16:36 +01:00
|
|
|
throw new RegistrationError( "Found blank item and expected a real item." );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-02-10 04:16:36 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.nameSpace.equalsIgnoreCase( "oreDictionary" ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-02-11 06:51:25 +01:00
|
|
|
throw new RegistrationError( "Recipe format expected a single item, but got a set of items." );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-02-11 06:51:25 +01:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
Block blk = GameRegistry.findBlock( this.nameSpace, this.itemName );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( blk == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
blk = GameRegistry.findBlock( this.nameSpace, "tile." + this.itemName );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-02-10 04:16:36 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( blk != null )
|
2014-03-18 04:58:20 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final Item it = Item.getItemFromBlock( blk );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( it != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-05-17 23:52:15 +02:00
|
|
|
return this.makeItemStack( it, this.qty, this.meta, this.nbt );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-03-18 04:58:20 +01:00
|
|
|
}
|
2014-02-10 04:16:36 +01:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
Item it = GameRegistry.findItem( this.nameSpace, this.itemName );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( it == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
it = GameRegistry.findItem( this.nameSpace, "item." + this.itemName );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-02-11 06:51:25 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( it != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-05-17 23:52:15 +02:00
|
|
|
return this.makeItemStack( it, this.qty, this.meta, this.nbt );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
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-12-29 15:13:47 +01:00
|
|
|
throw new MissingIngredientError( "Unable to find item: " + this.toString() );
|
2014-02-10 04:16:36 +01:00
|
|
|
}
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
private ItemStack makeItemStack( final Item it, final int quantity, final int damageValue, final NBTTagCompound compound )
|
2014-07-29 04:52:17 +02:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final ItemStack is = new ItemStack( it, quantity, damageValue );
|
2014-07-29 04:52:17 +02:00
|
|
|
is.setTagCompound( compound );
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
|
2014-02-20 00:33:36 +01:00
|
|
|
@Override
|
|
|
|
public ItemStack[] getItemStackSet() throws RegistrationError, MissingIngredientError
|
2014-02-11 06:51:25 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.baked != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.baked;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-07-26 02:31:12 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.nameSpace.equalsIgnoreCase( "oreDictionary" ) )
|
2014-02-11 06:51:25 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final List<ItemStack> ores = OreDictionary.getOres( this.itemName );
|
|
|
|
final ItemStack[] set = ores.toArray( new ItemStack[ores.size()] );
|
2014-02-11 06:51:25 +01:00
|
|
|
|
|
|
|
// clone and set qty.
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int x = 0; x < set.length; x++ )
|
2014-02-11 06:51:25 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final ItemStack is = set[x].copy();
|
2014-12-29 15:13:47 +01:00
|
|
|
is.stackSize = this.qty;
|
2014-02-11 06:51:25 +01:00
|
|
|
set[x] = is;
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( set.length == 0 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-03-26 10:23:59 +01:00
|
|
|
throw new MissingIngredientError( this.itemName + " - ore dictionary could not be resolved to any items." );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-02-25 06:33:31 +01:00
|
|
|
|
2014-02-11 06:51:25 +01:00
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
return new ItemStack[] { this.getItemStack() };
|
2014-02-11 06:51:25 +01:00
|
|
|
}
|
|
|
|
|
2014-02-20 00:33:36 +01:00
|
|
|
@Override
|
2014-02-25 06:33:31 +01:00
|
|
|
public String getNameSpace()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.nameSpace;
|
2014-02-20 00:33:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-25 06:33:31 +01:00
|
|
|
public String getItemName()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.itemName;
|
2014-02-20 00:33:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-25 06:33:31 +01:00
|
|
|
public int getDamageValue()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.meta;
|
2014-02-20 00:33:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-25 06:33:31 +01:00
|
|
|
public int getQty()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.qty;
|
2014-02-20 00:33:36 +01:00
|
|
|
}
|
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-12-29 15:13:47 +01:00
|
|
|
return this.isAir;
|
2014-02-20 00:33:36 +01:00
|
|
|
}
|
|
|
|
|
2014-07-26 00:49:49 +02:00
|
|
|
@Override
|
|
|
|
public void bake() throws RegistrationError, MissingIngredientError
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.baked = null;
|
|
|
|
this.baked = this.getItemStackSet();
|
2014-07-26 00:49:49 +02:00
|
|
|
}
|
2014-02-10 04:16:36 +01:00
|
|
|
}
|