+ /* find next reaction. */
+ SortedMap<Long, Pair<String, Reaction>> nextReactions = newTreeMap();
+ synchronized (reactions) {
+ for (Entry<String, Reaction> reactionEntry : reactions.entrySet()) {
+ Optional<net.pterodactylus.rhynodge.State> state = stateManager.loadLastState(reactionEntry.getKey());
+ long stateTime = state.isPresent() ? state.get().time() : 0;
+ nextReactions.put(stateTime + reactionEntry.getValue().updateInterval(), Pair.of(reactionEntry.getKey(), reactionEntry.getValue()));
+ }
+ Pair<String, Reaction> keyReaction = nextReactions.get(nextReactions.firstKey());
+ return of(new NextReaction(keyReaction.getKey(), keyReaction.getValue(), nextReactions.firstKey()));
+ }
+ }
+ return absent();
+ }
+
+ private static class NextReaction {
+
+ private final String key;
+ private final Reaction reaction;
+ private final long nextTime;
+
+ private NextReaction(String key, Reaction reaction, long nextTime) {
+ this.key = key;
+ this.reaction = reaction;
+ this.nextTime = nextTime;