change cycle time

Discussion in 'Plugin Development' started by Xhork, Aug 5, 2019.

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

    Xhork

    hey

    i would like to change the cycle speed of day and light, who is normally to 10min day and 10min night, i want to change it to 5min day and 5min night, but there is no .setTimeSpeed, only .setTime or setTimeFull..

    So what i'm trying to do is:

    Code:
    private void dayChanger() {
            Bukkit.getScheduler().runTaskTimer(this, new BukkitRunnable() {
                @Override
                public void run() {
                    Bukkit.getWorld("world").setFullTime(Bukkit.getWorld("world").getFullTime() + LONG VALUE);
    
    
    
    
    
                }
            }, 0, 1);
        }
    but is the long value in ticks? help me please :'(
     
  2. Offline

    CraftCreeper6

    @Xhork
    The long value is in Minecraft ticks. 20 ticks to a second in real life.

    You could proportionally increase it every 10 ticks to half the time.
     
  3. Offline

    Xhork

    thank's for reply but i don't understand the "to half the time", setTime(getTime *2 ) ? cause this will not work
     
  4. Offline

    CraftCreeper6

    @Xhork
    You need some sort of proportion.

    What value does getTime() return at midnight?
     
  5. Offline

    Xhork

    It's 6000 at midday and 18000 at midnight but i can't find the right long tick value to put to have a day of 5min but i'll find it (i hope)
     
  6. Offline

    CraftCreeper6

    @Xhork

    Okay so use the setTime() function in increments of 2 instead of 1 every tick.
     
  7. Offline

    Xhork

    So, when i add 1 tick to the time (gettime +1) every tick, the sun sets at 5:21, and when i add 2 ticks the sun si too fast ! I can't find how to do this, cause we can't add 1.5 ticks
     
  8. Offline

    CraftCreeper6

    @Xhork
    What currently happens is that it increments once every tick right?
    So you just increment it twice every tick instead?
     
  9. Offline

    Xhork

    When I increment it once every tick the sun sets à 5:21, and when I increment it twice every tick the sun sets too fast
     
  10. Offline

    CraftCreeper6

    @Xhork
    Incrementing by two is double the speed which halves the time taken. Could you time how long it takes to complete 1 day with the increment of 2?
     
  11. Offline

    Xhork

    Hi,

    so i tested something,
    Code:
    double d = 1.8;
                    Bukkit.getWorld("world").setTime((long) (Bukkit.getWorld("world").getTime() + d));
    but i can't set the day duration to 5min :(
     
  12. Offline

    CraftCreeper6

    @Xhork
    I don't think you'll be able to get it to exactly 5 minutes but you can definitely get somewhere close.
     
  13. Offline

    Xhork

    @CraftCreeper6
    I play on a UHC server, and the day is exacly 5min so it's possible but i can't understand how
     
  14. Offline

    CraftCreeper6

    It'll take alot of fine tuning.


    Sent from my LYA-L09 using Tapatalk
     
  15. Offline

    Machine Maker

    Make sure you set the day cycle gamerule to false when you are doing this. Otherwise you won't be factoring the natural progression of time.
     
  16. Offline

    wand555

    Heya @Xhork
    I found this issue quite interesting and I think I came up with a compromising solution.
    Calculate <ticks per cycle you want> / 24000 Ticks. Now don't save it as double, instead save it as numerator and denominator.
    Let's make an example with 12.000 Ticks per cycle.
    12000 / 24000 = 0.5 (1/2) -> But we cant call a runnable every 0.5 ticks to increment the time by one, so instead we run it every whole tick and increment int by two.
    14000 / 24000 = 0.58333 (7/12) -> Run every 7 ticks and increment the time by 12 ticks.
    As far as I can think this works.
    Code:java
    1. private void setDayNightSpeed(long ticks) {
    2. if(ticks != 0 && ticks <= 24000) {
    3. for(World world : Bukkit.getWorlds()) {
    4. final long finalTime;
    5. final long runnableTicks;
    6.  
    7. Bruch bruch = new Bruch(ticks, 24000).kuerzen();
    8. runnableTicks = bruch.getNum();
    9. finalTime = bruch.getDenom();
    10.  
    11. new BukkitRunnable() {
    12.  
    13. @Override
    14. public void run() {
    15. world.setTime(world.getTime() + finalTime);
    16. }
    17. }.runTaskTimer(this, 0L, runnableTicks);
    18. }
    19. }
    20. }


    Code:java
    1. public class Bruch {
    2.  
    3. private long num;
    4. private long denom;
    5.  
    6. public Bruch(long num, long denom) {
    7. this.num = num;
    8. this.denom = denom;
    9. }
    10.  
    11. public long getNum() {
    12. return this.num;
    13. }
    14.  
    15. public long getDenom() {
    16. return this.denom;
    17. }
    18.  
    19. public Bruch kuerzen() {
    20. long numResult = this.num / ggT();
    21. long denumResult = this.denom / ggT();
    22. return new Bruch(numResult, denumResult);
    23. }
    24.  
    25. public long ggT() {
    26. ArrayList<Long> longListNum = new ArrayList<Long>();
    27. for(long i=1; i<=this.num; i++) {
    28. if(this.num % i == 0) {
    29. longListNum.add(i);
    30. }
    31. }
    32. ArrayList<Long> longListdenum = new ArrayList<Long>();
    33. for(long i=1; i<=this.denom; i++) {
    34. if(this.denom % i == 0) {
    35. longListdenum.add(i);
    36. }
    37. }
    38.  
    39. ArrayList<Long> complete = new ArrayList<Long>();
    40. for(long l1 : longListNum) {
    41. for(long l2 : longListdenum) {
    42. if(l1 == l2) {
    43. complete.add(l1);
    44. }
    45. }
    46. }
    47. long max = 0;
    48. for(int i=0; i<complete.size(); i++) {
    49. max = Long.MIN_VALUE;
    50. if(complete.get(i) > max) {
    51. max = complete.get(i);
    52. }
    53. }
    54. return max;
    55. }
    56.  
    57. public Bruch addBruch(Bruch other) {
    58. long numResult = this.num * other.denom + this.denom * other.num;
    59. long denumResult = this.num * other.denom;
    60. Bruch ausgabe = new Bruch(numResult, denumResult);
    61. return ausgabe;
    62. }
    63.  
    64. public Bruch subBruch(Bruch other) {
    65. long numResult = this.num * other.denom - this.denom * other.num;
    66. long denumResult = this.num * other.denom;
    67. Bruch ausgabe = new Bruch(numResult, denumResult);
    68. return ausgabe;
    69. }
    70.  
    71. public Bruch multBruch(Bruch other) {
    72. long numResult = this.num * other.denom;
    73. long denumResult = this.denom * other.num;
    74. return new Bruch(numResult, denumResult);
    75. }
    76. }
     
Thread Status:
Not open for further replies.

Share This Page