Closes #1054: Adds an Inscriber API for Developers

This commit is contained in:
thatsIch
2015-04-05 20:16:02 +02:00
parent 1bb8de3570
commit 06bca227d0
19 changed files with 904 additions and 187 deletions
@@ -30,6 +30,8 @@ import java.util.Map.Entry;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.annotation.Nonnull;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
@@ -63,6 +65,12 @@ import appeng.recipes.handlers.IWebsiteSerializer;
import appeng.recipes.handlers.OreRegistration;
/**
* @author AlgorithmX2
* @author thatsIch
* @version rv2
* @since rv0
*/
public class RecipeHandler implements IRecipeHandler
{
@@ -84,21 +92,18 @@ public class RecipeHandler implements IRecipeHandler
this.data.Handlers.add( ch );
}
public String getName( IIngredient i )
public String getName( @Nonnull IIngredient i )
{
try
{
for( ItemStack is : i.getItemStackSet() )
{
try
{
return this.getName( is );
}
catch( RecipeError ignored )
{
}
return this.getName( is );
}
}
catch( RecipeError ignored )
{
}
catch( Throwable t )
{
t.printStackTrace();
@@ -19,113 +19,48 @@
package appeng.recipes.handlers;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import net.minecraft.item.ItemStack;
import appeng.api.AEApi;
import appeng.api.exceptions.MissingIngredientError;
import appeng.api.exceptions.RecipeError;
import appeng.api.exceptions.RegistrationError;
import appeng.api.recipes.ICraftHandler;
import appeng.api.recipes.IIngredient;
import appeng.recipes.RecipeHandler;
import appeng.util.Platform;
import appeng.api.features.IInscriberRecipe;
import appeng.api.features.InscriberProcessType;
import appeng.core.features.registries.entries.InscriberRecipe;
public class Inscribe implements ICraftHandler, IWebsiteSerializer
/**
* recipe translation for inscribe process
*
* @author AlgorithmX2
* @author thatsIch
* @version rv2
* @since rv0
*/
public final class Inscribe extends InscriberProcess
{
public static final HashSet<ItemStack> PLATES = new HashSet<ItemStack>();
public static final HashSet<ItemStack> INPUTS = new HashSet<ItemStack>();
public static final LinkedList<InscriberRecipe> RECIPES = new LinkedList<InscriberRecipe>();
public boolean usePlates = false;
IIngredient imprintable;
IIngredient plateA;
IIngredient plateB;
IIngredient output;
@Override
public void setup( List<List<IIngredient>> input, List<List<IIngredient>> output ) throws RecipeError
{
if( output.size() == 1 && output.get( 0 ).size() == 1 )
{
if( input.size() == 1 && input.get( 0 ).size() > 1 )
{
this.imprintable = input.get( 0 ).get( 0 );
this.plateA = input.get( 0 ).get( 1 );
if( input.get( 0 ).size() > 2 )
this.plateB = input.get( 0 ).get( 2 );
this.output = output.get( 0 ).get( 0 );
}
else
throw new RecipeError( "Inscriber recipes cannot have rows, and must have more then one input." );
}
else
throw new RecipeError( "Inscriber recipes must produce a single output." );
}
@Override
public void register() throws RegistrationError, MissingIngredientError
{
if( this.imprintable != null )
Collections.addAll( INPUTS, this.imprintable.getItemStackSet() );
if ( this.getImprintable() == null )
return;
if ( this.getOutput() == null )
return;
if( this.plateA != null )
Collections.addAll( PLATES, this.plateA.getItemStackSet() );
final ItemStack[] realInput = this.getImprintable().getItemStackSet();
final List<ItemStack> inputs = new ArrayList<ItemStack>( realInput.length );
Collections.addAll( inputs, realInput );
final ItemStack top = (this.getTopOptional() == null) ? null : this.getTopOptional().getItemStack();
final ItemStack bot = (this.getBotOptional() == null) ? null : this.getBotOptional().getItemStack();
final ItemStack output = this.getOutput().getItemStack();
final InscriberProcessType type = InscriberProcessType.Inscribe;
if( this.plateB != null )
Collections.addAll( PLATES, this.plateB.getItemStackSet() );
IInscriberRecipe recipe = new InscriberRecipe( inputs, output, top, bot, type );
InscriberRecipe ir = new InscriberRecipe( this.imprintable.getItemStackSet(), this.plateA == null ? null : this.plateA.getItemStack(), this.plateB == null ? null : this.plateB.getItemStack(), this.output.getItemStack(), this.usePlates );
RECIPES.add( ir );
}
@Override
public String getPattern( RecipeHandler h )
{
String o = "inscriber " + this.output.getQty() + '\n';
o += h.getName( this.output ) + '\n';
if( this.plateA != null )
o += h.getName( this.plateA ) + '\n';
o += h.getName( this.imprintable );
if( this.plateB != null )
o += '\n' + h.getName( this.plateB );
return o;
}
@Override
public boolean canCraft( ItemStack reqOutput ) throws RegistrationError, MissingIngredientError
{
return Platform.isSameItemPrecise( this.output.getItemStack(), reqOutput );
}
public static class InscriberRecipe
{
public final boolean usePlates;
public final ItemStack plateA;
public final ItemStack[] imprintable;
public final ItemStack plateB;
public final ItemStack output;
public InscriberRecipe( ItemStack[] imprintable, ItemStack plateA, ItemStack plateB, ItemStack out, boolean usePlates )
{
this.imprintable = imprintable;
this.usePlates = usePlates;
this.plateA = plateA;
this.plateB = plateB;
this.output = out;
}
AEApi.instance().registries().inscriber().addRecipe( recipe );
}
}
@@ -0,0 +1,115 @@
package appeng.recipes.handlers;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.item.ItemStack;
import appeng.api.exceptions.MissingIngredientError;
import appeng.api.exceptions.RecipeError;
import appeng.api.exceptions.RegistrationError;
import appeng.api.recipes.ICraftHandler;
import appeng.api.recipes.IIngredient;
import appeng.recipes.RecipeHandler;
import appeng.util.Platform;
/**
* basic inscriber process for recipes
*
* @author AlgorithmX2
* @author thatsIch
* @version rv2
* @since rv0
*/
public abstract class InscriberProcess implements ICraftHandler, IWebsiteSerializer
{
@Nullable
private IIngredient imprintable;
@Nullable
private IIngredient topOptional;
@Nullable
private IIngredient botOptional;
@Nullable
private IIngredient output;
@Override
public void setup( List<List<IIngredient>> input, List<List<IIngredient>> output ) throws RecipeError
{
if( output.size() == 1 && output.get( 0 ).size() == 1 )
{
if( input.size() == 1 && input.get( 0 ).size() > 1 )
{
this.imprintable = input.get( 0 ).get( 0 );
this.topOptional = input.get( 0 ).get( 1 );
if( input.get( 0 ).size() > 2 )
this.botOptional = input.get( 0 ).get( 2 );
this.output = output.get( 0 ).get( 0 );
}
else
throw new RecipeError( "Inscriber recipes cannot have rows, and must have more then one input." );
}
else
throw new RecipeError( "Inscriber recipes must produce a single output." );
}
@Override
public boolean canCraft( ItemStack reqOutput ) throws RegistrationError, MissingIngredientError
{
return this.output != null && Platform.isSameItemPrecise( this.output.getItemStack(), reqOutput );
}
@Override
public String getPattern( RecipeHandler handler )
{
String pattern = "inscriber ";
if ( this.output != null )
{
pattern += this.output.getQty() + '\n';
pattern += handler.getName( this.output ) + '\n';
}
if( this.topOptional != null )
pattern += handler.getName( this.topOptional ) + '\n';
if ( this.imprintable != null )
pattern += handler.getName( this.imprintable );
if( this.botOptional != null )
pattern += '\n' + handler.getName( this.botOptional );
return pattern;
}
@Nullable
protected IIngredient getImprintable()
{
return this.imprintable;
}
@Nullable
protected IIngredient getTopOptional()
{
return this.topOptional;
}
@Nullable
protected IIngredient getBotOptional()
{
return this.botOptional;
}
@Nullable
protected IIngredient getOutput()
{
return this.output;
}
}
@@ -19,11 +19,48 @@
package appeng.recipes.handlers;
public class Press extends Inscribe
{
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public Press()
import net.minecraft.item.ItemStack;
import appeng.api.AEApi;
import appeng.api.exceptions.MissingIngredientError;
import appeng.api.exceptions.RegistrationError;
import appeng.api.features.IInscriberRecipe;
import appeng.api.features.InscriberProcessType;
import appeng.core.features.registries.entries.InscriberRecipe;
/**
* recipe translation for pressing in the inscriber
*
* @author AlgorithmX2
* @author thatsIch
* @version rv2
* @since rv0
*/
public final class Press extends InscriberProcess
{
@Override
public void register() throws RegistrationError, MissingIngredientError
{
this.usePlates = true;
if ( this.getImprintable() == null )
return;
if ( this.getOutput() == null )
return;
final ItemStack[] realInput = this.getImprintable().getItemStackSet();
final List<ItemStack> inputs = new ArrayList<ItemStack>( realInput.length );
Collections.addAll( inputs, realInput );
final ItemStack top = ( this.getTopOptional() == null ) ? null : this.getTopOptional().getItemStack();
final ItemStack bot = ( this.getBotOptional() == null ) ? null : this.getBotOptional().getItemStack();
final ItemStack output = this.getOutput().getItemStack();
final InscriberProcessType type = InscriberProcessType.Press;
IInscriberRecipe recipe = new InscriberRecipe( inputs, output, top, bot, type );
AEApi.instance().registries().inscriber().addRecipe( recipe );
}
}