Skip to content

Advanced API Example

Uristqwerty edited this page Jun 18, 2017 · 6 revisions

Here is a modified version of the API usage example that demonstrates more of the slot types provided by the API.

class DoohickeyRecipes extends CraftGuideAPIObject implements RecipeProvider
{
	@Override
	public void generateRecipes(RecipeGenerator generator)
	{
		ItemStack doohickeyMachine = MyAPI.getBlockItem("doohickey");
		
		RecipeTemplate template = generator.createRecipeTemplate(
				new Slot[]{
						new ItemSlot(12, 12, 16, 16, true).drawOwnBackground(true),
						new LiquidSlot(12, 30),
						new ItemSlot(50, 13, 16, 16, true).setSlotType(SlotType.OUTPUT_SLOT).drawOwnBackground(true),
						new ChanceSlot(50, 30, 16, 16).setSlotType(SlotType.OUTPUT_SLOT).drawOwnBackground(true)
						new ItemSlot(31, 21, 16, 16, true).setSlotType(SlotType.MACHINE_SLOT).drawOwnBackground(false),
				}, doohickeyMachine);
			
		for(DoohickeyRecipe recipe: MyAPI.getDoohickeyRecipes())
		{
			recipeGenerator.addRecipe(template,
					new Object[] {
							recipe.getInputItem(),
							recipe.getInputFluidStack(),
							recipe.getPrimaryOutput(),
							new Object[] {
								recipe.getSecondaryOutput(),
								recipe.getSecondaryChance(),
							},
							doohickeyMachine
					});
		}
	}
}

a 4x4 vanilla-style crafting recipe with the builder API

class DoohickeyRecipes extends CraftGuideAPIObject implements RecipeProvider
{
	@Override
	public void generateRecipes(RecipeGenerator generator)
	{
		ItemStack crafting = new ItemStack(MyMod.expanded_crafting_table);
		ConstructedRecipeTemplate shapeless = generator.buildTemplate(crafting)
			.shapelessItemGrid(4, 4)
			.nextColumn(1) // 1px gap between input and output columns. Purely a personal convention.
			.outputItem()
			.machineItem()
			.finishTemplate();
	
		ConstructedRecipeTemplate shaped = generator.buildTemplate(crafting)
			.shapedItemGrid(4, 4)
			.nextColumn(1)
			.outputItem()
			.machineItem()
			.finishTemplate();
	
		for(IRecipe recipe: MyMod.craftingRecipes().getRecipeList())
		{
			if(recipe instanceof ShapelessRecipe)
			{
				ShapelessRecipe r = (ShapelessRecipe)recipe;
				shapeless.buildRecipe()
					.shapelessItemGrid(r.getInputs())
					.item(r.getOutput())
					.item(crafting)
					.addRecipe(generator);
			}
			else if(recipe instanceof ShapedRecipe)
			{
				ShapedRecipe r = (ShapedRecipe)recipe;
				shaped.buildRecipe()
					.shapedItemGrid(r.width(), r.height(), r.getInputs())
					.item(r.getOutput())
					.item(crafting)
					.addRecipe(generator);
			}
		}
	}
}

Clone this wiki locally