ASyncRepeatingTasks...Seem to not repeat?

Discussion in 'Plugin Development' started by DocRedstone, May 8, 2012.

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

    DocRedstone

    So basically I want a task to repeat indefinately. I have been able to accomplish this, the issue seems to be that ASyncRepeatingTask needs it's delay in long and I am providing it as an int. Therefore when the int is converted to long, it gives me a long, long delay that will never be executed. That is what I have gotten from trouble shooting.

    CODE:
    Show Spoiler

    Code:
    [LIST=1]
    [*]package me.docredstone.battleroyal;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Random;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Main extends JavaPlugin{
    public boolean containsneg = true;
    public int x = 0;
    public int z = 0;
    public int counter = 0;
    public int toreturn = 0;
    public int maxx = 0;
    public int minx = 0;
    public int maxz = 0;
    public int minz = 0;
    public int KZT = 0;
    public int KZD = 0;
    public int MKZT = 0;
    public int SOKZ = 0;
    int waittimer = KZT*60*1000 - 60000;
     
    //Start Code
    @Override
    public void onDisable() {
    System.out.println("[Battle Royal] Has been disabled.");
    }
     
    @Override
    public void onEnable() {
    loadConfiguration();
    System.out.println("[Battle Rotal] Has been enabled!");
    maxx = getConfig().getInt("XMax");
    minx = getConfig().getInt("XMin");
    maxz = getConfig().getInt("ZMax");
    minz = getConfig().getInt("ZMin");
    KZT = getConfig().getInt("KillZoneTime");
    KZD = getConfig().getInt("KillZoneDecrement");
    MKZT = getConfig().getInt("MinimumKillZoneTime");
    SOKZ = getConfig().getInt("SizeOfKillZone");
    }
     
    public void loadConfiguration() {
    getConfig().options().header("Battle Royal - By DocRedstone");
    getConfig().addDefault("UseDefault", true);
    getConfig().addDefault("XMax", 1000);
    getConfig().addDefault("XMin", -1000);
    getConfig().addDefault("ZMax", 1000);
    getConfig().addDefault("ZMin", -1000);
    getConfig().addDefault("KillZoneTime", 2);
    getConfig().addDefault("KillZoneDecrement", 1);
    getConfig().addDefault("MinimumKillZoneTime", 1);
    getConfig().addDefault("SizeOfKillZone", 100);
    getConfig().options().copyDefaults(true);
    saveConfig();
    }
     
    public int randomNumber(int start, int end, int varibletocheck)
    {
    //stuff
    }
     
    private void showRandomInteger(int aStart, int aEnd, Random aRandom)
    {
    //stuff
    }
     
    public void generateRandomChunk()
    {
    //stuff
    }
     
    public void checkLocation(int secondsleft)
    {
    //stuff
    }
     
    public void killPlayer()
    {
    //stuff
    }
     
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[]) {
    if(commandLabel.equalsIgnoreCase("brstart")) {
    Bukkit.broadcastMessage("[Battle Royal] Starting...");
    this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
    public void run() {
    generateRandomChunk();
    if (waittimer < MKZT)
    {
    waittimer = MKZT*60*1000;
    }
    checkLocation(60);
    try {
    Thread.sleep(30000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    checkLocation(30);
    try {
    Thread.sleep(20000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    checkLocation(10);
    try {
    Thread.sleep(10000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    killPlayer();
    int temp = (int)waittimer - KZD;
    temp = temp*60*1000;
    waittimer = temp;
    Bukkit.broadcastMessage("" +  waittimer);
    }
    }, 0L, waittimer);
    }
    return false;
    }
     
    }
    


    It executes fine the first round, the second loops never seems to come.
     
  2. Offline

    civ77

    Converting it to a long should not increase the length of the delay
     
  3. Offline

    DocRedstone

    civ77 Can you see any other reason the delay is increased?
     
  4. Offline

    Njol

    Task delays/periods are measured in ticks (1/20th second), not milliseconds.
     
  5. Offline

    DocRedstone

    Njol so if I would do something like

    int minute = 20*60

    That should work fine?
     
  6. Offline

    Njol

    Yes, though it will be longer than a minute if the server laggs.
     
    DocRedstone likes this.
  7. Offline

    CorrieKay

    Hmm... Is there a safe way to utilize your own task handling system that doesnt interfere with bukkits scheduler?
     
  8. Offline

    Double0negative

    Just make your own thread
     
  9. Offline

    VeryBIgCorp

    Timer and TimerTask, my favorite :)
     
  10. Offline

    CorrieKay

    hnng... the last time i used my own timer in a bukkit server, it severely messed up some of my plugins scheduling that WAS using the scheduler.

    However, this was a long long time ago, and im fairly sure i could have been making a mistake. :x
     
  11. Offline

    DocRedstone

    I ended up getting it to work. The whole code was using milliseconds instead of ticks, causing for the timing to by severely increased.
     
  12. Offline

    TopGear93

    O:0! Never EVER use thread sleep. This will cuase the server to have extreme lag or even crash it. Use a better timer system. Create another class in another package.

    ie.
    us.topgear93.plugin.timer
    TimerEngine.java
    PHP:
    public class TimerEngine {
    public 
    PluginName plugin;
    public 
    void PluginName(PluginName plugin){
    thisplugin plugin
     
    public 
    void CommandTimer(){
    Bukkit.getServer().getScheduler().scheduleSyncRepeating(plugin, new Runnable(){
    @
    override
    public void run(){
    ///do timer
    }
    },
    23L,24L);
    }
    public 
    void AnotherTimer(){
    Bukkit.getServer().getScheduler().scheduleSyncRepeating(plugin, new Runnable(){
    @
    override
    public void run(){
    ///do timer
    }
    },
    23L,24L);
    }
    public 
    void AnotherTimer(){
    Bukkit.getServer().getScheduler().scheduleSyncRepeating(plugin, new Runnable(){
    @
    override
    public void run(){
    ///do timer
    }
    },
    23L,24L);
    }
     
  13. Offline

    Double0negative

    It will only cause the server to "lag" aka "hang" (never will it crash) from using Thread.sleep if you use it in the main thread. As long as Thread.sleep is used on a different thread there is no reason not to use it.
     
Thread Status:
Not open for further replies.

Share This Page