+ private Optional<Reaction> getNextReaction() {
+ while (isRunning()) {
+ synchronized (reactions) {
+ if (reactions.isEmpty()) {
+ logger.debug("Sleeping while no Reactions available.");
+ try {
+ reactions.wait();
+ } catch (InterruptedException ie1) {
+ /* ignore, we’re looping anyway. */
+ }
+ continue;
+ }
+ }
+
+ /* find next reaction. */
+ SortedMap<Long, Pair<String, Reaction>> nextReactions = Maps.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()));
+ }
+ return of(nextReactions.get(nextReactions.firstKey()).getRight());
+ }
+ }
+ return absent();
+ }
+