Skip to content

Commit

Permalink
inventory fixes for 1.20.3+
Browse files Browse the repository at this point in the history
  • Loading branch information
AoElite committed Jan 2, 2024
1 parent 02c3007 commit 01e3630
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 51 deletions.
9 changes: 5 additions & 4 deletions src/main/java/ac/grim/grimac/checks/impl/crash/CrashD.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
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;
import ac.grim.grimac.utils.anticheat.update.BlockPlace;
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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 01e3630

Please sign in to comment.