From b60769864fddd637c64b6eab10522b2f6ca207bc Mon Sep 17 00:00:00 2001 From: koolkrafter5 Date: Mon, 30 Mar 2026 01:19:34 -0400 Subject: [PATCH] Improve image exporter data dumper (#902) --- .../nei/config/ItemPanelDumper.java | 101 ++++++++++++++---- src/main/resources/assets/nei/lang/en_US.lang | 2 + 2 files changed, 82 insertions(+), 21 deletions(-) diff --git a/src/main/java/codechicken/nei/config/ItemPanelDumper.java b/src/main/java/codechicken/nei/config/ItemPanelDumper.java index f18dce7e..a7c16d4b 100644 --- a/src/main/java/codechicken/nei/config/ItemPanelDumper.java +++ b/src/main/java/codechicken/nei/config/ItemPanelDumper.java @@ -5,6 +5,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.LinkedList; +import java.util.List; import net.minecraft.client.Minecraft; import net.minecraft.item.Item; @@ -23,7 +24,11 @@ import codechicken.nei.guihook.GuiContainerManager; public class ItemPanelDumper extends DataDumper { - private static final int[] resolutions = new int[] { 16, 32, 48, 64, 128, 256 }; + public static final int CSV = 0; + public static final int NBT = 1; + public static final int JSON = 2; + public static final int PNG = 3; + private static final Minecraft mc = Minecraft.getMinecraft(); public ItemPanelDumper(String name) { super(name); @@ -54,19 +59,29 @@ public class ItemPanelDumper extends DataDumper { public int getRes() { int i = renderTag(name + ".res").getIntValue(0); - if (i >= resolutions.length || i < 0) renderTag().setIntValue(i = 0); - return resolutions[i]; + int size = 16 << i; + if (size >= getSmallerScreenAxisSize() || i < 0) { + getTag(name + ".res").setIntValue(0); + size = 16; + } + return size; + } + + private int getSmallerScreenAxisSize() { + int width = mc.displayWidth; + int height = mc.displayHeight; + return Math.min(width, height); } public Rectangle4i resButtonSize() { - int width = 50; + int width = 60; return new Rectangle4i(modeButtonSize().x - width - 6, 0, width, 20); } @Override public void draw(int mousex, int mousey, float frame) { super.draw(mousex, mousey, frame); - if (getMode() == 3) { + if (getMode() == PNG) { int res = getRes(); drawButton(mousex, mousey, resButtonSize(), res + "x" + res); } @@ -74,23 +89,35 @@ public class ItemPanelDumper extends DataDumper { @Override public void mouseClicked(int mousex, int mousey, int button) { - if (getMode() == 3 && resButtonSize().contains(mousex, mousey)) { + if (getMode() != PNG) { + super.mouseClicked(mousex, mousey, button); + return; + } + if (resButtonSize().contains(mousex, mousey)) { NEIClientUtils.playClickSound(); - getTag(name + ".res").setIntValue((renderTag(name + ".res").getIntValue(0) + 1) % resolutions.length); - } else super.mouseClicked(mousex, mousey, button); + int current = renderTag(name + ".res").getIntValue(0); + int next; + // 31 - numberOfLeadingZeros(x) = floor(log2(x)) + int max = 31 - Integer.numberOfLeadingZeros(getSmallerScreenAxisSize()) - 3; + if (button == 1) { // Right click to cycle backward + next = (current - 1 + max) % max; + } else { // Left click to cycle forward + next = (current + 1) % max; + } + getTag(name + ".res").setIntValue(next); + } else if (!dumpButtonSize().contains(mousex, mousey) || acknowledgedItemSizeWarning()) { + super.mouseClicked(mousex, mousey, button); + } } @Override public String getFileExtension() { - switch (getMode()) { - case 0: - return ".csv"; - case 1: - return ".nbt"; - case 2: - return ".json"; - } - return null; + return switch (getMode()) { + case CSV -> ".csv"; + case NBT -> ".nbt"; + case JSON -> ".json"; + default -> null; + }; } @Override @@ -105,15 +132,29 @@ public class ItemPanelDumper extends DataDumper { @Override public void dumpFile() { - if (getMode() == 3) Minecraft.getMinecraft().displayGuiScreen(new GuiItemIconDumper(this, getRes())); + if (getMode() == PNG && acknowledgedItemSizeWarning()) + mc.displayGuiScreen(new GuiItemIconDumper(this, getRes())); else super.dumpFile(); } + private boolean acknowledgedItemSizeWarning() { + return !showItemSizeWarning() || NEIClientUtils.shiftKey(); + } + + private boolean showItemSizeWarning() { + int res = getRes(); + // More than 4 items at 2048x2048, 16 items at 1024x1024, 64 at 512x512, etc. + int WARNING_SIZE = 16_777_216; + return ItemPanels.itemPanel.getItems().size() * res * res > WARNING_SIZE; + } + @Override public void dumpTo(File file) throws IOException { - if (getMode() == 0) super.dumpTo(file); - else if (getMode() == 1) dumpNBT(file); - else dumpJson(file); + switch (getMode()) { + case CSV -> super.dumpTo(file); + case NBT -> dumpNBT(file); + default -> dumpJson(file); + } } public void dumpNBT(File file) throws IOException { @@ -141,4 +182,22 @@ public class ItemPanelDumper extends DataDumper { public int modeCount() { return 4; } + + @Override + public List handleTooltip(int mousex, int mousey, List currenttip) { + if (getMode() == PNG && dumpButtonSize().contains(mousex, mousey) && showItemSizeWarning()) { + int res = getRes(); + currenttip.add( + NEIClientUtils.translate( + "options.tools.dump.itempanel.icon.warning.1", + ItemPanels.itemPanel.getItems().size(), + res, + res)); + currenttip.add( + NEIClientUtils.translate( + "options.tools.dump.itempanel.icon.warning.2", + NEIClientUtils.translate("key.shift"))); + } + return currenttip; + } } diff --git a/src/main/resources/assets/nei/lang/en_US.lang b/src/main/resources/assets/nei/lang/en_US.lang index 7c772007..bb3f98b0 100644 --- a/src/main/resources/assets/nei/lang/en_US.lang +++ b/src/main/resources/assets/nei/lang/en_US.lang @@ -519,6 +519,8 @@ nei.options.tools.dump.itempanel.mode.2=Json nei.options.tools.dump.itempanel.mode.3=PNG nei.options.tools.dump.itempanel.icon.dumped=Item Panel icons were dumped to %s nei.options.tools.dump.itempanel.icon.cancelled=Dump cancelled +nei.options.tools.dump.itempanel.icon.warning.1=§cWARNING§r: You are about to dump %s items at %sx%s resolution +nei.options.tools.dump.itempanel.icon.warning.2=Hold %s while clicking this button to proceed nei.options.tools.dump.handlers=NEI Handlers nei.options.tools.dump.handlers.dumped=handlers were dumped to %s nei.options.tools.dump.handlers.mode.0=CSV