1.8.8 Inventory click and move item cancel HELP

Discussion in 'Spigot Plugin Development' started by DevelopmentHigh, May 13, 2020.

  1. Hello, i have error when i setup event what cancel's move item in inventory and click on them. I am so bad and i can't fix that. Can you maybe help me to solve the problem?


    Error in console:

    Could not pass event InventoryCreativeEvent to Cloth v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[patched.jar:git-PaperSpigot-"4c7641d"]
    at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:78) ~[patched.jar:git-PaperSpigot-"4c7641d"]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[patched.jar:git-PaperSpigot-"4c7641d"]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:517) [patched.jar:git-PaperSpigot-"4c7641d"]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:502) [patched.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1816) [patched.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.PacketPlayInSetCreativeSlot.a(SourceFile:23) [patched.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.PacketPlayInSetCreativeSlot.a(SourceFile:9) [patched.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [patched.jar:git-PaperSpigot-"4c7641d"]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_171]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_171]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [patched.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:774) [patched.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:378) [patched.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:713) [patched.jar:git-PaperSpigot-"4c7641d"]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:616) [patched.jar:git-PaperSpigot-"4c7641d"]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_171]
    Caused by: java.lang.NullPointerException
    at com.rpg97.cloth.ClothListener.onClickInventory(ClothListener.java:111) ~[?:?]
    at sun.reflect.GeneratedMethodAccessor289.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_171]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_171]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[patched.jar:git-PaperSpigot-"4c7641d"]
    ... 16 more

    Code
    :
    Code (Java):
    String a = "Cloth helmet";  

    public void onClickInventory(InventoryClickEvent event){
           
           
            if(event.getCurrentItem().getType() == Material.LEATHER_HELMET || event.getCurrentItem().getItemMeta().getDisplayName().equals(a)){
               
                event.setCancelled(true);
               
            }
           
        }
     
  2. Inventory click event runs every time an inventory is clicked, eaven if no slot has been clicked or the click has been outside the inventory
    If the click is outside the inventory then getInventory() will return null
    If no item has been clicked getCurrentItem will return null
    https://hub.spigotmc.org/javadocs/s...ory/InventoryClickEvent.html#getCurrentItem--
    There you can see it and read about this event
    Also before using getItemMeta you shuld use hasItemMeta since getItemMeta can return null
    And you shuld use hasDisplayName before using the getDisplayName
     
    #2 Nemo_64, May 13, 2020
    Last edited: May 13, 2020
  3. I suppose that line 111 is the first line in the click listener's method?
    If so, try removing || event.getCurrentItem().getItemMeta().getDisplayName().equals(a) and see if it works then. I'm guessing that the ItemMeta returns a null pointer.
     
  4. solved:

    Code (Java):
            Player player = (Player) event.getWhoClicked();
           
            if(!(event.getClickedInventory() == player.getInventory())){
               
                return;
            }
           
           
            if(event.getCurrentItem().getType() == null){
                return;
            }
           
            if(event.getClick() == ClickType.SHIFT_LEFT || event.getClick() == ClickType.SHIFT_RIGHT){
                if(event.getCurrentItem().getType() == Material.LEATHER_HELMET || event.getCurrentItem().getItemMeta().getDisplayName().equals(a)){
                    event.setCancelled(true);
                }
               
            }