下面列出了怎么用net.minecraftforge.common.crafting.JsonContext的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public IRecipe parse(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 result = JsonUtils.getJsonObject(json, "result");
String name = JsonUtils.getString(result, "name");
int amount = JsonUtils.getInt(result, "amount", 1);
ItemStack stack = ItemStack.EMPTY;
for (MetaItem<?> item : MetaItems.ITEMS) {
MetaItem<?>.MetaValueItem value = item.getItem(name);
if (value != null) {
stack = value.getStackForm(amount);
}
}
return new ShapelessOreRecipe(group.isEmpty() ? null : new ResourceLocation(group), ings, stack);
}
@Override
public Ingredient parse(JsonContext context, JsonObject json) {
String name = JsonUtils.getString(json, "name");
int amount = JsonUtils.getInt(json, "amount", 1);
for (MetaItem<?> item : MetaItems.ITEMS) {
MetaItem<?>.MetaValueItem value = item.getItem(name);
if (value != null) {
return Ingredient.fromStacks(value.getStackForm(amount));
}
}
return Ingredient.EMPTY;
}
@Override
public IRecipe parse(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");
ItemStack itemstack = CraftingHelper.getItemStack(JsonUtils.getJsonObject(json, "result"), context);
int[] damage = new int[ings.size()];
if (JsonUtils.hasField(json, "damage"))
{
JsonArray array = JsonUtils.getJsonArray(json, "damage");
if (array.size() > damage.length)
throw new JsonParseException("Too many values for damage array: got " + array.size() + ", expected " + damage.length);
for (int i = 0; i < array.size(); i++)
{
JsonElement element = array.get(i);
if (!element.isJsonPrimitive() || !element.getAsJsonPrimitive().isNumber())
throw new JsonSyntaxException("Entry in damage array is not a number, got " + element);
damage[i] = element.getAsJsonPrimitive().getAsInt();
}
}
return new DamageableShapelessOreRecipe(group.isEmpty() ? null : new ResourceLocation(group), damage, ings, itemstack);
}
@Nonnull
@Override
public Ingredient parse(JsonContext context, JsonObject json) {
String name = JsonUtils.getString(json, "fluid");
int amount = JsonUtils.getInt(json, "amount", 1000);
Fluid fluid = FluidRegistry.getFluid(name);
if (fluid == null)
throw new JsonSyntaxException("Fluid with name " + name + " could not be found");
return new IngredientFluidStack(fluid, amount);
}
@Override
public IRecipe parse(JsonContext context, JsonObject json) {
String group = JsonUtils.getString(json, "group", "");
NonNullList<Ingredient> ingredients = NonNullList.create();
for (JsonElement element : JsonUtils.getJsonArray(json, "ingredients"))
ingredients.add(CraftingHelper.getIngredient(element, context));
if (ingredients.isEmpty())
throw new JsonParseException("No ingredients in shapeless recipe");
ItemStack result = CraftingHelper.getItemStack(JsonUtils.getJsonObject(json, "result"), context);
RecipeShapelessFluid recipe = new RecipeShapelessFluid(group.isEmpty() ? null : new ResourceLocation(group), result, ingredients);
return recipe;
}
@Override
public IRecipe parse(JsonContext context, JsonObject json) {
ShapedOreRecipe fake = ShapedOreRecipe.factory(context, json);
CraftingHelper.ShapedPrimer primer = new CraftingHelper.ShapedPrimer();
primer.width = fake.getRecipeWidth();
primer.height = fake.getRecipeHeight();
primer.input = fake.getIngredients();
primer.mirrored = JsonUtils.getBoolean(json, "mirrored", true); // Hack
return new CustomWoodShapedRecipe(fake.getRegistryName(), fake.getRecipeOutput(), primer);
}
@Override
public BooleanSupplier parse(JsonContext context, JsonObject json) {
final String enchId = JsonUtils.getString(json, "id");
return () -> Enchantment.REGISTRY.containsKey(new ResourceLocation(enchId));
}