Compare commits

...
Author SHA1 Message Date
slprime 7817640d96 Check unknown bookmark group (#1014) 2026-08-26 16:57:07 +03:00
vcwdfcaandslprime bd595f739c Add feature to save history like bookmarks (#1011)
Co-authored-by: slprime <[email protected]>
2026-08-26 13:40:18 +03:00
slprime 4c74a01930 Fix crash when recipe search results change mid-rebuild (#1012) 2026-08-25 09:09:53 +03:00
9 changed files with 206 additions and 57 deletions

No files matched your search

@@ -903,14 +903,18 @@ public class BookmarkPanel extends PanelWidget<BookmarkGrid> {
}
private List<String> recipeChainTooltip(int groupId, List<String> currenttip) {
boolean crafting = this.grid.getGroup(groupId).crafting != null;
boolean crafting = this.grid.isCraftingMode(groupId);
if (this.recipeChainTooltipLineHandler == null || this.recipeChainTooltipLineHandler.groupId != groupId
|| this.recipeChainTooltipLineHandler.crafting != crafting) {
this.recipeChainTooltipLineHandler = new RecipeChainTooltipLineHandler(
groupId,
crafting,
this.grid.createRecipeChainMath(groupId));
final RecipeChainMath recipeChainMath = this.grid.createRecipeChainMath(groupId);
if (recipeChainMath != null) {
this.recipeChainTooltipLineHandler = new RecipeChainTooltipLineHandler(
groupId,
crafting,
recipeChainMath);
}
}
currenttip.add(GuiDraw.TOOLTIP_HANDLER + GuiDraw.getTipLineId(this.recipeChainTooltipLineHandler));
@@ -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<ItemsGrid<ItemHistoryPanel.HistoryGridSlot, MouseContext>> {
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<ItemsGrid<ItemHistoryPane
}
}
public void load() {
if (!NEIClientConfig.getBooleanSetting("inventory.history.save")) {
return;
}
String worldPath = "global";
if (NEIClientConfig.getBooleanSetting("inventory.history.worldSpecific")) {
worldPath = NEIClientConfig.getWorldPath();
}
final File dir = new File(CommonUtils.getMinecraftDir(), "saves/NEI/" + worldPath);
if (!dir.exists()) {
dir.mkdirs();
}
final File historyFile = new File(dir, "history.ini");
if (historyFile.equals(this.historyFile)) {
return;
}
List<String> 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<String> 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);
}
}
}
@@ -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();
}
};
@@ -288,6 +288,32 @@ 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) {
@Override
public boolean onClick(int button) {
super.onClick(button);
ItemPanels.itemPanel.historyPanel.save();
ItemPanels.itemPanel.historyPanel.load();
return true;
}
});
tag.getTag("inventory.craftables.enabled").setComment("Enable/disable Craftables Panel").getBooleanValue(false);
API.addOption(new OptionToggleButton("inventory.craftables.enabled", true));
@@ -927,6 +953,7 @@ public class NEIClientConfig {
}
ItemPanels.bookmarkPanel.save();
ItemPanels.itemPanel.historyPanel.save();
SubsetWidget.saveHidden();
FavoriteRecipes.save();
@@ -600,6 +600,11 @@ public class BookmarkGrid extends ItemsGrid<BookmarksGridSlot, BookmarkGrid.Book
public RecipeChainMath createRecipeChainMath(int groupId, RecipeId recipeId) {
final BookmarkGroup group = getGroup(groupId);
if (group == null) {
return null;
}
final List<BookmarkItem> chainItems = new ArrayList<>();
final Set<RecipeId> collapsedRecipes = group.crafting != null && recipeId == null ? group.collapsedRecipes
: Collections.emptySet();
@@ -50,12 +50,13 @@ public class RecipePageManager {
final int heightAvailable = this.container.getVisibleHeight();
final int width = Math.max(166, this.handlerInfo.getWidth());
final int numRecipes = getNumRecipes();
final List<Integer> searchRecipes = this.handler.getFilteredRecipes();
final int numRecipes = searchRecipes.size();
List<Widget> widgets = new ArrayList<>();
int shiftY = 0;
for (int i = 0; i < numRecipes; i++) {
final int recipeIndex = this.handler.ref(i);
final int recipeIndex = searchRecipes.get(i);
final NEIRecipeWidget widget = RecipeHandlerRef.of(this.handler.original, recipeIndex).getRecipeWidget();
widget.w = width;
@@ -97,6 +97,22 @@ class SearchRecipeHandler<H extends IRecipeHandler> {
this.searchRecipes = searchRecipes;
}
public List<Integer> getFilteredRecipes() {
if (this.searchRecipes == null) {
return this.filteredRecipes;
}
final List<Integer> search = this.searchRecipes;
final List<Integer> resolved = new ArrayList<>(search.size());
for (int idx : search) {
resolved.add(this.filteredRecipes.get(idx));
}
return resolved;
}
public int ref(int index) {
if (searchRecipes != null) {
@@ -25,34 +25,6 @@ import codechicken.nei.recipe.StackInfo;
public class RecipeChainMath {
public interface ChainVisitor {
void enterRecipe(RecipeId recipeId, long multiplier);
void addIngredient(BookmarkItem ingrItem, long requestedAmount);
void addIngredientFresh(BookmarkItem ingrItem, long freshAmount);
void leaveRecipe();
}
private static class EmptyChainVisitor implements ChainVisitor {
@Override
public void enterRecipe(RecipeId recipeId, long multiplier) {}
@Override
public void addIngredient(BookmarkItem ingrItem, long requestedAmount) {}
@Override
public void addIngredientFresh(BookmarkItem ingrItem, long freshAmount) {}
@Override
public void leaveRecipe() {}
}
private static class ContainerItemResult {
public ItemStack stack;
@@ -66,8 +38,6 @@ public class RecipeChainMath {
}
}
private static final ChainVisitor EMPTY_CHAIN_VISITOR = new EmptyChainVisitor();
private static final ItemStack ROOT_ITEM = new ItemStack(Blocks.fire);
private static final RecipeId ROOT_RECIPE_ID = RecipeId
.of(ROOT_ITEM, "recipe-autocrafting", Collections.emptyList());
@@ -328,10 +298,6 @@ public class RecipeChainMath {
}
public RecipeChainMath refresh() {
return refresh(EMPTY_CHAIN_VISITOR);
}
public RecipeChainMath refresh(ChainVisitor visitor) {
final boolean isPausedItemDamageSound = StackInfo.isPausedItemDamageSound();
StackInfo.pauseItemDamageSound(true);
@@ -356,7 +322,7 @@ public class RecipeChainMath {
}
this.preferredItems.put(prefItem, prefItem);
calculateSuitableRecipe(prefItem, prefMultiplier, new ArrayList<>(), visitor);
calculateSuitableRecipe(prefItem, prefMultiplier, new ArrayList<>());
this.preferredItems.remove(prefItem);
}
}
@@ -375,14 +341,11 @@ public class RecipeChainMath {
return this;
}
private void calculateSuitableRecipe(BookmarkItem ingrItem, long ingrMultiplier, List<RecipeId> visited,
ChainVisitor visitor) {
private void calculateSuitableRecipe(BookmarkItem ingrItem, long ingrMultiplier, List<RecipeId> visited) {
final BookmarkItem prefItem = this.preferredItems.get(ingrItem);
long ingrAmount = ingrItem
.getAmount(ingrMultiplier, prefItem != null ? prefItem.itemStack : ingrItem.itemStack);
visitor.addIngredient(prefItem != null ? prefItem : ingrItem, ingrAmount);
// calculate existing containers
if (ingrAmount > 0) {
for (ItemStack stack : ingrItem.permutations.values()) {
@@ -400,7 +363,7 @@ public class RecipeChainMath {
if (ingrAmount > 0) {
for (BookmarkItem item : this.initialItems) {
if (item.containsItems(ingrItem)
&& (ingrAmount = addRequiredAmount(item, ingrAmount, item.getAmount(), visitor)) == 0) {
&& (ingrAmount = addRequiredAmount(item, ingrAmount, item.getAmount())) == 0) {
break;
}
}
@@ -408,37 +371,35 @@ public class RecipeChainMath {
// shift amount
if (prefItem == null) {
addRequiredAmount(ingrItem, ingrAmount, Long.MAX_VALUE, visitor);
addRequiredAmount(ingrItem, ingrAmount, Long.MAX_VALUE);
} else if (visited.contains(prefItem.recipeId)) {
addRequiredAmount(prefItem, ingrAmount, Long.MAX_VALUE, visitor);
addRequiredAmount(prefItem, ingrAmount, Long.MAX_VALUE);
} else {
addRequiredAmount(prefItem, ingrAmount, Long.MAX_VALUE, visitor);
addRequiredAmount(prefItem, ingrAmount, Long.MAX_VALUE);
final long multiplier = Math
.max(0, prefItem.getMultiplierFromAmount(this.requiredAmount.get(prefItem)) - prefItem.multiplier);
if (multiplier > 0) {
addShift(prefItem.recipeId, multiplier);
visited.add(prefItem.recipeId);
prepareIngredients(prefItem.recipeId, multiplier, visited, visitor);
prepareIngredients(prefItem.recipeId, multiplier, visited);
visited.remove(prefItem.recipeId);
}
}
}
private void prepareIngredients(RecipeId recipeId, long multiplier, List<RecipeId> visited, ChainVisitor visitor) {
visitor.enterRecipe(recipeId, multiplier);
private void prepareIngredients(RecipeId recipeId, long multiplier, List<RecipeId> visited) {
for (BookmarkItem item : this.recipeIngredients) {
if (!item.emptyFactor() && recipeId.equals(item.recipeId)) {
calculateSuitableRecipe(item, multiplier, visited, visitor);
calculateSuitableRecipe(item, multiplier, visited);
}
}
visitor.leaveRecipe();
}
private long addRequiredAmount(BookmarkItem prefItem, long ingrAmount, long maxAmount, ChainVisitor visitor) {
private long addRequiredAmount(BookmarkItem prefItem, long ingrAmount, long maxAmount) {
long shiftAmount = this.requiredAmount.getOrDefault(prefItem, 0L);
if (hasContainerItem(prefItem.itemStack)) {
@@ -495,7 +456,6 @@ public class RecipeChainMath {
ingrAmount -= initAmount;
}
visitor.addIngredientFresh(prefItem, shiftAmount - this.requiredAmount.getOrDefault(prefItem, 0L));
this.requiredAmount.put(prefItem, shiftAmount);
return ingrAmount;
@@ -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=History Persistence
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