Skip to content

Commit

Permalink
Optimise MenuType#getMenuType
Browse files Browse the repository at this point in the history
  • Loading branch information
MachineBreaker committed Nov 17, 2024
1 parent 39a812c commit 93f14a5
Showing 1 changed file with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 93f14a5

Please sign in to comment.