InventoryClickEvent help

Discussion in 'Plugin Development' started by ItsMas_, Jan 23, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    ItsMas_

    When I click on an item in one of my menus in my plugin, this error appears:
    Code:
    [10:34:55 ERROR]: Could not pass event InventoryClickEvent to StaffMenu v1.1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1564) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_79]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_79]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit.jar:git-Bukkit-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.7.0_79]
    Caused by: java.lang.NullPointerException
            at me.mas.staffmenu.StaffMenuPunish.onInventoryClick(StaffMenuPunish.java:74) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_79]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_79]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_79]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            ... 15 more
    [10:35:42 INFO]: ItsMas_ issued server command: /pl
    [10:35:45 INFO]: ItsMas_ issued server command: /smp ItsMas_ ItsMas_
    [10:35:47 ERROR]: Could not pass event InventoryClickEvent to StaffMenu v1.1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1564) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_79]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_79]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit.jar:git-Bukkit-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit.jar:git-Bukkit-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.7.0_79]
    Caused by: java.lang.NullPointerException
            at me.mas.staffmenu.StaffMenuPunish.onInventoryClick(StaffMenuPunish.java:74) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_79]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_79]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_79]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit.jar:git-Bukkit-18fbb24]
            ... 15 more
    
    Here is the code:
    Code:
    package me.mas.staffmenu;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class StaffMenuPunish implements CommandExecutor, Listener {
    
       
        String finalCommand;
        Player target;
        String reason;
       
        public static Inventory punishMenu = Bukkit.createInventory(null, 27, ChatColor.RED + "Punish Player");
       
        public static void createItem(Material material, int slot, String name) {
            ItemStack item = new ItemStack(material);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(name);
            item.setItemMeta(meta);
           
            punishMenu.setItem(slot, item);   
        }
       
        static {
        createItem(Material.SIGN, 11, ChatColor.RED + "Kick Player");
        createItem(Material.IRON_SWORD, 15, ChatColor.RED + "Ban Player");
        }
       
        @SuppressWarnings("deprecation")
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){       
            if (cmd.getName().equalsIgnoreCase("smp")){
                if (!sender.hasPermission("mas.staffmenu.punish")){
                    sender.sendMessage("§e[§c!§e] §cYou may not use the punish menu");
                }else{
                    if (args.length <= 1){
                        sender.sendMessage("§e[§c!§e] §cUse " + ChatColor.GOLD + "/smp <player> <reason>");
                    }else{
                        target = Bukkit.getServer().getPlayer(args[0]);
                        for (int i = 1; i <args.length; i++){
                                reason += args[i] + " ";
                        }
                            reason = reason.trim();
                            Player player = (Player) sender;
                            player.openInventory(punishMenu);
                        }
                    }
                }
            return true;
        }
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent x){
            Player clicker = (Player) x.getWhoClicked();
            ItemStack clicked = x.getCurrentItem();
            Inventory inventory = x.getInventory();
            if (inventory.getName().equals(punishMenu.getName())){
                x.setCancelled(true);
                clicker.closeInventory();
                if (clicked.getType() == null){
                    return;
                }else if (clicked.getType() == Material.SIGN){
                    finalCommand = "/kick " + target.getName() + " " + reason;
                    clicker.chat(finalCommand);
                    finalCommand = "";
                }else if (clicked.getType() == Material.IRON_SWORD){
                    finalCommand = "/ban " + target.getName() + " " + reason;
                    clicker.chat(finalCommand);
                    finalCommand = "";
                }
            }
        }
    }
    
    Could someone tell me how to fix this? Thanks
     
  2. Offline

    SnelleFrikandel

    The error is at } else if (clicked.getType() == Material.SIGN){
    I believe it has something to do with Material.SIGN, Material.SIGN is a placed sign. and not an Item.
     
  3. Offline

    ItsMas_

    Tried it with MATERIAL>SIGN_POST, still doesn't work. Anything else the error could have been caused by?
     
  4. Offline

    Caedus

    You could perhaps try Material.SIGN_POST
     
  5. Offline

    SnelleFrikandel

    @ItsMas_ You are checking what item it is, That doesnt work for you. What you could try is if(clicked.getItemMeta().getDisplayName() == "Your item name") { That works to :)
     
  6. Offline

    HenrySartori

    Yes, this is the best way to do this. Also, he could use many events with the same item w/o any problem =D
     
    Last edited: Jan 23, 2016
  7. Offline

    ShowbizLocket61

    @ItsMas_ @SnelleFrikandel @Caedus @HenrySartori
    Ugh... you guys! What are you even saying?
    The stack trace shows that it's a NullPointerException on this line:
    Code:
    }elseif(clicked.getType()== Material.SIGN){
    You can't have a NPE on a class... so it must be your clicked variable! It's null! However, it doesn't throw an exception a few lines up there... so you have something strange going on. Are you doing anything to your clicked item? Removing it perhaps? That would make it null.

    Plus, this is directed to @SnelleFrikandel @Caedus @HenrySartori
    getType returns a primitive Material object. Comparing it to Material.SIGN is the best way. Not comparing the display name, as entirely different objects could have same display names. Additionally, that puts you at risk of NPEs when the item either does not have an item meta, or no custom display name.

    In the future, when answering questions like these, please read the question more carefully, especially the stack traces... I beg you not to just "intuitively" suggest a solution. Thank you.
     
  8. Offline

    Zombie_Striker

    Wrong. WALL_SIGN and SIGN_POST are the blocks.


    To add on to what @ShowbizLocket61 said, you need to take this line
    And remove .getType() . Enums are not normally null, but itemstacks can be.
     
    ShowbizLocket61 likes this.
  9. Offline

    ShowbizLocket61

    @Zombie_Striker
    Do you see any reason why it would not throw a NPE at clicked.getType() in that line, but does the next time it appears?
     
  10. Offline

    Zombie_Striker

    @ShowbizLocket61
    I don't really, except that maybe that isn't the line that is causing the error, since the first line is not the package name. He may have added a new line/ taken one away by accident.
     
  11. Offline

    ShowbizLocket61

    @Zombie_Striker
    Ayyyy you are totally correct!

    @ItsMas_
    It's this bugger:
    Code:
    finalCommand ="/kick "+ target.getName()+" "+ reason;
    You don't have target instantiated. It's null, and throwing you a NPE when you call getName on it. Are you sure you didn't mean clicker?
     
Thread Status:
Not open for further replies.

Share This Page