Skip to content

Commit

Permalink
Fix #3731 Ingredient list gets disordered after a resource reload
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Sep 18, 2024
1 parent 88e6fcc commit 287dcd9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Gui/src/main/java/mezz/jei/gui/ingredients/IListElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface IListElement<V> {

void setSortedIndex(int sortIndex);

int getCreatedIndex();

boolean isVisible();

void setVisible(boolean visible);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ public static <V> List<IListElementInfo<V>> createTestList(IIngredientManager in
.toList();
}

public static List<IListElementInfo<?>> rebuildList(IIngredientManager ingredientManager, Collection<IListElement<?>> ingredients, IModIdHelper modIdHelper) {
public static List<IListElementInfo<?>> rebuildList(IIngredientManager ingredientManager, Collection<IListElement<?>> elements, IModIdHelper modIdHelper) {
List<IListElementInfo<?>> 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);
}
Expand Down
13 changes: 10 additions & 3 deletions Gui/src/main/java/mezz/jei/gui/ingredients/ListElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

public class ListElement<V> implements IListElement<V> {
private final ITypedIngredient<V> ingredient;
private int sortIndex = Integer.MAX_VALUE;
private final int createdIndex;
private int sortIndex;
private boolean visible = true;

public ListElement(ITypedIngredient<V> ingredient, int sortIndex) {
public ListElement(ITypedIngredient<V> ingredient, int createdIndex) {
this.ingredient = ingredient;
this.sortIndex = sortIndex;
this.createdIndex = createdIndex;
this.sortIndex = createdIndex;
}

@Override
Expand All @@ -27,6 +29,11 @@ public void setSortedIndex(int sortIndex) {
this.sortIndex = sortIndex;
}

@Override
public int getCreatedIndex() {
return createdIndex;
}

@Override
public boolean isVisible() {
return visible;
Expand Down
20 changes: 12 additions & 8 deletions Gui/src/main/java/mezz/jei/gui/ingredients/ListElementInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,23 @@ public class ListElementInfo<V> implements IListElementInfo<V> {
private final List<String> modIds;
private final List<String> modNames;
private final ResourceLocation resourceLocation;
private final int createdIndex;

@Nullable
public static <V> IListElementInfo<V> create(ITypedIngredient<V> value, IIngredientManager ingredientManager, IModIdHelper modIdHelper) {
int createdIndex = elementCount++;
IIngredientHelper<V> ingredientHelper = ingredientManager.getIngredientHelper(value.getType());
ListElement<V> element = new ListElement<>(value, createdIndex);
return createFromElement(element, ingredientManager, modIdHelper);
}

@Nullable
public static <V> IListElementInfo<V> createFromElement(IListElement<V> 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<V> typedIngredient = element.getTypedIngredient();
IIngredientHelper<V> 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);
Expand All @@ -57,12 +62,11 @@ public static <V> IListElementInfo<V> create(ITypedIngredient<V> value, IIngredi
}
}

protected ListElementInfo(IListElement<V> element, IIngredientHelper<V> ingredientHelper, IIngredientManager ingredientManager, IModIdHelper modIdHelper, int createdIndex) {
this.createdIndex = createdIndex;

protected ListElementInfo(IListElement<V> element, IIngredientManager ingredientManager, IModIdHelper modIdHelper) {
this.element = element;
ITypedIngredient<V> value = element.getTypedIngredient();
V ingredient = value.getIngredient();
IIngredientHelper<V> ingredientHelper = ingredientManager.getIngredientHelper(value.getType());
this.resourceLocation = ingredientHelper.getResourceLocation(ingredient);
String displayModId = ingredientHelper.getDisplayModId(ingredient);
String modId = this.resourceLocation.getNamespace();
Expand Down Expand Up @@ -193,6 +197,6 @@ public ITypedIngredient<V> getTypedIngredient() {

@Override
public int getCreatedIndex() {
return createdIndex;
return element.getCreatedIndex();
}
}

0 comments on commit 287dcd9

Please sign in to comment.