Number key detection

Discussion in 'Plugin Development' started by Peepeepoopoomantime, Sep 25, 2019.

  1. Whats the function to detect if a player presses a number hotkey over an item?
     
  2. Offline

    timtower Moderator Moderator

  3. Offline

    KarimAKL

  4. That in mind, how would I go about detecting if the player doesnt click, but presses the number hoykey over an item. I know its possible but I'm not sure how to go about it
     
  5. Offline

    KarimAKL

  6. KarimAKL likes this.
  7. After a long time of experimentation and investigation I haven't been able to place this in an if-statement that detects the number. Here's a recent iteration of the code I've tried to work with.


    Code:
    public int getHotbarButton() {
            return getHotbarButton();
        }
      
      
        @EventHandler
        public void InvenClick(InventoryClickEvent event) {
          
          
            Player player = (Player) event.getWhoClicked();
            Inventory open = event.getClickedInventory();
            ItemStack item = event.getCurrentItem();
            if(open == null) {
                return;
            }
            if(open.getName().equals("Yellow Abilities")) {
                if(item.equals(null) || !item.hasItemMeta()) {
                    return;
                }
                if(item.getItemMeta().getDisplayName().equals(ChatColor.GOLD + "" + ChatColor.BOLD + "Super Yellow")){
                    getHotbarButton();
                  
                  
                }
                if(event.getHotbarButton == 1) {
                    //do stuff
                  
                }
              
            }
        }
     
  8. Offline

    KarimAKL

    @Peepeepoopoomantime
    1. Check for null with '==', not '.equals()'
    2. Why do you have 'getHotbarButton()' without accessing the event first, and not saving it?
    3. You forgot to add '()' after the method name, when checking if it's 1.

    You can set a variable for the hotbar button, like 'player', 'open', and 'item', then you can send a message to the player containing the value of that variable.
     
  9. With the checking for null, that works perfectly fine but i'll change it just in case. I have the getHotbarButton() because I didnt know what I was doing and frankly still dont, and what () after the method name? there isnt a method name at all. How do I set the variable for the hotbarbutton, I tried doing something along those line but it would either return an error or nothing at all.
     
  10. Offline

    KarimAKL

    @Peepeepoopoomantime I'd recommend learning Java before trying the Bukkit/Spigot API but, i'll list some things here:
    1. Your check for null would throw a NullPointerException because you are trying to use a method from something that's null.
    2. Parenthesis ( '()' ) are needed to call a method. Look at "if(event.getHotbarButton == 1) {"
    3. What are variables?
     

Share This Page