From 287dcd936240280d2da28de4e15b5a5620d0eb81 Mon Sep 17 00:00:00 2001 From: mezz Date: Wed, 18 Sep 2024 17:24:12 +0900 Subject: [PATCH] Fix #3731 Ingredient list gets disordered after a resource reload --- .../jei/gui/ingredients/IListElement.java | 2 ++ .../IngredientListElementFactory.java | 7 +++---- .../mezz/jei/gui/ingredients/ListElement.java | 13 +++++++++--- .../jei/gui/ingredients/ListElementInfo.java | 20 +++++++++++-------- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Gui/src/main/java/mezz/jei/gui/ingredients/IListElement.java b/Gui/src/main/java/mezz/jei/gui/ingredients/IListElement.java index 5b41b5fd6..77136d063 100644 --- a/Gui/src/main/java/mezz/jei/gui/ingredients/IListElement.java +++ b/Gui/src/main/java/mezz/jei/gui/ingredients/IListElement.java @@ -9,6 +9,8 @@ public interface IListElement { void setSortedIndex(int sortIndex); + int getCreatedIndex(); + boolean isVisible(); void setVisible(boolean visible); diff --git a/Gui/src/main/java/mezz/jei/gui/ingredients/IngredientListElementFactory.java b/Gui/src/main/java/mezz/jei/gui/ingredients/IngredientListElementFactory.java index 952058471..b2265755b 100644 --- a/Gui/src/main/java/mezz/jei/gui/ingredients/IngredientListElementFactory.java +++ b/Gui/src/main/java/mezz/jei/gui/ingredients/IngredientListElementFactory.java @@ -38,12 +38,11 @@ public static List> createTestList(IIngredientManager in .toList(); } - public static List> rebuildList(IIngredientManager ingredientManager, Collection> ingredients, IModIdHelper modIdHelper) { + public static List> rebuildList(IIngredientManager ingredientManager, Collection> elements, IModIdHelper modIdHelper) { List> results = new ArrayList<>(); - for (IListElement ingredient : ingredients) { - ITypedIngredient typedIngredient = ingredient.getTypedIngredient(); - IListElementInfo orderedElement = ListElementInfo.create(typedIngredient, ingredientManager, modIdHelper); + for (IListElement element : elements) { + IListElementInfo orderedElement = ListElementInfo.createFromElement(element, ingredientManager, modIdHelper); if (orderedElement != null) { results.add(orderedElement); } diff --git a/Gui/src/main/java/mezz/jei/gui/ingredients/ListElement.java b/Gui/src/main/java/mezz/jei/gui/ingredients/ListElement.java index b9009d5d9..efb727446 100644 --- a/Gui/src/main/java/mezz/jei/gui/ingredients/ListElement.java +++ b/Gui/src/main/java/mezz/jei/gui/ingredients/ListElement.java @@ -4,12 +4,14 @@ public class ListElement implements IListElement { private final ITypedIngredient ingredient; - private int sortIndex = Integer.MAX_VALUE; + private final int createdIndex; + private int sortIndex; private boolean visible = true; - public ListElement(ITypedIngredient ingredient, int sortIndex) { + public ListElement(ITypedIngredient ingredient, int createdIndex) { this.ingredient = ingredient; - this.sortIndex = sortIndex; + this.createdIndex = createdIndex; + this.sortIndex = createdIndex; } @Override @@ -27,6 +29,11 @@ public void setSortedIndex(int sortIndex) { this.sortIndex = sortIndex; } + @Override + public int getCreatedIndex() { + return createdIndex; + } + @Override public boolean isVisible() { return visible; diff --git a/Gui/src/main/java/mezz/jei/gui/ingredients/ListElementInfo.java b/Gui/src/main/java/mezz/jei/gui/ingredients/ListElementInfo.java index 8c9ed69c7..47e356a2c 100644 --- a/Gui/src/main/java/mezz/jei/gui/ingredients/ListElementInfo.java +++ b/Gui/src/main/java/mezz/jei/gui/ingredients/ListElementInfo.java @@ -37,18 +37,23 @@ public class ListElementInfo implements IListElementInfo { private final List modIds; private final List modNames; private final ResourceLocation resourceLocation; - private final int createdIndex; @Nullable public static IListElementInfo create(ITypedIngredient value, IIngredientManager ingredientManager, IModIdHelper modIdHelper) { int createdIndex = elementCount++; - IIngredientHelper ingredientHelper = ingredientManager.getIngredientHelper(value.getType()); ListElement element = new ListElement<>(value, createdIndex); + return createFromElement(element, ingredientManager, modIdHelper); + } + + @Nullable + public static IListElementInfo createFromElement(IListElement element, IIngredientManager ingredientManager, IModIdHelper modIdHelper) { try { - return new ListElementInfo<>(element, ingredientHelper, ingredientManager, modIdHelper, createdIndex); + return new ListElementInfo<>(element, ingredientManager, modIdHelper); } catch (RuntimeException e) { try { - String ingredientInfo = ingredientHelper.getErrorInfo(value.getIngredient()); + ITypedIngredient typedIngredient = element.getTypedIngredient(); + IIngredientHelper ingredientHelper = ingredientManager.getIngredientHelper(typedIngredient.getType()); + String ingredientInfo = ingredientHelper.getErrorInfo(typedIngredient.getIngredient()); LOGGER.warn("Found a broken ingredient {}", ingredientInfo, e); } catch (RuntimeException e2) { LOGGER.warn("Found a broken ingredient.", e2); @@ -57,12 +62,11 @@ public static IListElementInfo create(ITypedIngredient value, IIngredi } } - protected ListElementInfo(IListElement element, IIngredientHelper ingredientHelper, IIngredientManager ingredientManager, IModIdHelper modIdHelper, int createdIndex) { - this.createdIndex = createdIndex; - + protected ListElementInfo(IListElement element, IIngredientManager ingredientManager, IModIdHelper modIdHelper) { this.element = element; ITypedIngredient value = element.getTypedIngredient(); V ingredient = value.getIngredient(); + IIngredientHelper ingredientHelper = ingredientManager.getIngredientHelper(value.getType()); this.resourceLocation = ingredientHelper.getResourceLocation(ingredient); String displayModId = ingredientHelper.getDisplayModId(ingredient); String modId = this.resourceLocation.getNamespace(); @@ -193,6 +197,6 @@ public ITypedIngredient getTypedIngredient() { @Override public int getCreatedIndex() { - return createdIndex; + return element.getCreatedIndex(); } }