-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunnable
69 lines (61 loc) · 2.03 KB
/
Runnable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package me.longerdays_fork;
import me.longerdays_fork.ConfigManager;
import me.longerdays_fork.Longerdays_fork;
import me.longerdays_fork.LongerDaysUtil;
import org.bukkit.World;
import org.bukkit.scheduler.BukkitRunnable;
public class Runnable {
private final Longerdays_fork plugin;
private final ConfigManager cm;
private long count;
public Runnable(final Longerdays_fork plugin) {
this.plugin = plugin;
this.cm = this.plugin.getConfigManager();
}
public void runCycles(final World world) {
new BukkitRunnable() {
@Override
public void run() {
final long time = world.getTime();
if (isDay(world)) {
setTime(world, convertMinsToTicks(cm.getDay()));
} else if (isNight(world)) {
setTime(world, convertMinsToTicks(cm.getNight()));
} else {
LongerDaysUtil.consoleWarning(world.getName() + " world time " + time + " is impossible");
}
}
}.runTaskTimer(this.plugin, 0,1);
}
private boolean isDay(final World world) {
final long time = world.getTime();
return time >= 0 && time < 12000;
}
private boolean isNight(final World world) {
return (!(this.isDay(world)));
}
private void setTime(final World world, final long val) {
final double ratio = (1.0 / (val / 12000.0));
long time = world.getTime();
// Speedup
if(ratio > 1.0) {
time += Math.round(ratio);
world.setTime(time);
this.count = 0;
// Slowdown
} else if(ratio < 1.0) {
if(this.count <= 0) {
// Slow
time += 1;
world.setTime(time);
this.count = Math.round(1.0 / ratio) - 1;
} else {
// Wait
this.count--;
}
}
}
private long convertMinsToTicks(final long min) {
return min * 60 * 20;
}
}