|
3 | 3 | import com.google.gson.*; |
4 | 4 | import net.minecraft.core.Holder; |
5 | 5 | import net.minecraft.core.Registry; |
| 6 | +import net.minecraft.core.RegistryAccess; |
6 | 7 | import net.minecraft.core.registries.BuiltInRegistries; |
7 | 8 | import net.minecraft.resources.ResourceKey; |
8 | 9 | import net.minecraft.resources.ResourceLocation; |
|
13 | 14 | import java.util.List; |
14 | 15 |
|
15 | 16 | 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 | + |
16 | 40 | public static <T> List<T> deserializeList(JsonObject jObject, String memberName, JsonDeserializationContext context, Class<T> clazz) throws JsonParseException { |
17 | 41 | return deserializeList(jObject, memberName, context, clazz, true); |
18 | 42 | } |
@@ -86,8 +110,7 @@ public static void serializeLocationList(JsonObject jObject, String memberName, |
86 | 110 | public static <T> T deserializeRegistryObject(JsonElement jElement, ResourceKey<Registry<T>> registry) throws JsonParseException { |
87 | 111 | if (!jElement.isJsonPrimitive()) |
88 | 112 | 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); |
91 | 114 | if (reg == null) |
92 | 115 | throw new JsonParseException("Unknown registry %s".formatted(registry.location())); |
93 | 116 | ResourceLocation objectId = ResourceLocation.parse(jElement.getAsString()); |
@@ -141,8 +164,7 @@ public static <T> List<T> deserializeRegistryObjectList(JsonObject jObject, Stri |
141 | 164 | * Serializes a registry object (raw T) |
142 | 165 | */ |
143 | 166 | 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); |
146 | 168 | if (reg == null) |
147 | 169 | throw new JsonParseException("Unknown registry %s".formatted(registry.location())); |
148 | 170 | ResourceLocation objectId = reg.getKey(object); |
@@ -181,8 +203,7 @@ public static <T> Holder<T> deserializeRegistryObjectAsHolder(JsonElement jEleme |
181 | 203 | if (!jElement.isJsonPrimitive()) |
182 | 204 | throw new JsonParseException("Expected %s to be a string".formatted(jElement)); |
183 | 205 |
|
184 | | - //noinspection unchecked |
185 | | - Registry<T> reg = (Registry<T>) BuiltInRegistries.REGISTRY.get(registry.location()); |
| 206 | + Registry<T> reg = getRegistry(registry); |
186 | 207 | if (reg == null) |
187 | 208 | throw new JsonParseException("Unknown registry %s".formatted(registry.location())); |
188 | 209 |
|
|
0 commit comments