Solved open player inventory

Discussion in 'Spigot Plugin Development' started by Marshmalah, Jan 11, 2020.

  1. hello I would like to be able to put items by default as soon as the player opens his inventory in the location 80 81 82 83 of inventory crafting someone would know what event to use to do this please ?

    Thanks for watching
    ;)
     
  2. The player inventory opening is client-side. The server can't detect when it happens. You'll have to find a different method.
     
  3. One of the things you could do is to listen to a player join and set the inventory contents once he joins. Depending on what you want to do exactly, you then have to Listen to InventoryClickEvent's and disable the ability to move items out of said slots...
     
  4. I will use the action when the player clicks on one of the boxes (80, 81, 82, 83).
    How I can define an action on these boxes with the inventoryclickevent
    it works when we open a chest or something similar I already tried this method
     
    #6 Marshmalah, Jan 11, 2020
    Last edited: Jan 11, 2020
  5. Use InventoryCLickEvent. It gives you the slot that the player clicked in. Probably you'll want to use the InventoryCLickEvent#rawSlot(), not just InventoryCLickEvent#getSlot() (method names may vary from the actual name)
     
    • Like Like x 1
  6. Have use that
    Code (Java):
        @EventHandler
        public void onPlayerOpenInventory(InventoryClickEvent e)
        {
            Player p = (Player) e.getWhoClicked();
           
            int slot = e.getRawSlot();
            System.out.println(slot);
            if(slot == 1)
            {
                p.sendMessage(""+e.getWhoClicked().getName());
                return;
            }
            return;
        }
    but how I can define the condition to send a message to the player if the box is in the player's craft inventory
     
  7. FrostedSnowman

    Resource Staff

    InventoryClickEvent, or any of the InventoryEvent(s) return an Inventory. An Inventory allows you to get the type, so just check the type.

    https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/Inventory.html#getType--

    Code (Java):
    if (e.getClickedInventory().getType() == InventoryType.CRAFTING) {
    }
    Inside the condition block you can debug the slot number (if you want to target a specific slot for some code), since I don't know the slot #'s for the grid off the top of my head
     
    • Agree Agree x 2
  8. So you want to send a message whenever he clicks inside his crafting box is that right?
    Either like this:

    Code (Java):
    if (slot == CB_0 || slot == CB_1 || slot == CB_1 || slot==CB_2) {
        // ...
    }
    where CB_0-1 are the slots 0-3 of the 2x2 crafting-slot

    or like this:

    Code (Java):
    if (e.getInventory().getType() == InventoryType.CRAFTING) {
        // ...
    }
    second should work, but I'm not 100% sure
     
    • Agree Agree x 1
  9. Have try that
    Code (Java):
        @EventHandler
        public void onPlayerOpenInventory(InventoryClickEvent e)
        {
            Player p = (Player) e.getWhoClicked();
           
            int slot = e.getRawSlot();
            System.out.println(slot);
            InventoryType type = p.getInventory().getType();
            System.out.println(type);
            if((p.getInventory().getType() == InventoryType.WORKBENCH)) {
                if(slot == 1)
                {
                    p.sendMessage(""+e.getWhoClicked().getName());
                    e.setCancelled(true);
                    return;
                }
                if(slot == 2)
                {
                    e.setCancelled(true);
                    return;
                }
                if(slot == 3)
                {
                    e.setCancelled(true);
                    return;
                }
                if(slot == 4)
                {
                    e.setCancelled(true);
                    return;
                }
            }
            return;
        }
    That return in console PLAYER in chest/inventoryplayer etc and i don't know how use CB_0 in my code
     
  10. InventoryType#CRAFTING not WORKBENCH
    Also use event#getClickedInventory not p#getInventory
     
    • Agree Agree x 1
  11. The problem is that it always returns me an inventory type = PLAYER
    Edit : Ok have see i use player instand of event
    Thanks all :)
     
  12. That's because you got the players inventory not the clicked inventory. p#getInventory returns the players 9 hotbbar slots, 27 container slots and 4 armor slots. You need to check that event#getClickedInventory is InventoryType#CRAFTING which is the 4 slots and result slot of the players inventory