Update to Vazkii/Patchouli#225 spec (#3340)

* Update to the new Patchouli IVariable system

* missed a spotless
This commit is contained in:
Al Winfy authored and GitHub committed 2020-07-01 22:04:20 -05:00
1 parent 991198118b
commit 0dff00c506
31 files changed
+497 -212

No files matched your search

@@ -11,11 +11,13 @@ package vazkii.botania.client.patchouli;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.Ingredient;
import vazkii.patchouli.api.IVariable;
import vazkii.patchouli.api.PatchouliAPI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;
public class PatchouliUtils {
@@ -28,9 +30,9 @@ public class PatchouliUtils {
* @param longestIngredientSize Longest ingredient in the entire recipe
* @return Serialized Patchouli ingredient string
*/
public static String interweaveIngredients(List<Ingredient> ingredients, int longestIngredientSize) {
public static IVariable interweaveIngredients(List<Ingredient> ingredients, int longestIngredientSize) {
if (ingredients.size() == 1) {
return PatchouliAPI.instance.serializeIngredient(ingredients.get(0));
return IVariable.wrapList(Arrays.stream(ingredients.get(0).getMatchingStacks()).map(IVariable::from).collect(Collectors.toList()));
}
ItemStack[] empty = { ItemStack.EMPTY };
@@ -42,19 +44,19 @@ public class PatchouliUtils {
stacks.add(empty);
}
}
StringJoiner joiner = new StringJoiner(",");
List<IVariable> list = new ArrayList<>(stacks.size() * longestIngredientSize);
for (int i = 0; i < longestIngredientSize; i++) {
for (ItemStack[] stack : stacks) {
joiner.add(PatchouliAPI.instance.serializeItemStack(stack[i % stack.length]));
list.add(IVariable.from(stack[i % stack.length]));
}
}
return joiner.toString();
return IVariable.wrapList(list);
}
/**
* Overload of the method above that uses the provided list's longest ingredient size.
*/
public static String interweaveIngredients(List<Ingredient> ingredients) {
public static IVariable interweaveIngredients(List<Ingredient> ingredients) {
return interweaveIngredients(ingredients, ingredients.stream().mapToInt(ingr -> ingr.getMatchingStacks().length).max().orElse(1));
}
}
@@ -8,6 +8,7 @@
*/
package vazkii.botania.client.patchouli.component;
import com.google.common.collect.Streams;
import com.mojang.blaze3d.matrix.MatrixStack;
import net.minecraft.client.Minecraft;
@@ -19,9 +20,10 @@ import vazkii.botania.client.core.handler.HUDHandler;
import vazkii.botania.common.block.tile.mana.TilePool;
import vazkii.patchouli.api.IComponentRenderContext;
import vazkii.patchouli.api.ICustomComponent;
import vazkii.patchouli.api.IVariable;
import java.util.Arrays;
import java.util.function.Function;
import java.util.function.UnaryOperator;
/**
* A custom component that renders a mana bar.
@@ -32,17 +34,12 @@ public class ManaComponent implements ICustomComponent {
private transient int x, y;
private transient int[] manaValues;
public String mana;
public IVariable mana;
@Override
public void build(int componentX, int componentY, int pageNum) {
this.x = componentX != -1 ? componentX : 7;
this.y = componentY;
if (mana.length() > 0) {
this.manaValues = Arrays.stream(mana.split(";")).mapToInt(Integer::valueOf).toArray();
} else {
this.manaValues = new int[] { 0 };
}
}
@Override
@@ -63,7 +60,8 @@ public class ManaComponent implements ICustomComponent {
}
@Override
public void onVariablesAvailable(Function<String, String> lookup) {
mana = lookup.apply(mana);
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() };
}
}
@@ -10,27 +10,32 @@ package vazkii.botania.client.patchouli.component;
import net.minecraft.item.crafting.Ingredient;
import vazkii.patchouli.api.IVariable;
import vazkii.patchouli.api.PatchouliAPI;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
/**
* Patchouli custom component that draws a rotating circle of items from a provided list.
*/
public class RotatingItemListComponent extends RotatingItemListComponentBase {
public List<String> ingredients;
public List<IVariable> ingredients;
private transient List<Ingredient> theIngredients = new ArrayList<>();
@Override
protected List<Ingredient> makeIngredients() {
return ingredients.stream().map(PatchouliAPI.instance::deserializeIngredient).collect(Collectors.toList());
return theIngredients;
}
@Override
public void onVariablesAvailable(Function<String, String> lookup) {
public void onVariablesAvailable(UnaryOperator<IVariable> lookup) {
theIngredients.clear();
for (int i = 0; i < ingredients.size(); i++) {
ingredients.set(i, lookup.apply(ingredients.get(i)));
theIngredients.add(lookup.apply(ingredients.get(i)).as(Ingredient.class));
}
}
}
@@ -16,10 +16,11 @@ import net.minecraft.item.crafting.Ingredient;
import net.minecraft.util.ResourceLocation;
import vazkii.botania.common.crafting.ModRecipeTypes;
import vazkii.patchouli.api.IVariable;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.UnaryOperator;
/**
* Patchouli custom component that draws a rotating circle of items from the defined recipe.
@@ -49,8 +50,8 @@ public class RotatingRecipeComponent extends RotatingItemListComponentBase {
}
@Override
public void onVariablesAvailable(Function<String, String> lookup) {
recipeName = lookup.apply(recipeName);
recipeType = lookup.apply(recipeType);
public void onVariablesAvailable(UnaryOperator<IVariable> lookup) {
recipeName = lookup.apply(IVariable.wrap(recipeName)).asString();
recipeType = lookup.apply(IVariable.wrap(recipeType)).asString();
}
}
@@ -14,9 +14,10 @@ import net.minecraft.item.ItemStack;
import vazkii.patchouli.api.IComponentRenderContext;
import vazkii.patchouli.api.ICustomComponent;
import vazkii.patchouli.api.IVariable;
import vazkii.patchouli.api.PatchouliAPI;
import java.util.function.Function;
import java.util.function.UnaryOperator;
/**
* Patchouli custom component that draws provided stacks arranged like the Terrestial Agglomeration Plate multiblock.
@@ -24,10 +25,10 @@ import java.util.function.Function;
* Parameters: corner, center, edge, plate can be provided to override default blocks.
*/
public class TerraPlateComponent implements ICustomComponent {
public String corner = "botania:livingrock";
public String center = "botania:livingrock";
public String edge = "minecraft:lapis_block";
public String plate = "botania:terra_plate";
public IVariable corner = IVariable.wrap("botania:livingrock");
public IVariable center = IVariable.wrap("botania:livingrock");
public IVariable edge = IVariable.wrap("minecraft:lapis_block");
public IVariable plate = IVariable.wrap("botania:terra_plate");
private transient int x, y;
private transient ItemStack cornerBlock, centerBlock, middleBlock, plateBlock;
@@ -36,10 +37,6 @@ public class TerraPlateComponent implements ICustomComponent {
public void build(int componentX, int componentY, int pageNum) {
this.x = componentX;
this.y = componentY;
this.cornerBlock = PatchouliAPI.instance.deserializeItemStack(corner);
this.centerBlock = PatchouliAPI.instance.deserializeItemStack(center);
this.middleBlock = PatchouliAPI.instance.deserializeItemStack(edge);
this.plateBlock = PatchouliAPI.instance.deserializeItemStack(plate);
}
@Override
@@ -72,10 +69,10 @@ public class TerraPlateComponent implements ICustomComponent {
}
@Override
public void onVariablesAvailable(Function<String, String> lookup) {
corner = lookup.apply(corner);
center = lookup.apply(center);
edge = lookup.apply(edge);
plate = lookup.apply(plate);
public void onVariablesAvailable(UnaryOperator<IVariable> lookup) {
cornerBlock = lookup.apply(corner).as(ItemStack.class);
centerBlock = lookup.apply(center).as(ItemStack.class);
middleBlock = lookup.apply(edge).as(ItemStack.class);
plateBlock = lookup.apply(plate).as(ItemStack.class);
}
}
@@ -19,15 +19,19 @@ import vazkii.botania.common.Botania;
import vazkii.botania.common.crafting.ModRecipeTypes;
import vazkii.botania.common.item.ModItems;
import vazkii.patchouli.api.IComponentProcessor;
import vazkii.patchouli.api.IVariable;
import vazkii.patchouli.api.IVariableProvider;
import vazkii.patchouli.api.PatchouliAPI;
import java.util.Arrays;
import java.util.stream.Collectors;
public class BrewRecipeProcessor implements IComponentProcessor {
private IBrewRecipe recipe;
@Override
public void setup(IVariableProvider<String> variables) {
ResourceLocation id = new ResourceLocation(variables.get("recipe"));
public void setup(IVariableProvider variables) {
ResourceLocation id = new ResourceLocation(variables.get("recipe").asString());
IRecipe<?> recipe = Minecraft.getInstance().world.getRecipeManager().getRecipes(ModRecipeTypes.BREW_TYPE).get(id);
if (recipe instanceof IBrewRecipe) {
this.recipe = (IBrewRecipe) recipe;
@@ -37,27 +41,27 @@ public class BrewRecipeProcessor implements IComponentProcessor {
}
@Override
public String process(String key) {
public IVariable process(String key) {
if (recipe == null) {
return null;
} else if (key.equals("heading")) {
return I18n.format("botaniamisc.brewOf", I18n.format(recipe.getBrew().getTranslationKey()));
return IVariable.wrap(I18n.format("botaniamisc.brewOf", I18n.format(recipe.getBrew().getTranslationKey())));
} else if (key.equals("vial")) {
return PatchouliAPI.instance.serializeItemStack(recipe.getOutput(new ItemStack(ModItems.vial)));
return IVariable.from(recipe.getOutput(new ItemStack(ModItems.vial)));
} else if (key.equals("flask")) {
return PatchouliAPI.instance.serializeItemStack(recipe.getOutput(new ItemStack(ModItems.flask)));
return IVariable.from(recipe.getOutput(new ItemStack(ModItems.flask)));
} else if (key.startsWith("input")) {
int requestedIndex = Integer.parseInt(key.substring(5)) - 1;
int indexOffset = (6 - recipe.getIngredients().size()) / 2; //Center the brew ingredients
int index = requestedIndex - indexOffset;
if (index < recipe.getIngredients().size() && index >= 0) {
return PatchouliAPI.instance.serializeIngredient(recipe.getIngredients().get(index));
return IVariable.wrapList(Arrays.stream(recipe.getIngredients().get(index).getMatchingStacks()).map(IVariable::from).collect(Collectors.toList()));
} else {
return null;
}
} else if (key.equals("is_offset")) {
return Boolean.toString(recipe.getIngredients().size() % 2 == 0);
return IVariable.wrap(recipe.getIngredients().size() % 2 == 0);
}
return null;
}
@@ -21,8 +21,8 @@ import vazkii.botania.client.patchouli.PatchouliUtils;
import vazkii.botania.common.Botania;
import vazkii.botania.common.crafting.ModRecipeTypes;
import vazkii.patchouli.api.IComponentProcessor;
import vazkii.patchouli.api.IVariable;
import vazkii.patchouli.api.IVariableProvider;
import vazkii.patchouli.api.PatchouliAPI;
import java.util.List;
import java.util.stream.Collectors;
@@ -32,10 +32,10 @@ public class ElvenTradeProcessor implements IComponentProcessor {
private int longestIngredientSize, mostInputs, mostOutputs;
@Override
public void setup(IVariableProvider<String> variables) {
public void setup(IVariableProvider variables) {
ImmutableList.Builder<IElvenTradeRecipe> builder = ImmutableList.builder();
for (String s : variables.get("recipes").split(";")) {
IRecipe<?> recipe = Minecraft.getInstance().world.getRecipeManager().getRecipes(ModRecipeTypes.ELVEN_TRADE_TYPE).get(new ResourceLocation(s));
for (IVariable s : variables.get("recipes").asListOrSingleton()) {
IRecipe<?> recipe = Minecraft.getInstance().world.getRecipeManager().getRecipes(ModRecipeTypes.ELVEN_TRADE_TYPE).get(new ResourceLocation(s.asString()));
if (recipe instanceof IElvenTradeRecipe) {
builder.add((IElvenTradeRecipe) recipe);
} else {
@@ -61,12 +61,12 @@ public class ElvenTradeProcessor implements IComponentProcessor {
}
@Override
public String process(String key) {
public IVariable process(String key) {
if (recipes.isEmpty()) {
return null;
}
if (key.equals("heading")) {
return recipes.get(0).getOutputs().get(0).getDisplayName().getString();
return IVariable.wrap(recipes.get(0).getOutputs().get(0).getDisplayName().getString());
} else if (key.startsWith("input")) {
int index = Integer.parseInt(key.substring(5)) - 1;
if (index < mostInputs) {
@@ -78,17 +78,16 @@ public class ElvenTradeProcessor implements IComponentProcessor {
if (key.startsWith("output")) {
int index = Integer.parseInt(key.substring(6)) - 1;
if (index < mostOutputs) {
return recipes.stream().map(IElvenTradeRecipe::getOutputs)
return IVariable.wrapList(recipes.stream().map(IElvenTradeRecipe::getOutputs)
.map(l -> index < l.size() ? l.get(index) : ItemStack.EMPTY)
.map(PatchouliAPI.instance::serializeItemStack).collect(Collectors.joining(","));
} else {
return null;
.map(IVariable::from)
.collect(Collectors.toList()));
}
}
return null;
}
private String interweaveIngredients(int inputIndex) {
private IVariable interweaveIngredients(int inputIndex) {
List<Ingredient> recipes = this.recipes.stream().map(IElvenTradeRecipe::getIngredients).map(ingredients -> {
if (inputIndex < ingredients.size()) {
return ingredients.get(inputIndex);
@@ -25,8 +25,8 @@ import vazkii.botania.common.Botania;
import vazkii.botania.common.block.tile.mana.TilePool;
import vazkii.botania.common.crafting.ModRecipeTypes;
import vazkii.patchouli.api.IComponentProcessor;
import vazkii.patchouli.api.IVariable;
import vazkii.patchouli.api.IVariableProvider;
import vazkii.patchouli.api.PatchouliAPI;
import java.util.List;
import java.util.stream.Collectors;
@@ -36,20 +36,20 @@ public class ManaInfusionProcessor implements IComponentProcessor {
private boolean hasCustomHeading;
@Override
public void setup(IVariableProvider<String> variables) {
public void setup(IVariableProvider variables) {
if (variables.has("recipes") && variables.has("group")) {
Botania.LOGGER.warn("Mana infusion template has both 'recipes' and 'group', ignoring 'recipes'");
}
ImmutableList.Builder<IManaInfusionRecipe> builder = ImmutableList.builder();
if (variables.has("group")) {
String group = variables.get("group");
String group = variables.get("group").asString();
TilePool.manaInfusionRecipes(Minecraft.getInstance().world.getRecipeManager()).stream()
.filter(r -> r.getGroup().equals(group))
.forEach(builder::add);
} else {
for (String s : variables.get("recipes").split(";")) {
IRecipe<?> recipe = Minecraft.getInstance().world.getRecipeManager().getRecipes(ModRecipeTypes.MANA_INFUSION_TYPE).get(new ResourceLocation(s));
for (IVariable s : variables.get("recipes").asListOrSingleton()) {
IRecipe<?> recipe = Minecraft.getInstance().world.getRecipeManager().getRecipes(ModRecipeTypes.MANA_INFUSION_TYPE).get(new ResourceLocation(s.asString()));
if (recipe instanceof IManaInfusionRecipe) {
builder.add((IManaInfusionRecipe) recipe);
} else {
@@ -63,39 +63,38 @@ public class ManaInfusionProcessor implements IComponentProcessor {
}
@Override
public String process(String key) {
public IVariable process(String key) {
if (recipes.isEmpty()) {
return null;
}
switch (key) {
case "heading":
if (!hasCustomHeading) {
return recipes.get(0).getRecipeOutput().getDisplayName().getString();
return IVariable.wrap(recipes.get(0).getRecipeOutput().getDisplayName().getString());
}
return null;
case "input":
return PatchouliUtils.interweaveIngredients(recipes.stream().map(r -> r.getIngredients().get(0)).collect(Collectors.toList()));
case "output":
return recipes.stream().map(IManaInfusionRecipe::getRecipeOutput).map(PatchouliAPI.instance::serializeItemStack).collect(Collectors.joining(","));
return IVariable.wrapList(recipes.stream().map(IManaInfusionRecipe::getRecipeOutput).map(IVariable::from).collect(Collectors.toList()));
case "catalyst":
return recipes.stream().map(IManaInfusionRecipe::getCatalyst)
return IVariable.wrapList(recipes.stream().map(IManaInfusionRecipe::getCatalyst)
.map(state -> {
if (state == null) {
return ItemStack.EMPTY;
}
return new ItemStack(state.getBlock().asItem());
})
.map(PatchouliAPI.instance::serializeItemStack)
.collect(Collectors.joining(","));
.map(IVariable::from)
.collect(Collectors.toList()));
case "mana":
return recipes.stream().mapToInt(IManaInfusionRecipe::getManaToConsume).mapToObj(Integer::toString).collect(Collectors.joining(";"));
return IVariable.wrapList(recipes.stream().mapToInt(IManaInfusionRecipe::getManaToConsume).mapToObj(IVariable::wrap).collect(Collectors.toList()));
case "drop":
return I18n.format("botaniamisc.drop") + TextFormatting.BOLD + " (?)";
return IVariable.wrap(I18n.format("botaniamisc.drop") + TextFormatting.BOLD + " (?)");
case "dropTip0":
case "dropTip1": {
case "dropTip1":
ITextComponent drop = new KeybindTextComponent("key.drop").func_240699_a_(TextFormatting.GREEN);
return I18n.format("botaniamisc.dropTip" + key.substring(7), drop);
}
return IVariable.wrap(I18n.format("botaniamisc.dropTip" + key.substring(7), drop));
}
return null;
}
@@ -21,6 +21,7 @@ import net.minecraftforge.common.crafting.IShapedRecipe;
import vazkii.botania.client.patchouli.PatchouliUtils;
import vazkii.botania.common.Botania;
import vazkii.patchouli.api.IComponentProcessor;
import vazkii.patchouli.api.IVariable;
import vazkii.patchouli.api.IVariableProvider;
import vazkii.patchouli.api.PatchouliAPI;
@@ -36,9 +37,9 @@ public class MultiCraftingProcessor implements IComponentProcessor {
private boolean hasCustomHeading;
@Override
public void setup(IVariableProvider<String> variables) {
public void setup(IVariableProvider variables) {
Map<ResourceLocation, IRecipe<CraftingInventory>> recipeMap = Minecraft.getInstance().world.getRecipeManager().getRecipes(IRecipeType.CRAFTING);
String[] names = variables.get("recipes").split(";");
List<String> names = variables.get("recipes").asStream().map(IVariable::asString).collect(Collectors.toList());
this.recipes = new ArrayList<>();
for (String name : names) {
IRecipe<?> recipe = recipeMap.get(new ResourceLocation(name));
@@ -61,13 +62,13 @@ public class MultiCraftingProcessor implements IComponentProcessor {
}
@Override
public String process(String key) {
public IVariable process(String key) {
if (recipes.isEmpty()) {
return null;
}
if (key.equals("heading")) {
if (!hasCustomHeading) {
return recipes.get(0).getRecipeOutput().getDisplayName().getString();
return IVariable.wrap(recipes.get(0).getRecipeOutput().getDisplayName().getString());
}
return null;
}
@@ -95,9 +96,11 @@ public class MultiCraftingProcessor implements IComponentProcessor {
return PatchouliUtils.interweaveIngredients(ingredients, longestIngredientSize);
}
if (key.equals("output")) {
return recipes.stream().map(ICraftingRecipe::getRecipeOutput).map(PatchouliAPI.instance::serializeItemStack).collect(Collectors.joining(","));
return IVariable.wrapList(recipes.stream().map(ICraftingRecipe::getRecipeOutput).map(IVariable::from).collect(Collectors.toList()));
}
if (key.equals("shapeless")) {
return IVariable.wrap(shapeless);
}
if (key.equals("shapeless")) {}
return null;
}
}
@@ -15,15 +15,15 @@ import net.minecraft.util.ResourceLocation;
import vazkii.botania.common.Botania;
import vazkii.botania.common.crafting.ModRecipeTypes;
import vazkii.patchouli.api.IComponentProcessor;
import vazkii.patchouli.api.IVariable;
import vazkii.patchouli.api.IVariableProvider;
import vazkii.patchouli.api.PatchouliAPI;
public class PetalApothecaryProcessor implements IComponentProcessor {
protected IRecipe<?> recipe;
@Override
public void setup(IVariableProvider<String> variables) {
ResourceLocation id = new ResourceLocation(variables.get("recipe"));
public void setup(IVariableProvider variables) {
ResourceLocation id = new ResourceLocation(variables.get("recipe").asString());
this.recipe = Minecraft.getInstance().world.getRecipeManager().getRecipes(ModRecipeTypes.PETAL_TYPE).get(id);
if (recipe == null) {
Botania.LOGGER.warn("Missing apothecary recipe " + id);
@@ -31,17 +31,17 @@ public class PetalApothecaryProcessor implements IComponentProcessor {
}
@Override
public String process(String key) {
public IVariable process(String key) {
if (recipe == null) {
return null;
}
switch (key) {
case "recipe":
return recipe.getId().toString();
return IVariable.wrap(recipe.getId().toString());
case "output":
return PatchouliAPI.instance.serializeItemStack(recipe.getRecipeOutput());
return IVariable.from(recipe.getRecipeOutput());
case "heading":
return recipe.getRecipeOutput().getDisplayName().getString();
return IVariable.wrap(recipe.getRecipeOutput().getDisplayName().getString());
}
return null;
}
@@ -14,12 +14,13 @@ import net.minecraft.util.ResourceLocation;
import vazkii.botania.api.recipe.IRuneAltarRecipe;
import vazkii.botania.common.Botania;
import vazkii.botania.common.crafting.ModRecipeTypes;
import vazkii.patchouli.api.IVariable;
import vazkii.patchouli.api.IVariableProvider;
public class RunicAltarProcessor extends PetalApothecaryProcessor {
@Override
public void setup(IVariableProvider<String> variables) {
ResourceLocation id = new ResourceLocation(variables.get("recipe"));
public void setup(IVariableProvider variables) {
ResourceLocation id = new ResourceLocation(variables.get("recipe").asString());
this.recipe = Minecraft.getInstance().world.getRecipeManager().getRecipes(ModRecipeTypes.RUNE_TYPE).get(id);
if (recipe == null) {
Botania.LOGGER.warn("Missing rune altar recipe " + id);
@@ -27,9 +28,9 @@ public class RunicAltarProcessor extends PetalApothecaryProcessor {
}
@Override
public String process(String key) {
public IVariable process(String key) {
if (key.equals("mana")) {
return String.valueOf(((IRuneAltarRecipe) recipe).getManaUsage());
return IVariable.wrap(((IRuneAltarRecipe) recipe).getManaUsage());
}
return super.process(key);
}