Could not pass event InventoryClickEvent

Discussion in 'Spigot Plugin Development' started by MaxKrissigo, May 13, 2016.

  1. I'm getting an error in the console when I right click on hologram item in inv2. It sends the message I specify (for debugging), but nothing else happens.

    Console error on the click:
    Code (Text):
    Could not pass event InventoryClickEvent to MainPlugin v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1852) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_92]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_92]
        at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:721) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_92]
    Caused by: java.lang.NullPointerException
        at me.MaxKrissigo.MainPlugin.Cosmetic.onInventoryClickinv2(Cosmetic.java:111) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_92]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_92]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_92]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_92]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        ... 15 more
     

    My cosmetic class:
    Code (Text):
    package me.MaxKrissigo.MainPlugin;

    import java.util.Arrays;
    import java.util.HashMap;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;

    import com.gmail.filoghost.holographicdisplays.api.Hologram;
    import com.gmail.filoghost.holographicdisplays.api.HologramsAPI;
    import com.gmail.filoghost.holographicdisplays.api.line.TextLine;

    public class Cosmetic implements Listener {

        HashMap<String, Boolean> Hologram = new HashMap<String, Boolean>();
       
            private Inventory inv;
            private Inventory inv1;
            private Inventory inv2;
            private ItemStack killMessage, killDisplay, hologram;
           
            public void onPlayerJoinEvent(PlayerJoinEvent e) {
               
                Player p = e.getPlayer();
                String player = p.getName();
               
                Hologram.put(player, false);

               
            }
           
            public Cosmetic(Plugin p) {
               
                    inv1 = Bukkit.getServer().createInventory(null, 45, "Kill Messages");
                    inv2 = Bukkit.getServer().createInventory(null, 45, "Kill Displays");
                    inv = Bukkit.getServer().createInventory(null, 45, "Cosmetic");
                   
                       
                        hologram = createItem2(Material.DIAMOND, ChatColor.GOLD + "Hologram ");
                       
                       

                   
                    killMessage = createItem(Material.DIAMOND_SWORD, ChatColor.GOLD + "Kill Message");
                    killDisplay = createItem1(Material.GLASS, ChatColor.RED + "Kill Display");
                   
                    inv2.setItem(12, hologram);
                    inv.setItem(20, killMessage);
                    inv.setItem(24, killDisplay);
                   
                    Bukkit.getServer().getPluginManager().registerEvents(this, p);
            }
           
            private ItemStack createItem(Material material, String name) {
                    ItemStack i = new ItemStack(material, 1);
                    ItemMeta im = i.getItemMeta();
                    im.setDisplayName(name);
                    im.setLore(Arrays.asList("Change your kill message selection"));
                    i.setItemMeta(im);
                    return i;
            }
            private ItemStack createItem1(Material material, String name) {
                ItemStack i = new ItemStack(material, 1);
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList("Change your kill message display"));
                i.setItemMeta(im);
                return i;
        }
            private ItemStack createItem2(Material material, String name) {
                ItemStack i = new ItemStack(material, 1);
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList("On killing another player," , "a hologram will be placed" , "it will last for 5mins" , "and the death messages", "will equal your selected", "death messages"));
                i.setItemMeta(im);
                return i;
        }
           
            public void show(Player p) {
                    p.openInventory(inv);
            }
           
            @EventHandler
            public void onInventoryClickinv2(InventoryClickEvent e) {
                if (!e.getInventory().getName().equalsIgnoreCase(inv2.getName())) return;
                if (e.getCurrentItem().getItemMeta() == null) return;
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Hologram")) {
                    e.setCancelled(true);
                    Player p = (Player) e.getWhoClicked();
                   
                    if (e.getCurrentItem() == null){
                       
                        p.sendMessage("null");
                       
                    }
                   
                    p.sendMessage("clickedhologram");
                   
                    if (Hologram.get(p.getName())) {
                       
                        hologram = createItem2(Material.DIAMOND, ChatColor.GOLD + "Hologram " + ChatColor.RED +  "[DISABLED]");
                       
                        Hologram.put(p.getName(), false);
                        p.sendMessage("Disabled");
                       
                    } else if (!Hologram.get(p.getName())) {

                        hologram = createItem2(Material.DIAMOND, ChatColor.GOLD + "Hologram " + ChatColor.GREEN + "[ENABLED]");
                       
                        Hologram.put(p.getName(), true);
                        p.sendMessage("Enabled");
                    }
            }
               
            }
           
            @EventHandler
            public void onInventoryClick(InventoryClickEvent e) {
                    if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
                    if (e.getCurrentItem().getItemMeta() == null) return;
                    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Kill Message")) {
                            e.setCancelled(true);
                            e.getWhoClicked().openInventory(inv1);
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Kill Display")) {
                            e.setCancelled(true);
                            e.getWhoClicked().openInventory(inv2);
                    }
                   
            }
           
            public void onPlayerDeath(PlayerDeathEvent e) {
               
                Player p = e.getEntity();
               
                if(p.getKiller() instanceof Player) {
                   
                    String player = e.getEntity().getName();
                    String killer = p.getKiller().getName();
                   
                    if (Hologram.get(p.getName())) {
                   
                   
                    Location where = e.getEntity().getLocation().add(0, 2, 0);
                    Hologram hologram = HologramsAPI.createHologram((Plugin) this, where);
                    TextLine textLine = hologram.appendTextLine(killer + "has rekt" + player);
                   
                    long fiveMinutesMillis = 10 * 60 * 5000; // Ten minutes in milliseconds
                    long elapsedMillis = System.currentTimeMillis() - hologram.getCreationTimestamp(); // Milliseconds elapsed from the creation of the hologram

                    if (elapsedMillis > fiveMinutesMillis) {
                        hologram.delete();
                    }
                    }
                   
                }
               
            }
           
        }
     
  2. MiniDigger

    Supporter

    check if e.getCurrentitem is null before calling it in your second line of your event handler.
     
  3. I changed the code to this:

    Code (Text):
            @EventHandler
            public void onInventoryClickinv2(InventoryClickEvent e) {
                if (!e.getInventory().getName().equalsIgnoreCase(inv2.getName())) return;
                if (e.getCurrentItem() == null) return;
                if (e.getCurrentItem().getItemMeta() == null) return;
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Hologram")) {
                    e.setCancelled(true);
                    Player p = (Player) e.getWhoClicked();
                   
                    if (e.getCurrentItem() == null){
                       
                        p.sendMessage("null");
                       
                    }
                   
                    p.sendMessage("clickedhologram");
                   
                    if (Hologram.get(p.getName())) {
                       
                        hologram = createItem2(Material.DIAMOND, ChatColor.GOLD + "Hologram " + ChatColor.RED +  "[DISABLED]");
                       
                        Hologram.put(p.getName(), false);
                        p.sendMessage("Disabled");
                       
                    } else if (!Hologram.get(p.getName())) {

                        hologram = createItem2(Material.DIAMOND, ChatColor.GOLD + "Hologram " + ChatColor.GREEN + "[ENABLED]");
                       
                        Hologram.put(p.getName(), true);
                        p.sendMessage("Enabled");
                    }
            }
               
            }
    But I still got an error in the console when I clicked on it.
    Code (Text):
    Could not pass event InventoryClickEvent to MainPlugin v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1852) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_92]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_92]
        at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:721) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_92]
    Caused by: java.lang.NullPointerException
        at me.MaxKrissigo.MainPlugin.Cosmetic.onInventoryClickinv2(Cosmetic.java:112) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_92]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_92]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_92]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_92]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        ... 15 more
     
     
  4. MiniDigger

    Supporter

    can you highlight this line? Cosmetic.java:112
     
  5. This problem must come to "Hologram", in my opinion
     
  6. I've figured out how to fix some of my code, and it seems to be working, but instead of sending the enable message, after enabling it, it continuously sends the disabled message.
    EDIT: The line of code which isn't working is
    Code (Text):
                    } else if (!Hologram.containsKey(p)) { //THIS LINE ISN'T WORKING
    and it isn't out putting if the boolean were to be set to false
    Code (Text):
    package me.MaxKrissigo.MainPlugin;

    import java.util.Arrays;
    import java.util.HashMap;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;

    import com.gmail.filoghost.holographicdisplays.api.Hologram;
    import com.gmail.filoghost.holographicdisplays.api.HologramsAPI;
    import com.gmail.filoghost.holographicdisplays.api.line.TextLine;

    public class Cosmetic implements Listener {

        HashMap<Player, Boolean> Hologram = new HashMap<Player, Boolean>();
     
            private Inventory inv;
            private Inventory inv1;
            private Inventory inv2;
            private ItemStack killMessage, killDisplay, hologram;
         
            public void onPlayerJoinEvent(PlayerJoinEvent e) {
             
             
             
                Player p = e.getPlayer();
                String player = p.getName();
             
                p.setDisplayName("[Recruit]" + player);
                  if(!p.hasPlayedBefore()) {
                   
                      Hologram.put(p, false);
                   
                      }
               
                    }
             
            public Cosmetic(Plugin p) {
             
                    inv1 = Bukkit.getServer().createInventory(null, 45, "Kill Messages");
                    inv2 = Bukkit.getServer().createInventory(null, 45, "Kill Displays");
                    inv = Bukkit.getServer().createInventory(null, 45, "Cosmetic");
                 
                     
                        hologram = createItem2(Material.DIAMOND, ChatColor.GOLD + "Hologram ");
                     
                     

                 
                    killMessage = createItem(Material.DIAMOND_SWORD, ChatColor.GOLD + "Kill Message");
                    killDisplay = createItem1(Material.GLASS, ChatColor.RED + "Kill Display");
                 
                    inv2.setItem(12, hologram);
                    inv.setItem(20, killMessage);
                    inv.setItem(24, killDisplay);
                 
                    Bukkit.getServer().getPluginManager().registerEvents(this, p);
            }
         
            private ItemStack createItem(Material material, String name) {
                    ItemStack i = new ItemStack(material, 1);
                    ItemMeta im = i.getItemMeta();
                    im.setDisplayName(name);
                    im.setLore(Arrays.asList("Change your kill message selection"));
                    i.setItemMeta(im);
                    return i;
            }
            private ItemStack createItem1(Material material, String name) {
                ItemStack i = new ItemStack(material, 1);
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList("Change your kill message display"));
                i.setItemMeta(im);
                return i;
        }
            private ItemStack createItem2(Material material, String name) {
                ItemStack i = new ItemStack(material, 1);
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList("On killing another player," , "a hologram will be placed" , "it will last for 5mins" , "and the death messages", "will equal your selected", "death messages"));
                i.setItemMeta(im);
                return i;
        }
         
            public void show(Player p) {
                    p.openInventory(inv);
            }
         
            @EventHandler
            public void onInventoryClickinv2(InventoryClickEvent e) {
                if (!e.getInventory().getName().equalsIgnoreCase(inv2.getName())) return;
                if (e.getCurrentItem() == null) return;
                if (e.getCurrentItem().getItemMeta() == null) return;
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Hologram")) {
                    e.setCancelled(true);
                    Player p = (Player) e.getWhoClicked();
                 
                    if (e.getCurrentItem() == null) {
                     
                        p.sendMessage("null");
                     
                    }
                 
                    p.sendMessage("clickedhologram");
                 
                    if (Hologram.containsKey(p)) {
                                         
                        Hologram.put(p, false);
                        p.sendMessage("Disabled");
                     
                    } else if (!Hologram.containsKey(p)) { //THIS LINE ISN'T WORKING

                        hologram = createItem2(Material.DIAMOND, ChatColor.GOLD + "Hologram " + ChatColor.GREEN + "[ENABLED]");
                     
                        Hologram.put(p, true);
                        p.sendMessage("Enabled");
                    }
            }
             
            }
         
            @EventHandler
            public void onInventoryClick(InventoryClickEvent e) {
                    if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
                    if (e.getCurrentItem().getItemMeta() == null) return;
                    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Kill Message")) {
                            e.setCancelled(true);
                            e.getWhoClicked().openInventory(inv1);
                    }
                    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Kill Display")) {
                            e.setCancelled(true);
                            e.getWhoClicked().openInventory(inv2);
                    }
                 
            }
         
            public void onPlayerDeath(PlayerDeathEvent e) {
             
                Player p = e.getEntity();
             
                if(p.getKiller() instanceof Player) {
                 
                    String player = e.getEntity().getName();
                    String killer = p.getKiller().getName();
                 
                    if (Hologram.get(p.getName())) {
                 
                 
                    Location where = e.getEntity().getLocation().add(0, 2, 0);
                    Hologram hologram = HologramsAPI.createHologram((Plugin) this, where);
                    TextLine textLine = hologram.appendTextLine(killer + "has rekt" + player);
                 
                    long fiveMinutesMillis = 10 * 60 * 5000; // Ten minutes in milliseconds
                    long elapsedMillis = System.currentTimeMillis() - hologram.getCreationTimestamp(); // Milliseconds elapsed from the creation of the hologram

                    if (elapsedMillis > fiveMinutesMillis) {
                        hologram.delete();
                    }
                    }
                 
                }
             
            }
         
        }
     
  7. You may do this steps:
    • Check if the clicked inv is not null
    • Check if the current item is not null
    • Check if it has item meta using the 'hasItemMeta()' method.
    • Check if it has a displayName.
     
  8. You are checking if a HashMap which has a String as key has a Player in it. Change p to p.getName() or p.getUniqueID().toString().
     
  9. That just made it output the Enable over and over again.
     
  10. Ohw nvm I didn't saw that String was changed to Player uhhm...

    Why are you checking if the HashMap is containig the player and than if it is not containing the player...
    Try to change the else if statement to just else.
     
  11. Changing the else statement also just made it so when you first click it enabled an then continuously disabled. Oh yeah fair point, checking if it is not continuing the player wouldn't work at all.