|
8 | 8 | import io.papermc.paper.registry.keys.EnchantmentKeys; |
9 | 9 | import net.minecraft.core.Holder; |
10 | 10 | import net.minecraft.core.MappedRegistry; |
| 11 | +import net.minecraft.core.Registry; |
11 | 12 | import net.minecraft.core.WritableRegistry; |
12 | 13 | import net.minecraft.core.registries.BuiltInRegistries; |
13 | 14 | import net.minecraft.core.registries.Registries; |
|
22 | 23 | public class ParallelUtilsBootstrapper implements PluginBootstrap { |
23 | 24 | @Override |
24 | 25 | public void bootstrap(BootstrapContext context) { |
25 | | - MappedRegistry<WritableRegistry<?>> writable_registry = (MappedRegistry<WritableRegistry<?>>) getPrivateField("WRITABLE_REGISTRY", BuiltInRegistries.class, null, WritableRegistry.class); |
26 | | - |
27 | | - Map<WritableRegistry<?>, Holder.Reference<WritableRegistry<?>>> byValue = getPrivateField("byValue", MappedRegistry.class, writable_registry, Map.class); |
28 | 26 |
|
29 | | - WritableRegistry<Block> blockRegistry = null; |
30 | | - |
31 | | - for (WritableRegistry i : byValue.keySet()) { |
32 | | - if (String.valueOf(i.key().location()).equals(String.valueOf(Registries.BLOCK.location()))) { |
33 | | - blockRegistry = i; |
34 | | - break; |
35 | | - } |
36 | | - } |
| 27 | + WritableRegistry<Block> blockRegistry = getWritableRegistry(Registries.BLOCK); |
37 | 28 |
|
38 | 29 | if (blockRegistry == null) { |
39 | 30 | return; |
@@ -63,4 +54,23 @@ public static <T, U> U getPrivateField(String fieldName, Class<T> clazz, T objec |
63 | 54 | return null; |
64 | 55 | } |
65 | 56 | } |
| 57 | + |
| 58 | + // This is private since it can ONLY be run during bootstrap or things break real bad |
| 59 | + @Nullable |
| 60 | + private static <T> WritableRegistry<T> getWritableRegistry(ResourceKey<Registry<T>> registryKey) { |
| 61 | + |
| 62 | + // Get the global WRITABLE_REGISTRY registry |
| 63 | + MappedRegistry<WritableRegistry<?>> writable_registry = (MappedRegistry<WritableRegistry<?>>) getPrivateField("WRITABLE_REGISTRY", BuiltInRegistries.class, null, WritableRegistry.class); |
| 64 | + |
| 65 | + // Search registry byValue (since byKey doesn't work for whatever reason...) |
| 66 | + Map<WritableRegistry<?>, Holder.Reference<WritableRegistry<?>>> byValue = getPrivateField("byValue", MappedRegistry.class, writable_registry, Map.class); |
| 67 | + |
| 68 | + for (WritableRegistry i : byValue.keySet()) { |
| 69 | + if (String.valueOf(i.key().location()).equals(String.valueOf(registryKey.location()))) { |
| 70 | + return i; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return null; |
| 75 | + } |
66 | 76 | } |
0 commit comments