Allow condition factories to be registered from JSON (#4015)
This commit is contained in:
parent
4a4c38b4e0
commit
f009435fa4
3 changed files with 46 additions and 30 deletions
|
@ -533,21 +533,7 @@ public class CraftingHelper {
|
|||
{
|
||||
ResourceLocation key = new ResourceLocation(context.getModId(), entry.getKey());
|
||||
String clsName = JsonUtils.getString(entry.getValue(), "ingredients[" + entry.getValue() + "]");
|
||||
try
|
||||
{
|
||||
Class<?> cls = Class.forName(clsName);
|
||||
if (!IIngredientFactory.class.isAssignableFrom(cls))
|
||||
throw new JsonSyntaxException("Class '" + clsName + "\' is not a IIngredientFactory!");
|
||||
register(key, (IIngredientFactory)cls.newInstance());
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
throw new JsonSyntaxException("Could not find ingredient factory: " + clsName, e);
|
||||
}
|
||||
catch (InstantiationException | IllegalAccessException e)
|
||||
{
|
||||
throw new JsonSyntaxException("Could not instantiate ingredient factory: " + clsName, e);
|
||||
}
|
||||
register(key, getClassInstance(clsName, IIngredientFactory.class));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -557,23 +543,38 @@ public class CraftingHelper {
|
|||
{
|
||||
ResourceLocation key = new ResourceLocation(context.getModId(), entry.getKey());
|
||||
String clsName = JsonUtils.getString(entry.getValue(), "recipes[" + entry.getValue() + "]");
|
||||
try
|
||||
{
|
||||
Class<?> cls = Class.forName(clsName);
|
||||
if (!IRecipeFactory.class.isAssignableFrom(cls))
|
||||
throw new JsonSyntaxException("Class '" + clsName + "\' is not a IRecipeFactory!");
|
||||
register(key, (IRecipeFactory)cls.newInstance());
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
throw new JsonSyntaxException("Could not find recipe factory: " + clsName, e);
|
||||
}
|
||||
catch (InstantiationException | IllegalAccessException e)
|
||||
{
|
||||
throw new JsonSyntaxException("Could not instantiate recipe factory: " + clsName, e);
|
||||
}
|
||||
register(key, getClassInstance(clsName, IRecipeFactory.class));
|
||||
}
|
||||
}
|
||||
|
||||
if (json.has("conditions"))
|
||||
{
|
||||
for (Entry<String, JsonElement> entry : JsonUtils.getJsonObject(json, "conditions").entrySet())
|
||||
{
|
||||
ResourceLocation key = new ResourceLocation(context.getModId(), entry.getKey());
|
||||
String clsName = JsonUtils.getString(entry.getValue(), "conditions[" + entry.getValue() + "]");
|
||||
register(key, getClassInstance(clsName, IConditionFactory.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> T getClassInstance(String clsName, Class<T> expected)
|
||||
{
|
||||
try
|
||||
{
|
||||
Class<?> cls = Class.forName(clsName);
|
||||
if (!expected.isAssignableFrom(cls))
|
||||
throw new JsonSyntaxException("Class '" + clsName + "' is not an " + expected.getSimpleName());
|
||||
return (T)cls.newInstance();
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
throw new JsonSyntaxException("Could not find " + expected.getSimpleName() + ": " + clsName, e);
|
||||
}
|
||||
catch (InstantiationException | IllegalAccessException e)
|
||||
{
|
||||
throw new JsonSyntaxException("Could not instantiate " + expected.getSimpleName() + ": " + clsName, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void loadRecipes()
|
||||
|
|
|
@ -4,6 +4,7 @@ import com.google.gson.JsonObject;
|
|||
|
||||
import net.minecraft.item.crafting.IRecipe;
|
||||
import net.minecraft.item.crafting.Ingredient;
|
||||
import net.minecraftforge.common.crafting.IConditionFactory;
|
||||
import net.minecraftforge.common.crafting.IIngredientFactory;
|
||||
import net.minecraftforge.common.crafting.IRecipeFactory;
|
||||
import net.minecraftforge.common.crafting.JsonContext;
|
||||
|
@ -12,6 +13,8 @@ import net.minecraftforge.fml.common.FMLLog;
|
|||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
@Mod(modid = CraftingSystemTest.MOD_ID, name = "CraftingTestMod", version = "1.0", acceptableRemoteVersions = "*")
|
||||
@Mod.EventBusSubscriber
|
||||
public class CraftingSystemTest
|
||||
|
@ -43,4 +46,13 @@ public class CraftingSystemTest
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
public static class ConditionFactory implements IConditionFactory
|
||||
{
|
||||
@Override
|
||||
public BooleanSupplier parse(JsonContext context, JsonObject json)
|
||||
{
|
||||
return () -> true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,5 +4,8 @@
|
|||
},
|
||||
"recipes": {
|
||||
"free": "net.minecraftforge.debug.CraftingSystemTest$RecipeFactory"
|
||||
},
|
||||
"conditions": {
|
||||
"true": "net.minecraftforge.debug.CraftingSystemTest$ConditionFactory"
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue