use of gregtech.api.items.metaitem.MetaItem in project GregTech by GregTechCE.
the class ModHandler method addShapelessRecipe.
/**
* Add Shapeless Crafting Recipes
*/
public static void addShapelessRecipe(String regName, ItemStack result, Object... recipe) {
boolean skip = false;
if (result.isEmpty()) {
GTLog.logger.error("Result ItemStack cannot be empty", new IllegalArgumentException());
skip = true;
}
skip |= validateRecipe(recipe);
if (skip)
return;
for (byte i = 0; i < recipe.length; i++) {
if (recipe[i] instanceof MetaItem.MetaValueItem) {
recipe[i] = ((MetaItem<?>.MetaValueItem) recipe[i]).getStackForm();
} else if (recipe[i] instanceof Enum) {
recipe[i] = ((Enum<?>) recipe[i]).name();
} else if (recipe[i] instanceof UnificationEntry) {
recipe[i] = recipe[i].toString();
} else if (recipe[i] instanceof Character) {
String toolName = getToolNameByCharacter((char) recipe[i]);
if (toolName == null) {
throw new IllegalArgumentException("Tool name is not found for char " + recipe[i]);
}
recipe[i] = toolName;
} else if (!(recipe[i] instanceof ItemStack || recipe[i] instanceof Item || recipe[i] instanceof Block || recipe[i] instanceof String)) {
throw new IllegalArgumentException(recipe.getClass().getSimpleName() + " type is not suitable for crafting input.");
}
}
IRecipe shapelessRecipe = new ShapelessOreRecipe(null, result.copy(), recipe).setRegistryName(regName);
ForgeRegistries.RECIPES.register(shapelessRecipe);
}
use of gregtech.api.items.metaitem.MetaItem in project GregTech by GregTechCE.
the class MetaItemShapedRecipeFactory method parse.
@Override
public IRecipe parse(JsonContext context, JsonObject json) {
String group = JsonUtils.getString(json, "group", "");
Map<Character, Ingredient> ingMap = Maps.newHashMap();
for (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(' ', 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 imageWidth");
pattern[x] = line;
}
ShapedPrimer primer = new 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, Ingredient.EMPTY);
Set<Character> keys = Sets.newHashSet(ingMap.keySet());
keys.remove(' ');
int x = 0;
for (String line : pattern) {
for (char chr : line.toCharArray()) {
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 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 ShapedOreRecipe(group.isEmpty() ? null : new ResourceLocation(group), stack, primer);
}
use of gregtech.api.items.metaitem.MetaItem in project GregTech by GregTechCE.
the class MetaItemShapelessRecipeFactory method parse.
@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);
}
Aggregations