Remove the item stack reference thread

This commit is contained in:
embeddedt 2026-06-06 21:04:09 -04:00
parent f1492cc829
commit 0f94634361
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 4 additions and 16 deletions

View File

@ -135,6 +135,7 @@ public abstract class IngredientMixin implements ExtendedIngredient {
return stacks;
}
}
IngredientItemStacksSoftReference.clearReferences();
ItemStack[] result = computeItemsArray();
this.mfix$cachedItemStacks = new IngredientItemStacksSoftReference((Ingredient)(Object)this, result);
return result;

View File

@ -11,28 +11,15 @@ public class IngredientItemStacksSoftReference extends SoftReference<ItemStack[]
private final Ingredient ingredient;
private static final ReferenceQueue<ItemStack[]> QUEUE = new ReferenceQueue<>();
private static final Thread DISCARD_THREAD = new Thread(IngredientItemStacksSoftReference::clearReferences, "Ingredient reference clearing thread");
static {
DISCARD_THREAD.setPriority(Thread.NORM_PRIORITY + 2);
DISCARD_THREAD.setDaemon(true);
DISCARD_THREAD.start();
}
public IngredientItemStacksSoftReference(Ingredient ingredient, ItemStack[] stacks) {
super(stacks, QUEUE);
this.ingredient = ingredient;
}
private static void clearReferences() {
while (true) {
Reference<? extends ItemStack[]> ref;
try {
ref = QUEUE.remove();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
public static void clearReferences() {
Reference<? extends ItemStack[]> ref;
while ((ref = QUEUE.poll()) != null) {
if (ref instanceof IngredientItemStacksSoftReference ingRef && ingRef.ingredient instanceof ExtendedIngredient extIng) {
// Null out the reference to the SoftReference object, to allow the SoftReference itself to be garbage collected.
extIng.mfix$clearReference();