aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlease <4586901+Glease@users.noreply.github.com>2021-08-29 00:48:30 +0800
committerGlease <4586901+Glease@users.noreply.github.com>2021-08-29 00:48:30 +0800
commita65f2900a108d672531f28fc2b0738326755c81b (patch)
treeed8591b09373354aa162f7386500e2da5a6aa6bb
parentf6359df82d1c520b9043b2c0346a388d2b8beebf (diff)
downloadGT5-Unofficial-a65f2900a108d672531f28fc2b0738326755c81b.tar.gz
GT5-Unofficial-a65f2900a108d672531f28fc2b0738326755c81b.tar.bz2
GT5-Unofficial-a65f2900a108d672531f28fc2b0738326755c81b.zip
Fix potential race condition on client side
Former-commit-id: 2616978ec4ca6bed7892afe0f135768801bb0a41
-rw-r--r--src/main/java/com/github/bartimaeusnek/ASM/BWCoreStaticReplacementMethodes.java15
-rw-r--r--src/main/java/com/github/bartimaeusnek/bartworks/common/commands/ClearCraftingCache.java4
-rw-r--r--src/main/java/com/github/bartimaeusnek/bartworks/system/material/CircuitGeneration/CircuitImprintLoader.java2
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 ->
{