Search in sources :

Example 26 with ItemStackIngredient

use of mekanism.api.recipes.inputs.ItemStackIngredient in project Mekanism by mekanism.

the class BaseCrTExampleProvider method getIngredientRepresentation.

private String getIngredientRepresentation(CrTImportsComponent imports, ItemStackIngredient ingredient) {
    if (ingredient instanceof ItemStackIngredient.Single) {
        JsonObject serialized = ingredient.serialize().getAsJsonObject();
        // While it is easier to compare types of some stuff using the inner ingredient, some things
        // are easier to get the information of out of the serialized ingredient
        JsonObject serializedIngredient = serialized.getAsJsonObject(JsonConstants.INGREDIENT);
        Ingredient vanillaIngredient = ((ItemStackIngredient.Single) ingredient).getInputRaw();
        int amount = JSONUtils.getAsInt(serialized, JsonConstants.AMOUNT, 1);
        String representation = null;
        if (amount > 1) {
            // Special case handling for when we would want to use a different constructor
            if (vanillaIngredient.isVanilla() && !serializedIngredient.isJsonArray() && serializedIngredient.has(JsonConstants.ITEM)) {
                Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(JSONUtils.getAsString(serializedIngredient, JsonConstants.ITEM)));
                representation = ItemStackHelper.getCommandString(new ItemStack(item, amount));
                amount = 1;
            } else if (vanillaIngredient instanceof NBTIngredient) {
                ItemStack stack = CraftingHelper.getItemStack(serializedIngredient, true);
                stack.setCount(amount);
                representation = ItemStackHelper.getCommandString(stack);
                amount = 1;
            }
        }
        if (representation == null) {
            representation = IIngredient.fromIngredient(vanillaIngredient).getCommandString();
        }
        String path = imports.addImport(CrTConstants.CLASS_ITEM_STACK_INGREDIENT);
        if (amount == 1) {
            return path + ".from(" + representation + ")";
        }
        return path + ".from(" + representation + ", " + amount + ")";
    } else if (ingredient instanceof ItemStackIngredient.Multi) {
        ItemStackIngredient.Multi multiIngredient = (ItemStackIngredient.Multi) ingredient;
        StringBuilder builder = new StringBuilder(imports.addImport(CrTConstants.CLASS_ITEM_STACK_INGREDIENT) + ".createMulti(");
        if (!multiIngredient.forEachIngredient(i -> {
            String rep = getIngredientRepresentation(imports, i);
            if (rep == null) {
                return true;
            }
            builder.append(rep).append(", ");
            return false;
        })) {
            // Remove trailing comma and space
            builder.setLength(builder.length() - 2);
            builder.append(")");
            return builder.toString();
        }
    }
    return null;
}
Also used : ItemStackIngredient(mekanism.api.recipes.inputs.ItemStackIngredient) JsonObject(com.google.gson.JsonObject) Item(net.minecraft.item.Item) ItemStackIngredient(mekanism.api.recipes.inputs.ItemStackIngredient) IIngredient(com.blamejared.crafttweaker.api.item.IIngredient) NBTIngredient(net.minecraftforge.common.crafting.NBTIngredient) PigmentStackIngredient(mekanism.api.recipes.inputs.chemical.PigmentStackIngredient) SlurryStackIngredient(mekanism.api.recipes.inputs.chemical.SlurryStackIngredient) GasStackIngredient(mekanism.api.recipes.inputs.chemical.GasStackIngredient) IChemicalStackIngredient(mekanism.api.recipes.inputs.chemical.IChemicalStackIngredient) InfusionStackIngredient(mekanism.api.recipes.inputs.chemical.InfusionStackIngredient) Ingredient(net.minecraft.item.crafting.Ingredient) FluidStackIngredient(mekanism.api.recipes.inputs.FluidStackIngredient) ChemicalStackIngredient(mekanism.api.recipes.inputs.chemical.ChemicalStackIngredient) NBTIngredient(net.minecraftforge.common.crafting.NBTIngredient) ResourceLocation(net.minecraft.util.ResourceLocation) IItemStack(com.blamejared.crafttweaker.api.item.IItemStack) MCItemStack(com.blamejared.crafttweaker.impl.item.MCItemStack) MCWeightedItemStack(com.blamejared.crafttweaker.impl.item.MCWeightedItemStack) ItemStack(net.minecraft.item.ItemStack)

Aggregations

ItemStackIngredient (mekanism.api.recipes.inputs.ItemStackIngredient)26 ItemStack (net.minecraft.item.ItemStack)19 JsonSyntaxException (com.google.gson.JsonSyntaxException)18 JsonElement (com.google.gson.JsonElement)10 Nonnull (javax.annotation.Nonnull)10 GasStackIngredient (mekanism.api.recipes.inputs.chemical.GasStackIngredient)8 FloatingLong (mekanism.api.math.FloatingLong)5 FluidStackIngredient (mekanism.api.recipes.inputs.FluidStackIngredient)4 Ingredient (net.minecraft.item.crafting.Ingredient)4 IIngredient (com.blamejared.crafttweaker.api.item.IIngredient)3 GasStack (mekanism.api.chemical.gas.GasStack)3 Item (net.minecraft.item.Item)3 JsonObject (com.google.gson.JsonObject)2 ChemicalType (mekanism.api.chemical.ChemicalType)2 ChemicalStackIngredient (mekanism.api.recipes.inputs.chemical.ChemicalStackIngredient)2 IChemicalStackIngredient (mekanism.api.recipes.inputs.chemical.IChemicalStackIngredient)2 InfusionStackIngredient (mekanism.api.recipes.inputs.chemical.InfusionStackIngredient)2 PigmentStackIngredient (mekanism.api.recipes.inputs.chemical.PigmentStackIngredient)2 SlurryStackIngredient (mekanism.api.recipes.inputs.chemical.SlurryStackIngredient)2 ResourceLocation (net.minecraft.util.ResourceLocation)2