Hi there. Today I figured out how to send a packet of swapping two items in inventory by hovering on one and press the hotbar button of another. Clicking one is similar.
First I intercept all the outgoing packets and found one called `net.minecraft.network.protocol.game.ServerboundContainerClickPacket` which does the exactly thing you wished for.
Then search the source code (I get it in intellij idea's cache) and use grep to find all occurences (or you can use intellij or other IDE to grab all references but my IDEs all failed to do that) and read the code. Finally the wrapper for doing modification client-side and sending packet is `Minecraft.getInstance().gameMode.handleInventoryMouseClick`.
My code here:
private static void swapSlot(int slot_from, int slot_to){
// slot_from is the inventory id (for hotbar, it is [36, 45) )
// slot_to is the hotbar id [0, 9)
Minecraft mc = Minecraft.getInstance();
//mc.player.sendOpenInventory(); We don't even need to open the inventory. Actually there is not a packet telling server to open inventory, only a packet of telling server to close it but we don't need it either.
//Inventory inventory = mc.player.getInventory();
InventoryMenu inv_menu = mc.player.inventoryMenu;
mc.gameMode.handleInventoryMouseClick(
inv_menu.containerId,
slot_from,
slot_to,
ClickType.SWAP,
mc.player
);
}