Skip to content

Commit 8bbadfb

Browse files
authored
Merge pull request #6 from curz46/cleanup/sequence
cleanup/sequence
2 parents e72f362 + 360f681 commit 8bbadfb

2 files changed

Lines changed: 64 additions & 36 deletions

File tree

‎src/main/java/com/github/abilityapi/trigger/TriggerManager.java‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,14 @@ private <T> List<T> handleCurrent(Player player, Event event, Iterator<Map.Entry
113113
T key = entry.getKey();
114114
Sequence sequence = entry.getValue();
115115

116+
sequence.next(player, event);
117+
116118
if (sequence.isCancelled() || sequence.hasExpired()) {
117119
it.remove();
118120
continue;
119121
}
120122

121-
if (sequence.next(player, event) && sequence.hasFinished()) {
123+
if (sequence.hasFinished()) {
122124
finished.add(key);
123125
it.remove();
124126
}
@@ -136,14 +138,16 @@ private <T> List<T> handleNew(Player player, Event event, Iterator<Map.Entry<T,
136138
T key = entry.getKey();
137139
Sequence sequence = entry.getValue();
138140

139-
if (sequence.next(player, event)) {
140-
if (sequence.hasFinished()) {
141-
finished.add(key);
142-
continue;
143-
}
141+
if (!sequence.next(player, event)) {
142+
continue;
143+
}
144144

145-
potentials.put(key, sequence);
145+
if (sequence.hasFinished()) {
146+
finished.add(key);
147+
continue;
146148
}
149+
150+
potentials.put(key, sequence);
147151
}
148152

149153
return finished;

‎src/main/java/com/github/abilityapi/trigger/sequence/Sequence.java‎

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)