Improve the NEI handler for the Botanical Brewery (#144)

This commit is contained in:
Nikolay S. authored and GitHub committed 2026-07-26 15:08:12 +00:00
1 parent 0e88bd8a9c
commit 073ae658b5
10 files changed
+221 -85

No files matched your search

@@ -18,7 +18,12 @@ import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect;
/**
* The class for a Brew definition, each one is a singleton.
* A type of brew that can be created in the Botanical Brewery.
*
* <p>
* Each instance of this class represents a single <i>type</i> of brew that can be created in the brewery, similarly
* to how {@link net.minecraft.item.Item} and {@link net.minecraft.block.Block} work.
* </p>
*/
public class Brew {
@@ -31,11 +36,12 @@ public class Brew {
boolean canInfuseIncense = true;
/**
* @param key The key for this brew, used in NBT and the global map.
* @param name The unlocalized name of this potion.
* @param color The color for the potion to be rendered in the bottle, note that it will get
* changed a bit when it renders (for more or less brightness) to give a fancy effect.
* @param cost The cost, in Mana for this brew.
* @param effects A list of effects to apply to the player when they drink it.
* @param color The color for the potion to be rendered in the bottle. Note that it will get
* changed a bit when it renders (for more or less brightness) to give a fancy effect.
* @param cost The cost, in Mana for this brew. Must be positive.
* @param effects A list of potion effects to apply to the player when they drink it.
*/
public Brew(String key, String name, int color, int cost, PotionEffect... effects) {
this.key = key;
@@ -101,14 +107,14 @@ public class Brew {
}
/**
* Gets the insensitive unlocalized mana cost. This is used for the lexicon.
* Get the item-insensitive mana cost. This is used for the lexicon and for crafting costs.
*/
public int getManaCost() {
return cost;
}
/**
* Gets the mana cost for the ItemStack passed in.
* Gets the mana cost for the ItemStack passed in. In standard Botania, this is used only by the Cursed Blood Pendant.
*/
public int getManaCost(ItemStack stack) {
return getManaCost();
@@ -0,0 +1,32 @@
package vazkii.botania.api.brew;
import net.minecraft.item.ItemStack;
import vazkii.botania.api.BotaniaAPI;
/**
* Utility methods for checking the properties of {@link ItemStack}s related to Brews.
*/
public final class BrewUtilities {
/**
* Check if an {@link ItemStack} represents a filled Brew.
*
* @param stack The stack to check
* @return Whether this item contains a Brew.
*/
public static boolean isFilledBrew(ItemStack stack) {
if (!(stack.getItem() instanceof IBrewItem brewItem)) return false;
final Brew brew = brewItem.getBrew(stack);
return brew != null && brew != BotaniaAPI.fallbackBrew;
}
/**
* Check if an {@link ItemStack} is an empty Brew Container - an item that can go on the middle pedestal of the Botanical Brewery.
*
* @param stack The stack to check
* @return Whether this item is a Brew Container that does not contain a Brew.
*/
public static boolean isEmptyBrewContainer(ItemStack stack) {
return stack.getItem() instanceof IBrewContainer && !isFilledBrew(stack);
}
}
@@ -10,26 +10,32 @@
*/
package vazkii.botania.api.brew;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
/**
* An Item that implements this counts as a Brew Container, by which
* it can be used a center item for brew recipes and can contain
* a brew.
* An {@link Item} that implements this counts as a Brew Container, which allows it to be used a center item in the Botanical Brewery.
*/
public interface IBrewContainer {
/**
* Returs an ItemStack that should be an item that has the brew
* passed in.
* Fills an {@link ItemStack} of this container with a {@link Brew}.
* If the brew cannot be applied to this container, returns null instead.
*
* @param brew The brew which may or may not be applicable to this container.
* @param stack The stack of this item which is being filled. This stack is not mutated.
* @return A <i>new</i> {@link ItemStack} containing an {@link IBrewItem}, or null if this brew is not applicable.
* @apiNote The returned stack does not necessarily have to be the same {@link Item} as the empty container.
*/
public ItemStack getItemForBrew(Brew brew, ItemStack stack);
ItemStack getItemForBrew(Brew brew, ItemStack stack);
/**
* Gets the cost to add this brew onto this container. Return -1
* to not allow for the brew to be added. Normally you'd
* use brew.getManaCost(stack);
* Gets the cost to add this brew onto this container. The cost must be a positive number.
*
* @param brew The brew being applied, which must be applicable to this container. {@link IBrewContainer#getItemForBrew(Brew, ItemStack)} can be used to determine whether the brew is applicable.
* @param stack A stack of this empty container. This stack is not mutated.
* @return The mana cost for this recipe, which is a positive number.
*/
public int getManaCost(Brew brew, ItemStack stack);
int getManaCost(Brew brew, ItemStack stack);
}
@@ -10,15 +10,26 @@
*/
package vazkii.botania.api.brew;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import vazkii.botania.api.BotaniaAPI;
/**
* An Item that implements this is a Brew item, by which it contains
* a brew. This is only used in vanilla to prevent the brew item
* from going back into the brewery but other mods might use it for whatever.
* An {@link Item} that implements this is an item which can contain a {@link Brew}.
* <p>
* This is used in standard Botania to prevent finished brews from going back into the brewery & to enable NEI searches.
* However, other mods might use it for other purposes.
* </p>
*/
public interface IBrewItem {
public Brew getBrew(ItemStack brew);
/**
* Get the brew contained within a stack of this item.
*
* @param brew A stack of this item whose contents are being queried.
* @return The brew within this container, or {@link BotaniaAPI#fallbackBrew} or null if the stack does not contain
* a valid brew.
*/
Brew getBrew(ItemStack brew);
}
@@ -0,0 +1,57 @@
package vazkii.botania.client.core.helper;
import codechicken.nei.ItemList;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import vazkii.botania.api.brew.BrewUtilities;
import vazkii.botania.api.brew.IBrewContainer;
import vazkii.botania.common.Botania;
import java.util.ArrayList;
import java.util.List;
public final class BrewHelper {
public static List<ItemStack> getBrewContainers() {
class Cache {
private static final List<ItemStack> BREW_CONTAINERS = List.copyOf(scanBrewContainers());
}
return Cache.BREW_CONTAINERS;
}
private static List<ItemStack> scanBrewContainers() {
ArrayList<ItemStack> containers = new ArrayList<>();
if (Botania.neiLoaded) {
for (ItemStack item : ItemList.items) {
if (item != null && BrewUtilities.isEmptyBrewContainer(item)) {
containers.add(item);
}
}
}
if (containers.isEmpty()) {
@SuppressWarnings("unchecked")
final Iterable<Item> itemRegistry = Item.itemRegistry;
ArrayList<ItemStack> potentialContainers = new ArrayList<>();
for (Item item : itemRegistry) {
if (item instanceof IBrewContainer) {
item.getSubItems(item, item.getCreativeTab(), potentialContainers);
for (ItemStack maybeContainer : potentialContainers) {
if (maybeContainer != null && BrewUtilities.isEmptyBrewContainer(maybeContainer)) {
containers.add(maybeContainer);
}
}
potentialContainers.clear();
}
}
}
return containers;
}
}
@@ -1,22 +0,0 @@
package vazkii.botania.client.integration.nei;
import cpw.mods.fml.common.event.FMLInterModComms;
import net.minecraft.nbt.NBTTagCompound;
import vazkii.botania.client.integration.nei.recipe.RecipeHandlerBrewery;
public class IMCForNEI {
public static void IMCSender() {
sendCatalyst(RecipeHandlerBrewery.OVERLAY, "Botania:vial", -1);
sendCatalyst(RecipeHandlerBrewery.OVERLAY, "Botania:vial:1", -1);
sendCatalyst(RecipeHandlerBrewery.OVERLAY, "Botania:incenseStick", -1);
sendCatalyst(RecipeHandlerBrewery.OVERLAY, "Botania:bloodPendant", -1);
}
private static void sendCatalyst(String handlerName, String stack, int priority) {
NBTTagCompound nbt = new NBTTagCompound();
nbt.setString("handlerID", handlerName);
nbt.setString("itemName", stack);
nbt.setInteger("priority", priority);
FMLInterModComms.sendMessage("NotEnoughItems", "registerCatalystInfo", nbt);
}
}
@@ -3,6 +3,7 @@ package vazkii.botania.client.integration.nei.recipe;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import net.minecraft.item.ItemStack;
import net.minecraft.util.StatCollector;
@@ -11,10 +12,15 @@ import net.minecraftforge.oredict.OreDictionary;
import org.lwjgl.opengl.GL11;
import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.api.brew.Brew;
import vazkii.botania.api.brew.BrewUtilities;
import vazkii.botania.api.brew.IBrewContainer;
import vazkii.botania.api.brew.IBrewItem;
import vazkii.botania.api.recipe.RecipeBrew;
import vazkii.botania.client.core.handler.HUDHandler;
import vazkii.botania.client.core.helper.BrewHelper;
import vazkii.botania.client.lib.LibResources;
import vazkii.botania.common.block.tile.mana.TilePool;
import vazkii.botania.common.core.helper.ItemNBTHelper;
import vazkii.botania.common.item.ModItems;
import codechicken.lib.gui.GuiDraw;
@@ -45,6 +51,7 @@ public class RecipeHandlerBrewery extends TemplateRecipeHandler {
inputs.add(new PositionedStack(toVial, 39, 42));
output = new PositionedStack(recipe.getOutput(toVial), 87, 42);
mana = ((IBrewContainer) toVial.getItem()).getManaCost(recipe.getBrew(), toVial);
}
public CachedBreweryRecipe(RecipeBrew recipe) {
@@ -98,8 +105,13 @@ public class RecipeHandlerBrewery extends TemplateRecipeHandler {
@Override
public void drawBackground(int recipe) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
// Arrows
GuiDraw.changeTexture(getGuiTexture());
GuiDraw.drawTexturedModalRect(0, 0, 0, 0, 166, 65);
// Mana Bar
HUDHandler.renderManaBar(32, 67, 0x0000FF, 0.75F, ((CachedBreweryRecipe) arecipes.get(recipe)).mana, TilePool.MAX_MANA / 10);
}
@Override
@@ -116,22 +128,38 @@ public class RecipeHandlerBrewery extends TemplateRecipeHandler {
@Override
public void loadCraftingRecipes(ItemStack result) {
if (!(result.getItem() instanceof IBrewItem brew)) return;
final Brew targetBrew = brew.getBrew(result);
if (targetBrew == null || targetBrew == BotaniaAPI.fallbackBrew) return;
// Find the container(s) that can be filled to produce this result
for (RecipeBrew recipe : BotaniaAPI.brewRecipes) {
if (recipe != null && brew.getBrew(result) == recipe.getBrew()) {
arecipes.add(new CachedBreweryRecipe(recipe));
if (recipe == null || targetBrew != recipe.getBrew()) continue;
for (ItemStack emptyContainer : BrewHelper.getBrewContainers()) {
final ItemStack filledContainer = recipe.getOutput(emptyContainer);
if (result.isItemEqual(filledContainer) && Objects.equals(result.stackTagCompound, filledContainer.stackTagCompound)) {
arecipes.add(new CachedBreweryRecipe(recipe, emptyContainer));
}
}
}
}
@Override
public void loadUsageRecipes(ItemStack ingredient) {
if (BrewUtilities.isFilledBrew(ingredient)) {
// Filled brews cannot be inserted into a Botanical Brewery.
return;
}
if (ingredient.getItem() instanceof IBrewContainer) {
// Show all possible brews that can fill this empty container
for (RecipeBrew recipe : BotaniaAPI.brewRecipes) {
if (recipe != null && recipe.getOutput(ingredient) != null) {
arecipes.add(new CachedBreweryRecipe(recipe, ingredient));
}
}
} else {
// Show all brew recipes that can use this ingredient
for (RecipeBrew recipe : BotaniaAPI.brewRecipes) {
if (recipe == null) continue;
CachedBreweryRecipe crecipe = new CachedBreweryRecipe(recipe);
@@ -142,4 +170,8 @@ public class RecipeHandlerBrewery extends TemplateRecipeHandler {
}
}
@Override
public int getRecipeHeight(int recipe) {
return 80;
}
}
@@ -22,8 +22,8 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.api.brew.BrewUtilities;
import vazkii.botania.api.brew.IBrewContainer;
import vazkii.botania.api.brew.IBrewItem;
import vazkii.botania.api.internal.VanillaPacketDispatcher;
import vazkii.botania.api.mana.IManaReceiver;
import vazkii.botania.api.recipe.RecipeBrew;
@@ -42,7 +42,7 @@ public class TileBrewery extends TileSimpleInventory implements ISidedInventory,
public int signal = 0;
public boolean addItem(EntityPlayer player, ItemStack stack) {
if(recipe != null || stack == null || stack.getItem() instanceof IBrewItem && ((IBrewItem) stack.getItem()).getBrew(stack) != null && ((IBrewItem) stack.getItem()).getBrew(stack) != BotaniaAPI.fallbackBrew || getStackInSlot(0) == null != stack.getItem() instanceof IBrewContainer)
if (recipe != null || stack == null || BrewUtilities.isFilledBrew(stack) || (getStackInSlot(0) == null) != (stack.getItem() instanceof IBrewContainer))
return false;
boolean did = false;
@@ -153,10 +153,9 @@ public class TileBrewery extends TileSimpleInventory implements ISidedInventory,
public int getManaCost() {
ItemStack stack = getStackInSlot(0);
if(recipe == null || stack == null || !(stack.getItem() instanceof IBrewContainer))
if(recipe == null || stack == null || !(stack.getItem() instanceof IBrewContainer container))
return 0;
IBrewContainer container = (IBrewContainer) stack.getItem();
return container.getManaCost(recipe.getBrew(), stack);
return container.getManaCost(recipe.getBrew(), stack);
}
public void craftingFanciness() {
@@ -33,7 +33,6 @@ import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.api.lexicon.ITwoNamedPage;
import vazkii.botania.api.lexicon.LexiconEntry;
import vazkii.botania.api.lexicon.LexiconPage;
import vazkii.botania.client.integration.nei.IMCForNEI;
import vazkii.botania.common.Botania;
import vazkii.botania.common.achievement.ModAchievements;
import vazkii.botania.common.block.ModBlocks;
@@ -153,10 +152,6 @@ public class CommonProxy {
new StatementAPIPlugin();
LexiconData.init();
if (Botania.neiLoaded) {
IMCForNEI.IMCSender();
}
}
public void postInit(FMLPostInitializationEvent event) {
@@ -19,12 +19,13 @@ import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;
import net.minecraftforge.oredict.OreDictionary;
import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.api.brew.Brew;
import vazkii.botania.api.internal.IGuiLexiconEntry;
import vazkii.botania.api.lexicon.ILexicon;
import vazkii.botania.api.lexicon.ITwoNamedPage;
import vazkii.botania.api.lexicon.LexiconRecipeMappings;
import vazkii.botania.api.recipe.RecipeBrew;
import vazkii.botania.client.core.helper.BrewHelper;
import vazkii.botania.common.item.ModItems;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@@ -51,50 +52,48 @@ public class PageBrew extends PageRecipe implements ITwoNamedPage {
FontRenderer renderer = Minecraft.getMinecraft().fontRenderer;
boolean unicode = renderer.getUnicodeFlag();
renderer.setUnicodeFlag(true);
String s = EnumChatFormatting.BOLD + String.format(StatCollector.translateToLocal("botaniamisc.brewOf"), StatCollector.translateToLocal(brew.getUnlocalizedName()));
String s = EnumChatFormatting.BOLD + StatCollector.translateToLocalFormatted("botaniamisc.brewOf", StatCollector.translateToLocal(brew.getUnlocalizedName()));
renderer.drawString(s, gui.getLeft() + gui.getWidth() / 2 - renderer.getStringWidth(s) / 2, y, 0x222222);
renderer.setUnicodeFlag(unicode);
PageText.renderText(x, y + 22, width, height, text);
ItemStack book = Minecraft.getMinecraft().thePlayer.getCurrentEquippedItem();
if(book != null && book.getItem() instanceof ILexicon && ((ILexicon) book.getItem()).isKnowledgeUnlocked(book, BotaniaAPI.elvenKnowledge)) {
renderItemAtLinePos(gui, 20, 2, y + 12, recipe.getOutput(new ItemStack(ModItems.vial)));
renderItemAtLinePos(gui, 20, 3, y + 12, recipe.getOutput(new ItemStack(ModItems.vial, 1, 1)));
} else renderItemAtLinePos(gui, 0, -1, y + 12, recipe.getOutput(new ItemStack(ModItems.vial)));
renderItemRow(gui, getOutputs(book), y + 12);
int i = 0;
y = gui.getTop() + gui.getHeight() - 54;
y = gui.getTop() + gui.getHeight() - 54;
List<Object> inputs = new ArrayList<>(recipe.getInputs());
int offset = gui.getWidth() / 2 - inputs.size() * 9;
for(Object input : inputs) {
if(input instanceof String)
input = OreDictionary.getOres((String) input).get(0);
renderItemRow(gui, inputs, y);
renderItemAtLinePos(gui, offset, i, y, (ItemStack) input);
i++;
}
super.renderRecipe(gui, mx, my);
super.renderRecipe(gui, mx, my);
}
@SideOnly(Side.CLIENT)
public void renderItemAtLinePos(IGuiLexiconEntry gui, int offset, int pos, int yPos, ItemStack stack) {
if(stack == null || stack.getItem() == null)
return;
stack = stack.copy();
@SideOnly(Side.CLIENT)
private void renderItemRow(IGuiLexiconEntry gui, List<?> inputs, int y) {
int spacing = Math.min(gui.getWidth() / inputs.size(), 18);
int x = gui.getLeft() + (gui.getWidth() - spacing * inputs.size()) / 2;
if(stack.getItemDamage() == Short.MAX_VALUE)
stack.setItemDamage(0);
for (Object input : inputs) {
ItemStack stack;
if (input instanceof String) {
stack = OreDictionary.getOres((String) input).get(0);
} else {
stack = (ItemStack) input;
}
int xPos = gui.getLeft() + (pos == -1 ? gui.getWidth() / 2 - 8 : pos * 18) + offset;
if (stack == null || stack.getItem() == null) continue;
ItemStack stack1 = stack.copy();
if(stack1.getItemDamage() == -1)
stack1.setItemDamage(0);
if (stack.getItemDamage() == Short.MAX_VALUE || stack.getItemDamage() == -1) {
ItemStack newStack = new ItemStack(stack.getItem(), stack.stackSize, 0);
newStack.stackTagCompound = stack.stackTagCompound;
stack = newStack;
}
renderItem(gui, xPos, yPos, stack1, false);
}
renderItem(gui, x, y, stack, false);
x += spacing;
}
}
@Override
public List<ItemStack> getDisplayedRecipes() {
@@ -105,6 +104,27 @@ public class PageBrew extends PageRecipe implements ITwoNamedPage {
return list;
}
private List<ItemStack> getOutputs(ItemStack lexicon) {
ArrayList<ItemStack> list = new ArrayList<>();
for (ItemStack empty : BrewHelper.getBrewContainers()) {
if (lexicon != null && lexicon.getItem() instanceof ILexicon lexItem) {
// Check if the lexicon has this item unlocked.
LexiconRecipeMappings.EntryData entry = LexiconRecipeMappings.getDataForStack(empty);
if (entry != null && (!entry.entry.isVisible() || !lexItem.isKnowledgeUnlocked(lexicon, entry.entry.getKnowledgeType()))) {
continue;
}
}
ItemStack filled = recipe.getOutput(empty);
if (filled != null) {
list.add(filled);
}
}
return list;
}
@Override
public void setSecondUnlocalizedName(String name) {
text = name;