diff options
Diffstat (limited to 'EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java')
-rw-r--r-- | EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java | 121 |
1 files changed, 93 insertions, 28 deletions
diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java index 77fb9ce20..6db8734e7 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java @@ -1,82 +1,113 @@ package com.earth2me.essentials.update.states; -import com.earth2me.essentials.update.WorkListener; -import com.earth2me.essentials.update.VersionInfo; +import com.earth2me.essentials.update.UpdateCheck; +import com.earth2me.essentials.update.AbstractWorkListener; import java.util.Iterator; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; -public class StateMachine extends WorkListener +public class StateMachine extends AbstractWorkListener implements Runnable { public enum MachineResult { - ABORT, WAIT, DONE + ABORT, WAIT, DONE, NONE } private final transient StateMap states = new StateMap(); private transient AbstractState current; - private final transient Player player; + private transient Player player; + private transient MachineResult result = MachineResult.NONE; - public StateMachine(final Plugin plugin, final Player player, final VersionInfo newVersionInfo) + public StateMachine(final Plugin plugin, final Player player, final UpdateCheck updateCheck) { - super(plugin, newVersionInfo); + super(plugin, updateCheck.getNewVersionInfo()); this.player = player; states.clear(); - states.add(new EssentialsChat(states)); - states.add(new EssentialsSpawn(states)); - states.add(new EssentialsProtect(states)); - states.add(new EssentialsGeoIP(states)); - current = states.values().iterator().next(); + final UpdateOrInstallation state = new UpdateOrInstallation(states, updateCheck); + current = states.put(UpdateOrInstallation.class, state); } public MachineResult askQuestion() { - while (current.guessAnswer()) + try { - current = current.getNextState(); - if (current == null) + while (current.guessAnswer()) { - return MachineResult.DONE; + current = current.getNextState(); + if (current == null) + { + result = MachineResult.DONE; + break; + } } + if (current != null) + { + if (player.isOnline()) + { + current.askQuestion(player); + } + result = MachineResult.WAIT; + } + } + catch (RuntimeException ex) + { + player.sendMessage(ex.getMessage()); + finish(); + result = MachineResult.ABORT; } - current.askQuestion(player); - return MachineResult.WAIT; + return result; } public MachineResult reactOnMessage(final String message) { + result = MachineResult.NONE; final AbstractState next = current.reactOnAnswer(player, message); if (next == null) { if (current.isAbortion()) { - return MachineResult.ABORT; + finish(); + result = MachineResult.ABORT; } else { - return MachineResult.DONE; + result = MachineResult.DONE; } } - current = next; - return askQuestion(); + else + { + current = next; + askQuestion(); + } + return result; } private transient Iterator<AbstractState> iterator; public void startWork() { iterator = states.values().iterator(); - callStateWork(); + Bukkit.getScheduler().scheduleAsyncDelayedTask(getPlugin(), this); } - private void callStateWork() + @Override + public void run() { if (!iterator.hasNext()) { - if (player.isOnline()) + Bukkit.getScheduler().scheduleSyncDelayedTask(getPlugin(), new Runnable() { - player.sendMessage("Installation done."); - } + @Override + public void run() + { + if (StateMachine.this.player.isOnline()) + { + StateMachine.this.player.sendMessage("Installation done. Reloading server."); + } + finish(); + Bukkit.getServer().reload(); + } + }); return; } final AbstractState state = iterator.next(); @@ -86,6 +117,7 @@ public class StateMachine extends WorkListener @Override public void onWorkAbort(final String message) { + finish(); Bukkit.getScheduler().scheduleSyncDelayedTask(getPlugin(), new Runnable() { @Override @@ -111,8 +143,41 @@ public class StateMachine extends WorkListener { StateMachine.this.player.sendMessage(message); } - StateMachine.this.callStateWork(); + Bukkit.getScheduler().scheduleAsyncDelayedTask(getPlugin(), StateMachine.this); } }); } + + private void finish() + { + current = null; + iterator = null; + states.clear(); + getPlugin().getServer().getPluginManager().callEvent(new InstallationFinishedEvent()); + } + + public void resumeInstallation(final Player player) + { + this.player = player; + if (result == MachineResult.WAIT) + { + if (current == null) + { + throw new RuntimeException("State is WAIT, but current state is null!"); + } + current.askQuestion(player); + } + if (result == MachineResult.DONE && iterator != null) + { + player.sendMessage("Installation is still running."); + } + if (result == MachineResult.ABORT) + { + throw new RuntimeException("Player should not be able to resume an aborted installation."); + } + if (result == MachineResult.NONE) + { + throw new RuntimeException("State machine in an undefined state."); + } + } } |