修复GunData.from多线程冲突问题

This commit is contained in:
Light_Quanta 2025-04-26 03:28:09 +08:00
parent 7f410be13d
commit 3824806113
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959

View file

@ -8,6 +8,9 @@ import com.atsuishio.superbwarfare.perk.Perk;
import com.atsuishio.superbwarfare.tools.Ammo; import com.atsuishio.superbwarfare.tools.Ammo;
import com.atsuishio.superbwarfare.tools.GunsTool; import com.atsuishio.superbwarfare.tools.GunsTool;
import com.atsuishio.superbwarfare.tools.InventoryTool; import com.atsuishio.superbwarfare.tools.InventoryTool;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import net.minecraft.core.component.DataComponents; import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
@ -34,7 +37,13 @@ public class GunData {
public final CompoundTag attachmentTag; public final CompoundTag attachmentTag;
public final String id; public final String id;
private static final WeakHashMap<ItemStack, GunData> dataCache = new WeakHashMap<>(); private static final LoadingCache<ItemStack, GunData> dataCache = CacheBuilder.newBuilder()
.weakKeys()
.build(new CacheLoader<>() {
public @NotNull GunData load(@NotNull ItemStack stack) {
return new GunData(stack);
}
});
private GunData(ItemStack stack) { private GunData(ItemStack stack) {
if (!(stack.getItem() instanceof GunItem gunItem)) { if (!(stack.getItem() instanceof GunItem gunItem)) {
@ -101,12 +110,7 @@ public class GunData {
} }
public static GunData from(ItemStack stack) { public static GunData from(ItemStack stack) {
var value = dataCache.get(stack); return dataCache.getUnchecked(stack);
if (value == null) {
value = new GunData(stack);
dataCache.put(stack, value);
}
return value;
} }
public GunItem item() { public GunItem item() {