@@ -39,7 +39,6 @@ public static SequenceBuilder builder() {
3939
4040 /**
4141 * Test if all currently testable conditions are satisfied.
42- * @return true or false, depending on if testable conditions were satisfied.
4342 */
4443 public <T extends Event > boolean next (Player player , T event ) {
4544 Iterator <Action > it = actions .iterator ();
@@ -50,43 +49,26 @@ public <T extends Event> boolean next(Player player, T event) {
5049
5150 Action action = it .next ();
5251
53- if (action . getCancelEvents (). contains ( event . getClass () )) {
54- cancelled = true ;
55- return false ;
56- }
52+ if (! checkEvent ( action , event )) {
53+ if (! checkCancelledEvent ( action , event )) {
54+ cancelled = true ;
55+ }
5756
58- if (!action .getEventClass ().equals (event .getClass ())) {
5957 return false ;
6058 }
6159
6260 //noinspection unchecked
6361 Action <T > casted = (Action <T >) action ;
6462
65- if (action .getDelay ().isPresent ()) {
66- if (System .currentTimeMillis () < last + (int ) action .getDelay ().get () * 1000 ) {
67- cancelled = true ;
68- return false ; // condition failed because action delay hasn't completed.
69- }
70- }
71-
72- if (action .getExpire ().isPresent ()) {
73- if (System .currentTimeMillis () > last + (int ) action .getExpire ().get () * 1000 ) {
74- return false ; // condition failed because action has expired.
75- }
76- }
77-
78- Collection <Condition <T >> conditions = casted .getConditions ();
79- boolean result = !conditions .stream ()
80- .filter (condition -> !condition .test (player , event ))
81- .findFirst ()
82- .isPresent ();
83-
84- if (result ) {
85- last = System .currentTimeMillis ();
86- it .remove ();
63+ if (!checkDelay (casted )
64+ || !checkExpire (casted )
65+ || !checkConditions (casted , player , event )) {
66+ return false ;
8767 }
8868
89- return result ;
69+ last = System .currentTimeMillis ();
70+ actions .remove (casted );
71+ return true ;
9072 }
9173
9274 public boolean hasFinished () {
@@ -111,4 +93,46 @@ public boolean isCancelled() {
11193 return cancelled ;
11294 }
11395
96+ // the below return true if the condition is met.
97+
98+ private boolean checkCancelledEvent (Action action , Event event ) {
99+ return !action .getCancelEvents ().contains (event .getClass ());
100+ }
101+
102+ private boolean checkEvent (Action action , Event event ) {
103+ return action .getEventClass ().equals (event .getClass ());
104+ }
105+
106+ private boolean checkDelay (Action action ) {
107+ if (!action .getDelay ().isPresent ()) {
108+ return true ;
109+ }
110+
111+ long now = System .currentTimeMillis ();
112+ long delay = (int ) action .getDelay ().get () * 1000 ;
113+
114+ return now > last + delay ;
115+ }
116+
117+ private boolean checkExpire (Action action ) {
118+ if (!action .getExpire ().isPresent ()) {
119+ return true ;
120+ }
121+
122+ long now = System .currentTimeMillis ();
123+ long expiry = (int ) action .getExpire ().get () * 1000 ;
124+
125+ return now < last + expiry ;
126+ }
127+
128+ private <T extends Event > boolean checkConditions (Action <T > action , Player player , T event ) {
129+ Collection <Condition <T >> conditions = action .getConditions ();
130+ boolean result = !conditions .stream ()
131+ .filter (condition -> !condition .test (player , event ))
132+ .findFirst ()
133+ .isPresent ();
134+
135+ return result ;
136+ }
137+
114138}
0 commit comments