From 93f14a5b407e889beda33e2e271706a3019a1edd Mon Sep 17 00:00:00 2001 From: MachineBreaker Date: Sun, 17 Nov 2024 11:15:07 -0400 Subject: [PATCH] Optimise MenuType#getMenuType --- .../utils/inventory/inventory/MenuType.java | 48 ++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/main/java/ac/grim/grimac/utils/inventory/inventory/MenuType.java b/src/main/java/ac/grim/grimac/utils/inventory/inventory/MenuType.java index 769a8776f5..4b8f3fc530 100644 --- a/src/main/java/ac/grim/grimac/utils/inventory/inventory/MenuType.java +++ b/src/main/java/ac/grim/grimac/utils/inventory/inventory/MenuType.java @@ -39,23 +39,47 @@ public enum MenuType { private final int id; - //TODO: could be optimized - public static MenuType getMenuType(int id) { - if (id < 0) return UNKNOWN; + private static final MenuType[] MENU_BY_ID_ARRAY; + static { ServerVersion version = PacketEvents.getAPI().getServerManager().getVersion(); - // versions under 1.20.3 + MenuType[] menuTypes = MenuType.values(); + + int menuIdLimit; + if (version.isOlderThan(ServerVersion.V_1_20_3)) { - if (id > 23) return UNKNOWN; - MenuType[] values = MenuType.values(); - if (id >= 7) id++; - return values[id]; + // versions under 1.20.3 + menuIdLimit = 23; + } else { + // 1.20.3 & greater + menuIdLimit = menuTypes.length - 1; // Don't iterate the UNKNOWN menu type + } + + MENU_BY_ID_ARRAY = new MenuType[menuIdLimit]; + + for (int itr = 0; itr < menuIdLimit; itr++) { + MENU_BY_ID_ARRAY[itr] = menuTypes[itr]; } - // 1.20.3 & greater - MenuType[] values = MenuType.values(); - if (id >= values.length) return UNKNOWN; - return MenuType.values()[id]; } + public static MenuType getMenuType(int id) { + if (id < 0) { + return UNKNOWN; + } + + ServerVersion version = PacketEvents.getAPI().getServerManager().getVersion(); + // versions under 1.20.3 + if (version.isOlderThan(ServerVersion.V_1_20_3)) { // TODO: Can this be moved to the static block? + if (id >= 7) { + id++; + } + } + + if (id >= MENU_BY_ID_ARRAY.length) { + return UNKNOWN; + } + + return MENU_BY_ID_ARRAY[id]; + } public static AbstractContainerMenu getMenuFromID(GrimPlayer player, Inventory playerInventory, MenuType type) { return switch (type) {