Use the last successful state for the trigger.
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / engine / Engine.java
index 7c50081..2825b54 100644 (file)
@@ -64,15 +64,9 @@ public class Engine extends AbstractExecutionThreadService {
         *            The name of the reaction
         * @param reaction
         *            The reaction to add to this engine
-        * @throws IllegalStateException
-        *             if the engine already contains a {@link Reaction} with the
-        *             given name
         */
        public void addReaction(String name, Reaction reaction) {
                synchronized (reactions) {
-                       if (reactions.containsKey(name)) {
-                               throw new IllegalStateException(String.format("Engine already contains a Reaction named “%s!”", name));
-                       }
                        reactions.put(name, reaction);
                        reactions.notifyAll();
                }
@@ -124,7 +118,7 @@ public class Engine extends AbstractExecutionThreadService {
                        Reaction nextReaction;
                        synchronized (reactions) {
                                for (Entry<String, Reaction> reactionEntry : reactions.entrySet()) {
-                                       net.pterodactylus.reactor.State state = stateManager.loadState(reactionEntry.getKey());
+                                       net.pterodactylus.reactor.State state = stateManager.loadLastState(reactionEntry.getKey());
                                        long stateTime = (state != null) ? state.time() : 0;
                                        nextReactions.put(stateTime + reactionEntry.getValue().updateInterval(), Pair.of(reactionEntry.getKey(), reactionEntry.getValue()));
                                }
@@ -134,7 +128,7 @@ public class Engine extends AbstractExecutionThreadService {
                        logger.debug(String.format("Next Reaction: %s.", nextReaction));
 
                        /* wait until the next reaction has to run. */
-                       net.pterodactylus.reactor.State lastState = stateManager.loadState(reactionName);
+                       net.pterodactylus.reactor.State lastState = stateManager.loadLastState(reactionName);
                        long lastStateTime = (lastState != null) ? lastState.time() : 0;
                        int lastStateFailCount = (lastState != null) ? lastState.failCount() : 0;
                        long waitTime = (lastStateTime + nextReaction.updateInterval()) - System.currentTimeMillis();
@@ -183,14 +177,15 @@ public class Engine extends AbstractExecutionThreadService {
                        if (!state.success()) {
                                state.setFailCount(lastStateFailCount + 1);
                        }
+                       net.pterodactylus.reactor.State lastSuccessfulState = stateManager.loadLastSuccessfulState(reactionName);
                        stateManager.saveState(reactionName, state);
 
                        /* only run trigger if we have collected two successful states. */
                        Trigger trigger = nextReaction.trigger();
                        boolean triggerHit = false;
-                       if ((lastState != null) && lastState.success() && state.success()) {
+                       if ((lastSuccessfulState != null) && lastSuccessfulState.success() && state.success()) {
                                logger.debug("Checking Trigger for changes...");
-                               triggerHit = trigger.triggers(state, lastState);
+                               triggerHit = trigger.triggers(state, lastSuccessfulState);
                        }
 
                        /* run action if trigger was hit. */