Need help delaying task.

Discussion in 'Plugin Development' started by MrDent009, Jul 23, 2012.

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

    MrDent009

    Delaying a task![HELP]

    What I really need help with?

    I need help figuring out how to delay a task. I have googled stuff, but I still don't understand how I would do this, I don't understand if you have to register it as an event or what.

    Where do I put it?

    Do I follow it up on the onEnable() method?

    Do I put it into a listener class?

    I have been confused about this for a while, even when people tried to help (messaging). I still couldn't understand this.

    Makes me feel dumb in the head [skeleton]

    Thanks in advance!

    - MrDent009
     
  2. Offline

    Njol

    You can create a simple task that is run once in the future like this:
    Code:
    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
      public void run() {
        // your code here
      }
    }, <delay in ticks>);
    
    'plugin' is a variable that holds a reference to your plugin, or 'this' if this code is in your plugin's main class.
    Replace '<delay in ticks>' with the amount of ticks that should pass until the task is executed, e.g. 20 for one second.

    You can put this anywhere in your plugin as long as you have a variable 'plugin' as stated above.
     
    Limeth likes this.
  3. Offline

    MrDent009

    So, is this correct?

    Code:Java
    1. @EventHandler(priority = EventPriority.HIGH)
    2. public void onPlayerRespawn(PlayerRespawnEvent event) {
    3. Bukkit.getScheduler().scheduleSyncDelayedTask(Recovery, new Runnable() {
    4. public void run() {
    5. event.getPlayer();
    6.  
    7. ((Player)event.getPlayer()).addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 200, 1), true);
    8. ((Player)event.getPlayer()).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 200, 1), true);
    9. ((Player)event.getPlayer()).addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 200, 1), true);
    10. Bukkit.broadcastMessage(ChatColor.AQUA + "[" + ChatColor.YELLOW + "Recovery" + ChatColor.AQUA + "]" + ChatColor.GOLD + " Someone is on the path of Recovery");
    11. }
    12. },1L
    13. }
    14. }
     
  4. Offline

    Njol

    Depends on where you define 'Recovery' - it doesn't look like a variable.

    You also seem to have missed a ');' after '1L'

    And the event has to be final for it to be used in the task:
    Code:
    public void onPlayerRespawn(final PlayerRespawnEvent event) {
    BTW: why do you cast Player to Player?

    PS: are you even using an IDE (e.g. Eclipse)?
     
  5. Offline

    MrDent009

    Now I get an error on a players death.

    Code:
    01:56:57 [SEVERE] Could not pass event PlayerRespawnEvent to Recovery
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:303)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:459)
            at net.minecraft.server.ServerConfigurationManager.moveToWorld(ServerCon
    figurationManager.java:289)
            at net.minecraft.server.ServerConfigurationManager.moveToWorld(ServerCon
    figurationManager.java:248)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:1035)
            at net.minecraft.server.Packet9Respawn.handle(SourceFile:28)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
            at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:7
    8)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:558)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:450)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
            at org.bukkit.craftbukkit.scheduler.CraftScheduler.scheduleSyncRepeating
    Task(CraftScheduler.java:189)
            at org.bukkit.craftbukkit.scheduler.CraftScheduler.scheduleSyncDelayedTa
    sk(CraftScheduler.java:180)
            at me.Dent009.Recovery.Recovery.onPlayerRespawn(Recovery.java:37)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:301)
            ... 12 more
     
  6. Offline

    r0306

    MrDent009
    If that event is in your main class, then you can use 'this' in place of 'Recovery.' Otherwise, if it's in a different class, you have to make a constructor that references to the main class.
     
  7. Offline

    Njol

    You have to have a reference to your plugin's running instance, e.g. like this (in your plugin's main class):
    Code:
    private static Recovery instance;
    public static Recovery getInstance() {
      return instance;
    }
     
    public void onEnable() {
      instance = this;
      // rest of onEnable code here
    }
     
    public void onDisable() {
      // onDisable code here
      instance = null; // prevents memory leaks
    }
    You can then use it like
    Code:
    Bukkit.getScheduler().scheduleSyncDelayedTask(Recover.getInstance(), new Runnable() {
     
  8. Offline

    MrDent009

    Njol
    I finally get it now!
    Thank you bunches!
    I mean really, THANKS!!! :)
     
Thread Status:
Not open for further replies.

Share This Page