diff --git a/src/main/java/codechicken/nei/ItemHistoryPanel.java b/src/main/java/codechicken/nei/ItemHistoryPanel.java index f7fd7625..c6e0d955 100644 --- a/src/main/java/codechicken/nei/ItemHistoryPanel.java +++ b/src/main/java/codechicken/nei/ItemHistoryPanel.java @@ -1,17 +1,34 @@ package codechicken.nei; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import org.apache.commons.io.IOUtils; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.JsonSyntaxException; + +import codechicken.core.CommonUtils; import codechicken.nei.ItemsGrid.ItemsGridSlot; import codechicken.nei.ItemsGrid.MouseContext; import codechicken.nei.recipe.Recipe.RecipeId; import codechicken.nei.recipe.StackInfo; +import codechicken.nei.util.NBTJson; public class ItemHistoryPanel extends AbstractSubpanel> { + protected File historyFile = null; + public static class HistoryGridSlot extends ItemsGridSlot { public HistoryGridSlot(int slotIndex, int itemIndex, ItemStack item) { @@ -115,4 +132,107 @@ public class ItemHistoryPanel extends AbstractSubpanel itemStrings = Collections.emptyList(); + + if (historyFile.exists()) { + try (FileInputStream reader = new FileInputStream(historyFile)) { + NEIClientConfig.logger.info("Loading history from file {}", historyFile); + itemStrings = IOUtils.readLines(reader, StandardCharsets.UTF_8); + } catch (IOException e) { + NEIClientConfig.logger.error("Failed to load history from file {}", historyFile, e); + return; + } + } + + final JsonParser parser = new JsonParser(); + final int maxSize = Math.max(50, this.grid.rows * this.grid.columns); + + this.grid.realItems.clear(); + + for (String itemStr : itemStrings) { + + if (itemStr.isEmpty()) { + continue; + } + + if (this.grid.realItems.size() >= maxSize) { + break; + } + + try { + JsonObject row = parser.parse(itemStr).getAsJsonObject(); + NBTTagCompound itemStackNBT = (NBTTagCompound) NBTJson.toNbt(row.get("item")); + ItemStack itemStack = StackInfo.loadFromNBT(itemStackNBT); + + if (itemStack != null) { + this.grid.realItems.add(itemStack); + } else { + NEIClientConfig.logger.warn( + "Failed to load history ItemStack from json string, the item no longer exists:\n{}", + itemStr); + } + } catch (Exception e) { + NEIClientConfig.logger.error("Failed to load history ItemStack from json string:\n{}", itemStr); + } + } + + this.grid.onItemsChanged(); + + this.historyFile = historyFile; + } + + public void save() { + + if (!NEIClientConfig.getBooleanSetting("inventory.history.save")) { + return; + } + + if (this.historyFile == null) { + return; + } + + final List strings = new ArrayList<>(); + + for (ItemStack stack : this.grid.realItems) { + + try { + final JsonObject row = new JsonObject(); + row.add("item", NBTJson.toJsonObject(StackInfo.itemStackToNBT(stack))); + strings.add(NBTJson.toJson(row)); + } catch (JsonSyntaxException e) { + NEIClientConfig.logger.error("Failed to stringify history ItemStack to json string"); + } + } + + try (FileOutputStream output = new FileOutputStream(this.historyFile)) { + IOUtils.writeLines(strings, "\n", output, StandardCharsets.UTF_8); + } catch (IOException e) { + NEIClientConfig.logger.error("Failed to save history list to file {}", this.historyFile, e); + } + } + } diff --git a/src/main/java/codechicken/nei/ItemListLoader.java b/src/main/java/codechicken/nei/ItemListLoader.java index 9e0f60f3..6364a1ec 100644 --- a/src/main/java/codechicken/nei/ItemListLoader.java +++ b/src/main/java/codechicken/nei/ItemListLoader.java @@ -293,6 +293,7 @@ public class ItemListLoader { updateFilter.restart(); warmTooltips.restart(); loadBookmarks.restart(); + loadHistory.restart(); } }; @@ -319,6 +320,14 @@ public class ItemListLoader { } }; + public static final RestartableTask loadHistory = new RestartableTask("NEI History Loading") { + + @Override + public void execute() { + ItemPanels.itemPanel.historyPanel.load(); + } + }; + public static final RestartableTask refreshItems = new RestartableTask("NEI Item Reordering") { @Override @@ -340,6 +349,7 @@ public class ItemListLoader { SubsetWidget.updateHiddenItems(); updateFilter.restart(); loadBookmarks.restart(); + loadHistory.restart(); } }; diff --git a/src/main/java/codechicken/nei/NEIClientConfig.java b/src/main/java/codechicken/nei/NEIClientConfig.java index 64dc3ea0..faf4a8a5 100644 --- a/src/main/java/codechicken/nei/NEIClientConfig.java +++ b/src/main/java/codechicken/nei/NEIClientConfig.java @@ -288,6 +288,22 @@ public class NEIClientConfig { tag.getTag("inventory.history.useRows").setComment("Rows used in historical areas").getIntValue(2); API.addOption(new OptionIntegerField("inventory.history.useRows", 1, 5)); + tag.getTag("inventory.history.worldSpecific").setComment("Global or world specific history") + .getBooleanValue(false); + API.addOption(new OptionToggleButton("inventory.history.worldSpecific", true) { + + @Override + public boolean onClick(int button) { + super.onClick(button); + ItemPanels.itemPanel.historyPanel.save(); + ItemPanels.itemPanel.historyPanel.load(); + return true; + } + }); + + tag.getTag("inventory.history.save").setComment("Enable/disable History save").getBooleanValue(false); + API.addOption(new OptionToggleButton("inventory.history.save", true)); + tag.getTag("inventory.craftables.enabled").setComment("Enable/disable Craftables Panel").getBooleanValue(false); API.addOption(new OptionToggleButton("inventory.craftables.enabled", true)); @@ -927,6 +943,7 @@ public class NEIClientConfig { } ItemPanels.bookmarkPanel.save(); + ItemPanels.itemPanel.historyPanel.save(); SubsetWidget.saveHidden(); FavoriteRecipes.save(); diff --git a/src/main/resources/assets/nei/lang/en_US.lang b/src/main/resources/assets/nei/lang/en_US.lang index e0cf7e8f..1efdad47 100644 --- a/src/main/resources/assets/nei/lang/en_US.lang +++ b/src/main/resources/assets/nei/lang/en_US.lang @@ -439,6 +439,12 @@ nei.options.inventory.history.enabled.true=Enabled nei.options.inventory.history.enabled.false=Disabled nei.options.inventory.history.color=Color nei.options.inventory.history.useRows=Rows +nei.options.inventory.history.worldSpecific=World Specific +nei.options.inventory.history.worldSpecific.true=Yes +nei.options.inventory.history.worldSpecific.false=No +nei.options.inventory.history.save=Save +nei.options.inventory.history.save.true=Enabled +nei.options.inventory.history.save.false=Disabled nei.options.inventory.craftables=Craftables Panel nei.options.inventory.craftables.enabled=Craftables Panel Visibility nei.options.inventory.craftables.enabled.true=Enabled