Files
NotEnoughItems/src/main/java/codechicken/nei/config/DataDumper.java
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

145 lines
4.3 KiB
Java
Raw Normal View History

2014-04-29 10:54:20 +10:00
package codechicken.nei.config;
2023-09-25 00:35:24 +02:00
import static codechicken.lib.gui.GuiDraw.drawString;
import static codechicken.lib.gui.GuiDraw.drawStringC;
2023-09-25 00:34:42 +02:00
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
2023-09-25 00:35:24 +02:00
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.IChatComponent;
import org.lwjgl.opengl.GL11;
import codechicken.core.CommonUtils;
import codechicken.lib.vec.Rectangle4i;
import codechicken.nei.LayoutManager;
import codechicken.nei.NEIClientConfig;
import codechicken.nei.NEIClientUtils;
public abstract class DataDumper extends Option {
2014-04-29 10:54:20 +10:00
public DataDumper(String name) {
super(name);
}
public abstract String[] header();
public abstract Iterable<String[]> dump(int mode);
public String renderName() {
return translateN(name + "s");
}
public void dumpFile() {
try {
File file = new File(CommonUtils.getMinecraftDir(), "dumps/" + getFileName(name.replaceFirst(".+\\.", "")));
if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
if (!file.exists()) file.createNewFile();
2014-04-29 10:54:20 +10:00
dumpTo(file);
NEIClientUtils.printChatMessage(dumpMessage(file));
} catch (Exception e) {
NEIClientConfig.logger.error("Error dumping " + renderName() + " mode: " + getMode(), e);
2014-04-29 10:54:20 +10:00
}
}
public String getFileName(String prefix) {
return prefix + getFileExtension();
}
public String getFileExtension() {
return ".csv";
}
public IChatComponent dumpMessage(File file) {
return new ChatComponentTranslation(
"nei.options.tools.dump.dumped",
translateN(name),
"dumps/" + file.getName());
2014-04-29 10:54:20 +10:00
}
public void dumpTo(File file) throws IOException {
int mode = getMode();
PrintWriter w = new PrintWriter(file);
w.println(concat(header()));
for (String[] line : dump(mode)) w.println(concat(line));
2014-04-29 10:54:20 +10:00
w.close();
}
public static String concat(String[] header) {
StringBuilder sb = new StringBuilder();
for (String s : header) {
if (sb.length() > 0) sb.append(',');
if (s == null) s = "null";
if (s.indexOf(',') > 0 || s.indexOf('\"') > 0) s = '\"' + s.replace("\"", "\"\"") + '\"';
2014-04-29 10:54:20 +10:00
sb.append(s);
}
return sb.toString();
}
@Override
public void draw(int mousex, int mousey, float frame) {
drawPrefix();
if (modeCount() > 1) drawButton(mousex, mousey, modeButtonSize(), modeButtonText());
2014-12-14 14:32:30 +10:00
drawButton(mousex, mousey, dumpButtonSize(), dumpButtonText());
2014-04-29 10:54:20 +10:00
}
public void drawPrefix() {
drawString(renderName(), 10, 6, -1);
2014-04-29 10:54:20 +10:00
}
public Rectangle4i dumpButtonSize() {
2026-06-14 21:14:44 +03:00
int width = 50;
2026-05-06 23:36:07 +03:00
return new Rectangle4i(getSlot().slotWidth() - width, 0, width, 20);
2014-04-29 10:54:20 +10:00
}
public Rectangle4i modeButtonSize() {
int width = 60;
2014-12-14 14:32:30 +10:00
return new Rectangle4i(dumpButtonSize().x - width - 6, 0, width, 20);
2014-04-29 10:54:20 +10:00
}
public String dumpButtonText() {
return NEIClientUtils.lang.translate("options.tools.dump.dump");
}
public String modeButtonText() {
return NEIClientUtils.lang.translate("options.tools.dump.mode." + getMode());
}
public int getMode() {
2026-11-02 18:20:18 +02:00
return getTag().getIntValue(0);
2014-04-29 10:54:20 +10:00
}
2014-12-14 14:32:30 +10:00
public void drawButton(int mousex, int mousey, Rectangle4i b, String text) {
2014-04-29 10:54:20 +10:00
GL11.glColor4f(1, 1, 1, 1);
boolean hover = b.contains(mousex, mousey);
LayoutManager.drawButtonBackground(b.x, b.y, b.w, b.h, true, getButtonTex(hover));
2014-12-14 14:32:30 +10:00
drawStringC(text, b.x, b.y, b.w, b.h, getTextColour(hover));
2014-04-29 10:54:20 +10:00
}
public int getButtonTex(boolean hover) {
return hover ? 2 : 1;
}
public int getTextColour(boolean hover) {
return hover ? 0xFFFFFFA0 : 0xFFE0E0E0;
}
@Override
public void mouseClicked(int mousex, int mousey, int button) {
if (modeCount() > 1 && modeButtonSize().contains(mousex, mousey)) {
NEIClientUtils.playClickSound();
2014-04-29 10:54:20 +10:00
getTag().setIntValue((getMode() + 1) % modeCount());
} else if (dumpButtonSize().contains(mousex, mousey)) {
NEIClientUtils.playClickSound();
2014-04-29 10:54:20 +10:00
dumpFile();
}
}
public int modeCount() {
return 3;
}
}