1
+ package me .flame .menus .menu .animation ;
2
+
3
+ import me .flame .menus .items .MenuItem ;
4
+ import me .flame .menus .menu .BaseMenu ;
5
+
6
+ import org .bukkit .Bukkit ;
7
+
8
+ import org .bukkit .plugin .Plugin ;
9
+ import org .bukkit .scheduler .BukkitTask ;
10
+
11
+ import com .google .common .primitives .Ints ;
12
+
13
+ import java .util .Collection ;
14
+ import java .util .List ;
15
+ import java .util .Arrays ;
16
+
17
+ /**
18
+ * Animations in Woody are highly dependent on bukkit scheduler. shout out to bukkit
19
+ */
20
+ @ SuppressWarnings ("unused" )
21
+ public abstract class Animation {
22
+ protected static Plugin plugin ;
23
+
24
+ public Animation (BaseMenu menu , List <MenuItem > items ) {
25
+ this .menu = menu ;
26
+ this .items = items ;
27
+ }
28
+
29
+ public static void init (Plugin p ) {
30
+ plugin = p ;
31
+ }
32
+
33
+ private int currentStep = 0 ;
34
+ private BukkitTask task ;
35
+ private final BaseMenu menu ;
36
+ private final List <MenuItem > items ;
37
+
38
+ /**
39
+ * The amount of ticks per step
40
+ */
41
+ protected long ticksPerStep = 1 ;
42
+
43
+ /**
44
+ * Which slots will be set on which ticks?
45
+ */
46
+ protected int [][] steps = new int [10 ][];
47
+
48
+ /**
49
+ * Size of "steps" variable
50
+ */
51
+ protected int size = 0 ;
52
+
53
+ /**
54
+ * Set how many ticks it will take to execute one step.
55
+ * By default, 1.
56
+ *
57
+ * @param ticksPerStep The ticks required to perform one step
58
+ */
59
+ public void ticksPerStep (int ticksPerStep ) {
60
+ if (ticksPerStep < 1 ) {
61
+ throw new IllegalArgumentException ("Ticks cannot be under 1" );
62
+ }
63
+ this .ticksPerStep = ticksPerStep ;
64
+ }
65
+
66
+ /**
67
+ * Add which slots will be set during which tick
68
+ * If two of the same keys are registered they will be added together.
69
+ *
70
+ * @param tick The tick. This must start from 0 and increment by one each time.
71
+ * @param slots The slots which will be set during this time period
72
+ */
73
+ protected void add (int tick , int ... slots ) {
74
+ if (tick >= steps .length ) {
75
+ steps = Arrays .copyOf (steps , tick + 3 );
76
+ }
77
+
78
+ int [] tickStep = steps [tick ];
79
+ int slotsLength = slots .length ;
80
+ int tickStepLength = tickStep != null ? tickStep .length : 0 ;
81
+
82
+ if (tickStepLength < slotsLength ) {
83
+ tickStep = tickStep == null
84
+ ? new int [slotsLength ]
85
+ : Arrays .copyOf (tickStep , slotsLength );
86
+ steps [tick ] = tickStep ;
87
+ }
88
+
89
+ for (int i = 0 ; i < slotsLength ; i ++) {
90
+ tickStep [i ] += slots [i ];
91
+ }
92
+ }
93
+
94
+
95
+
96
+ /**
97
+ * Add which slots will be set during which tick.
98
+ * If two of the same keys are registered they will be added together.
99
+ *
100
+ * @param tick The tick. This must start from 0 and increment by one each time.
101
+ * @param slotsList The slots which will be set during this time period
102
+ */
103
+ protected void add (int tick , Collection <Integer > slotsList ) {
104
+ this .add (tick , Ints .toArray (slotsList ));
105
+ }
106
+
107
+ /**
108
+ * Initializes this animation, settings all slots
109
+ *
110
+ * @param rows The amount of rows the eventual inventory will have.
111
+ */
112
+ public abstract void init (int rows );
113
+
114
+ /**
115
+ * Starts the animation
116
+ */
117
+ public void animate () {
118
+ task = Bukkit .getScheduler ().runTaskTimer (plugin , () -> {
119
+ if (currentStep == steps .length ) {
120
+ task .cancel ();
121
+ return ;
122
+ }
123
+
124
+ int [] slots = steps [currentStep ];
125
+ if (slots == null || slots .length == 0 ) {
126
+ task .cancel ();
127
+ return ;
128
+ }
129
+
130
+ for (int slot : slots ) {
131
+ MenuItem item = items .get (slot );
132
+ if (item != null ) menu .setItem (slot , item );
133
+ }
134
+
135
+ currentStep ++;
136
+ }, 0 , ticksPerStep );
137
+ }
138
+
139
+ public void stop () {
140
+ task .cancel ();
141
+ }
142
+
143
+ /**
144
+ * Gets all slots from a beginning slot to delta amount extra.
145
+ * The default direction is always right (east). Turn it left by entering a negative delta.
146
+ *
147
+ * @param begin The beginning slot (inclusive)
148
+ * @param delta The difference between the two, where the last slot is excluded. A delta of 9 will result in 9 elements, but only go up to slot 8.
149
+ * @return the slots between begin and delta amount away
150
+ */
151
+ protected static int [] getHorizontal (int begin , int delta ) {
152
+ int direction = delta < 0 ? -1 : 1 ;
153
+ int newDelta = Math .abs (delta ) + 1 ;
154
+
155
+ int [] result = new int [newDelta ];
156
+ for (int i = 0 ; i <= newDelta ; i ++) {
157
+ result [i ] = begin + (i * direction );
158
+ }
159
+ return result ;
160
+ }
161
+
162
+ /**
163
+ * Gathers slots vertically down from the beginning slot.
164
+ * The default direction is always down (south). Turn it left by entering a negative delta.
165
+ *
166
+ * @param begin The beginning slot
167
+ * @param delta How many rows the vertical selection goes down
168
+ * @return the vertical slots between the beginning slots
169
+ */
170
+ protected static int [] getVertical (int begin , int delta ) {
171
+ int direction = delta < 0 ? -9 : 9 ;
172
+ int deltaAbs = Math .abs (delta );
173
+
174
+ int [] result = new int [deltaAbs ];
175
+ for (int i = 0 ; i <= deltaAbs ; i ++)
176
+ result [i ] = begin + (i * direction );
177
+ return result ;
178
+ }
179
+ }
0 commit comments