Skip to content

Commit f0b5478

Browse files
committed
perf: use cached method and class handle
1 parent d44eba6 commit f0b5478

File tree

1 file changed

+24
-6
lines changed
  • inventory-framework-platform-bukkit/src/main/java/me/devnatan/inventoryframework/runtime/thirdparty

1 file changed

+24
-6
lines changed

inventory-framework-platform-bukkit/src/main/java/me/devnatan/inventoryframework/runtime/thirdparty/InventoryUpdate.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ public final class InventoryUpdate {
4848
public static final Class<?> CONTAINER;
4949
private static final Class<?> CONTAINERS;
5050
private static final Class<?> I_CHAT_MUTABLE_COMPONENT;
51+
private static final Class<?> MINECRAFT_MENU_TYPE;
5152

5253
// Methods
5354
public static final MethodHandle getBukkitView;
5455
private static final MethodHandle literal;
56+
private static final MethodHandle asVanilla;
5557

5658
// Constructors
5759
private static final MethodHandle chatMessage;
@@ -78,6 +80,7 @@ public final class InventoryUpdate {
7880
CONTAINER = ReflectionUtils.getNMSClass("world.inventory", "Container");
7981
I_CHAT_MUTABLE_COMPONENT =
8082
SUPPORTS_19 ? ReflectionUtils.getNMSClass("network.chat", "IChatMutableComponent") : null;
83+
MINECRAFT_MENU_TYPE = getClassOrNull("net.minecraft.world.inventory.MenuType");
8184

8285
// Initialize methods.
8386
getBukkitView = getMethod(CONTAINER, "getBukkitView", MethodType.methodType(InventoryView.class));
@@ -86,6 +89,17 @@ public final class InventoryUpdate {
8689
I_CHAT_BASE_COMPONENT, "b", MethodType.methodType(I_CHAT_MUTABLE_COMPONENT, String.class), true)
8790
: null;
8891

92+
final Class<?> paperAdventure = getClassOrNull("io.papermc.paper.adventure.PaperAdventure");
93+
asVanilla = paperAdventure == null
94+
? null
95+
: getMethod(
96+
paperAdventure,
97+
"asVanilla",
98+
MethodType.methodType(
99+
getClassOrNull("net.minecraft.network.chat.Component"),
100+
getClassOrNull("net.kyori.adventure.text.Component")),
101+
true);
102+
89103
// Initialize constructors.
90104
chatMessage = SUPPORTS_19 ? null : getConstructor(CHAT_MESSAGE, String.class, Object[].class);
91105
packetPlayOutOpenWindow = (useContainers())
@@ -100,6 +114,14 @@ public final class InventoryUpdate {
100114
windowId = getField(CONTAINER, int.class, "windowId", "j", "containerId");
101115
}
102116

117+
private static Class<?> getClassOrNull(String className) {
118+
try {
119+
return Class.forName(className);
120+
} catch (final ClassNotFoundException ignored) {
121+
}
122+
return null;
123+
}
124+
103125
/**
104126
* Update the player inventory, so you can change the title.
105127
*
@@ -161,18 +183,14 @@ public static void updateInventory(Player player, Object newTitle) {
161183

162184
Object packet;
163185
if (!(newTitle instanceof String)) {
164-
final Class<?> menuTypeClass = Class.forName("net.minecraft.world.inventory.MenuType");
165-
final Class<?> paperAdventure = Class.forName("io.papermc.paper.adventure.PaperAdventure");
166-
final Class<?> componentClass = Class.forName("net.kyori.adventure.text.Component");
167-
final Object minecraftComponent =
168-
paperAdventure.getMethod("asVanilla", componentClass).invoke(null, newTitle);
186+
final Object minecraftComponent = asVanilla.invoke(newTitle);
169187

170188
// Bukkit uses uppercase "X", Minecraft uses lowercase "x"
171189
// Bukkit: GENERIC_9X3 / Minecraft: GENERIC_9x3
172190
final String minecraftEnumName = container.name().replace("X", "x");
173191

174192
final Object menuType =
175-
menuTypeClass.getField(minecraftEnumName).get(null);
193+
MINECRAFT_MENU_TYPE.getField(minecraftEnumName).get(null);
176194

177195
packet = packetPlayOutOpenWindow.invoke(windowId, menuType, minecraftComponent);
178196
} else {

0 commit comments

Comments
 (0)