1.12.2 Error with number item swap

Discussion in 'Spigot Plugin Development' started by MateoxPL, May 14, 2020.

  1. Hello
    I have problem with my plugin
    so.This code works fine when player have item on cursor but when player use numeric (1-9 swap) event isn't canceling and console sent error:

    Code:
    Code (Text):
        @EventHandler
        public void OffHandInventoryClick(InventoryClickEvent e) {
            Player p = (Player)e.getWhoClicked();
            if(p.getGameMode() != GameMode.SURVIVAL) return;{  
                if(e.getCurrentItem() != null) {
                    if (e.getClickedInventory().getType() == InventoryType.PLAYER) {
                        if(e.getSlot() == 40) {
                            if(e.getCursor().getItemMeta().getDisplayName().contains(ChatColor.translateAlternateColorCodes('&', this.getConfig().getString("Block-Char")))) {
                                e.setCancelled(true);
                                p.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getConfig().getString("Deny-Message")));
                               
                            }
                           
                        }
                       
                    }
                   
                }
            }
        }
    Error:
    Code (Text):
    [13:01:48 ERROR]: Could not pass event InventoryClickEvent to OffHandBlock v1.1
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:72) ~[mSpigot.jar:git-Paper-1618]
            at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:78) ~[mSpigot.jar:git-Paper-1618]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[mSpigot.jar:git-Paper-1618]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:513) ~[mSpigot.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:2002) ~[?:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) ~[mSpigot.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) ~[mSpigot.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:14) ~[mSpigot.jar:git-Paper-1618]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_241]
            at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_241]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) ~[mSpigot.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:850) ~[mSpigot.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:423) ~[mSpigot.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:774) ~[mSpigot.jar:git-Paper-1618]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:666) ~[mSpigot.jar:git-Paper-1618]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.NullPointerException
            at offhand.block.Main.OffHandInventoryClick(Main.java:46) ~[?:?]
            at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor102.execute(Unknown Source) ~[?:?]
            at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:70) ~[mSpigot.jar:git-Paper-1618]
            ... 15 more
     
  2. The cursor can be null. Furthermore not every item has an item meta. There are a lot of potential null errors in your code.
    What is line 46?
     
  3. Which line is line 46?
    Edit: ninja'd
     
  4. @alphalawz
    This is the line
    Code (Text):
     if(e.getCursor().getItemMeta().getDisplayName().contains(ChatColor.translateAlternateColorCodes('&', this.getConfig().getString("Block-Char")))) {
     
  5. As wand555 said, you're likely getting a null pointer from either the cursor or item meta. Both getCursor() and getItemMeta() are nullable.
    getCursor()
    getItemMeta()
     
  6. About anything can be null there.
    Cursor can be null, ItemMeta can be null, String (displayname) can be null, Config#getString() can be null. Print out each statement to see what is null.
    If you want to check if a specific item is clicked, you can use ItemStack#isSimilar. Also my favourite (and most efficient I think) solution is, to do these checks with the clicked slot rather than the type. If you have a static GUI where players only click and don't put in their own items, you can sort everything out with just the clicked slot. So instead of checking if the itemstack is correct, I just check if the slot matches the slot I want
     
  7. Not every item has an item meta. The cursor may be null, but most likely its just an itemstack of air - which will return null with the item meta. Similarly, the displayname can also be null.
     
  8. Thanks guys for help
    This is solution
    Code (Text):
                                int hotbar = e.getHotbarButton();
                                if (p.getInventory().getItem(hotbar) != null) {
     
  9. Glad you got it working, but please rework your code with the suggestions. Your code will throw a NPE as soon as a player does something that's not directly linear to your code.