77import org .bukkit .scheduler .BukkitRunnable ;
88import org .bukkit .util .Vector ;
99
10+ /**
11+ * Represents an animation that can be applied to elements or divisions in the Interactive Menu API.
12+ */
1013public class Animation {
1114 private AnimationType type ;
1215 private double stepper ;
1316 private int duration ;
1417
18+ /**
19+ * Constructs an Animation with the specified type, stepper, and tick duration.
20+ *
21+ * @param type The type of animation.
22+ * @param stepper The step value for the animation.
23+ * @param tickDuration The duration of the animation in ticks.
24+ */
1525 public Animation (AnimationType type , double stepper , int tickDuration ) {
1626 this .type = type ;
1727 this .stepper = stepper ;
1828 this .duration = tickDuration ;
1929 }
2030
31+ /**
32+ * Applies the animation and returns the result, which includes the vector change and opacity.
33+ *
34+ * @return The result of the animation, including the vector change and opacity.
35+ */
2136 public AnimationResult apply () {
2237 Vector vectorChange = new Vector (0 , 0 , 0 );
2338 int opacity = 100 ; // Default opacity
@@ -38,6 +53,12 @@ public AnimationResult apply() {
3853 return new AnimationResult (type , vectorChange , opacity );
3954 }
4055
56+ /**
57+ * Updates the location of the input division relative to the root menu location.
58+ *
59+ * @param division The division whose location is to be updated.
60+ * @param rootMenuLocation The root location of the menu.
61+ */
4162 public void updateInputDivisionLocation (Division division , Location rootMenuLocation ) {
4263 Plugin plugin = division .getParentMenu ().getPlugin ();
4364 new BukkitRunnable () {
@@ -55,57 +76,35 @@ public void run() {
5576 if (type != AnimationType .NONE ) {
5677 AnimationResult result = apply ();
5778 newLocation .add (result .vectorChange ());
58- setDivisionOpacity (division , result .opacity ());
59- }
60-
61- division .setCurrentLocation (newLocation );
62-
63- for (Element element : division .getElements ()) {
64- element .updateLocation (newLocation );
6579 }
6680
81+ // Update the division's location
82+ division .updateLocation (newLocation );
6783 ticks ++;
6884 }
69- }.runTaskTimerAsynchronously (plugin , 0 , 1 );
70- }
71-
72- private void setDivisionOpacity (Division division , int opacity ) {
73- for (Element element : division .getElements ()) {
74- element .setOpacity (opacity );
75- }
85+ }.runTaskTimer (plugin , 0 , 1 );
7686 }
7787
88+ /**
89+ * Updates the location of the specified element relative to its parent location.
90+ *
91+ * @param element The element whose location is to be updated.
92+ * @param parentLocation The location of the parent.
93+ */
7894 public void updateElementLocation (Element element , Location parentLocation ) {
79- Plugin plugin = element .getParentMenu ().getPlugin ();
80- new BukkitRunnable () {
81- int ticks = 0 ;
95+ Location newLocation = parentLocation .clone ().add (element .getOffset ());
8296
83- @ Override
84- public void run () {
85- if (ticks >= duration ) {
86- cancel ();
87- return ;
88- }
89-
90- Location newLocation = parentLocation .clone ().add (element .getOffset ());
91-
92- if (type != AnimationType .NONE ) {
93- AnimationResult result = apply ();
94- newLocation .add (result .vectorChange ());
95- setElementOpacity (element , result .opacity ());
96- }
97-
98- element .updateLocation (newLocation );
99-
100- ticks ++;
101- }
102- }.runTaskTimerAsynchronously (plugin , 0 , 1 );
103- }
97+ if (type != AnimationType .NONE ) {
98+ AnimationResult result = apply ();
99+ newLocation .add (result .vectorChange ());
100+ }
104101
105- private void setElementOpacity ( Element element , int opacity ) {
106- element .setOpacity ( opacity );
102+ // Update the element's location
103+ element .setLocation ( newLocation );
107104 }
108105
109- public record AnimationResult (AnimationType type , Vector vectorChange , int opacity ) {
110- }
106+ /**
107+ * Represents the result of an animation, including the type, vector change, and opacity.
108+ */
109+ public record AnimationResult (AnimationType type , Vector vectorChange , int opacity ) {}
111110}
0 commit comments