Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move new reflection to CraftBukkitAccess
- Loading branch information
1 parent
f9fb335
commit 62db23f
Showing
2 changed files
with
114 additions
and
79 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
platform-bukkit/src/main/java/net/kyori/adventure/platform/bukkit/CraftBukkitAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package net.kyori.adventure.platform.bukkit; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.reflect.Modifier; | ||
import java.util.Optional; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import static net.kyori.adventure.platform.bukkit.MinecraftReflection.findClass; | ||
import static net.kyori.adventure.platform.bukkit.MinecraftReflection.findConstructor; | ||
import static net.kyori.adventure.platform.bukkit.MinecraftReflection.findMcClassName; | ||
import static net.kyori.adventure.platform.bukkit.MinecraftReflection.findNmsClassName; | ||
import static net.kyori.adventure.platform.bukkit.MinecraftReflection.needClass; | ||
import static net.kyori.adventure.platform.bukkit.MinecraftReflection.searchMethod; | ||
import static net.kyori.adventure.platform.facet.Knob.logError; | ||
|
||
final class CraftBukkitAccess { | ||
|
||
static final @Nullable Class<?> CLASS_CHAT_COMPONENT = findClass( | ||
findNmsClassName("IChatBaseComponent"), | ||
findMcClassName("network.chat.IChatBaseComponent"), | ||
findMcClassName("network.chat.Component") | ||
); | ||
|
||
private CraftBukkitAccess() { | ||
} | ||
|
||
static final class Chat1_19_3 { | ||
static final Class<?> CLASS_REGISTRY = needClass( | ||
findMcClassName("core.IRegistry"), | ||
findMcClassName("core.Registry") | ||
); | ||
static final Class<?> CLASS_SERVER_LEVEL = needClass( | ||
findMcClassName("server.level.WorldServer"), | ||
findMcClassName("server.level.ServerLevel") | ||
); | ||
static final Class<?> CLASS_REGISTRY_ACCESS = needClass( | ||
findMcClassName("core.IRegistryCustom"), | ||
findMcClassName("core.RegistryAccess") | ||
); | ||
static final Class<?> CLASS_RESOURCE_KEY = needClass(findMcClassName("resources.ResourceKey")); | ||
static final Class<?> CLASS_RESOURCE_LOCATION = needClass( | ||
findMcClassName("resources.MinecraftKey"), | ||
findMcClassName("resources.ResourceLocation") | ||
); | ||
|
||
static final @Nullable MethodHandle NEW_RESOURCE_LOCATION = findConstructor(CLASS_RESOURCE_LOCATION, String.class, String.class); | ||
static final @Nullable MethodHandle RESOURCE_KEY_CREATE = searchMethod(CLASS_RESOURCE_KEY, Modifier.PUBLIC | Modifier.STATIC, "create", CLASS_RESOURCE_KEY, CLASS_RESOURCE_KEY, CLASS_RESOURCE_LOCATION); | ||
static final @Nullable MethodHandle SERVER_PLAYER_GET_LEVEL = searchMethod(CraftBukkitFacet.CRAFT_PLAYER_GET_HANDLE.type().returnType(), Modifier.PUBLIC, "getLevel", CLASS_SERVER_LEVEL); | ||
static final @Nullable MethodHandle SERVER_LEVEL_GET_REGISTRY_ACCESS = searchMethod(CLASS_SERVER_LEVEL, Modifier.PUBLIC, "registryAccess", CLASS_REGISTRY_ACCESS); | ||
static final @Nullable MethodHandle REGISTRY_ACCESS_GET_REGISTRY_OPTIONAL = searchMethod(CLASS_REGISTRY_ACCESS, Modifier.PUBLIC, "registry", Optional.class, CLASS_RESOURCE_KEY); | ||
static final @Nullable MethodHandle REGISTRY_GET_OPTIONAL = searchMethod(CLASS_REGISTRY, Modifier.PUBLIC, "getOptional", Optional.class, CLASS_RESOURCE_LOCATION); | ||
static final @Nullable MethodHandle REGISTRY_GET_ID = searchMethod(CLASS_REGISTRY, Modifier.PUBLIC, "getId", int.class, Object.class); | ||
static final @Nullable MethodHandle DISGUISED_CHAT_PACKET_CONSTRUCTOR; | ||
static final @Nullable MethodHandle CHAT_TYPE_BOUND_NETWORK_CONSTRUCTOR; | ||
|
||
static final @Nullable Object CHAT_TYPE_RESOURCE_KEY; | ||
|
||
static { | ||
MethodHandle boundNetworkConstructor = null; | ||
MethodHandle disguisedChatPacketConstructor = null; | ||
Object chatTypeResourceKey = null; | ||
|
||
try { | ||
Class<?> classChatTypeBoundNetwork = findClass(findMcClassName("network.chat.ChatType$BoundNetwork")); | ||
if (classChatTypeBoundNetwork == null) { | ||
final Class<?> parentClass = findClass(findMcClassName("network.chat.ChatMessageType")); | ||
if (parentClass != null) { | ||
for (final Class<?> childClass : parentClass.getClasses()) { | ||
boundNetworkConstructor = findConstructor(childClass, int.class, CLASS_CHAT_COMPONENT, CLASS_CHAT_COMPONENT); | ||
if (boundNetworkConstructor != null) { | ||
classChatTypeBoundNetwork = childClass; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
final Class<?> disguisedChatPacketClass = findClass(findMcClassName("network.protocol.game.ClientboundDisguisedChatPacket")); | ||
if (disguisedChatPacketClass != null && classChatTypeBoundNetwork != null) { | ||
disguisedChatPacketConstructor = findConstructor(disguisedChatPacketClass, CLASS_CHAT_COMPONENT, classChatTypeBoundNetwork); | ||
} | ||
|
||
if (NEW_RESOURCE_LOCATION != null && RESOURCE_KEY_CREATE != null) { | ||
final MethodHandle createRegistryKey = searchMethod(CLASS_RESOURCE_KEY, Modifier.PUBLIC | Modifier.STATIC, "createRegistryKey", CLASS_RESOURCE_KEY, CLASS_RESOURCE_LOCATION); | ||
if (createRegistryKey != null) { | ||
chatTypeResourceKey = createRegistryKey.invoke(NEW_RESOURCE_LOCATION.invoke("minecraft", "chat_type")); | ||
} | ||
} | ||
} catch (final Throwable error) { | ||
logError(error, "Failed to initialize 1.19.3 chat support"); | ||
} | ||
|
||
DISGUISED_CHAT_PACKET_CONSTRUCTOR = disguisedChatPacketConstructor; | ||
CHAT_TYPE_BOUND_NETWORK_CONSTRUCTOR = boundNetworkConstructor; | ||
CHAT_TYPE_RESOURCE_KEY = chatTypeResourceKey; | ||
} | ||
|
||
private Chat1_19_3() { | ||
} | ||
|
||
static boolean isSupported() { | ||
return SERVER_LEVEL_GET_REGISTRY_ACCESS != null && REGISTRY_ACCESS_GET_REGISTRY_OPTIONAL != null && REGISTRY_GET_OPTIONAL != null && CHAT_TYPE_BOUND_NETWORK_CONSTRUCTOR != null && DISGUISED_CHAT_PACKET_CONSTRUCTOR != null && CHAT_TYPE_RESOURCE_KEY != null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters