InventoryClickEvent not working with Config

Discussion in 'Spigot Plugin Development' started by ifinchyy, Jun 5, 2017.

  1. I'm trying to add to my config through an inventory click event. There's a GUI with 2 options "Hero" and "Villain" and I'm wanting it to send the information to a config file. Although, it simply throws an error.

    Code (Text):
        @EventHandler
        private static void mainGUII(InventoryClickEvent event) {
            player = (Player) event.getWhoClicked();
            ItemStack clicked = event.getCurrentItem();
            Inventory inventory = event.getInventory();
            if (inventory.getName().equals(alignmentGUI.getName())) {
                if (clicked.getType() == Material.BEDROCK) {
                    event.setCancelled(true);
                }
                if (clicked.getType() == Material.GOLD_SWORD) {
                    event.setCancelled(true);
                    plugin.getPlayerData().set("Player." + player.getName() + ".Alignment", "Hero");
                    player.closeInventory();
                    openRaceGUI(player);
                }
                if (clicked.getType() == Material.SKULL_ITEM) {
                    event.setCancelled(true);
                    plugin.getPlayerData().set("Player." + player.getName() + ".Alignment", "Hero");
                    player.closeInventory();
                    openRaceGUI(player);
                }
            }
        }
    it throws this error:
    Code (Text):
    [12:25:38] [Server thread/ERROR]: Could not pass event InventoryClickEvent to testplugin
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:338) ~[JavaPluginLoader$1.class:Bukkit-1.7.10]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[RegisteredListener.class:Bukkit-1.7.10]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:507) [SimplePluginManager.class:Bukkit-1.7.10]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:492) [SimplePluginManager.class:Bukkit-1.7.10]
        at net.minecraft.network.NetHandlerPlayServer.func_147351_a(NetHandlerPlayServer.java:2015) [nh.class:?]
        at net.minecraft.network.play.client.C0EPacketClickWindow.func_148833_a(SourceFile:32) [ix.class:?]
        at net.minecraft.network.play.client.C0EPacketClickWindow.func_148833_a(SourceFile:10) [ix.class:?]
        at net.minecraft.network.NetworkManager.func_74428_b(NetworkManager.java:244) [ej.class:?]
        at net.minecraft.network.NetworkSystem.func_151269_c(NetworkSystem.java:173) [nc.class:?]
        at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:980) [MinecraftServer.class:?]
        at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:430) [lt.class:?]
        at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:798) [MinecraftServer.class:?]
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:658) [MinecraftServer.class:?]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    Caused by: java.lang.NullPointerException
        at yy.iFinchyy.GUI.mainGUI.mainGUII(mainGUI.java:89) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
        at java.lang.reflect.Method.invoke(Unknown Source)
    edit: line 89 refers to 'plugin.getPlayerData().set("Player." + player.getName() + ".Alignment", "Hero");'. This is a config file and it won't allow me to add anything to said config file through the gui. What I'm wanting it to do is click the golden sword and align the player as a "hero".
     
    #1 ifinchyy, Jun 5, 2017
    Last edited: Jun 6, 2017
  2. The Error says that there is an NullPointerException in your "mainGUI" class at line 89, what is in line 89?
     
  3. Your error is a NullPointerException on line 89 in the mainGUII method of your class mainGUI.

    Since I do not know which line that is exactly I have to guess a bit. You should definitely add null checks for these two:
    Code (Java):
    ItemStack clicked = event.getCurrentItem();
    Inventory inventory = event.getInventory();
    If either clicked or inventory is null, return. The player could for example be clicking on an empty slot in your inventory or even next to the inventory (which still fires an event but the clicked inventory is null).

    Apart from that please consider starting to follow some basic naming conventions for Java. It makes reading your code much easier for yourself and for others. For starters: Classes are CamelCase => MainGUI and methods have descriptive names mainGUII -> onMainGuiClick()
    Just a suggestion...
     
  4. Sorry, I tried to edit it right after I had posted but it wouldn't let me. I have edited it now explaining what line is wrong.
     
  5. Andre_601

    Supporter

    there should be also if(event.getSlot() = inventory.getRawSlots()) to check, if it really is the custom inv and not the player inv.
    (I'm sorry, if this code is not 100% correct)