Compare commits

...
Author SHA1 Message Date
Connor Colenso addb2f92e1 Stop craftable fluids drawing their amount twice
The craftables panel drew the stack with its amount, so NEI's fluid item
drew the amount and drawStackSize drew it again on top. Draw fluids
without their amount, as bookmarks already do.
2026-11-26 23:48:17 +00:00
Connor Colenso b784769426 Show fluid amounts over slots in compact form, without the L
Fluid amounts drawn over slots drop their L, since 1 L is 1 mB, a unit
doesn't fit in a slot and it ignored the L/mB setting anyway. With GTNHLib
loaded they use its compact formatting, so they follow the player's number
settings: exact up to 9,999, then 12.3K, 1.5M, rounded down. Without it
they keep the old short form. Covers the fluid display item, bookmarks,
craftables, the crafting tree and item rows in tooltips. Item counts are
unchanged.
2026-11-26 23:47:50 +00:00
a665a5be56 Fix item hotkeys on crafting tree Crafting slots (#1151)
Co-authored-by: Lorenzo <[email protected]>
Co-authored-by: Chitak 985 <[email protected]>
Co-authored-by: slprime <[email protected]>
2026-11-25 09:08:16 +02:00
slprime bdadcc37a6 Extract inventory and missing ingredient helpers (#1164) 2026-11-25 01:04:09 +02:00
14 changed files with 112 additions and 117 deletions

No files matched your search

@@ -18,7 +18,6 @@ import codechicken.lib.vec.Rectangle4i;
import codechicken.nei.ItemsGrid.ItemsGridSlot;
import codechicken.nei.ItemsGrid.MouseContext;
import codechicken.nei.NEIClientUtils.Alignment;
import codechicken.nei.recipe.AutoCraftingManager;
import codechicken.nei.recipe.GuiCraftingRecipe;
import codechicken.nei.recipe.GuiRecipe;
import codechicken.nei.recipe.ICraftingHandler;
@@ -27,7 +26,6 @@ import codechicken.nei.recipe.Recipe;
import codechicken.nei.recipe.Recipe.RecipeId;
import codechicken.nei.recipe.RecipeInfo;
import codechicken.nei.recipe.StackInfo;
import codechicken.nei.util.ReadableNumberConverter;
public class ItemCraftablesPanel
extends AbstractSubpanel<ItemsGrid<ItemCraftablesPanel.CraftablesGridSlot, MouseContext>> {
@@ -37,12 +35,15 @@ public class ItemCraftablesPanel
protected final long realAmount;
protected final boolean isFluidDisplay;
protected final RecipeId recipeId;
protected final ItemStack drawnItemStack;
public CraftablesGridSlot(int slotIndex, int itemIndex, ItemStack itemStack, RecipeId recipeId) {
super(slotIndex, itemIndex, itemStack);
this.realAmount = StackInfo.getAmount(itemStack);
this.isFluidDisplay = StackInfo.isFluidDisplayItem(itemStack);
this.recipeId = recipeId;
final ItemStack empty = this.isFluidDisplay ? StackInfo.withAmount(itemStack, 0) : null;
this.drawnItemStack = empty != null ? empty : itemStack;
}
@Override
@@ -50,6 +51,11 @@ public class ItemCraftablesPanel
return this.recipeId;
}
@Override
public void drawItem(Rectangle4i rect) {
super.drawItem(this.drawnItemStack, rect);
}
@Override
public <M extends MouseContext> void afterDraw(Rectangle4i rect, M mouseContext) {
drawStackSize(rect);
@@ -60,17 +66,7 @@ public class ItemCraftablesPanel
if (stackSize > 1) {
final float panelFactor = (rect.w - 2) / (DEFAULT_SLOT_SIZE - 2);
String amountString = "";
if (stackSize < 10_000) {
amountString = String.valueOf(stackSize);
} else {
amountString = ReadableNumberConverter.INSTANCE.toWideReadableForm(stackSize);
}
if (this.isFluidDisplay) {
amountString += "L";
}
final String amountString = NEIClientUtils.formatSlotAmount(stackSize, this.isFluidDisplay);
NEIClientUtils.drawNEIOverlayText(
amountString,
@@ -268,7 +264,7 @@ public class ItemCraftablesPanel
if (firstGui != null && !ItemCraftablesPanel.guiBlacklist.contains(firstGui.getClass().getName())) {
final boolean showOnlyFavorites = NEIClientConfig
.getBooleanSetting("inventory.craftables.favoritesOnly");
final ItemStackAmount inv = AutoCraftingManager.getInventoryItems(firstGui);
final ItemStackAmount inv = ItemStackAmount.of(firstGui);
if (this.lastGuiContainer != firstGui
|| showOnlyFavorites && this.lastFavoritesCount != FavoriteRecipes.size()
@@ -317,7 +313,7 @@ public class ItemCraftablesPanel
final Map<ItemStack, RecipeId> availableRecipes = new HashMap<>();
if (!availableHandlers.isEmpty()) {
final Map<Item, List<ItemStack>> invByItem = indexInventoryByItem(snapshot.inventory.values());
final Map<Item, List<ItemStack>> invByItem = snapshot.inventory.groupByItem();
if (snapshot.showOnlyFavorites) {
for (IRecipeHandler handler : availableHandlers) {
@@ -349,16 +345,6 @@ public class ItemCraftablesPanel
return availableHandlers;
}
private static Map<Item, List<ItemStack>> indexInventoryByItem(List<ItemStack> invStacks) {
final Map<Item, List<ItemStack>> index = new HashMap<>();
for (ItemStack stack : invStacks) {
index.computeIfAbsent(stack.getItem(), i -> new ArrayList<>()).add(stack);
}
return index;
}
private void collectHandlerCandidates(IRecipeHandler handler, Map<Item, List<ItemStack>> invByItem,
List<RecipeCandidate> candidates) {
IntStream.range(0, handler.numRecipes()).parallel().forEach(recipeIndex -> {
@@ -448,36 +434,8 @@ public class ItemCraftablesPanel
return availableRecipes;
}
private boolean existsIngredients(List<PositionedStack> ingredients, Map<Item, List<ItemStack>> invByItem) {
final Map<ItemStack, Integer> usedItems = new HashMap<>();
for (PositionedStack pStack : ingredients) {
ItemStack used = null;
for (ItemStack permutation : pStack.items) {
final List<ItemStack> candidates = invByItem.get(permutation.getItem());
if (candidates == null) continue;
for (ItemStack is : candidates) {
if (usedItems.getOrDefault(is, is.stackSize) >= pStack.item.stackSize
&& NEIServerUtils.areStacksSameTypeCrafting(permutation, is)) {
used = is;
break;
}
}
if (used != null) break;
}
if (used != null) {
usedItems.put(used, usedItems.getOrDefault(used, used.stackSize) - pStack.item.stackSize);
} else {
return false;
}
}
return !usedItems.isEmpty();
private static boolean existsIngredients(List<PositionedStack> ingredients, Map<Item, List<ItemStack>> invByItem) {
return !ingredients.isEmpty() && NEIClientUtils.countMissingIngredients(ingredients, invByItem, 1) == 0;
}
}
@@ -1,12 +1,17 @@
package codechicken.nei;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
@@ -96,6 +101,16 @@ public class ItemStackAmount {
return list;
}
public Map<Item, List<ItemStack>> groupByItem() {
final Map<Item, List<ItemStack>> groups = new HashMap<>();
for (ItemStack stack : values()) {
groups.computeIfAbsent(stack.getItem(), i -> new ArrayList<>()).add(stack);
}
return groups;
}
public int size() {
return this.itemMap.size();
}
@@ -140,4 +155,16 @@ public class ItemStackAmount {
return result;
}
public static ItemStackAmount of(GuiContainer gui) {
final ItemStackAmount result = new ItemStackAmount();
for (Slot slot : gui.inventorySlots.inventorySlots) {
if (slot.getHasStack() && !(slot instanceof SlotCrafting) && slot.canTakeStack(gui.mc.thePlayer)) {
result.add(slot.getStack());
}
}
return result;
}
}
@@ -68,10 +68,9 @@ public class ItemsTooltipLineHandler implements ITooltipLineHandler {
protected void drawAmount(Rectangle4i rect, ItemStack stack, long amount) {
final AmountText amountText = this.cache.computeIfAbsent(stack, key -> {
final boolean isFluidDisplay = StackInfo.isFluidDisplayItem(key);
final String text = formatAmount(key, amount);
return new AmountText(
isFluidDisplay && !text.isEmpty() ? text + "L" : text,
formatAmount(key, amount),
isFluidDisplay ? Alignment.BottomLeft : Alignment.BottomRight);
});
@@ -81,7 +80,7 @@ public class ItemsTooltipLineHandler implements ITooltipLineHandler {
}
protected String formatAmount(ItemStack stack, long amount) {
return amount == 0 ? "" : ReadableNumberConverter.INSTANCE.toWideReadableForm(amount);
return amount == 0 ? "" : NEIClientUtils.formatSlotAmount(amount, StackInfo.isFluidDisplayItem(stack));
}
}
@@ -53,6 +53,7 @@ import org.lwjgl.opengl.GL11;
import com.google.common.collect.Iterables;
import com.gtnewhorizon.gtnhlib.util.numberformatting.NumberFormatUtil;
import com.gtnewhorizon.gtnhlib.util.numberformatting.options.CompactOptions;
import codechicken.lib.gui.GuiDraw;
import codechicken.lib.inventory.InventoryRange;
@@ -65,6 +66,7 @@ import codechicken.nei.api.ItemInfo;
import codechicken.nei.recipe.GuiOverlayButton.ItemOverlayState;
import codechicken.nei.recipe.StackInfo;
import codechicken.nei.util.NEIKeyboardUtils;
import codechicken.nei.util.ReadableNumberConverter;
public class NEIClientUtils extends NEIServerUtils {
@@ -154,6 +156,23 @@ public class NEIClientUtils extends NEIServerUtils {
return getDecimalFormat().format(aNumber) + " L";
}
/** Only loaded when GTNHLib is, since CompactOptions comes from it. */
private static final class FluidSlotAmountFormat {
private static final CompactOptions OPTIONS = new CompactOptions().setCompactThreshold(10_000)
.setDecimalPlaces(1).setRoundingMode(RoundingMode.DOWN);
}
/** The amount drawn over a slot. Fluids use GTNHLib's compact formatting when it's loaded and have no unit. */
public static String formatSlotAmount(long amount, boolean isFluid) {
if (isFluid && NEIModContainer.isGTNHLibLoaded()) {
return NumberFormatUtil.formatNumberCompact(amount, FluidSlotAmountFormat.OPTIONS);
}
return amount < 10_000 ? String.valueOf(amount) : ReadableNumberConverter.INSTANCE.toWideReadableForm(amount);
}
public static String formatChance(float chance) {
return CHANCE_FORMAT.format(chance);
}
@@ -538,6 +557,40 @@ public class NEIClientUtils extends NEIServerUtils {
return null;
}
public static int countMissingIngredients(List<PositionedStack> ingredients, Map<Item, List<ItemStack>> index,
int limit) {
Map<ItemStack, Integer> usedItems = new HashMap<>();
int missing = 0;
for (PositionedStack pStack : ingredients) {
ItemStack used = null;
for (ItemStack permutation : pStack.items) {
final List<ItemStack> candidates = index.get(permutation.getItem());
if (candidates == null) continue;
for (ItemStack is : candidates) {
if (usedItems.getOrDefault(is, is.stackSize) >= pStack.item.stackSize
&& NEIServerUtils.areStacksSameTypeCrafting(permutation, is)) {
used = is;
break;
}
}
if (used != null) break;
}
if (used != null) {
usedItems.put(used, usedItems.getOrDefault(used, used.stackSize) - pStack.item.stackSize);
} else if (++missing >= limit) {
return missing;
}
}
return missing;
}
public static boolean altKey() {
return Keyboard.isKeyDown(Keyboard.KEY_LMENU) || Keyboard.isKeyDown(Keyboard.KEY_RMENU);
}
@@ -21,6 +21,7 @@ import codechicken.nei.BookmarkContainerInfo;
import codechicken.nei.FavoriteRecipes;
import codechicken.nei.ItemPanels;
import codechicken.nei.ItemQuantityField;
import codechicken.nei.ItemStackAmount;
import codechicken.nei.KeyManager;
import codechicken.nei.LayoutManager;
import codechicken.nei.NEIClientConfig;
@@ -717,7 +718,7 @@ public abstract class ShortcutInputHandler {
private static void autocraftingIgnoreInventory(RecipeChainMath math) {
final GuiContainer guiContainer = NEIClientUtils.getGuiContainer();
final List<ItemStack> playerInventory = AutoCraftingManager.getInventoryItems(guiContainer).values();
final List<ItemStack> playerInventory = ItemStackAmount.of(guiContainer).values();
final RecipeId rootRecipeId = math.createMasterRoot();
for (int i = 0; i < math.recipeIngredients.size(); i++) {
@@ -363,17 +363,7 @@ public class BookmarksGridSlot extends ItemsGridSlot {
final float panelFactor = (rect.w - 2) / (DEFAULT_SLOT_SIZE - 2);
final long stackSize = this.bookmarkItem.getStackSize(amount);
int color = 0xFFFFFF;
String amountString = "";
if (stackSize < 10_000) {
amountString = String.valueOf(stackSize);
} else {
amountString = ReadableNumberConverter.INSTANCE.toWideReadableForm(stackSize);
}
if (this.isFluidDisplay) {
amountString += "L";
}
String amountString = NEIClientUtils.formatSlotAmount(stackSize, this.isFluidDisplay);
if (this.bookmarkItem.chance != PositionedStack.CHANCE_FULL) {
amountString = "~" + amountString;
@@ -29,7 +29,6 @@ import codechicken.nei.drawable.DrawableResource;
import codechicken.nei.guihook.GuiContainerManager;
import codechicken.nei.recipe.Recipe.RecipeId;
import codechicken.nei.scroll.GuiHelper;
import codechicken.nei.util.ReadableNumberConverter;
class CraftingTreeCanvas {
@@ -409,12 +408,7 @@ class CraftingTreeCanvas {
}
protected void drawStackSize(ItemTreeSlot node, long stackSize, int x, int y, boolean resolved) {
String amountString = stackSize < 10_000 ? String.valueOf(stackSize)
: ReadableNumberConverter.INSTANCE.toWideReadableForm(stackSize);
if (node.isFluidDisplay) {
amountString += "L";
}
String amountString = NEIClientUtils.formatSlotAmount(stackSize, node.isFluidDisplay);
final boolean hasOutputChance = node.prefItem != null && node.prefItem.chance != PositionedStack.CHANCE_FULL;
int color = 0xFFFFFF;
@@ -36,6 +36,7 @@ import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import codechicken.lib.gui.GuiDraw;
import codechicken.nei.ItemStackAmount;
import codechicken.nei.KeyManager;
import codechicken.nei.LRUCache;
import codechicken.nei.NEICPH;
@@ -60,7 +61,6 @@ import codechicken.nei.guihook.IGuiClientSide;
import codechicken.nei.guihook.IGuiHandleMouseWheel;
import codechicken.nei.guihook.IGuiHotkeyListener;
import codechicken.nei.recipe.AcceptsFollowingTooltipLineHandler;
import codechicken.nei.recipe.AutoCraftingManager;
import codechicken.nei.recipe.GuiCraftingRecipe;
import codechicken.nei.recipe.GuiOverlayButton;
import codechicken.nei.recipe.GuiRecipe;
@@ -284,10 +284,8 @@ public class GuiCraftingTree extends GuiContainer implements INEIGuiHandler, IGu
final GuiContainer firstGui = getFirstScreen();
if (firstGui != null) {
for (ItemStack stack : AutoCraftingManager.getInventoryItems(firstGui).values()) {
if (stack != null) {
this.math.initialItems.add(BookmarkItem.of(-1, stack));
}
for (ItemStack stack : ItemStackAmount.of(firstGui).values()) {
this.math.initialItems.add(BookmarkItem.of(-1, stack));
}
}
}
@@ -143,12 +143,7 @@ class StatsPanel extends ScrollContainer {
}
private void drawStackSize() {
String amountString = stackSize < 10_000 ? String.valueOf(stackSize)
: ReadableNumberConverter.INSTANCE.toWideReadableForm(stackSize);
if (this.isFluidDisplay) {
amountString += "L";
}
final String amountString = NEIClientUtils.formatSlotAmount(stackSize, this.isFluidDisplay);
NEIClientUtils.drawNEIOverlayText(
amountString,
@@ -524,7 +519,7 @@ class StatsPanel extends ScrollContainer {
public ItemStack getStackMouseOver(int mouseX, int mouseY) {
final Widget widget = getWidgetUnderMouse(mouseX, mouseY);
return widget instanceof ItemStatsSlot slot && slot.recipeId == null ? slot.getStack() : null;
return widget instanceof ItemStatsSlot slot ? slot.getStack() : null;
}
public void setHighlight(SearchResult searchResult) {
@@ -640,7 +640,7 @@ public class GuiContainerManager {
tooltip = handler.handleTooltip(window, mousex, mousey, tooltip);
}
ItemStack stack = getStackMouseOver(window);
ItemStack stack = tooltip.isEmpty() ? getStackMouseOver(window) : null;
boolean showTooltip = shouldShowTooltip(window);
if (tooltip.isEmpty() && showTooltip) { // mouseover tip, not holding an item
font = getFontRenderer(stack);
@@ -9,8 +9,8 @@ import net.minecraftforge.fluids.FluidRegistry;
import org.lwjgl.opengl.GL11;
import codechicken.nei.NEIClientUtils;
import codechicken.nei.item.FluidDrawer.FillDirection;
import codechicken.nei.util.ReadableNumberConverter;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@@ -48,14 +48,7 @@ public class FluidDisplayRenderer implements IItemRenderer {
return;
}
String amountString = "";
if (amount < 10_000) {
amountString = String.valueOf(amount) + "L";
} else {
amountString = ReadableNumberConverter.INSTANCE.toWideReadableForm(amount) + "L";
}
final String amountString = NEIClientUtils.formatSlotAmount(amount, true);
final FontRenderer fontRender = Minecraft.getMinecraft().fontRenderer;
float smallTextScale = fontRender.getUnicodeFlag() ? 3F / 4F : 1F / 2F;
GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
@@ -32,7 +32,7 @@ public class AutoCraftingManager {
public void execute() {
final GuiContainer guiContainer = NEIClientUtils.getGuiContainer();
final ItemStack[] layout = snapshotLayout(guiContainer);
final List<BookmarkItem> initialItems = prepareInitialItems(math, getInventoryItems(guiContainer));
final List<BookmarkItem> initialItems = prepareInitialItems(math, ItemStackAmount.of(guiContainer));
boolean processed = false;
boolean changed = false;
@@ -43,7 +43,7 @@ public class AutoCraftingManager {
changed = false;
RecipeChainIterator iterator = new RecipeChainIterator(math, initialItems);
iterator.updateInventory(getInventoryItems(guiContainer));
iterator.updateInventory(ItemStackAmount.of(guiContainer));
while (iterator.hasNext() && !interrupted(guiContainer)) {
final Map<RecipeId, Long> recipes = iterator.next();
@@ -69,7 +69,7 @@ public class AutoCraftingManager {
if (craft) {
changed = true;
processed = true;
iterator.updateInventory(getInventoryItems(guiContainer));
iterator.updateInventory(ItemStackAmount.of(guiContainer));
}
}
@@ -138,18 +138,6 @@ public class AutoCraftingManager {
return AutoCraftingManager.math != null && !task.interrupted();
}
public static ItemStackAmount getInventoryItems(GuiContainer guiContainer) {
final ItemStackAmount inventory = new ItemStackAmount();
for (Slot slot : guiContainer.inventorySlots.inventorySlots) {
if (slot.getHasStack() && !(slot instanceof SlotCrafting) && slot.canTakeStack(guiContainer.mc.thePlayer)) {
inventory.add(slot.getStack());
}
}
return inventory;
}
private static ItemStack[] snapshotLayout(GuiContainer guiContainer) {
final List<Slot> slots = guiContainer.inventorySlots.inventorySlots;
final ItemStack[] layout = new ItemStack[slots.size()];
@@ -271,7 +271,7 @@ public class RecipeItemsTooltipLineHandler extends CompositeTooltipLineHandler {
}
if (screen instanceof GuiContainer container) {
return AutoCraftingManager.getInventoryItems(container);
return ItemStackAmount.of(container);
}
return new ItemStackAmount();
@@ -27,7 +27,6 @@ import codechicken.nei.NEIClientUtils;
import codechicken.nei.bookmark.BookmarkItem;
import codechicken.nei.drawable.DrawableResource;
import codechicken.nei.guihook.GuiContainerManager;
import codechicken.nei.recipe.AutoCraftingManager;
import codechicken.nei.recipe.GuiRecipe;
import codechicken.nei.recipe.GuiRecipeTab;
import codechicken.nei.recipe.HandlerInfo;
@@ -137,7 +136,7 @@ public class RecipeChainTooltipLineHandler extends CompositeTooltipLineHandler {
final GuiContainer currentGui = NEIClientUtils.getGuiContainer();
if (this.lastShiftKey && currentGui != null && !(currentGui instanceof GuiRecipe<?>)) {
inventory.putAll(AutoCraftingManager.getInventoryItems(currentGui));
inventory.putAll(ItemStackAmount.of(currentGui));
}
if (!this.outputRecipes.isEmpty()) {