fix(D-01): synchronize reload paths and capture snapshot locally (RISK-001, RISK-002)
This commit is contained in:
@@ -48,6 +48,9 @@ public final class DataDrivenItemRegistry {
|
|||||||
*/
|
*/
|
||||||
private static volatile RegistrySnapshot SNAPSHOT = RegistrySnapshot.EMPTY;
|
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() {}
|
private DataDrivenItemRegistry() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -59,10 +62,12 @@ public final class DataDrivenItemRegistry {
|
|||||||
public static void reload(
|
public static void reload(
|
||||||
Map<ResourceLocation, DataDrivenItemDefinition> newDefs
|
Map<ResourceLocation, DataDrivenItemDefinition> newDefs
|
||||||
) {
|
) {
|
||||||
|
synchronized (RELOAD_LOCK) {
|
||||||
Map<ResourceLocation, DataDrivenItemDefinition> defs =
|
Map<ResourceLocation, DataDrivenItemDefinition> defs =
|
||||||
Collections.unmodifiableMap(new HashMap<>(newDefs));
|
Collections.unmodifiableMap(new HashMap<>(newDefs));
|
||||||
SNAPSHOT = new RegistrySnapshot(defs, buildComponentHolders(defs));
|
SNAPSHOT = new RegistrySnapshot(defs, buildComponentHolders(defs));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Atomically merge new definitions into the existing registry.
|
* Atomically merge new definitions into the existing registry.
|
||||||
@@ -78,6 +83,7 @@ public final class DataDrivenItemRegistry {
|
|||||||
public static void mergeAll(
|
public static void mergeAll(
|
||||||
Map<ResourceLocation, DataDrivenItemDefinition> newDefs
|
Map<ResourceLocation, DataDrivenItemDefinition> newDefs
|
||||||
) {
|
) {
|
||||||
|
synchronized (RELOAD_LOCK) {
|
||||||
Map<ResourceLocation, DataDrivenItemDefinition> merged = new HashMap<>(
|
Map<ResourceLocation, DataDrivenItemDefinition> merged = new HashMap<>(
|
||||||
SNAPSHOT.definitions
|
SNAPSHOT.definitions
|
||||||
);
|
);
|
||||||
@@ -86,6 +92,7 @@ public final class DataDrivenItemRegistry {
|
|||||||
Collections.unmodifiableMap(merged);
|
Collections.unmodifiableMap(merged);
|
||||||
SNAPSHOT = new RegistrySnapshot(defs, buildComponentHolders(defs));
|
SNAPSHOT = new RegistrySnapshot(defs, buildComponentHolders(defs));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a definition by its unique ID.
|
* Get a definition by its unique ID.
|
||||||
@@ -95,7 +102,8 @@ public final class DataDrivenItemRegistry {
|
|||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public static DataDrivenItemDefinition get(ResourceLocation id) {
|
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)
|
tag.getString(NBT_ITEM_ID)
|
||||||
);
|
);
|
||||||
if (id == null) return null;
|
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
|
* @return unmodifiable collection of all definitions
|
||||||
*/
|
*/
|
||||||
public static Collection<DataDrivenItemDefinition> getAll() {
|
public static Collection<DataDrivenItemDefinition> getAll() {
|
||||||
return SNAPSHOT.definitions.values();
|
RegistrySnapshot snap = SNAPSHOT;
|
||||||
|
return snap.definitions.values();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -168,7 +178,8 @@ public final class DataDrivenItemRegistry {
|
|||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public static ComponentHolder getComponents(ResourceLocation id) {
|
public static ComponentHolder getComponents(ResourceLocation id) {
|
||||||
return SNAPSHOT.holders.get(id);
|
RegistrySnapshot snap = SNAPSHOT;
|
||||||
|
return snap.holders.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user