Question

I currently am using Bukkit for plugin development, and I have 12 preferences that are configured in a file (12 ways a person can pay to fly.)

For each configuration (12), there are 2 timers needed. One 'withdraw' timer, and one 'fly check' timer. Each timer would be setup at different intervals, so I couldn't combine the two.

Then, when a player has a permission for one of the 12 configurations, he/she is placed in an Array, and the timer assigned to that configuration iterates through the array, running the methods for checking what's needed.

However, I believe there's a better way to do this, besides having 24 timers go off on server start. It just seems like it's too much, and will be a major hit to performance. The timers must also be SyncDelayedTask because they access the Bukkit API.

So I was wondering if there's a more efficient way to solve this problem?

Was it helpful?

Solution

There is a way that you could do this without setting 24 separate timers. You could create one, with the time interval being 1. Then, you could add one to a number every time the timer runs. Like this:

int loops;

this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){

          public void run() {
              loops++;//add one to the loops
          }
        },1, 1);

Then, for every time interval, you could set up a check to see if loops is divisible by the interval... For example, let's say that you would like to have a fly check every 5 ticks, and your withdraw timer every 7 ticks. You could do this:

int loops;

this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){

          public void run() {
              loops++;//add one to the loops

                      if(loops %  5 == 0){//if loops is divisible by 5
                          //fly check code
                      }
                      if(loops % 7 == 0){//if loops is divisible by 7
                          //withdraw code
                      }
          }
        },1, 1); //make the timer repeat every 1 tick

Just make sure that you only use if statements, not else if. If you use else if above, and loops is 35, which is divisible by 5 and 7, it will run the first statement, but not the second, because you are telling it to run that statement only if the above ones are false. Therefor, if you only use if statements, then it will be able to check every single one of them, and multiple will be able to be called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top