Search in sources :

Example 1 with MetaItem

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);
}
Also used : MetaItem(gregtech.api.items.metaitem.MetaItem) IRecipe(net.minecraft.item.crafting.IRecipe) UnificationEntry(gregtech.api.unification.stack.UnificationEntry) Item(net.minecraft.item.Item) MetaItem(gregtech.api.items.metaitem.MetaItem) IElectricItem(gregtech.api.capability.IElectricItem) ShapelessOreRecipe(net.minecraftforge.oredict.ShapelessOreRecipe) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack)

Example 2 with MetaItem

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);
}
Also used : MetaItem(gregtech.api.items.metaitem.MetaItem) ShapedOreRecipe(net.minecraftforge.oredict.ShapedOreRecipe) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) ShapedPrimer(net.minecraftforge.common.crafting.CraftingHelper.ShapedPrimer) JsonSyntaxException(com.google.gson.JsonSyntaxException) Ingredient(net.minecraft.item.crafting.Ingredient) JsonElement(com.google.gson.JsonElement) ResourceLocation(net.minecraft.util.ResourceLocation) ItemStack(net.minecraft.item.ItemStack)

Example 3 with MetaItem

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);
}
Also used : MetaItem(gregtech.api.items.metaitem.MetaItem) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) Ingredient(net.minecraft.item.crafting.Ingredient) JsonElement(com.google.gson.JsonElement) ResourceLocation(net.minecraft.util.ResourceLocation) ShapelessOreRecipe(net.minecraftforge.oredict.ShapelessOreRecipe) ItemStack(net.minecraft.item.ItemStack)

Aggregations

MetaItem (gregtech.api.items.metaitem.MetaItem)3 ItemStack (net.minecraft.item.ItemStack)3 JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 Ingredient (net.minecraft.item.crafting.Ingredient)2 ResourceLocation (net.minecraft.util.ResourceLocation)2 ShapelessOreRecipe (net.minecraftforge.oredict.ShapelessOreRecipe)2 JsonArray (com.google.gson.JsonArray)1 JsonParseException (com.google.gson.JsonParseException)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 IElectricItem (gregtech.api.capability.IElectricItem)1 UnificationEntry (gregtech.api.unification.stack.UnificationEntry)1 Block (net.minecraft.block.Block)1 Item (net.minecraft.item.Item)1 IRecipe (net.minecraft.item.crafting.IRecipe)1 ShapedPrimer (net.minecraftforge.common.crafting.CraftingHelper.ShapedPrimer)1 ShapedOreRecipe (net.minecraftforge.oredict.ShapedOreRecipe)1