X-Git-Url: https://git.pterodactylus.net/?p=rhynodge.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Freactor%2Fstates%2FStateManager.java;h=804615350ff2f19c8d442f6b49ec84a734115f13;hp=d9d6d564b2f07ef0b8ef5e4c8506c147b2fe77b8;hb=2a93b06248bc852307e652e869e57c6e17bce054;hpb=e700c7d888d36c44d648cff6344c876f71c0bba8 diff --git a/src/main/java/net/pterodactylus/reactor/states/StateManager.java b/src/main/java/net/pterodactylus/reactor/states/StateManager.java index d9d6d56..8046153 100644 --- a/src/main/java/net/pterodactylus/reactor/states/StateManager.java +++ b/src/main/java/net/pterodactylus/reactor/states/StateManager.java @@ -68,18 +68,19 @@ public class StateManager { * loaded */ public State loadLastState(String reactionName) { - File stateFile = stateFile(reactionName); - try { - State state = objectMapper.readValue(stateFile, AbstractState.class); - return state; - } catch (JsonParseException jpe1) { - logger.warn(String.format("State for Reaction “%s” could not be parsed.", reactionName), jpe1); - } catch (JsonMappingException jme1) { - logger.warn(String.format("State for Reaction “%s” could not be parsed.", reactionName), jme1); - } catch (IOException ioe1) { - logger.info(String.format("State for Reaction “%s” could not be found.", reactionName)); - } - return null; + return loadLastState(reactionName, false); + } + + /** + * Loads the last state with the given name. + * + * @param reactionName + * The name of the reaction + * @return The loaded state, or {@code null} if the state could not be + * loaded + */ + public State loadLastSuccessfulState(String reactionName) { + return loadLastState(reactionName, true); } /** @@ -92,8 +93,12 @@ public class StateManager { */ public void saveState(String reactionName, State state) { try { - File stateFile = stateFile(reactionName); + File stateFile = stateFile(reactionName, "last"); objectMapper.writeValue(stateFile, state); + if (state.success()) { + stateFile = stateFile(reactionName, "success"); + objectMapper.writeValue(stateFile, state); + } } catch (JsonGenerationException jge1) { logger.warn(String.format("State for Reaction “%s” could not be generated.", reactionName), jge1); } catch (JsonMappingException jme1) { @@ -112,10 +117,38 @@ public class StateManager { * * @param reactionName * The name of the reaction + * @param suffix + * An additional suffix (may be {@code null} * @return The file for the state */ - private File stateFile(String reactionName) { - return new File(directory, reactionName + ".json"); + private File stateFile(String reactionName, String suffix) { + return new File(directory, reactionName + ((suffix != null) ? "." + suffix : "") + ".json"); + } + + /** + * Load the given state for the reaction with the given name. + * + * @param reactionName + * The name of the reaction + * @param successful + * {@code true} to load the last successful state, {@code false} + * to load the last state + * @return The loaded state, or {@code null} if the state could not be + * loaded + */ + private State loadLastState(String reactionName, boolean successful) { + File stateFile = stateFile(reactionName, successful ? "success" : "last"); + try { + State state = objectMapper.readValue(stateFile, AbstractState.class); + return state; + } catch (JsonParseException jpe1) { + logger.warn(String.format("State for Reaction “%s” could not be parsed.", reactionName), jpe1); + } catch (JsonMappingException jme1) { + logger.warn(String.format("State for Reaction “%s” could not be parsed.", reactionName), jme1); + } catch (IOException ioe1) { + logger.info(String.format("State for Reaction “%s” could not be found.", reactionName)); + } + return null; } }