Skip to content

Commit b5c295b

Browse files
Abstract out writable registry
1 parent cd93935 commit b5c295b

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

api/src/main/java/parallelmc/parallelutils/ParallelUtilsBootstrapper.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.papermc.paper.registry.keys.EnchantmentKeys;
99
import net.minecraft.core.Holder;
1010
import net.minecraft.core.MappedRegistry;
11+
import net.minecraft.core.Registry;
1112
import net.minecraft.core.WritableRegistry;
1213
import net.minecraft.core.registries.BuiltInRegistries;
1314
import net.minecraft.core.registries.Registries;
@@ -22,18 +23,8 @@
2223
public class ParallelUtilsBootstrapper implements PluginBootstrap {
2324
@Override
2425
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);
2826

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);
3728

3829
if (blockRegistry == null) {
3930
return;
@@ -63,4 +54,23 @@ public static <T, U> U getPrivateField(String fieldName, Class<T> clazz, T objec
6354
return null;
6455
}
6556
}
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+
}
6676
}

0 commit comments

Comments
 (0)