Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimise MenuType#getMenuType #1804

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading