JSON Recipes initial import.
Moved the old recipe handler to a different direct to not confuse the vanilla/forge JSON loader.
This commit is contained in:
@@ -23,13 +23,8 @@ import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
@@ -38,12 +33,17 @@ import javax.annotation.Nonnull;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.collect.HashMultimap;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.crafting.IIngredientFactory;
|
||||
import net.minecraftforge.common.crafting.JsonContext;
|
||||
import net.minecraftforge.fml.common.LoaderState;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.recipes.*;
|
||||
import appeng.core.Api;
|
||||
import appeng.api.definitions.IBlocks;
|
||||
import appeng.api.definitions.IDefinitions;
|
||||
import appeng.api.definitions.IItems;
|
||||
@@ -51,10 +51,6 @@ import appeng.api.exceptions.MissingIngredientError;
|
||||
import appeng.api.exceptions.RecipeError;
|
||||
import appeng.api.exceptions.RegistrationError;
|
||||
import appeng.api.features.IRecipeHandlerRegistry;
|
||||
import appeng.api.recipes.ICraftHandler;
|
||||
import appeng.api.recipes.IIngredient;
|
||||
import appeng.api.recipes.IRecipeHandler;
|
||||
import appeng.api.recipes.IRecipeLoader;
|
||||
import appeng.core.AEConfig;
|
||||
import appeng.core.AELog;
|
||||
import appeng.core.AppEng;
|
||||
@@ -766,4 +762,32 @@ public class RecipeHandler implements IRecipeHandler
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static class PartFactory implements IIngredientFactory
|
||||
{
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public net.minecraft.item.crafting.Ingredient parse( JsonContext context, JsonObject json )
|
||||
{
|
||||
String partName = json.get( "part" ).getAsString();
|
||||
Object result = (Object) Api.INSTANCE.registries().recipes().resolveItem( AppEng.MOD_ID, partName );
|
||||
if( result instanceof ResolverResultSet )
|
||||
{
|
||||
ResolverResultSet resolverResultSet = (ResolverResultSet) result;
|
||||
return net.minecraft.item.crafting.Ingredient.fromStacks(resolverResultSet.results.toArray(new ItemStack[resolverResultSet.results.size()]));
|
||||
}
|
||||
else if( result instanceof ResolverResult )
|
||||
{
|
||||
ResolverResult resolverResult = (ResolverResult) result;
|
||||
|
||||
Item item = Item.getByNameOrId( AppEng.MOD_ID + ":" + resolverResult.itemName );
|
||||
ItemStack itemStack = new ItemStack( item, 1, resolverResult.damageValue, resolverResult.compound );
|
||||
|
||||
return net.minecraft.item.crafting.Ingredient.fromStacks( itemStack );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,8 +24,6 @@ import java.util.List;
|
||||
|
||||
import appeng.core.Registration;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.fml.common.registry.ForgeRegistries;
|
||||
import net.minecraftforge.fml.common.registry.GameRegistry;
|
||||
|
||||
import appeng.api.exceptions.MissingIngredientError;
|
||||
import appeng.api.exceptions.RecipeError;
|
||||
@@ -115,7 +113,8 @@ public class Shaped implements ICraftHandler, IWebsiteSerializer
|
||||
|
||||
try
|
||||
{
|
||||
Registration.addRecipeToRegister( new ShapedRecipe( outIS, args.toArray( new Object[args.size()] ) ) );
|
||||
// TODO : 1.12 Cleanup/remove the old recipe loader.
|
||||
// Registration.addRecipeToRegister( new ShapedRecipe( outIS, args.toArray( new Object[args.size()] ) ) );
|
||||
}
|
||||
catch( final Throwable e )
|
||||
{
|
||||
|
||||
@@ -24,8 +24,6 @@ import java.util.List;
|
||||
|
||||
import appeng.core.Registration;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.fml.common.registry.ForgeRegistries;
|
||||
import net.minecraftforge.fml.common.registry.GameRegistry;
|
||||
|
||||
import appeng.api.exceptions.MissingIngredientError;
|
||||
import appeng.api.exceptions.RecipeError;
|
||||
@@ -78,7 +76,8 @@ public class Shapeless implements ICraftHandler, IWebsiteSerializer
|
||||
|
||||
try
|
||||
{
|
||||
Registration.addRecipeToRegister( new ShapelessRecipe( outIS, args.toArray( new Object[args.size()] ) ) );
|
||||
// TODO : 1.12 Cleanup/remove the old recipe loader.
|
||||
// Registration.addRecipeToRegister( new ShapelessRecipe( outIS, args.toArray( new Object[args.size()] ) ) );
|
||||
}
|
||||
catch( final Throwable e )
|
||||
{
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package appeng.recipes.helpers;
|
||||
|
||||
import appeng.core.AppEng;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.minecraft.item.crafting.IRecipe;
|
||||
import net.minecraft.util.JsonUtils;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.crafting.CraftingHelper;
|
||||
import net.minecraftforge.common.crafting.IRecipeFactory;
|
||||
import net.minecraftforge.common.crafting.JsonContext;
|
||||
|
||||
/**
|
||||
* @author GuntherDW
|
||||
*/
|
||||
public class PartRecipeFactory implements IRecipeFactory
|
||||
{
|
||||
@Override
|
||||
public IRecipe parse( JsonContext context, JsonObject json )
|
||||
{
|
||||
String type = JsonUtils.getString( json, "type" );
|
||||
if( type.contains( "shaped" ) )
|
||||
{
|
||||
PartShapedCraftingFactory recipe = PartShapedCraftingFactory.factory( context, json );
|
||||
CraftingHelper.ShapedPrimer primer = new CraftingHelper.ShapedPrimer();
|
||||
primer.width = recipe.getWidth();
|
||||
primer.height = recipe.getHeight();
|
||||
primer.mirrored = JsonUtils.getBoolean( json, "mirrored", true );
|
||||
primer.input = recipe.getIngredients();
|
||||
|
||||
return new PartShapedCraftingFactory( new ResourceLocation( AppEng.MOD_ID, "part_shaped_crafting" ), recipe.getRecipeOutput(), primer );
|
||||
}
|
||||
else if( type.contains( "shapeless" ) )
|
||||
{
|
||||
PartShapelessCraftingFactory recipe = PartShapelessCraftingFactory.factory( context, json );
|
||||
|
||||
return new PartShapelessCraftingFactory( new ResourceLocation( AppEng.MOD_ID, "part_shapeless_crafting" ), recipe.getIngredients(), recipe.getRecipeOutput() );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package appeng.recipes.helpers;
|
||||
|
||||
import appeng.api.recipes.ResolverResult;
|
||||
import appeng.api.recipes.ResolverResultSet;
|
||||
import appeng.core.AELog;
|
||||
import appeng.core.Api;
|
||||
import appeng.core.AppEng;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.Ingredient;
|
||||
import net.minecraft.util.JsonUtils;
|
||||
import net.minecraft.util.NonNullList;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.crafting.CraftingHelper;
|
||||
import net.minecraftforge.common.crafting.JsonContext;
|
||||
import net.minecraftforge.oredict.ShapedOreRecipe;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author GuntherDW
|
||||
*/
|
||||
public class PartShapedCraftingFactory extends ShapedOreRecipe
|
||||
{
|
||||
|
||||
|
||||
public PartShapedCraftingFactory( ResourceLocation group, @Nonnull ItemStack result, CraftingHelper.ShapedPrimer primer )
|
||||
{
|
||||
super( group, result, primer );
|
||||
}
|
||||
|
||||
// Copied from ShapedOreRecipe.java, modified a bit.
|
||||
public static PartShapedCraftingFactory factory( JsonContext context, JsonObject json )
|
||||
{
|
||||
String group = JsonUtils.getString( json, "group", "" );
|
||||
|
||||
Map<Character, Ingredient> ingMap = Maps.newHashMap();
|
||||
for( Map.Entry<String, JsonElement> entry : JsonUtils.getJsonObject( json, "key" ).entrySet() )
|
||||
{
|
||||
if( entry.getKey().length() != 1 )
|
||||
throw new JsonSyntaxException( "Invalid key entry: '" + entry.getKey() + "' is an invalid symbol (must be 1 character only)." );
|
||||
if( " ".equals( entry.getKey() ) )
|
||||
throw new JsonSyntaxException( "Invalid key entry: ' ' is a reserved symbol." );
|
||||
|
||||
ingMap.put( entry.getKey().toCharArray()[0], CraftingHelper.getIngredient( entry.getValue(), context ) );
|
||||
}
|
||||
|
||||
ingMap.put( ' ', net.minecraft.item.crafting.Ingredient.EMPTY );
|
||||
|
||||
JsonArray patternJ = JsonUtils.getJsonArray( json, "pattern" );
|
||||
|
||||
if( patternJ.size() == 0 )
|
||||
throw new JsonSyntaxException( "Invalid pattern: empty pattern not allowed" );
|
||||
|
||||
String[] pattern = new String[patternJ.size()];
|
||||
for( int x = 0; x < pattern.length; ++x )
|
||||
{
|
||||
String line = JsonUtils.getString( patternJ.get( x ), "pattern[" + x + "]" );
|
||||
if( x > 0 && pattern[0].length() != line.length() )
|
||||
throw new JsonSyntaxException( "Invalid pattern: each row must be the same width" );
|
||||
pattern[x] = line;
|
||||
}
|
||||
|
||||
CraftingHelper.ShapedPrimer primer = new CraftingHelper.ShapedPrimer();
|
||||
primer.width = pattern[0].length();
|
||||
primer.height = pattern.length;
|
||||
primer.mirrored = JsonUtils.getBoolean( json, "mirrored", true );
|
||||
primer.input = NonNullList.withSize( primer.width * primer.height, net.minecraft.item.crafting.Ingredient.EMPTY );
|
||||
|
||||
Set<Character> keys = Sets.newHashSet( ingMap.keySet() );
|
||||
keys.remove( ' ' );
|
||||
|
||||
int x = 0;
|
||||
for( String line : pattern )
|
||||
{
|
||||
for( char chr : line.toCharArray() )
|
||||
{
|
||||
net.minecraft.item.crafting.Ingredient ing = ingMap.get( chr );
|
||||
if( ing == null )
|
||||
throw new JsonSyntaxException( "Pattern references symbol '" + chr + "' but it's not defined in the key" );
|
||||
primer.input.set( x++, ing );
|
||||
keys.remove( chr );
|
||||
}
|
||||
}
|
||||
|
||||
if( !keys.isEmpty() )
|
||||
throw new JsonSyntaxException( "Key defines symbols that aren't used in pattern: " + keys );
|
||||
|
||||
JsonObject resultObject = (JsonObject) json.get( "result" );
|
||||
|
||||
String ingredient = resultObject.get( "part" ).getAsString();
|
||||
Object result = (Object) Api.INSTANCE.registries().recipes().resolveItem( AppEng.MOD_ID, ingredient );
|
||||
if( result instanceof ResolverResult )
|
||||
{
|
||||
ResolverResult resolverResult = (ResolverResult) result;
|
||||
|
||||
Item item = Item.getByNameOrId( AppEng.MOD_ID + ":" + resolverResult.itemName );
|
||||
if(item == null)
|
||||
AELog.warn( "item was null for " + resolverResult.itemName + " ( " + ingredient + " )!" );
|
||||
ItemStack itemStack = new ItemStack( item, 1, resolverResult.damageValue, resolverResult.compound );
|
||||
|
||||
return new PartShapedCraftingFactory( group.isEmpty() ? null : new ResourceLocation( group ), itemStack, primer );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package appeng.recipes.helpers;
|
||||
|
||||
import appeng.api.recipes.ResolverResult;
|
||||
import appeng.api.recipes.ResolverResultSet;
|
||||
import appeng.core.AELog;
|
||||
import appeng.core.Api;
|
||||
import appeng.core.AppEng;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.Ingredient;
|
||||
import net.minecraft.util.JsonUtils;
|
||||
import net.minecraft.util.NonNullList;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.crafting.CraftingHelper;
|
||||
import net.minecraftforge.common.crafting.JsonContext;
|
||||
import net.minecraftforge.oredict.ShapelessOreRecipe;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* @author GuntherDW
|
||||
*/
|
||||
public class PartShapelessCraftingFactory extends ShapelessOreRecipe
|
||||
{
|
||||
|
||||
public PartShapelessCraftingFactory( ResourceLocation group, NonNullList<Ingredient> input, @Nonnull ItemStack result )
|
||||
{
|
||||
super( group, input, result );
|
||||
}
|
||||
|
||||
// Copied from ShapelessOreRecipe.java, modified a bit.
|
||||
public static PartShapelessCraftingFactory factory( JsonContext context, JsonObject json)
|
||||
{
|
||||
String group = JsonUtils.getString(json, "group", "");
|
||||
|
||||
NonNullList<Ingredient> ings = NonNullList.create();
|
||||
for( JsonElement ele : JsonUtils.getJsonArray( json, "ingredients" ) )
|
||||
ings.add( CraftingHelper.getIngredient( ele, context ) );
|
||||
|
||||
if( ings.isEmpty() )
|
||||
throw new JsonParseException("No ingredients for shapeless recipe");
|
||||
|
||||
JsonObject resultObject = (JsonObject) json.get( "result" );
|
||||
|
||||
String ingredient = resultObject.get( "part" ).getAsString();
|
||||
Object result = (Object) Api.INSTANCE.registries().recipes().resolveItem( AppEng.MOD_ID, ingredient );
|
||||
if( result instanceof ResolverResultSet )
|
||||
{
|
||||
ResolverResultSet resolverResultSet = (ResolverResultSet) result;
|
||||
return new PartShapelessCraftingFactory(group.isEmpty() ? null : new ResourceLocation(group), ings, resolverResultSet.results.toArray( new ItemStack[resolverResultSet.results.size()] )[0]);
|
||||
}
|
||||
else if( result instanceof ResolverResult )
|
||||
{
|
||||
ResolverResult resolverResult = (ResolverResult) result;
|
||||
|
||||
Item item = Item.getByNameOrId( AppEng.MOD_ID + ":" + resolverResult.itemName );
|
||||
|
||||
if( item == null )
|
||||
AELog.warn( "item was null for " + resolverResult.itemName + " ( " + ingredient + " )!" );
|
||||
|
||||
ItemStack itemStack = new ItemStack( item, 1, resolverResult.damageValue, resolverResult.compound );
|
||||
|
||||
return new PartShapelessCraftingFactory(group.isEmpty() ? null : new ResourceLocation(group), ings, itemStack);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user