Applied-Energistics-2-tiler.../src/main/java/appeng/recipes/Ingredient.java

276 lines
6.9 KiB
Java
Raw Normal View History

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>.
*/
package appeng.recipes;
import java.util.List;
import com.google.common.base.Preconditions;
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;
import net.minecraftforge.oredict.OreDictionary;
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;
import appeng.api.recipes.ResolverResult;
import appeng.api.recipes.ResolverResultSet;
2014-02-20 00:33:36 +01:00
public class Ingredient implements IIngredient
{
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;
2015-09-30 14:24:40 +02:00
public Ingredient( final RecipeHandler handler, final String input, final int qty ) throws RecipeError, MissedIngredientSet
{
Preconditions.checkNotNull( handler );
Preconditions.checkNotNull( input );
Preconditions.checkState( qty > 0 );
// works no matter wat!
this.qty = qty;
if( input.equals( "_" ) )
{
2014-12-29 15:13:47 +01:00
this.isAir = true;
this.nameSpace = "";
this.itemName = "";
this.meta = OreDictionary.WILDCARD_VALUE;
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( ":" );
if( parts.length >= 2 )
{
2014-12-29 15:13:47 +01:00
this.nameSpace = handler.alias( parts[0] );
String tmpName = handler.alias( parts[1] );
if( parts.length != 3 )
{
int sel = 0;
if( this.nameSpace.equals( "oreDictionary" ) )
{
if( parts.length == 3 )
2015-04-29 02:30:53 +02:00
{
throw new RecipeError( "Cannot specify meta when using ore dictionary." );
2015-04-29 02:30:53 +02:00
}
sel = OreDictionary.WILDCARD_VALUE;
}
else
{
try
{
2015-09-30 14:24:40 +02:00
final Object ro = AEApi.instance().registries().recipes().resolveItem( this.nameSpace, tmpName );
if( ro instanceof ResolverResult )
{
2015-09-30 14:24:40 +02:00
final ResolverResult rr = (ResolverResult) ro;
tmpName = rr.itemName;
sel = rr.damageValue;
2014-12-29 15:13:47 +01:00
this.nbt = rr.compound;
}
else if( ro instanceof ResolverResultSet )
{
throw new MissedIngredientSet( (ResolverResultSet) ro );
}
}
2015-09-30 14:24:40 +02:00
catch( final IllegalArgumentException e )
{
2014-09-21 02:13:15 +02:00
throw new RecipeError( tmpName + " is not a valid ae2 item definition." );
}
}
2014-12-29 15:13:47 +01:00
this.meta = sel;
}
else
{
if( parts[2].equals( "*" ) )
{
2014-12-29 15:13:47 +01:00
this.meta = OreDictionary.WILDCARD_VALUE;
}
else
{
try
{
2014-12-29 15:13:47 +01:00
this.meta = Integer.parseInt( parts[2] );
}
2015-09-30 14:24:40 +02:00
catch( final NumberFormatException e )
{
throw new RecipeError( "Invalid Metadata." );
}
}
}
2014-12-29 15:13:47 +01:00
this.itemName = tmpName;
}
else
2015-04-29 02:30:53 +02:00
{
throw new RecipeError( input + " : Needs at least Namespace and Name." );
2015-04-29 02:30:53 +02:00
}
handler.getData().knownItem.add( this.toString() );
}
@Override
public String toString()
{
return this.nameSpace + ':' + this.itemName + ':' + this.meta;
}
2014-02-20 00:33:36 +01:00
@Override
public ItemStack getItemStack() throws RegistrationError, MissingIngredientError
{
if( this.isAir )
2015-04-29 02:30:53 +02:00
{
throw new RegistrationError( "Found blank item and expected a real item." );
2015-04-29 02:30:53 +02:00
}
if( this.nameSpace.equalsIgnoreCase( "oreDictionary" ) )
2015-04-29 02:30:53 +02: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-12-29 15:13:47 +01:00
Block blk = GameRegistry.findBlock( this.nameSpace, this.itemName );
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
}
if( blk != null )
{
2015-09-30 14:24:40 +02:00
final Item it = Item.getItemFromBlock( blk );
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-12-29 15:13:47 +01:00
Item it = GameRegistry.findItem( this.nameSpace, this.itemName );
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
}
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
}
/*
* 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-12-29 15:13:47 +01:00
throw new MissingIngredientError( "Unable to find item: " + this.toString() );
}
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
{
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
}
if( this.nameSpace.equalsIgnoreCase( "oreDictionary" ) )
{
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()] );
// clone and set qty.
for( int x = 0; x < set.length; x++ )
{
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;
set[x] = is;
}
if( set.length == 0 )
2015-04-29 02:30:53 +02:00
{
throw new MissingIngredientError( this.itemName + " - ore dictionary could not be resolved to any items." );
2015-04-29 02:30:53 +02:00
}
return set;
}
2014-12-29 15:13:47 +01:00
return new ItemStack[] { this.getItemStack() };
}
2014-02-20 00:33:36 +01:00
@Override
public String getNameSpace()
{
2014-12-29 15:13:47 +01:00
return this.nameSpace;
2014-02-20 00:33:36 +01:00
}
@Override
public String getItemName()
{
2014-12-29 15:13:47 +01:00
return this.itemName;
2014-02-20 00:33:36 +01:00
}
@Override
public int getDamageValue()
{
2014-12-29 15:13:47 +01:00
return this.meta;
2014-02-20 00:33:36 +01:00
}
@Override
public int getQty()
{
2014-12-29 15:13:47 +01:00
return this.qty;
2014-02-20 00:33:36 +01:00
}
2014-02-20 00:33:36 +01:00
@Override
public boolean isAir()
{
2014-12-29 15:13:47 +01:00
return this.isAir;
2014-02-20 00:33:36 +01:00
}
@Override
public void bake() throws RegistrationError, MissingIngredientError
{
2014-12-29 15:13:47 +01:00
this.baked = null;
this.baked = this.getItemStackSet();
}
}