Stop item movement on key pressing

Discussion in 'Spigot Plugin Development' started by iDoomfull, Sep 15, 2017.

  1. Hello, I want to stop players moving items from their hotbar using key 1,2,3 and so on. Look at this GIF if you didn't understand yet (I've pressed key 2 to move the second item into any slot I hover my cursor on)

    https://gyazo.com/048d05a45c37de919a8842f2c58fd94b

    InventoryClickEvent is not an option because there are no items that are clicked to be detected, so what event should I use to stop this
     
  2. In that case, your listener must not be registered or broken, since it does call an InventoryClickEvent.
    To be more exact:

    Code (Text):
    @EventHandler(priority = EventPriority.NORMAL)
        public void onInventoryClick(InventoryClickEvent event) {
            if(event.getAction() == InventoryAction.HOTBAR_SWAP){
                player.sendMessage(ChatColor.RED + "Please do not use hotkeys to move items.");
                event.setCancelled(true);
                return;
            }
        }
     
  3. It doesn't work, it only works when I swap items with each other
     
  4. This will stop all movements using the number shortcuts
    Code (Text):
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            if(event.getClick() == ClickType.NUMBER_KEY) {
                event.setCancelled(true);
            }
        }
    In order to preserve number shortcuts, the only solution I can see is to
    1. take a snapshot of the inventory
    2. let the event go through
    3. compare the result with the snapshot of the inventory to see what slots were changed.
    4. if a locked slot was involved in the action then set the inventory back to the snapshot
     
    #7 TheLoneSniper470, Jun 30, 2020
    Last edited: Jun 30, 2020
    • Like Like x 2