PlayerInteractEvent error

Discussion in 'Plugin Development' started by AlmostHelpful, Nov 5, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    AlmostHelpful

    Hello, earlier today I (tried) making a plugin with claimables. I could get the claimables to my inventory without a problem but I can't seem to figure out how to make the claimables claimable. I've tried using this code in my Events.class. I've also tried specifying what item these "claimables" are but then it hasn't even showed up with an error. Also if you find anything that could have been done better please point that out since I'm new to this and still learning.

    Thanks in advance
    ~AlmostHelpful

    The Class (open)

    Code:
    package me.almosthelpful.surgeclaimables;
    
    import org.bukkit.Bukkit;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class Events 
       implements Listener
    {
        public Events()
        {
            Bukkit.getPluginManager().registerEvents(this, Main.getInstance());
        }
       
    @SuppressWarnings("null")
    @EventHandler
      public void PlayerInteractEvent(PlayerInteractEvent e)
      {
        ItemStack is = e.getPlayer().getItemInHand();
        if ((is.hasItemMeta()) && (is.getItemMeta().hasDisplayName()) && (is.getItemMeta().getDisplayName().equals("§6§lRANK '§fSpartan§6§l'")) && (is.getItemMeta().hasEnchant(Enchantment.DURABILITY)))
        {
            PlayerInteractEvent event = null;
            if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
            {
                if (e.getPlayer().hasPermission("surgeclaimables.usespartan"))
                {
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "manuaddsub " + e.getPlayer() + " spartan");
                    e.getPlayer().sendMessage("§6§lSurge §8// §7You have claimed the §8Spartan §7Rank!");
                   e.getPlayer().getInventory().setItemInHand(null);
                   e.getPlayer().updateInventory();
                }
                else
                {
                    e.getPlayer().sendMessage("§6§lSurge §8// §cYou already have Spartan or a higher rank!");
                }
            }
        }
        if ((is.hasItemMeta()) && (is.getItemMeta().hasDisplayName()) && (is.getItemMeta().getDisplayName().equals("§6§lRANK '§fLegend§6§l'")) && (is.getItemMeta().hasEnchant(Enchantment.DURABILITY)))
        {
            PlayerInteractEvent event = null;
            if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
            {
                if (e.getPlayer().hasPermission("surgeclaimables.uselegend"))
                {
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "manuaddsub " + e.getPlayer() + " legend");
                    e.getPlayer().sendMessage("§6§lSurge §8// §7You have claimed the §8Legend §7Rank!");
                   e.getPlayer().getInventory().setItemInHand(null);
                   e.getPlayer().updateInventory();
                }
                else
                {
                    e.getPlayer().sendMessage("§6§lSurge §8// §cYou already have Legend or a higher rank!");
                }
            }
        }
        if ((is.hasItemMeta()) && (is.getItemMeta().hasDisplayName()) && (is.getItemMeta().getDisplayName().equals("§6§lRANK '§fPegasus§6§l'")) && (is.getItemMeta().hasEnchant(Enchantment.DURABILITY)))
        {
            PlayerInteractEvent event = null;
            if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
            {
                if (e.getPlayer().hasPermission("surgeclaimables.usepegasus"))
                {
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "manuaddsub " + e.getPlayer() + " pegasus");
                    e.getPlayer().sendMessage("§6§lSurge §8// §7You have claimed the §8Pegasus §7Rank!");
                   e.getPlayer().getInventory().setItemInHand(null);
                   e.getPlayer().updateInventory();
                }
                else
                {
                    e.getPlayer().sendMessage("§6§lSurge §8// §cYou already have Pegasus or a higher rank!");
                }
            }
        }
        if ((is.hasItemMeta()) && (is.getItemMeta().hasDisplayName()) && (is.getItemMeta().getDisplayName().equals("§6§lRANK '§fKraken§6§l'")) && (is.getItemMeta().hasEnchant(Enchantment.DURABILITY)))
        {
            PlayerInteractEvent event = null;
            if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
            {
                if (e.getPlayer().hasPermission("surgeclaimables.usekraken"))
                {
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "manuaddsub " + e.getPlayer() + " kraken");
                    e.getPlayer().sendMessage("§6§lSurge §8// §7You have claimed the §8Kraken §7Rank!");
                   e.getPlayer().getInventory().setItemInHand(null);
                   e.getPlayer().updateInventory();
                }
                else
                {
                    e.getPlayer().sendMessage("§6§lSurge §8// §cYou already have Kraken or a higher rank!");
                }
            }
        }
        if ((is.hasItemMeta()) && (is.getItemMeta().hasDisplayName()) && (is.getItemMeta().getDisplayName().equals("§6§lRANK '§fPoseidon§6§l'")) && (is.getItemMeta().hasEnchant(Enchantment.DURABILITY)))
        {
            PlayerInteractEvent event = null;
            if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
            {
                if (e.getPlayer().hasPermission("surgeclaimables.useposeidon"))
                {
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "manuaddsub " + e.getPlayer() + " poseidon");
                    e.getPlayer().sendMessage("§6§lSurge §8// §7You have claimed the §8Poseidon §7Rank!");
                   e.getPlayer().getInventory().setItemInHand(null);
                   e.getPlayer().updateInventory();
                }
                else
                {
                    e.getPlayer().sendMessage("§6§lSurge §8// §cYou already have Poseidon or a higher rank!");
                }
            }
        }
        if ((is.hasItemMeta()) && (is.getItemMeta().hasDisplayName()) && (is.getItemMeta().getDisplayName().equals("§6§lRANK '§fZeus§6§l'")) && (is.getItemMeta().hasEnchant(Enchantment.DURABILITY)))
        {
            PlayerInteractEvent event = null;
            if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
            {
                if (e.getPlayer().hasPermission("surgeclaimables.usezeus"))
                {
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "manuaddsub " + e.getPlayer() + " zeus");
                    e.getPlayer().sendMessage("§6§lSurge §8// §7You have claimed the §8Zeus §7Rank!");
                   e.getPlayer().getInventory().setItemInHand(null);
                   e.getPlayer().updateInventory();
                }
                else
                {
                    e.getPlayer().sendMessage("§6§lSurge §8// §cYou already have Zeus!");
                }
            }
        }
      }
    
    }
    
    

    Error Code (open)

    Code:
    [21:25:24] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to SurgeClaimables v1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[PaperSpigot.jar:git-PaperSpigot-731e35d]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[PaperSpigot.jar:git-PaperSpigot-731e35d]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:509) [PaperSpigot.jar:git-PaperSpigot-731e35d]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:494) [PaperSpigot.jar:git-PaperSpigot-731e35d]
        at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:242) [PaperSpigot.jar:git-PaperSpigot-731e35d]
        at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:212) [PaperSpigot.jar:git-PaperSpigot-731e35d]
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:639) [PaperSpigot.jar:git-PaperSpigot-731e35d]
        at net.minecraft.server.v1_7_R4.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:50) [PaperSpigot.jar:git-PaperSpigot-731e35d]
        at net.minecraft.server.v1_7_R4.PacketPlayInBlockPlace.handle(PacketPlayInBlockPlace.java:86) [PaperSpigot.jar:git-PaperSpigot-731e35d]
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:189) [PaperSpigot.jar:git-PaperSpigot-731e35d]
        at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [PaperSpigot.jar:git-PaperSpigot-731e35d]
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:790) [PaperSpigot.jar:git-PaperSpigot-731e35d]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:307) [PaperSpigot.jar:git-PaperSpigot-731e35d]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:638) [PaperSpigot.jar:git-PaperSpigot-731e35d]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:544) [PaperSpigot.jar:git-PaperSpigot-731e35d]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [PaperSpigot.jar:git-PaperSpigot-731e35d]
    Caused by: java.lang.NullPointerException
        at me.almosthelpful.surgeclaimables.Events.PlayerInteractEvent(Events.java:81) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_60]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_60]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_60]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_60]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:298) ~[PaperSpigot.jar:git-PaperSpigot-731e35d]
        ... 15 more
    
     
  2. Offline

    Zombie_Striker

    Code:
    Caused by: java.lang.NullPointerException
        at me.almosthelpful.surgeclaimables.Events.PlayerInteractEvent(Events.java:81) ~[?:?]
    .......
        PlayerInteractEvent event = null;
    Nope, You cant do that. You just cant.
     
  3. Offline

    AlmostHelpful

    Can you tell me why and how to fix it though?
     
  4. Offline

    Zombie_Striker

  5. Offline

    AlmostHelpful

    @Zombie_Striker That messes up this if I remove that. Which I've already tried too:
    if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
     
  6. Offline

    Zombie_Striker

    @AlmostHelpful
    Maybe it's because the event in the parameter (which is player interact event) is called "e", while you're creating a variable that is called "Event", which is null. Also, That if statement is checking if Null.getAction is RIGHTCLICK, which if you know anything about Java will throw an NPE. So, what you need to do is either:
    • Remove "event" that equals null
    • Set "event" equal to "e" (the actual event)
     
  7. Offline

    87pen

  8. Offline

    sebagius7110

    Ok, so your fix is remove 'PlayerInteractEvent event = null;' And wherever you have used 'event' replace it with 'e'
     
Thread Status:
Not open for further replies.

Share This Page