|
3 | 3 | import io.papermc.paper.plugin.bootstrap.BootstrapContext; |
4 | 4 | import io.papermc.paper.plugin.bootstrap.PluginBootstrap; |
5 | 5 | import io.papermc.paper.plugin.bootstrap.PluginProviderContext; |
| 6 | +import io.papermc.paper.registry.RegistryAccess; |
| 7 | +import io.papermc.paper.registry.event.RegistryEvents; |
| 8 | +import io.papermc.paper.registry.keys.EnchantmentKeys; |
| 9 | +import net.minecraft.core.Holder; |
| 10 | +import net.minecraft.core.MappedRegistry; |
| 11 | +import net.minecraft.core.WritableRegistry; |
| 12 | +import net.minecraft.core.registries.BuiltInRegistries; |
| 13 | +import net.minecraft.core.registries.Registries; |
| 14 | +import net.minecraft.resources.ResourceKey; |
| 15 | +import net.minecraft.world.level.block.Block; |
6 | 16 | import org.bukkit.plugin.java.JavaPlugin; |
| 17 | +import org.jetbrains.annotations.Nullable; |
| 18 | + |
| 19 | +import java.lang.reflect.Field; |
| 20 | +import java.util.Map; |
7 | 21 |
|
8 | 22 | public class ParallelUtilsBootstrapper implements PluginBootstrap { |
9 | 23 | @Override |
10 | 24 | 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 | + |
| 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 | + } |
| 37 | + |
| 38 | + if (blockRegistry == null) { |
| 39 | + return; |
| 40 | + } |
11 | 41 |
|
12 | 42 | } |
13 | 43 |
|
14 | 44 | @Override |
15 | 45 | public JavaPlugin createPlugin(PluginProviderContext context) { |
16 | 46 | return PluginBootstrap.super.createPlugin(context); |
17 | 47 | } |
| 48 | + |
| 49 | + @Nullable |
| 50 | + public static <T, U> U getPrivateField(String fieldName, Class<T> clazz, T object, Class<U> retClazz) throws ClassCastException{ |
| 51 | + Field field; |
| 52 | + |
| 53 | + try { |
| 54 | + field = clazz.getDeclaredField(fieldName); |
| 55 | + |
| 56 | + field.setAccessible(true); |
| 57 | + |
| 58 | + Object o = field.get(object); |
| 59 | + |
| 60 | + return (U) o; |
| 61 | + } catch (NoSuchFieldException | IllegalAccessException e) { |
| 62 | + e.printStackTrace(); |
| 63 | + return null; |
| 64 | + } |
| 65 | + } |
18 | 66 | } |
0 commit comments