diff --git a/src/main/java/ac/grim/grimac/checks/impl/crash/CrashD.java b/src/main/java/ac/grim/grimac/checks/impl/crash/CrashD.java index 24743cb0ed..9de441f903 100644 --- a/src/main/java/ac/grim/grimac/checks/impl/crash/CrashD.java +++ b/src/main/java/ac/grim/grimac/checks/impl/crash/CrashD.java @@ -4,6 +4,7 @@ import ac.grim.grimac.checks.CheckData; import ac.grim.grimac.checks.type.PacketCheck; import ac.grim.grimac.player.GrimPlayer; +import ac.grim.grimac.utils.inventory.inventory.MenuType; import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.event.PacketReceiveEvent; import com.github.retrooper.packetevents.event.PacketSendEvent; @@ -19,15 +20,15 @@ public CrashD(GrimPlayer playerData) { super(playerData); } - private int type = -1; + private MenuType type = MenuType.UNKNOWN; private int lecternId = -1; @Override public void onPacketSend(final PacketSendEvent event) { if (event.getPacketType() == PacketType.Play.Server.OPEN_WINDOW && isSupportedVersion()) { WrapperPlayServerOpenWindow window = new WrapperPlayServerOpenWindow(event); - this.type = window.getType(); - if (type == 16) lecternId = window.getContainerId(); + this.type = MenuType.getMenuType(window.getType()); + if (type == MenuType.LECTERN) lecternId = window.getContainerId(); } } @@ -39,7 +40,7 @@ public void onPacketReceive(final PacketReceiveEvent event) { int button = click.getButton(); int windowId = click.getWindowId(); - if (type == 16 && windowId > 0 && windowId == lecternId) { + if (type == MenuType.LECTERN && windowId > 0 && windowId == lecternId) { if (flagAndAlert("clickType=" + clickType + " button=" + button)) { event.setCancelled(true); player.onPacketCancel(); 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 new file mode 100644 index 0000000000..3350a45c17 --- /dev/null +++ b/src/main/java/ac/grim/grimac/utils/inventory/inventory/MenuType.java @@ -0,0 +1,95 @@ +package ac.grim.grimac.utils.inventory.inventory; + +import ac.grim.grimac.player.GrimPlayer; +import ac.grim.grimac.utils.inventory.Inventory; +import com.github.retrooper.packetevents.PacketEvents; +import com.github.retrooper.packetevents.manager.server.ServerVersion; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Getter +public enum MenuType { + GENERIC_9x1(0), + GENERIC_9x2(1), + GENERIC_9x3(2), + GENERIC_9x4(3), + GENERIC_9x5(4), + GENERIC_9x6(5), + GENERIC_3x3(6), + CRAFTER_3x3(7), // only in versions 1.20.3 & greater + ANVIL(8), + BEACON(9), + BLAST_FURNACE(10), + BREWING_STAND(11), + CRAFTING(12), + ENCHANTMENT(13), + FURNACE(14), + GRINDSTONE(15), + HOPPER(16), + LECTERN(17), + LOOM(18), + MERCHANT(19), + SHULKER_BOX(20), + SMITHING(21), + SMOKER(22), + CARTOGRAPHY_TABLE(23), + STONECUTTER(24), + UNKNOWN(-1); + + private final int id; + + //TODO: could be optimized + 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)) { + if (id > 23) return UNKNOWN; + MenuType[] values = MenuType.values(); + if (id >= 7) id++; + return values[id]; + } + // 1.20.3 & greater + MenuType[] values = MenuType.values(); + if (id >= values.length) return UNKNOWN; + return MenuType.values()[id]; + } + + + public static AbstractContainerMenu getMenuFromID(GrimPlayer player, Inventory playerInventory, MenuType type) { + switch (type) { + case GENERIC_9x1: + case GENERIC_9x2: + case GENERIC_9x3: + case GENERIC_9x4: + case GENERIC_9x5: + case GENERIC_9x6: + return new BasicInventoryMenu(player, playerInventory, type.getId() + 1); + case GENERIC_3x3: + return new DispenserMenu(player, playerInventory); + case HOPPER: + return new HopperMenu(player, playerInventory); + default: + return new NotImplementedMenu(player, playerInventory); + } + } + + public static AbstractContainerMenu getMenuFromString(GrimPlayer player, Inventory inventory, String legacyType, int slots, int horse) { + switch (legacyType) { + case "minecraft:chest": + case "minecraft:container": + return new BasicInventoryMenu(player, inventory, slots / 9); + case "minecraft:dispenser": + case "minecraft:dropper": + return new DispenserMenu(player, inventory); + case "minecraft:hopper": + return new HopperMenu(player, inventory); + case "minecraft:shulker_box": + return new BasicInventoryMenu(player, inventory, 3); + default: // Villager menu + return new NotImplementedMenu(player, inventory); + } + } + +} diff --git a/src/main/java/ac/grim/grimac/utils/inventory/inventory/MenuTypes.java b/src/main/java/ac/grim/grimac/utils/inventory/inventory/MenuTypes.java deleted file mode 100644 index 309c0f3968..0000000000 --- a/src/main/java/ac/grim/grimac/utils/inventory/inventory/MenuTypes.java +++ /dev/null @@ -1,43 +0,0 @@ -package ac.grim.grimac.utils.inventory.inventory; - -import ac.grim.grimac.player.GrimPlayer; -import ac.grim.grimac.utils.inventory.Inventory; - -public class MenuTypes { - // I am unsure if I will write this, it might be needed for proxy support, although I am unsure. - public static AbstractContainerMenu getMenuFromID(GrimPlayer player, Inventory playerInventory, int id) { - switch (id) { - // All these use the same menu and a SimpleContainer - case 0: // 9x1 - case 1: // 9x2 - case 2: // 9x3 - case 3: // 9x4 - case 4: // 9x5 - case 5: // 9x6 - return new BasicInventoryMenu(player, playerInventory, id + 1); // don't repeat yourself :) - case 6: // 3x3 (SimpleContainer) - return new DispenserMenu(player, playerInventory); - case 15: // hopper - SimpleContainer - return new HopperMenu(player, playerInventory); - default: - return new NotImplementedMenu(player, playerInventory); - } - } - - public static AbstractContainerMenu getMenuFromString(GrimPlayer player, Inventory inventory, String legacyType, int slots, int horse) { - switch (legacyType) { - case "minecraft:chest": - case "minecraft:container": - return new BasicInventoryMenu(player, inventory, slots / 9); - case "minecraft:dispenser": - case "minecraft:dropper": - return new DispenserMenu(player, inventory); - case "minecraft:hopper": - return new HopperMenu(player, inventory); - case "minecraft:shulker_box": - return new BasicInventoryMenu(player, inventory, 3); - default: // Villager menu - return new NotImplementedMenu(player, inventory); - } - } -} diff --git a/src/main/java/ac/grim/grimac/utils/latency/CompensatedInventory.java b/src/main/java/ac/grim/grimac/utils/latency/CompensatedInventory.java index c34cdc8bf5..ed199a2e4d 100644 --- a/src/main/java/ac/grim/grimac/utils/latency/CompensatedInventory.java +++ b/src/main/java/ac/grim/grimac/utils/latency/CompensatedInventory.java @@ -1,6 +1,5 @@ package ac.grim.grimac.utils.latency; -import ac.grim.grimac.GrimAPI; import ac.grim.grimac.checks.Check; import ac.grim.grimac.checks.type.PacketCheck; import ac.grim.grimac.player.GrimPlayer; @@ -8,7 +7,7 @@ import ac.grim.grimac.utils.inventory.EquipmentType; import ac.grim.grimac.utils.inventory.Inventory; import ac.grim.grimac.utils.inventory.inventory.AbstractContainerMenu; -import ac.grim.grimac.utils.inventory.inventory.MenuTypes; +import ac.grim.grimac.utils.inventory.inventory.MenuType; import ac.grim.grimac.utils.inventory.inventory.NotImplementedMenu; import ac.grim.grimac.utils.lists.CorrectingPlayerInventoryStorage; import com.github.retrooper.packetevents.PacketEvents; @@ -343,11 +342,13 @@ public void onPacketSend(final PacketSendEvent event) { if (event.getPacketType() == PacketType.Play.Server.OPEN_WINDOW) { WrapperPlayServerOpenWindow open = new WrapperPlayServerOpenWindow(event); + MenuType menuType = MenuType.getMenuType(open.getType()); + AbstractContainerMenu newMenu; if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_14)) { - newMenu = MenuTypes.getMenuFromID(player, inventory, open.getType()); + newMenu = MenuType.getMenuFromID(player, inventory, menuType); } else { - newMenu = MenuTypes.getMenuFromString(player, inventory, open.getLegacyType(), open.getLegacySlots(), open.getHorseId()); + newMenu = MenuType.getMenuFromString(player, inventory, open.getLegacyType(), open.getLegacySlots(), open.getHorseId()); } packetSendingInventorySize = newMenu instanceof NotImplementedMenu ? UNSUPPORTED_INVENTORY_CASE : newMenu.getSlots().size();