From 90bc890b95479d7603e0a60ed992032a001bb6ba Mon Sep 17 00:00:00 2001 From: NotEvil Date: Tue, 14 Apr 2026 02:46:09 +0200 Subject: [PATCH] fix(D-01): synchronize reload paths and capture snapshot locally (RISK-001, RISK-002) --- .../datadriven/DataDrivenItemRegistry.java | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/tiedup/remake/v2/bondage/datadriven/DataDrivenItemRegistry.java b/src/main/java/com/tiedup/remake/v2/bondage/datadriven/DataDrivenItemRegistry.java index 5e726e2..1c72af3 100644 --- a/src/main/java/com/tiedup/remake/v2/bondage/datadriven/DataDrivenItemRegistry.java +++ b/src/main/java/com/tiedup/remake/v2/bondage/datadriven/DataDrivenItemRegistry.java @@ -48,6 +48,9 @@ public final class DataDrivenItemRegistry { */ private static volatile RegistrySnapshot SNAPSHOT = RegistrySnapshot.EMPTY; + /** Guards read-then-write sequences in {@link #reload} and {@link #mergeAll}. */ + private static final Object RELOAD_LOCK = new Object(); + private DataDrivenItemRegistry() {} /** @@ -59,9 +62,11 @@ public final class DataDrivenItemRegistry { public static void reload( Map newDefs ) { - Map defs = - Collections.unmodifiableMap(new HashMap<>(newDefs)); - SNAPSHOT = new RegistrySnapshot(defs, buildComponentHolders(defs)); + synchronized (RELOAD_LOCK) { + Map defs = + Collections.unmodifiableMap(new HashMap<>(newDefs)); + SNAPSHOT = new RegistrySnapshot(defs, buildComponentHolders(defs)); + } } /** @@ -78,13 +83,15 @@ public final class DataDrivenItemRegistry { public static void mergeAll( Map newDefs ) { - Map merged = new HashMap<>( - SNAPSHOT.definitions - ); - merged.putAll(newDefs); - Map defs = - Collections.unmodifiableMap(merged); - SNAPSHOT = new RegistrySnapshot(defs, buildComponentHolders(defs)); + synchronized (RELOAD_LOCK) { + Map merged = new HashMap<>( + SNAPSHOT.definitions + ); + merged.putAll(newDefs); + Map defs = + Collections.unmodifiableMap(merged); + SNAPSHOT = new RegistrySnapshot(defs, buildComponentHolders(defs)); + } } /** @@ -95,7 +102,8 @@ public final class DataDrivenItemRegistry { */ @Nullable public static DataDrivenItemDefinition get(ResourceLocation id) { - return SNAPSHOT.definitions.get(id); + RegistrySnapshot snap = SNAPSHOT; + return snap.definitions.get(id); } /** @@ -113,7 +121,8 @@ public final class DataDrivenItemRegistry { tag.getString(NBT_ITEM_ID) ); if (id == null) return null; - return SNAPSHOT.definitions.get(id); + RegistrySnapshot snap = SNAPSHOT; + return snap.definitions.get(id); } /** @@ -122,7 +131,8 @@ public final class DataDrivenItemRegistry { * @return unmodifiable collection of all definitions */ public static Collection getAll() { - return SNAPSHOT.definitions.values(); + RegistrySnapshot snap = SNAPSHOT; + return snap.definitions.values(); } /** @@ -168,7 +178,8 @@ public final class DataDrivenItemRegistry { */ @Nullable public static ComponentHolder getComponents(ResourceLocation id) { - return SNAPSHOT.holders.get(id); + RegistrySnapshot snap = SNAPSHOT; + return snap.holders.get(id); } /**