daychange event?

Discussion in 'Plugin Development' started by MG127, Dec 22, 2011.

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

    MG127

    is there any way to detect a new day without making a repeating task that looks how late it is?
     
  2. Offline

    bergerkiller

    @MG127 afraid not, but you can use a repeating task of 1 interval to check it. (the time increments by one every tick)
     
  3. Offline

    unicode21B9

    Make a repeating task that is executed every 24000 ticks (1 day), starting after 24000-world.getTime() ticks. But i guess this will break if any plugin changes the time of the world, although i don't know for sure.
     
  4. Offline

    user_43347

    If you do a repeating task, make sure it stays consistant with the world with something like this...
    Code:
    int time = 0;
    (repeating task here)
    if (time == world.getTime()) {
       time++;
    } else {
       time = world.getTime();
       time++;
    }
    (end repeating task)
     
  5. Offline

    unicode21B9

    Please re-read what you posted, it currently simply sets time to world.getTime()+1, no matter what is was before.
     
  6. Offline

    MG127

    @steaks4uce
    never ask if a variable is identical with a large value (long,float,double etc)
    you will never hit it
     
  7. Offline

    user_43347

    No, it keeps it in line with the world time and adds one, which is why its in a async task, and if it doesn't, set it to the world time (too keep from being invalid), and adds one, like the earlier statement.
     
  8. Offline

    bergerkiller

    @MG127 that is only the case with values that use decimal calculations, like the location values of an entity. The long time value is simply incremented with one every tick, so it's perfectly fine to use == every tick there.

    == Always works, unless the value to check against is not bit-precise. (checking against locX == 12.0 is such an example, this will never happen unless the entity is spawned at those coordinates)
     
  9. Offline

    unicode21B9

    Just look at your code again:
    Code:
    int time = 0;
    (repeating task here)// time can be anything after this
    if (time == world.getTime()) {//if true, time is world.getTime() here
       time++; //time is world.getTime()+1 here
    } else {
       time = world.getTime();//time is world.getTime() here
       time++; //time is world.getTime()+1 here
    }
    //time is world.getTime()+1 here, no matter what. The if statement has no effect at all.
    (end repeating task)
     
  10. Offline

    MG127

    checking every tick if the time has reached the target value is damn ineffective. i would check at least every 200 ticks. even if you start at a value that would reach the target value after x*200, there is at least a 50% chance to miss it with check for being equal.
     
  11. Offline

    bergerkiller

    @MG127 What exactly do you mean with 'miss it'? The synchronized tasks are executed right before/after the world tick functions, which means they are 100% synchronized with the world time. If you increment the time in your task and check it with the world time, it is guaranteed it will always be equal, unless another plugin altered the time in the meantime. Anyway, this is not needed at all, since you only want to know if the time changed from night to day. So all you have to do is get if it's day from the time value and store a boolean with 'isDay'. If they are not the same, then it either turned day or night.
     
  12. Offline

    MG127

    @bergerkiller i'll allmost never use synchonized thasks, they use more performance or drain more from the main thread
    and for such a simple task to check if it's a new day it's just a waste of ressources
    the isDay boolean is maybe something good to start with
     
  13. Offline

    user_43347

    Thats the point, it keeps the world time inlined with the integer, so then you could create a simple method to check, or just operate off world.getTime().
     
  14. Offline

    unicode21B9

    short version of your code:
    Code:
    if (x != 1)
        x = 1;
    
     
    hammale likes this.
  15. Offline

    user_43347

    Hence is why I said "something like this" and not "exactly like this", or just "use this."
     
Thread Status:
Not open for further replies.

Share This Page