Can not pass InventoryClickEvent

Discussion in 'Plugin Development' started by MounirDev, Feb 12, 2021.

  1. Offline

    MounirDev

    Hey,

    i maked a Plugin but there is this Error in the Console:

    Code:
    [19:02:35 ERROR]: Could not pass event InventoryClickEvent to RandSystem v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1891) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
            at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot.jar:git-Spigot-dcd1643-e60fc34]
            at java.lang.Thread.run(Thread.java:832) [?:?]
    Caused by: java.lang.NullPointerException: Cannot invoke "org.bukkit.inventory.meta.ItemMeta.getDisplayName()" because the return value of "org.bukkit.inventory.ItemStack.getItemMeta()" is null
            at de.mounir.listener.InventoryEvent.onInventoryClick(InventoryEvent.java:19) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[?:?]
            at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
            at java.lang.reflect.Method.invoke(Method.java:564) ~[?:?]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-dcd1643-e60fc34]
            ... 15 more
    
    Sourcecode:
    Code:
    package de.mounir.listener;
    
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    
    import de.mounir.main.Main;
    
    
    public class InventoryEvent implements Listener {
     
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
         
                 
                    if(e.getCurrentItem().getItemMeta().getDisplayName().equals("§f§lNORMAL")) {
                        if(p.hasPermission("normal.rand")) {
                            p.sendMessage(Main.getPre() + " §aDu hast den normalen Rand ausgewählt!");
                            p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1F, 1F);
                            e.setCancelled(true);
                         
                            if(e.getCurrentItem().getItemMeta().getDisplayName().equals("§6§lGOLD")) {
                                if(p.hasPermission("gold.rand")) {
                                    p.sendMessage(Main.getPre() + " §aDu hast den §6§lGOLD §aRand ausgewählt!");
                                    p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1F, 1F);
                                    e.setCancelled(true);
                                 
                                    if(e.getCurrentItem().getItemMeta().getDisplayName().equals("§b§lDIAMANT")) {
                                        if(p.hasPermission("diamant.rand")) {
                                            p.sendMessage(Main.getPre() + " §aDu hast den §b§lDIAMANT §ARand ausgwählt!");
                                            p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1F, 1F);
                                            e.setCancelled(true);
                                         
                                            if(e.getCurrentItem().getItemMeta().getDisplayName().equals("§7§lEISEN")) {
                                                if(p.hasPermission("iron.rand")) {
                                                    p.sendMessage(Main.getPre() + " §aDu hast den §7§lEISEN §aRand ausgewählt!");
                                                    p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1F, 1F);
                                                    e.setCancelled(true);
                                                 
                                                    if(e.getCurrentItem().getItemMeta().getDisplayName().equals("§0§lBEDROCK")) {
                                                        if(p.hasPermission("bedrock.rand")) {
                                                            p.sendMessage(Main.getPre() + " §aDu hast den §0§lBEDROCK §ARand ausgwählt!");
                                                            p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1F, 1F);
                                                            e.setCancelled(true);
                                                         
                                                            if(e.getCurrentItem().getItemMeta().getDisplayName().equals("§b§lBEACON")) {
                                                                if(p.hasPermission("beacon.rand")) {
                                                                    p.sendMessage(Main.getPre() + " §aDu hast den §b§lBEACON §ARand ausgwählt!");
                                                                    p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1F, 1F);
                                                                    e.setCancelled(true);
                                                                 
                                                                    if(e.getCurrentItem().getItemMeta().getDisplayName().equals("§8§lOBSIDIAN")) {
                                                                        if(p.hasPermission("obsidian.rand")) {
                                                                            p.sendMessage(Main.getPre() + " §aDu hast den §8§lOBSIDIAN §ARand ausgwählt!");
                                                                            p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1F, 1F);
                                                                            e.setCancelled(true);
                                                                         
                                                                            if(e.getCurrentItem().getItemMeta().getDisplayName().equals("§a§lENDPORTAL")) {
                                                                                if(p.hasPermission("endframe.rand")) {
                                                                                    p.sendMessage(Main.getPre() + " §aDu hast den §a§lENDPORTAL §ARand ausgwählt!");
                                                                                    p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1F, 1F);
                                                                                    e.setCancelled(true);
                                                                                 
                                                                                    if(e.getCurrentItem().getItemMeta().getDisplayName().equals("§4§lCOMMANDBLOCK")) {
                                                                                        if(p.hasPermission("cmdblock.rand")) {
                                                                                            p.sendMessage(Main.getPre() + " §aDu hast den §4§lCOMMANDBLOCK §ARand ausgwählt!");
                                                                                            p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1F, 1F);
                                                                                            e.setCancelled(true);
                                                                                         
                                                                                        } else
                                                                                            p.sendMessage(Main.getPre() + " §cDu brauchst ein höheren Rang!");
                                                                                    }
                                                                                 
                                                                                } else
                                                                                    p.sendMessage(Main.getPre() + " §cDu brauchst ein höheren Rang!");
                                                                            }
                                                                         
                                                                        } else
                                                                            p.sendMessage(Main.getPre() + " §cDu brauchst ein höheren Rang!");
                                                                    }
                                                                 
                                                                } else
                                                                    p.sendMessage(Main.getPre() + " §cDu brauchst ein höheren Rang!");
                                                            }
                                                        } else
                                                            p.sendMessage(Main.getPre() + " §cDu brauchst ein höheren Rang!");
                                                    }
                                                } else
                                                    p.sendMessage(Main.getPre() + " §cDu brauchst ein höheren Rang!");
                                            }
                                        } else
                                            p.sendMessage(Main.getPre() + " §cDu brauchst ein höheren Rang!");
                                    }
                                } else
                                    p.sendMessage(Main.getPre() + " §cDu brauchst ein höheren Rang!");
                            }
                        } else
                            p.sendMessage(Main.getPre() + " §cDazu hast du keine Rechte!");
                    }
                }
            }
    Please help me
     
    Last edited by a moderator: Feb 12, 2021
  2. Offline

    timtower Administrator Administrator Moderator

    @MounirDev Use "else if" checks instead of the nested stuff that you have now (won't work anyways)
    And check if there is an item, check if there is item meta, check if there is a displayname.
     
  3. Offline

    Strahan

    Wow. That's... wow. When your code resembles a huge greater-than sign, you need to stop and rethink your approach :)

    As timtower said, you need to add checks. InventoryClickEvent#getCurrentItem() is annotated nullable, as is ItemStack#getItemMeta(). If either of those are null, the plugin will crash. Also since you are nesting everything, only the normal item will ever work. Because if they click an item with GOLD name, for it to get to the point where it would check that, it must have already checked if the name is NORMAL. Obviously, if the name is NORMAL it can't also be GOLD.

    Also I would highly suggest you learn about abstraction and learn how to use the config system. I wouldn't be hardcoding any of this. Also using negative check and return is my personal preference to avoid huge indentaiton.

    What I'd do instead is check if they are in the desired inv in the first place, then get the display name stripped of color and use that as a key to pull the requisite permission and messaging from config. This would allow this mess to be distilled down to:
    Code:
    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) {
      if (!ChatColor.stripColor(e.getView().getTitle()).equalsIgnoreCase("my special inv")) return;
      if (e.getCurrentItem() == null) return;
      if (!e.getCurrentItem().hasItemMeta()) return;
    
      String code = ChatColor.stripColor(e.getCurrentItem().getItemMeta().getDisplayName().toLowerCase());
      ConfigurationSection cfg = plugin.getConfig().getConfigurationSection("config.invstuff." + code);
      if (cfg == null) {
        plugin.getLogger().warning("** An item in the special inv is missing the config data!  [" + code + "]");
        return;
      }
    
      Sound sound = getSound(cfg.getString("sound", "ENTITY_EXPERIENCE_ORB_PICKUP"));
      String perm = cfg.getString("perm");
      String display = cfg.getString("disp");
      if (perm == null || display == null) {
        plugin.getLogger().warning("** An item in the special inv is missing perm or disp config data!  [" + code + "]");
        return;
      }
    
      Player p = (Player)e.getWhoClicked();
      if (!p.hasPermission(perm)) {
        // send access denied error
        return;
      }
    
      p.sendMessage(ChatColor.translateAlternateColorCodes('&', display));
      if (sound != null) p.playSound(p.getLocation(), sound, 1F, 1F);
      e.setCancelled(true);
    }
    
    public Sound getSound(String name) {
      try {
        Sound returnValue = Sound.valueOf(name.toUpperCase().replace(" ", "_"));
        return returnValue;
      } catch (IllegalArgumentException ex) {
        return null;
      }
    }
    I wrote this from memory, it's not validated through the IDE but it should at least give you an idea of what I mean. Normally I would check things before casting, but I don't think anything but a Player can trigger an InventoryClickEvent.
     

Share This Page