Solved Help need with PlayerInteractEvent

Discussion in 'Plugin Development' started by shadow5353, May 17, 2014.

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

    shadow5353

    Hello,

    When a player is right clicking on a item called hearthstone, they will have to wait 10 seconds to be teleported, but it will not work

    Code:
    Code:
    public class HeathstoneTel implements Listener{
       
        SettingsManager settings = SettingsManager.getInstance();
        Plugin plugin;
       
        ArrayList<Player> cooldown = new ArrayList<Player>();
        ArrayList<Player> countdown = new ArrayList<Player>();
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e){
            //Hearthstone Item
            ItemStack hearthstone = new ItemStack(Material.NETHER_STAR, 1);
            ItemMeta hearthstonemeta = hearthstone.getItemMeta();
            hearthstonemeta.setDisplayName(ChatColor.AQUA + "Hearthstone");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GRAY + "Talk with an inkeeper to set your home");
            lore.add(ChatColor.GRAY + "and use this hearthstone to teleport to your home location.");
            hearthstonemeta.setLore(lore);
            hearthstone.setItemMeta(hearthstonemeta);
           
            //Check for click
            if(!(e.getAction() == Action.RIGHT_CLICK_AIR) && !(e.getAction() == Action.RIGHT_CLICK_BLOCK))  return;
           
            //check for item
            if(!(e.getItem().equals(hearthstone))) return;
           
            final Player p = e.getPlayer();
            final String uuid = p.getUniqueId().toString();
           
            if(cooldown.contains(p)){
                mm.getInstance().error(p, "Your hearthstone is under cooldown!");
                return;
            }
            cooldown.add(p);
     
            mm.getInstance().good(p, "Teleporting in 10 seconds, do not move");
           
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
                public void run(){
                    World w = Bukkit.getServer().getWorld(settings.getData().getString(uuid + ".world"));
                    double x = settings.getData().getDouble(uuid + ".x");
                    double y = settings.getData().getDouble(uuid + ".y");
                    double z = settings.getData().getDouble(uuid + ".z");
                    p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 10, 0));
                    p.teleport(new Location(w, x, y, z));
                }
            }, 200);
            }
    }
    Error:
    Code:
    [12:56:13] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to Hearthstone v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at org.bukkit.craftbukkit.v1_7_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:216) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at org.bukkit.craftbukkit.v1_7_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:186) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:605) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInBlockPlace.a(SourceFile:60) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInBlockPlace.handle(SourceFile:9) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:203) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.validate(CraftScheduler.java:391) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:120) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.scheduleSyncRepeatingTask(CraftScheduler.java:116) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.scheduleSyncDelayedTask(CraftScheduler.java:100) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        at me.shadow5353.heartstone.events.HeathstoneTel.onPlayerInteract(HeathstoneTel.java:62) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_51]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_51]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_51]
        at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
        ... 15 more
    
     
  2. Offline

    DxDy

    That tells us that the problem is that your plugin-instance in your listener is null. Where does this happen?
    Code:java
    1. at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.[B] ---> scheduleSyncDelayedTask[/B](CraftScheduler.java:100) <----- ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-59-g6efeddf-b3074jnks]
    2. [B] ->>>> at me.shadow5353.heartstone.events.HeathstoneTel.onPlayerInteract(HeathstoneTel.java:62)[/B] <-------------~[?:?]
    3. at


    Hence you need to set the Plugin-Reference to your actual plugin first before you can use it, because the scheduler methods verify that the plugin-pointer is not null.
     
  3. Offline

    shadow5353

    DxDy After the 10 seconds it should teleport me to a location, but it do not teleport me.
     
  4. Offline

    DxDy

    Without intending any offense: Have you even read what I wrote?

    You need to make sure that your plugin-pointer is not null when you give it to the scheduler. E.g. you could add a constructor that thakes the plugin-pointer as parameter:

    Code:java
    1. private final Plugin plugin;
    2.  
    3. public HeathstoneTel(Plugin plugin){
    4. this.plugin = plugin;
    5. }
     
  5. Offline

    Azubuso

    shadow5353 Did you even read what DxDy just told you? Your "plugin" instance is null, and therefor cannot schedule the task to start your countdown. Pass an instance of your main class to your command class and set that "plugin" variable to the returned instance. (Just a precaution, I doubt your main class is called "Plugin", but if it is, make sure your import is correct as well.)
     
  6. Offline

    shadow5353

    DxDy Azubuso Sorry I must have miss read what DxDy wrote.

    Should I change Plugin plugin; to MainClass plugin; ?
     
  7. Offline

    DxDy

    With the plugin he is actually correct. He's coding against the Plugin-interface instead of his concrete class ;)

    Edit: Okay, my dear shadow, I literally posted the code that will fix your problem and I bet you you haven't even read it ...
     
  8. Offline

    AoH_Ruthless

    shadow5353
    That's not the issue. You haven't passed it through a constructor and initialized the constructor in your main class so the plugin is null.
     
  9. Offline

    Azubuso

    shadow5353 No worries, we all tend to do that. ;)

    Yep. But make sure to initialize it as well.
     
  10. Offline

    DxDy

    Nope. Why should he? With his current code he only needs the Plugin-Part. Why should he then artificially limit his code to instances of his MainClass?
     
  11. Offline

    AoH_Ruthless

    Azubuso
    That's false. What he has is fine. As DxDy said, Plugin plugin is better than passing an instance of the concrete class. I'm not sure if JavaPlugin plugin is any different than Plugin plugin, but I'm not sure.

    Edit: Damn it, ninja'd by DxDy
     
    DxDy likes this.
  12. Offline

    Azubuso

    DxDy EDIT: Woops. Oh well.
     
  13. Offline

    DxDy

    :D

    Btw, regarding your JavaPlugin part: Plugin is the interface. That defines a contract which all classes that deem themselves to be a plugin have to satisfy. JavaPlugin is a class that bukkit is so neat as to offer us, which implements the plugin-part. Since most bukkit plugins need to do exactly the same for most of the Plugin-Contract (methods like getDatabase(), getCommand(), ...) they have taken the liberty to make a class that will handle all that. By now extending that class (JavaPlugin), we don't need to have the hassle with reimplementing all that and can just override the methods that we want to change (most likely onEnable, onDisable, onCommand, ...)
     
  14. Offline

    shadow5353

    Azubuso DxDy I am so confused right now, but now I tried to put it into my main class and now it works. Thanks for the help :)
     
Thread Status:
Not open for further replies.

Share This Page