-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectAnimation.java
More file actions
95 lines (80 loc) · 2.62 KB
/
Copy pathObjectAnimation.java
File metadata and controls
95 lines (80 loc) · 2.62 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package ru.cwcode.cwutils.animation;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class ObjectAnimation<T> {
AnimationProperties properties;
BiConsumer<Double, T> action;
Supplier<T> before = null;
Consumer<T> after = null;
T object;
private ObjectAnimation(Supplier<T> before) {
this.before = before;
}
public static <T> ObjectAnimation<T> before(Supplier<T> before) {
return new ObjectAnimation<>(before);
}
public ObjectAnimation<T> setProperties(AnimationProperties properties) {
this.properties = properties;
return this;
}
public ObjectAnimation<T> setAction(BiConsumer<Double, T> action) {
this.action = action;
return this;
}
public void start(JavaPlugin plugin, ExecutionMode mode) {
runBefore(plugin, mode);
runMain(plugin, mode);
runAfter(plugin, mode);
}
public ObjectAnimation<T> after(Consumer<T> after) {
this.after = after;
return this;
}
private void runAfter(JavaPlugin plugin, ExecutionMode mode) {
if (after == null) return;
switch (mode) {
case ASYNC:
case INSTANT_ASYNC:
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> after.accept(object), properties.frameDelayInTicks() + 1);
break;
case SYNC:
case INSTANT_SYNC:
Bukkit.getScheduler().runTaskLater(plugin, () -> after.accept(object), properties.frameDelayInTicks() + 1);
break;
}
}
private void runMain(JavaPlugin plugin, ExecutionMode mode) {
while (properties.hasNextFrame()) {
double finalCurrent = properties.nextFrame();
//properties.debug();
switch (mode) {
case ASYNC:
case INSTANT_ASYNC:
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> action.accept(finalCurrent, object), properties.frameDelayInTicks());
break;
case SYNC:
case INSTANT_SYNC:
Bukkit.getScheduler().runTaskLater(plugin, () -> action.accept(finalCurrent, object), properties.frameDelayInTicks());
break;
}
}
}
private void runBefore(JavaPlugin plugin, ExecutionMode mode) {
if (before == null) return;
switch (mode) {
case ASYNC:
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> object = before.get());
break;
case SYNC:
Bukkit.getScheduler().runTask(plugin, () -> object = before.get());
break;
case INSTANT_SYNC:
case INSTANT_ASYNC:
object = before.get();
break;
}
}
}