-
Notifications
You must be signed in to change notification settings - Fork 0
/
BobbingTask.java
49 lines (39 loc) · 1.4 KB
/
BobbingTask.java
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
package fi.natroutter.natlibs.handlers.bobbingscheduler;
import org.bukkit.entity.Entity;
import java.util.function.Consumer;
public class BobbingTask {
private long currentTick = 0;
private double lastYOffset = 0;
protected Consumer<Double> height;
protected double amplitude;
protected double frequency;
public BobbingTask(Entity entity, double amplitude, double frequency) {
this.height = y -> {
if (entity != null && !entity.isDead()) {
entity.teleport(entity.getLocation().add(0,y,0));
}
};
this.amplitude = amplitude;
this.frequency = frequency;
}
public BobbingTask(Consumer<Double> height, double amplitude, double frequency) {
this.height = height;
this.amplitude = amplitude;
this.frequency = frequency;
}
protected BobbingTask(Consumer<Double> height, double amplitude, double frequency, long startingTicks) {
this.height = height;
this.amplitude = amplitude;
this.frequency = frequency;
this.currentTick = startingTicks;
}
public void tick() {
if (currentTick == (Long.MAX_VALUE - 1)) {
currentTick = 0;
}
double yOffset = Math.sin(Math.toRadians(currentTick * frequency)) * amplitude;
height.accept((yOffset - lastYOffset));
lastYOffset = yOffset;
currentTick++;
}
}