[Server] ERROR Could not pass event InventoryClickEvent to (my plugin)

Discussion in 'Spigot Plugin Help' started by Mattgamee, May 29, 2016.

  1. Hello,
    I have created a in-game GUI so that players can change their languages, it works perfectly well, only problem is that when anyone click in any sort of Container, this error shows up:
    Code (Text):
    29.05 13:53:46 [Server] ERROR Could not pass event InventoryClickEvent to Core v1.7
    29.05 13:53:46 [Server] INFO org.bukkit.event.EventException
    29.05 13:53:46 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-8a048fe-d8637df]
    29.05 13:53:46 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-8a048fe-d8637df]
    29.05 13:53:46 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-8a048fe-d8637df]
    29.05 13:53:46 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-8a048fe-d8637df]
    29.05 13:53:46 [Server] INFO at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:1851) [spigot.jar:git-Spigot-8a048fe-d8637df]
    29.05 13:53:46 [Server] INFO at net.minecraft.server.v1_9_R2.PacketPlayInWindowClick.a(SourceFile:33) [spigot.jar:git-Spigot-8a048fe-d8637df]
    29.05 13:53:46 [Server] INFO at net.minecraft.server.v1_9_R2.PacketPlayInWindowClick.a(SourceFile:10) [spigot.jar:git-Spigot-8a048fe-d8637df]
    29.05 13:53:46 [Server] INFO at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-8a048fe-d8637df]
    29.05 13:53:46 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_72-internal]
    29.05 13:53:46 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_72-internal]
    29.05 13:53:46 [Server] INFO at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-8a048fe-d8637df]
    29.05 13:53:46 [Server] INFO at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:726) [spigot.jar:git-Spigot-8a048fe-d8637df]
    29.05 13:53:46 [Server] INFO at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-8a048fe-d8637df]
    29.05 13:53:46 [Server] INFO at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [spigot.jar:git-Spigot-8a048fe-d8637df]
    29.05 13:53:46 [Server] INFO at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [spigot.jar:git-Spigot-8a048fe-d8637df]
    29.05 13:53:46 [Server] INFO at java.lang.Thread.run(Thread.java:745) [?:1.8.0_72-internal]
    29.05 13:53:46 [Server] INFO Caused by: java.lang.NullPointerException
    29.05 13:53:46 [Server] INFO at zone.mine.core.events.InventoryListener.onInventoryClick(InventoryListener.java:28) ~[?:?]
    29.05 13:53:46 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_72-internal]
    29.05 13:53:46 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_72-internal]
    29.05 13:53:46 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_72-internal]
    29.05 13:53:46 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_72-internal]
    29.05 13:53:46 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-8a048fe-d8637df]
    29.05 13:53:46 [Server] INFO ... 15 more
    Here is code by the way:
    Code (Text):
    public class InventoryListener implements Listener {

        private Player p;
        //private Plugin p; // We only create this variable if we need it later. As of now, it's useless.

        public InventoryListener(Plugin pl){
        }
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            Player p = (Player)e.getWhoClicked();
           
            if (e.getCurrentItem() == null){ //this here is what someone suggested on a forum but it doesn't work :(
            }
            else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("English")) {
                Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "pex user " +p.getDisplayName() + " remove mz.fr");
                Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "pex user " +p.getDisplayName() + " add mz.en");
                Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "pex user " +p.getDisplayName() + " prefix &eEn &r");
                p.sendMessage(ChatColor.GREEN + "Your language has been successfully updated!");
                p.closeInventory();
            }
            else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Francais")) {
                Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "pex user " +p.getDisplayName() + " remove mz.en");
                Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "pex user " + p.getDisplayName() + " add mz.fr");
                Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "pex user " + p.getDisplayName() + " prefix &eFr &r");
                p.sendMessage(ChatColor.GREEN + "Votre langue a bien ete mise a jour!");
                p.closeInventory();
            }

        }
    }
    Thank you in advance for your help
     
  2. Here's how Chest Commands does it...
    Code (Text):
    if (icon != null && event.getInventory().getItem(slot) != null) {
                        Player clicker = (Player) event.getWhoClicked();
    Code (Text):
    if (command.isEmpty()) {
                return;
     
  3. Use a return statement so it knows not to do anything if the clicked item is null.
     
    • Informative Informative x 1
  4. I would suggest you to use getName() instead of getDisplayName(), I'm not sure if getDisplayName() always works good when using in commands (may contain wrong names).
     
    • Informative Informative x 1
  5. As @ghac said, you should change
    Code (Java):
    p.getDisplayName()
    to
    Code (Java):
    p.getName()
    as the display name is not guaranteed to be the actual name of the player.

    And as @pcgamers123 said, you also have to change
    Code (Java):
            if (e.getCurrentItem() == null){ //this here is what someone suggested on a forum but it doesn't work :(
            }
    to
    Code (Java):
            if (e.getCurrentItem() == null){ //this here is what someone suggested on a forum but it doesn't work :(
                return;
            }
     
    • Like Like x 1
    • Agree Agree x 1
  6. Can someone send me the full code please! im new on this
     
    • Funny Funny x 1