2021-03-20 22:38:46 -07:00
|
|
|
package codechicken.nei.config;
|
|
|
|
|
|
2023-09-25 00:35:24 +02:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileWriter;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.util.ChatComponentTranslation;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
|
|
|
|
|
|
import com.google.common.base.Objects;
|
|
|
|
|
|
2021-03-20 22:38:46 -07:00
|
|
|
import codechicken.nei.NEIClientConfig;
|
|
|
|
|
import codechicken.nei.recipe.GuiRecipeTab;
|
|
|
|
|
import codechicken.nei.recipe.GuiUsageRecipe;
|
2021-03-28 00:12:17 -07:00
|
|
|
import codechicken.nei.recipe.HandlerInfo;
|
2021-03-20 22:38:46 -07:00
|
|
|
import codechicken.nei.recipe.IRecipeHandler;
|
|
|
|
|
import codechicken.nei.recipe.TemplateRecipeHandler;
|
|
|
|
|
import codechicken.nei.util.NBTJson;
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public class HandlerDumper extends DataDumper {
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2021-03-20 22:38:46 -07:00
|
|
|
public HandlerDumper(String name) {
|
|
|
|
|
super(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String[] header() {
|
2022-06-24 12:32:13 +01:00
|
|
|
return new String[] { "Handler Recipe Name", "Handler Class", "Overlay Identifier", "Mod DisplayName",
|
|
|
|
|
"ItemStack" };
|
2021-03-20 22:38:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Iterable<String[]> dump(int mode) {
|
|
|
|
|
LinkedList<String[]> list = new LinkedList<>();
|
|
|
|
|
for (IRecipeHandler handler : GuiUsageRecipe.usagehandlers) {
|
2021-08-24 22:14:34 -06:00
|
|
|
final String handlerName = handler.getHandlerId();
|
2022-06-24 12:32:13 +01:00
|
|
|
final String handlerId = Objects.firstNonNull(
|
|
|
|
|
handler instanceof TemplateRecipeHandler ? ((TemplateRecipeHandler) handler).getOverlayIdentifier()
|
|
|
|
|
: null,
|
|
|
|
|
"null");
|
2021-03-28 00:12:17 -07:00
|
|
|
HandlerInfo info = GuiRecipeTab.getHandlerInfo(handlerName, handlerId);
|
|
|
|
|
|
2021-03-20 22:38:46 -07:00
|
|
|
list.add(
|
|
|
|
|
new String[] { handler.getRecipeName(), handlerName, handlerId,
|
2021-03-28 00:12:17 -07:00
|
|
|
info != null ? info.getModName() : "Unknown",
|
2022-06-24 12:32:13 +01:00
|
|
|
info != null && info.getItemStack() != null ? info.getItemStack().toString() : "Unknown" });
|
2021-03-20 22:38:46 -07:00
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String renderName() {
|
|
|
|
|
return translateN(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getFileExtension() {
|
2022-06-24 12:32:13 +01:00
|
|
|
switch (getMode()) {
|
|
|
|
|
case 0:
|
|
|
|
|
return ".csv";
|
|
|
|
|
case 1:
|
|
|
|
|
return ".json";
|
2021-03-20 22:38:46 -07:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ChatComponentTranslation dumpMessage(File file) {
|
|
|
|
|
return new ChatComponentTranslation(namespaced(name + ".dumped"), "dumps/" + file.getName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String modeButtonText() {
|
|
|
|
|
return translateN(name + ".mode." + getMode());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void dumpTo(File file) throws IOException {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (getMode() == 0) super.dumpTo(file);
|
|
|
|
|
else dumpJson(file);
|
2021-03-20 22:38:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void dumpJson(File file) throws IOException {
|
|
|
|
|
final String[] header = header();
|
|
|
|
|
final FileWriter writer;
|
|
|
|
|
try {
|
|
|
|
|
writer = new FileWriter(file);
|
|
|
|
|
for (String[] list : dump(getMode())) {
|
|
|
|
|
NBTTagCompound tag = new NBTTagCompound();
|
|
|
|
|
for (int i = 0; i < header.length; i++) {
|
|
|
|
|
tag.setString(header[i], list[i]);
|
|
|
|
|
}
|
|
|
|
|
IOUtils.write(NBTJson.toJson(tag) + "\n", writer);
|
|
|
|
|
}
|
|
|
|
|
writer.close();
|
2022-06-24 12:32:13 +01:00
|
|
|
} catch (IOException e) {
|
2025-08-16 14:56:59 +03:00
|
|
|
NEIClientConfig.logger.error("Failed to save dump handler list to file {}", file, e);
|
2022-06-24 12:32:13 +01:00
|
|
|
}
|
2021-03-20 22:38:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int modeCount() {
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
}
|