1.12.2 Error at InventoryClickEvent

Discussion in 'Spigot Plugin Development' started by Bosternike, Nov 21, 2020.

Thread Status:
Not open for further replies.
  1. 1 ERROR. I have noticed an error which happens because of InventoryClickEvent when I throw away items from my inventory. Also I don't have InventoryCreativeEvent.

    2 ERROR. When I'm clicking out of gui Inventory, it errors to console.

    Here is 1 ERROR:
    ERROR]: Could not pass event InventoryCreativeEvent to BosterImperial v1.0.0
    org.bukkit.event.EventException: null
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:2047) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PacketPlayInSetCreativeSlot.a(SourceFile:23) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PacketPlayInSetCreativeSlot.a(SourceFile:9) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_261]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_261]
    at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_261]
    Caused by: java.lang.NullPointerException
    at Boster.main.Imperial.ClickEvents.clickEvent(ClickEvents.java:25) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_261]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_261]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_261]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_261]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    ... 15 more


    Here is 2 ERROR:
    ERROR]: Could not pass event InventoryClickEvent to BosterImperial v1.0.0
    org.bukkit.event.EventException: null
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1891) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_261]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_261]
    at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_261]
    Caused by: java.lang.NullPointerException
    at Boster.main.Imperial.ClickEvents.clickEvent(ClickEvents.java:25) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_261]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_261]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_261]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_261]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
    ... 15 more
     
  2. Where is your code?

    You have to do a null check for the inventory.
    Code (Text):
    getClickedInventory
    @Nullable
    public Inventory getClickedInventory()
    Gets the inventory corresponding to the clicked slot.
    Returns:
    inventory, or null if clicked outside
    See Also:
    InventoryView.getInventory(int)
    This can return null too:
    Code (Text):
    getCursor
    @Nullable
    public ItemStack getCursor()
    Gets the current ItemStack on the cursor.
    Returns:
    the cursor ItemStack


    getCurrentItem
    @Nullable
    public ItemStack getCurrentItem()
    Gets the ItemStack currently in the clicked slot.
    Returns:
    the item in the clicked
     
  3. I make some checks but it still send me errors.

    if (e.getClickedInventory() == null) {
    return;
    }
    if(e.getInventory() == null) {
    return;
    }
    if (e.getCursor() == null) {
    return;
    }
    if(e.getCurrentItem() == null) {
    return;
    }
     
  4. Can you link your code that is causing the error so we can review it.
     
  5. Code (Text):

    @EventHandler
        public void clickEvent(InventoryClickEvent e) {
           
            Player player = (Player) e.getWhoClicked();
            ItemStack clicked = e.getCurrentItem();
           
            if(e.getClickedInventory().getTitle() == null) return;
            if(!e.getClickedInventory().getTitle().equalsIgnoreCase(plugin.getConfig().getString("classic_world.shop.title").replace("&", "\u00a7"))) return;
            String classic_world = plugin.getConfig().getString("classic_world.world_name");
            if(player.getWorld().getName().equals(classic_world)) {
                if(e.getClickedInventory().getTitle().equalsIgnoreCase(plugin.getConfig().getString("classic_world.shop.title").replace("&", "\u00a7"))) {
                    if (e.getClickedInventory() == null) {
                        return;
                    }
                    if(e.getInventory() == null) {
                        return;
                    }
                    if (e.getCursor() == null) {
                        return;
                    }
                    if(e.getCurrentItem() == null) {
                         return;
                    }
                   
                    if(clicked.getType().equals(Material.BREAD)) {
                        int breadpr = plugin.getConfig().getInt("classic_world.shop.bread.price");
                        ItemStack ironprice = new ItemStack(Material.IRON_INGOT, breadpr);
                        if(!player.getInventory().contains(Material.IRON_INGOT, breadpr)) {
                            String s = plugin.getConfig().getString("classic_world.shop.messages.not_enought_resource");
                            s = s.replace("{prefix}", plugin.getConfig().getString("settings.prefix"));
                            s = s.replace("{resource}", plugin.getConfig().getString("classic_world.shop.price_items.iron"));
                            s = s.replace("&", "\u00a7");
                            player.sendMessage(s);
                            if(plugin.getConfig().getString("settings.if_not_enough_resource-close").contains("true")) {
                                player.closeInventory();
                            }
                            e.setCancelled(true);
                            return;
                        }
                       
                        String s = plugin.getConfig().getString("classic_world.shop.messages.bought");
                        s = s.replace("{prefix}", plugin.getConfig().getString("settings.prefix"));
                        s = s.replace("{item}", plugin.getConfig().getString("classic_world.shop.item_names.bread"));
                        s = s.replace("&", "\u00a7");
                        //ITEMS
                        /*BREAD*/
                        int breadam = plugin.getConfig().getInt("classic_world.shop.bread.amount");
                        ItemStack bread = new ItemStack(Material.BREAD, breadam);
                        String sbread = plugin.getConfig().getString("classic_world.shop.item_names.bread");
                        sbread = sbread.replace("&", "\u00a7");
                        ItemMeta bread_meta = bread.getItemMeta();
                        bread_meta.setDisplayName(sbread);
                        bread.setItemMeta(bread_meta);
                        /*BREAD*/
                        //ITEMS
                       
                       
                        player.getInventory().removeItem(ironprice);
                        player.getInventory().addItem(bread);
                        player.sendMessage(s);
                        e.setCancelled(true);
                        return;
                    }
                   
                    if(clicked.getType().equals(Material.ARROW)) {
                        int arrowpr = plugin.getConfig().getInt("classic_world.shop.arrow.price");
                        ItemStack goldprice = new ItemStack(Material.GOLD_INGOT, arrowpr);
                        if(!player.getInventory().contains(Material.GOLD_INGOT, arrowpr)) {
                            String s = plugin.getConfig().getString("classic_world.shop.messages.not_enought_resource");
                            s = s.replace("{prefix}", plugin.getConfig().getString("settings.prefix"));
                            s = s.replace("{resource}", plugin.getConfig().getString("classic_world.shop.price_items.gold"));
                            s = s.replace("&", "\u00a7");
                            player.sendMessage(s);
                            if(plugin.getConfig().getString("settings.if_not_enough_resource-close").contains("true")) {
                                player.closeInventory();
                            }
                            e.setCancelled(true);
                            return;
                        }
                       
                        String s = plugin.getConfig().getString("classic_world.shop.messages.bought");
                        s = s.replace("{prefix}", plugin.getConfig().getString("settings.prefix"));
                        s = s.replace("{item}", plugin.getConfig().getString("classic_world.shop.item_names.arrow"));
                        s = s.replace("&", "\u00a7");
                        //ITEMS
                        /*BREAD*/
                        int arrowam = plugin.getConfig().getInt("classic_world.shop.arrow.amount");
                        ItemStack item = new ItemStack(Material.ARROW, arrowam);
                        String sitem = plugin.getConfig().getString("classic_world.shop.item_names.arrow");
                        sitem = sitem.replace("&", "\u00a7");
                        ItemMeta item_meta = item.getItemMeta();
                        item_meta.setDisplayName(sitem);
                        item.setItemMeta(item_meta);
                        /*BREAD*/
                        //ITEMS
                       
                       
                        player.getInventory().removeItem(goldprice);
                        player.getInventory().addItem(item);
                        player.sendMessage(s);
                        e.setCancelled(true);
                        return;
                    }
                    e.setCancelled(true);
                }
            }
        }
    }
     
     
  6. I posted my code above.
     
  7. ah yes it totally makes sense to check if something is null after you use it.
    Move your clickedInventory == null check to the very top as that's where it should be (not saying this will fix your code or errors entirely as that's just a mess imo but it's a start).
     
    • Agree Agree x 1
  8. Well you are checking if the title of the inventory is null and then trying to compare the title before checking if the inventory itself is null.
     
  9. Thanks, It's working successful!
     
Thread Status:
Not open for further replies.