Solved Changing delay of repeating task

Discussion in 'Plugin Development' started by Marcoral, Aug 19, 2016.

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

    Marcoral

    Hello! I have problem with repeating tasks. I want to execute a code inside repeating task, which should be delayed by value of variable initialised inside this task. For example I want my task to repeat with progressing delay until it is long enough. I tried something like:


    Code:
    int delay = 4;
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable(){
        @Override
        public void run() {
            if(delay < 10){
                //CODE
                delay++;
            }
            else{
                this.cancel();
            }
        }
    }, 0, delay);
    

    But the problem is that I receive Eclipse error "Cannot refer to the non-final variable defined in an enclosed scope" and I totally don't have an idea how to get it work. Any proposals?
     
    Last edited: Aug 19, 2016
  2. Offline

    Zombie_Striker

    @Marcoral
    You cannot actually modify the delay. However, you can force the code to run every _ seconds. Try using the following:
    1. Create an int. This will represent the "delay". For this example, set it equal to 10
    2. Create another int. This will represent what the previous delay was set to.
    3. Create a repeating task. The delay will only be one tick.
    4. Inside the task, check if the int from #1 is equal to zero.
    5. If it is equal, then set the delay (int from #1) equal to the int from #2 + the added delay. Set the int from #2 equal to the int from #1. After this, run your code.
    6. If it is not equal to zero, then subtract one from the delay.
     
  3. Offline

    Marcoral

    Thank you Zombie_Striker. I think I made a little progress, but I still can't get it working - one more time because of an compilation error.

    Code:
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable(){
        double speed = 100;
        double decelleration = ThreadLocalRandom.current().nextDouble(1, 0.11);
        double recentspeed;
        boolean previousLoopDone = true;
        @Override
        public void run() {
            if(previousLoopDone){
                previousLoopDone = false;
                Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                    public void run(){
                        //smthg
                        previousLoopDone = true;
                        if(speed > 5){
                            recentspeed = speed;
                            speed = recentspeed - decelleration;
                        }
                        else{
                            Bukkit.getScheduler().cancelAllTasks();
                        }
                    }
                }, (int) Math.floor(400/speed));
            }
        }
    }, 0, 1);
    For me everything looks OK, but it gives me error in line
    Code:
    Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    It tells, that
    Code:
    the method scheduleSyncDelayedTask(Plugin, Runnable, long) in the type BukkitScheduler is not applicable for the arguments (new BukkitRunnable(), new Runnable(), int);
    I tried to cast (Plugin) in first argument and the error changed to
    Code:
    BukkitScheduler is not applicable for the arguments (Plugin, new Runnable(), int);
    I find this kinda weird. What should I do?

    A little screenshoot to clarify the code: [​IMG]
    [​IMG]
     
  4. Offline

    SantaClawz69

    This class isn't getting the instance of your main class. Unless this runnable is inside the main class you must create an instance of the main class, and then use it instead of saying "this".

    EDIT: What Zombie was saying, is to make a whole new Repeating task. Don't put the repeating inside your delayed. That's why you're having the problems.
     
  5. Offline

    Marcoral

    I thought I made whole new repeating task :confused:
    Delayed is put inside repeating, not conversely.
    And yes, I have a whole code in my Main class, I forgot to add.

    OK, it seems, that "this" would always refer to the Runnable() inside this Task. I defined new Class-object and got it worked, yay! Thank you for your help, especially to you Zombie_Striker! Now it works the way I want to!

    //Little effect of my work:
    [​IMG]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 20, 2016
  6. Offline

    Zombie_Striker

    @Marcoral
    Great! If your problem has been solved, mark this thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page