Skip to content

Commit

Permalink
Stop ItemOutput from caching the result if the preference is missing
Browse files Browse the repository at this point in the history
Can cause issues if someone is to fetch recipe oututs before tags are loaded.
  • Loading branch information
KnightMiner committed Apr 4, 2024
1 parent 5769a45 commit 1ab30ac
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/main/java/slimeknights/mantle/recipe/helper/ItemOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import slimeknights.mantle.data.loadable.primitive.IntLoadable;
import slimeknights.mantle.data.loadable.record.RecordLoadable;

import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;

Expand Down Expand Up @@ -173,9 +174,14 @@ public ItemStack get() {
// cache the result from the tag preference to save effort, especially helpful if the tag becomes invalid
// this object should only exist in recipes so no need to invalidate the cache
if (cachedResult == null) {
cachedResult = TagPreference.getPreference(tag)
.map(item -> new ItemStack(item, count))
.orElse(ItemStack.EMPTY);
// if the preference is empty, do not cache it.
// This should only happen if someone scans recipes before tag are computed in which case we cache the wrong resolt.
// We protect against empty tags in our recipes via conditions.
Optional<Item> preference = TagPreference.getPreference(tag);
if (preference.isEmpty()) {
return ItemStack.EMPTY;
}
cachedResult = new ItemStack(preference.orElseThrow(), count);
}
return cachedResult;
}
Expand Down

0 comments on commit 1ab30ac

Please sign in to comment.