Search in sources :

Example 1 with IVariable

use of vazkii.patchouli.api.IVariable in project Botania by VazkiiMods.

the class PatchouliUtils method interweaveIngredients.

/**
 * Combines the ingredients, returning the first matching stack of each, then the second stack of each, etc.
 * looping back ingredients that run out of matched stacks, until the ingredients reach the length
 * of the longest ingredient in the recipe set.
 *
 * @param ingredients           List of ingredients in the specific slot
 * @param longestIngredientSize Longest ingredient in the entire recipe
 * @return Serialized Patchouli ingredient string
 */
public static IVariable interweaveIngredients(List<Ingredient> ingredients, int longestIngredientSize) {
    if (ingredients.size() == 1) {
        return IVariable.wrapList(Arrays.stream(ingredients.get(0).getItems()).map(IVariable::from).collect(Collectors.toList()));
    }
    ItemStack[] empty = { ItemStack.EMPTY };
    List<ItemStack[]> stacks = new ArrayList<>();
    for (Ingredient ingredient : ingredients) {
        if (ingredient != null && !ingredient.isEmpty()) {
            stacks.add(ingredient.getItems());
        } else {
            stacks.add(empty);
        }
    }
    List<IVariable> list = new ArrayList<>(stacks.size() * longestIngredientSize);
    for (int i = 0; i < longestIngredientSize; i++) {
        for (ItemStack[] stack : stacks) {
            list.add(IVariable.from(stack[i % stack.length]));
        }
    }
    return IVariable.wrapList(list);
}
Also used : Ingredient(net.minecraft.world.item.crafting.Ingredient) IVariable(vazkii.patchouli.api.IVariable) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.world.item.ItemStack)

Example 2 with IVariable

use of vazkii.patchouli.api.IVariable in project Botania by VazkiiMods.

the class ManaComponent method onVariablesAvailable.

@Override
public void onVariablesAvailable(UnaryOperator<IVariable> lookup) {
    IVariable manaVar = lookup.apply(mana);
    manaValues = manaVar.unwrap().isJsonArray() ? manaVar.asStream().map(IVariable::asNumber).mapToInt(Number::intValue).toArray() : new int[] { manaVar.asNumber(0).intValue() };
}
Also used : IVariable(vazkii.patchouli.api.IVariable)

Example 3 with IVariable

use of vazkii.patchouli.api.IVariable in project Botania by VazkiiMods.

the class BrewRecipeProcessor method process.

@Override
public IVariable process(String key) {
    if (recipe == null) {
        if (key.equals("is_offset")) {
            return IVariable.wrap(false);
        }
        return null;
    } else if (key.equals("heading")) {
        return IVariable.from(new TranslatableComponent("botaniamisc.brewOf", new TranslatableComponent(recipe.getBrew().getTranslationKey())));
    } else if (key.equals("vial")) {
        return IVariable.from(recipe.getOutput(new ItemStack(ModItems.vial)));
    } else if (key.equals("flask")) {
        return IVariable.from(recipe.getOutput(new ItemStack(ModItems.flask)));
    } else if (key.startsWith("input")) {
        int requestedIndex = Integer.parseInt(key.substring(5)) - 1;
        // Center the brew ingredients
        int indexOffset = (6 - recipe.getIngredients().size()) / 2;
        int index = requestedIndex - indexOffset;
        if (index < recipe.getIngredients().size() && index >= 0) {
            return IVariable.wrapList(Arrays.stream(recipe.getIngredients().get(index).getItems()).map(IVariable::from).collect(Collectors.toList()));
        } else {
            return null;
        }
    }
    if (key.equals("is_offset")) {
        return IVariable.wrap(recipe.getIngredients().size() % 2 == 0);
    }
    return null;
}
Also used : TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) IVariable(vazkii.patchouli.api.IVariable) ItemStack(net.minecraft.world.item.ItemStack)

Example 4 with IVariable

use of vazkii.patchouli.api.IVariable in project Patchouli by VazkiiMods.

the class TemplateInclusion method process.

public void process(IComponentProcessor processor) {
    if (processor == null) {
        return;
    }
    for (Map.Entry<String, JsonElement> entry : localBindings.entrySet()) {
        String key = entry.getKey();
        JsonElement val = entry.getValue();
        if (val.isJsonPrimitive() && val.getAsString().startsWith("#")) {
            String realVal = val.getAsString().substring(1);
            IVariable res = processor.process(realVal);
            if (res != null) {
                entry.setValue(res.unwrap());
            }
        }
    }
}
Also used : JsonElement(com.google.gson.JsonElement) IVariable(vazkii.patchouli.api.IVariable)

Example 5 with IVariable

use of vazkii.patchouli.api.IVariable in project Patchouli by VazkiiMods.

the class VariableAssigner method resolveStringFunctions.

private static IVariable resolveStringFunctions(String curr, Context c) {
    IVariable cached = c.getCached(curr);
    if (cached != null) {
        return cached;
    }
    Matcher m = FUNCTION_PATTERN.matcher(curr);
    if (m.matches()) {
        String funcStr = m.group(2);
        String arg = m.group(1);
        if (FUNCTIONS.containsKey(funcStr)) {
            UnaryOperator<IVariable> func = FUNCTIONS.get(funcStr);
            IVariable parsedArg = resolveStringFunctions(arg, c);
            return c.cache(curr, func.apply(parsedArg));
        } else {
            throw new IllegalArgumentException("Invalid Function " + funcStr);
        }
    }
    IVariable ret = resolveStringVar(curr, c);
    return c.cache(curr, ret);
}
Also used : Matcher(java.util.regex.Matcher) IVariable(vazkii.patchouli.api.IVariable)

Aggregations

IVariable (vazkii.patchouli.api.IVariable)10 ItemStack (net.minecraft.world.item.ItemStack)3 Ingredient (net.minecraft.world.item.crafting.Ingredient)3 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 ResourceLocation (net.minecraft.resources.ResourceLocation)2 JsonElement (com.google.gson.JsonElement)1 Matcher (java.util.regex.Matcher)1 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)1 IElvenTradeRecipe (vazkii.botania.api.recipe.IElvenTradeRecipe)1 IManaInfusionRecipe (vazkii.botania.api.recipe.IManaInfusionRecipe)1