ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftRegistryAccess.java

61 lines
1.8 KiB
Java
Raw Normal View History

2024-04-24 00:17:14 +02:00
package com.comphenix.protocol.utility;
import java.lang.reflect.Modifier;
import org.bukkit.Bukkit;
import com.comphenix.protocol.reflect.FuzzyReflection;
import com.comphenix.protocol.reflect.accessors.Accessors;
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
import com.comphenix.protocol.reflect.fuzzy.FuzzyMethodContract;
2024-04-24 18:30:19 +02:00
/**
* Static getter for the registry access accessor which is need for most of the methods
* since 1.20.5 that access the registry and in form.
*/
2024-04-24 00:17:14 +02:00
public class MinecraftRegistryAccess {
private static MethodAccessor GET_SERVER = null;
private static MethodAccessor REGISTRY_ACCESS = null;
2024-04-24 18:30:19 +02:00
// lazy initialized
2024-04-24 00:17:14 +02:00
private static Object registryAccess = null;
static {
if (MinecraftVersion.v1_20_5.atOrAbove()) {
GET_SERVER = Accessors.getMethodAccessor(
FuzzyReflection.fromClass(MinecraftReflection.getCraftServer(), false)
.getMethod(FuzzyMethodContract.newBuilder()
.banModifier(Modifier.STATIC)
.returnDerivedOf(MinecraftReflection.getMinecraftServerClass())
.build()));
REGISTRY_ACCESS = Accessors.getMethodAccessor(
FuzzyReflection.fromClass(MinecraftReflection.getMinecraftServerClass(), false)
.getMethod(FuzzyMethodContract.newBuilder()
.banModifier(Modifier.STATIC)
.returnDerivedOf(MinecraftReflection.getHolderLookupProviderClass())
.build()));
}
}
2024-04-24 18:30:19 +02:00
/**
* Returns the composite global registry access. Equiv. of
* <pre>((CraftServer) Bukkit.getServer()).getServer().registryAccess()</pre>
*
* @return composite registy acesss
*/
2024-04-24 00:17:14 +02:00
public static Object get() {
if (GET_SERVER == null || REGISTRY_ACCESS == null) {
return null;
}
if (registryAccess == null) {
Object server = GET_SERVER.invoke(Bukkit.getServer());
registryAccess = REGISTRY_ACCESS.invoke(server);
}
return registryAccess;
}
}