11package com .abilityapi .sequenceapi ;
22
33import com .abilityapi .sequenceapi .action .Action ;
4+ import com .abilityapi .sequenceapi .action .type .after .AfterAction ;
45import com .abilityapi .sequenceapi .action .type .observe .ObserverAction ;
56import com .abilityapi .sequenceapi .action .type .schedule .ScheduleAction ;
67import com .abilityapi .sequenceapi .util .EventComparator ;
@@ -34,7 +35,7 @@ public static <E> EventComparator<Class<? extends E>, E> getComparatorAssignable
3435 private final int actionsSize ;
3536
3637 private int index = 0 ;
37- private long ticks = 0 ;
38+ private long scheduleTicks = 0 ;
3839 private long lastTime = System .currentTimeMillis ();
3940 private State state = State .INACTIVE ;
4041
@@ -111,18 +112,55 @@ public boolean applyObserve(final T event, final SequenceContext sequenceContext
111112 }
112113
113114 /**
114- * Applies the next {@link ScheduleAction } in the {@link Sequence}
115+ * Applies the next {@link AfterAction } in the {@link Sequence}
115116 * with an appropriate {@link SequenceContext}.
116117 *
117118 * @param sequenceContext the sequence context
118119 * @return true if the action was successful and false if it was not
119120 */
121+ public boolean applyAfter (final SequenceContext sequenceContext ) {
122+ final ListIterator <Action > iterator = this .actions .listIterator ();
123+
124+ if (iterator .hasNext ()) {
125+ final Action rawAction = iterator .next ();
126+
127+ final AfterAction action ;
128+ if (rawAction instanceof AfterAction ) {
129+ action = (AfterAction ) rawAction ;
130+ } else return false ;
131+
132+ if (this .state .equals (State .INACTIVE )) this .state = State .ACTIVE ;
133+
134+ final long current = System .currentTimeMillis ();
135+
136+ if (!(this .lastTime + ((action .getDelay () / 20 ) * 1000 ) > current )) {
137+ this .index ++;
138+
139+ iterator .remove ();
140+
141+ if (!iterator .hasNext ()) {
142+ if (this .index == this .actionsSize ) this .state = State .FINISHED ;
143+ }
144+
145+ return this .succeed (action , sequenceContext );
146+ }
147+ }
148+
149+ return false ;
150+ }
151+
152+ /**
153+ * Applies the next {@link ScheduleAction} in the {@link Sequence}
154+ * with an appropriate {@link SequenceContext}.
155+ *
156+ * @param sequenceContext the sequence context
157+ */
120158 public void applySchedule (final SequenceContext sequenceContext ) {
121159 final ListIterator <Action > iterator = this .actions .listIterator ();
122160
123161 if (this .state .equals (State .INACTIVE )) this .state = State .ACTIVE ;
124162
125- this .ticks ++;
163+ this .scheduleTicks ++;
126164
127165 if (iterator .hasNext ()) {
128166 final Action rawAction = iterator .next ();
@@ -138,7 +176,7 @@ public void applySchedule(final SequenceContext sequenceContext) {
138176
139177 // 1. Check that the tick is being executed in the period wanted.
140178
141- if (action .getPeriod () != 0 && this .ticks % action .getPeriod () != 0 ) return ;
179+ if (action .getPeriod () != 0 && this .scheduleTicks % action .getPeriod () != 0 ) return ;
142180
143181 // 2. Fail the action if it is being executed before the delay.
144182
@@ -161,9 +199,8 @@ public void applySchedule(final SequenceContext sequenceContext) {
161199 return ;
162200 }
163201
164- // If this is a repeating task that will expire next tick OR a delayed task, remove it.
165- if (action .getPeriod () == 0
166- || current + ((action .getPeriod () / 20 ) * 1000 ) > this .lastTime + ((action .getExpire () / 20 ) * 1000 )) {
202+ // If this is a repeating task that will expire next tick remove it.
203+ if (current + ((action .getPeriod () / 20 ) * 1000 ) > this .lastTime + ((action .getExpire () / 20 ) * 1000 )) {
167204 this .index ++;
168205
169206 iterator .remove ();
0 commit comments