diff options
author | Martin Robertz <dream-master@gmx.net> | 2021-08-29 00:55:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-29 00:55:31 +0200 |
commit | 1e375fdfd71d1a57bcd82ec353798baa37ea02f4 (patch) | |
tree | ed8591b09373354aa162f7386500e2da5a6aa6bb | |
parent | f6359df82d1c520b9043b2c0346a388d2b8beebf (diff) | |
parent | a65f2900a108d672531f28fc2b0738326755c81b (diff) | |
download | GT5-Unofficial-1e375fdfd71d1a57bcd82ec353798baa37ea02f4.tar.gz GT5-Unofficial-1e375fdfd71d1a57bcd82ec353798baa37ea02f4.tar.bz2 GT5-Unofficial-1e375fdfd71d1a57bcd82ec353798baa37ea02f4.zip |
Merge pull request #30 from GTNewHorizons/fix-8468
Fix potential race condition on client side
Former-commit-id: 1dfb463e9486fb123c34debaf05fbff8bd2b47e2
3 files changed, 13 insertions, 8 deletions
diff --git a/src/main/java/com/github/bartimaeusnek/ASM/BWCoreStaticReplacementMethodes.java b/src/main/java/com/github/bartimaeusnek/ASM/BWCoreStaticReplacementMethodes.java index 6399a5119b..47705042a9 100644 --- a/src/main/java/com/github/bartimaeusnek/ASM/BWCoreStaticReplacementMethodes.java +++ b/src/main/java/com/github/bartimaeusnek/ASM/BWCoreStaticReplacementMethodes.java @@ -32,14 +32,18 @@ import net.minecraft.item.crafting.CraftingManager; import net.minecraft.item.crafting.IRecipe; import net.minecraft.world.World; -import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; public class BWCoreStaticReplacementMethodes { + private static ThreadLocal<AccessPriorityList<IRecipe>> RECENTLYUSEDRECIPES = ThreadLocal.withInitial(AccessPriorityList::new); - public static final AccessPriorityList<IRecipe> RECENTLYUSEDRECIPES = new AccessPriorityList<>(); + public static void clearRecentlyUsedRecipes() { + // the easiest way to ensure the cache is flushed without causing synchronization overhead + // is to just replace the whole ThreadLocal instance. + RECENTLYUSEDRECIPES = ThreadLocal.withInitial(AccessPriorityList::new); + } @SuppressWarnings("ALL") public static ItemStack findCachedMatchingRecipe(InventoryCrafting inventoryCrafting, World world) { @@ -79,7 +83,8 @@ public class BWCoreStaticReplacementMethodes { } else { IRecipe iPossibleRecipe = null; - Iterator<AccessPriorityListNode<IRecipe>> it = RECENTLYUSEDRECIPES.nodeIterator(); + AccessPriorityList<IRecipe> cache = RECENTLYUSEDRECIPES.get(); + Iterator<AccessPriorityListNode<IRecipe>> it = cache.nodeIterator(); while (it.hasNext()) { AccessPriorityListNode<IRecipe> recipeNode = it.next(); @@ -88,7 +93,7 @@ public class BWCoreStaticReplacementMethodes { if (!iPossibleRecipe.matches(inventoryCrafting, world)) continue; - RECENTLYUSEDRECIPES.addPrioToNode(recipeNode); + cache.addPrioToNode(recipeNode); return iPossibleRecipe.getCraftingResult(inventoryCrafting); } @@ -115,7 +120,7 @@ public class BWCoreStaticReplacementMethodes { return stack; if (stack != null) - RECENTLYUSEDRECIPES.addLast(recipe); + cache.addLast(recipe); return stack; } diff --git a/src/main/java/com/github/bartimaeusnek/bartworks/common/commands/ClearCraftingCache.java b/src/main/java/com/github/bartimaeusnek/bartworks/common/commands/ClearCraftingCache.java index 42385c60f1..f9aba701a6 100644 --- a/src/main/java/com/github/bartimaeusnek/bartworks/common/commands/ClearCraftingCache.java +++ b/src/main/java/com/github/bartimaeusnek/bartworks/common/commands/ClearCraftingCache.java @@ -40,7 +40,7 @@ public class ClearCraftingCache extends CommandBase { @Override public void processCommand(ICommandSender p_71515_1_, String[] p_71515_2_) { - BWCoreStaticReplacementMethodes.RECENTLYUSEDRECIPES.clear(); - p_71515_1_.addChatMessage(new ChatComponentText("Recipe Cache cleared? "+(BWCoreStaticReplacementMethodes.RECENTLYUSEDRECIPES.size() == 0))); + BWCoreStaticReplacementMethodes.clearRecentlyUsedRecipes(); + p_71515_1_.addChatMessage(new ChatComponentText("Recipe Cache cleared ")); } } diff --git a/src/main/java/com/github/bartimaeusnek/bartworks/system/material/CircuitGeneration/CircuitImprintLoader.java b/src/main/java/com/github/bartimaeusnek/bartworks/system/material/CircuitGeneration/CircuitImprintLoader.java index bad997cb1e..3c4cb02013 100644 --- a/src/main/java/com/github/bartimaeusnek/bartworks/system/material/CircuitGeneration/CircuitImprintLoader.java +++ b/src/main/java/com/github/bartimaeusnek/bartworks/system/material/CircuitGeneration/CircuitImprintLoader.java @@ -219,7 +219,7 @@ public class CircuitImprintLoader { private static void removeOldRecipesFromRegistries() { recipeWorldCache.forEach(CraftingManager.getInstance().getRecipeList()::remove); - BWCoreStaticReplacementMethodes.RECENTLYUSEDRECIPES.clear(); + BWCoreStaticReplacementMethodes.clearRecentlyUsedRecipes(); gtrecipeWorldCache.forEach(GT_Recipe.GT_Recipe_Map.sSlicerRecipes.mRecipeList::remove); recipeWorldCache.forEach( r -> { |