[SOLVED] Problem with PlayerInteractEvent

Discussion in 'Skript' started by Lammendam, May 9, 2017.

Thread Status:
Not open for further replies.
  1. Have this code...
    Code (Text):
      @EventHandler
      public void itemDurability(PlayerInteractEvent e) {
       
      Player p = e.getPlayer();  
      ItemStack item = e.getItem();
      short durMax = item.getType().getMaxDurability();
      short durCur = item.getDurability();
      int sum = (durMax - 1) - durCur;
      int sumPercent = 100 - (99 * durCur / durMax);
      String durMsg = ChatColor.RED + "" + ChatColor.BOLD + "[!] " +
                        ChatColor.YELLOW + "Item durability: " +
                        ChatColor.DARK_GREEN + sumPercent + "%";

        if (sum == 15)
                    p.sendMessage(durMsg);
         
        else if (sum == 5)
                    p.sendMessage(durMsg);
     
      }
    It works well, but when i use an empty hand (hit anywhere, even in air), console throws me this error:
    Code (Text):
    [17:28:04] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to CubeScubaPlugin v0.2b
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at org.bukkit.craftbukkit.v1_11_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:232) [spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at org.bukkit.craftbukkit.v1_11_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:199) [spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at org.bukkit.craftbukkit.v1_11_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:195) [spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1382) [spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at net.minecraft.server.v1_11_R1.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:24) [spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at net.minecraft.server.v1_11_R1.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:1) [spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_131]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
        at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
        at ua.lammendam.cubescubaplugin.EListener.itemDurability(EListener.java:343) ~[?:?]
        at sun.reflect.GeneratedMethodAccessor34.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_131]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-b32c8f8-4d3bf20]
        ... 18 more
    What this mean and how I can solve this? Please help :oops:
     
    #1 Lammendam, May 9, 2017
    Last edited: May 9, 2017
  2. You need to do a null check, because the hand has no item/type.

    insert this at the beginning:
    if(item == null || item.getType() == null) return;

    you could also do p.hasItemInHand() for example...
     
    • Like Like x 1
  3. This is the Skript section... not the java section.
     
  4. Thank you very much :) Did not think of this.. but it was very simple.

    That's true, i'm sorry... I'll immidiately close this thread.
     
Thread Status:
Not open for further replies.