Asyncronous Tasks...

Discussion in 'Plugin Development' started by Michael Rhodes, Apr 19, 2013.

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

    Michael Rhodes

    Hey everyone,

    I am currently working on my plugin and I want to run some code every few seconds or so. The code I am writing is a Mob / Animal manager that is responsable for spawning and setting up levels of mobs.

    But, I see in the javadocs for async code that I should not use any Bukkit calls.

    Can anyone tell me if there'd be a problem using these calls to spawn mobs if I prevent the default spawning from happening ?

    Thanks,

    Michael
     
  2. Offline

    evilmidget38

    Michael Rhodes Spawning mobs, like almost all other Bukkit calls, is not thread safe and cannot be safely done asynchronously.
     
  3. Offline

    nitrousspark

    oh hey michael :p

    Code:
    public class Timer extends BukkitRunnable
    {
     
    private booleanactive;
     
    private Coreplugin;
     
    public Timer(Core plugin, long initialDelay, long delayBetweenRuns)
    {
     
    active = false;
    this.plugin = plugin; // For further use
    this.runTaskTimer(plugin, initialDelay, delayBetweenRuns);
    }
     
    public Timer()
    {
     
    }
     
    @Override
    public void run()
    {
     
    if(active)
    {
    //spawn the mobs here
    }
    }
     
    public void enable()
    {
     
    this.active = true;
    }
     
    public void disable()
    {
     
    this.active = false;
    }
     
    public void stop()
    {
     
    Bukkit.getScheduler().cancelTask(this.getTaskId());
    }
    }
     
  4. Offline

    evilmidget38

    nitrousspark Why are you defining a no args constructor, and doesn't BukkitRunnable already have a method to cancel itself?
     
Thread Status:
Not open for further replies.

Share This Page