I can't seem to get delayed tasks working.

Discussion in 'Plugin Development' started by Caedus, Mar 22, 2016.

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

    Caedus

    Hi,

    I've been looking at different threads and tutorials for hours now and I can't seem to get a decent understanding on delayed tasks. What screws me over is the "this" portion of the delayed task, I can't seem to get it to work in game when I cast it as a plugin (the option that Eclipse allows) and when I try other methods that I have found online, Eclipse tells me they won't work. Perhaps someone can offer some advice, quite possibly the problem stems from my ignorance of the "this" concept.

    The following code is the basics, I've tried adding and removing different things in an attempt to get it to work.

    Code:
    package me.Caedus;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileLaunchEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class ProjLaunchListener implements Listener {
      
        @EventHandler
        public void onProjectileLaunch(ProjectileLaunchEvent e){
      
        if(e.getEntity() instanceof Arrow){
          
            Arrow a = (Arrow) e.getEntity();
          
            if(a.getShooter() instanceof Player){
                Player p = (Player) a.getShooter();
              
                p.sendMessage("You'll receive a new arrow in 5 seconds...");
              
                new BukkitRunnable() {
                  
                    @Override
                    public void run() {
                      
                        p.getInventory().addItem(new ItemStack(Material.ARROW, 1));
                      
                    }
                  
                }.runTaskLater(this, 5*20);
            }
              
            }  
        }
    }
    
    The "this" on Line 37 is what gets me.

    Cheers
     
  2. Offline

    Gonmarte

    You need to use the BukkitScheduler.
    Instead of giving you some code how to actually do it, i will give you this website - http://wiki.bukkit.org/Scheduler_Programming - whitch cointains everything that you need to know about delayed tasks, if have any doubt tag me back!
     
  3. Offline

    mine-care

    Calling this method starts the task. The method requires 2 parameters. A Plugin, and a long
    The Plugin object represents the plugin that runs the task, and the long is the time delay.
    Using the keyword 'this' you are trying to give the method your current class as a "Plugin" but it isn't. What you need to do, is pass the instance of your main class, which extends JavaPlugin, and therefore is a Plugin.
     
  4. Offline

    Caedus

    I've tried adding
    Code:
    final OITQ instance = this;
    in both my main class (OITQ) and ProjLaunchListener, neither seem to work. So i'm at loss, perhaps you can show me exactly what you mean? Cheers.
     
  5. Offline

    Lordloss

    Please read the link provided by @Gonmarte
     
    Gonmarte likes this.
Thread Status:
Not open for further replies.

Share This Page