Skip to content

Commit 8378c05

Browse files
committed
Added dynamic registry access via AddReloadListenerEvent
1 parent 59e4e01 commit 8378c05

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ mod_license=All Rights Reserved
1717
mod_version=6.0.0
1818
mod_group_id=insane96mcp.mobspropertiesrandomness
1919

20-
insanelib_version=2.0.0.2-alpha
20+
insanelib_version=2.0.0.3-alpha
2121
insanelib_version_range=[2.0.0.0-alpha,)

src/main/java/insane96mcp/mobspropertiesrandomness/MPR.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import insane96mcp.mobspropertiesrandomness.data.serializer.*;
2121
import insane96mcp.mobspropertiesrandomness.setup.MPRConfig;
2222
import insane96mcp.mobspropertiesrandomness.util.MPRLogger;
23+
import insane96mcp.mobspropertiesrandomness.util.SerializerUtils;
2324
import net.minecraft.commands.CommandBuildContext;
2425
import net.minecraft.commands.CommandSourceStack;
2526
import net.minecraft.resources.ResourceLocation;
@@ -65,6 +66,7 @@ public MPR(IEventBus modEventBus, ModContainer modContainer) {
6566

6667
@SubscribeEvent(priority = EventPriority.LOW)
6768
public void onAddReloadListener(AddReloadListenerEvent event) {
69+
SerializerUtils.REGISTRY_ACCESS = event.getRegistryAccess();
6870
event.addListener(MPRPresetReloadListener.INSTANCE);
6971
event.addListener(MPRMobReloadListener.INSTANCE);
7072
}

src/main/java/insane96mcp/mobspropertiesrandomness/util/SerializerUtils.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.gson.*;
44
import net.minecraft.core.Holder;
55
import net.minecraft.core.Registry;
6+
import net.minecraft.core.RegistryAccess;
67
import net.minecraft.core.registries.BuiltInRegistries;
78
import net.minecraft.resources.ResourceKey;
89
import net.minecraft.resources.ResourceLocation;
@@ -13,6 +14,29 @@
1314
import java.util.List;
1415

1516
public class SerializerUtils {
17+
/**
18+
* Set from AddReloadListenerEvent before any reload listener runs.
19+
* Provides access to dynamic registries (e.g. minecraft:enchantment in 1.21+).
20+
*/
21+
@Nullable
22+
public static RegistryAccess REGISTRY_ACCESS = null;
23+
24+
/**
25+
* Gets a registry by key, checking BuiltInRegistries first, then falling back to
26+
* the RegistryAccess captured from AddReloadListenerEvent (needed for data-driven
27+
* registries like enchantments in 1.21+).
28+
*/
29+
@SuppressWarnings("unchecked")
30+
@Nullable
31+
private static <T> Registry<T> getRegistry(ResourceKey<Registry<T>> registryKey) {
32+
Registry<T> reg = (Registry<T>) BuiltInRegistries.REGISTRY.get(registryKey.location());
33+
if (reg != null)
34+
return reg;
35+
if (REGISTRY_ACCESS != null)
36+
return REGISTRY_ACCESS.registry(registryKey).orElse(null);
37+
return null;
38+
}
39+
1640
public static <T> List<T> deserializeList(JsonObject jObject, String memberName, JsonDeserializationContext context, Class<T> clazz) throws JsonParseException {
1741
return deserializeList(jObject, memberName, context, clazz, true);
1842
}
@@ -86,8 +110,7 @@ public static void serializeLocationList(JsonObject jObject, String memberName,
86110
public static <T> T deserializeRegistryObject(JsonElement jElement, ResourceKey<Registry<T>> registry) throws JsonParseException {
87111
if (!jElement.isJsonPrimitive())
88112
throw new JsonParseException("Expected %s to be a string".formatted(jElement));
89-
//noinspection unchecked
90-
Registry<T> reg = (Registry<T>) BuiltInRegistries.REGISTRY.get(registry.location());
113+
Registry<T> reg = getRegistry(registry);
91114
if (reg == null)
92115
throw new JsonParseException("Unknown registry %s".formatted(registry.location()));
93116
ResourceLocation objectId = ResourceLocation.parse(jElement.getAsString());
@@ -141,8 +164,7 @@ public static <T> List<T> deserializeRegistryObjectList(JsonObject jObject, Stri
141164
* Serializes a registry object (raw T)
142165
*/
143166
public static <T> JsonElement serializeRegistryObject(T object, ResourceKey<Registry<T>> registry) throws JsonParseException {
144-
//noinspection unchecked
145-
Registry<T> reg = (Registry<T>) BuiltInRegistries.REGISTRY.get(registry.location());
167+
Registry<T> reg = getRegistry(registry);
146168
if (reg == null)
147169
throw new JsonParseException("Unknown registry %s".formatted(registry.location()));
148170
ResourceLocation objectId = reg.getKey(object);
@@ -181,8 +203,7 @@ public static <T> Holder<T> deserializeRegistryObjectAsHolder(JsonElement jEleme
181203
if (!jElement.isJsonPrimitive())
182204
throw new JsonParseException("Expected %s to be a string".formatted(jElement));
183205

184-
//noinspection unchecked
185-
Registry<T> reg = (Registry<T>) BuiltInRegistries.REGISTRY.get(registry.location());
206+
Registry<T> reg = getRegistry(registry);
186207
if (reg == null)
187208
throw new JsonParseException("Unknown registry %s".formatted(registry.location()));
188209

0 commit comments

Comments
 (0)