diff options
Diffstat (limited to 'EssentialsUpdate/src')
3 files changed, 95 insertions, 9 deletions
diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateProcess.java b/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateProcess.java index 80434480f..c582ebd53 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateProcess.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateProcess.java @@ -1,10 +1,13 @@ package com.earth2me.essentials.update; +import com.earth2me.essentials.update.states.InstallationFinishedEvent; import com.earth2me.essentials.update.states.StateMachine; import java.util.List; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.event.CustomEventListener; +import org.bukkit.event.Event; import org.bukkit.event.Event.Priority; import org.bukkit.event.Event.Type; import org.bukkit.event.player.PlayerChatEvent; @@ -32,6 +35,18 @@ public class UpdateProcess extends PlayerListener final PluginManager pluginManager = plugin.getServer().getPluginManager(); pluginManager.registerEvent(Type.PLAYER_QUIT, this, Priority.Low, plugin); pluginManager.registerEvent(Type.PLAYER_CHAT, this, Priority.Lowest, plugin); + pluginManager.registerEvent(Type.PLAYER_JOIN, this, Priority.Normal, plugin); + pluginManager.registerEvent(Type.CUSTOM_EVENT, new CustomEventListener(){ + + @Override + public void onCustomEvent(final Event event) + { + if(event instanceof InstallationFinishedEvent) { + UpdateProcess.this.currentPlayer = null; + } + } + + }, Priority.Normal, plugin); } @Override @@ -58,6 +73,13 @@ public class UpdateProcess extends PlayerListener public void onPlayerJoin(final PlayerJoinEvent event) { final Player player = event.getPlayer(); + if (currentPlayer.getName().equals(player.getName())) { + currentPlayer = player; + player.sendMessage("You quit the game, while the installion wizard was running."); + player.sendMessage("The installation wizard will now resume."); + player.sendMessage("You can exit the wizard by typing quit into the chat."); + stateMachine.resumeInstallation(player); + } if (player.hasPermission("essentials.update") && !updateCheck.isEssentialsInstalled()) { player.sendMessage("Hello " + player.getDisplayName()); diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/InstallationFinishedEvent.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/InstallationFinishedEvent.java new file mode 100644 index 000000000..cb88cc642 --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/InstallationFinishedEvent.java @@ -0,0 +1,12 @@ +package com.earth2me.essentials.update.states; + +import org.bukkit.event.Event; + + +public class InstallationFinishedEvent extends Event +{ + public InstallationFinishedEvent() + { + super(Type.CUSTOM_EVENT); + } +} diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java index 77fb9ce20..9a9c64134 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java @@ -12,11 +12,12 @@ public class StateMachine extends WorkListener { 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) { @@ -37,29 +38,43 @@ public class StateMachine extends WorkListener current = current.getNextState(); if (current == null) { - return MachineResult.DONE; + result = MachineResult.DONE; + break; } } - current.askQuestion(player); - return MachineResult.WAIT; + if (current != null) + { + if (player.isOnline()) + { + current.askQuestion(player); + } + result = 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; @@ -77,6 +92,7 @@ public class StateMachine extends WorkListener { player.sendMessage("Installation done."); } + finish(); return; } final AbstractState state = iterator.next(); @@ -86,6 +102,7 @@ public class StateMachine extends WorkListener @Override public void onWorkAbort(final String message) { + finish(); Bukkit.getScheduler().scheduleSyncDelayedTask(getPlugin(), new Runnable() { @Override @@ -115,4 +132,39 @@ public class StateMachine extends WorkListener } }); } + + private void finish() + { + iterator = null; + states.clear(); + getPlugin().getServer().getPluginManager().callEvent(new InstallationFinishedEvent()); + } + + public void resumeInstallation(Player player) + { + this.player = player; + if (result == MachineResult.WAIT) + { + if (current != null) + { + current.askQuestion(player); + } + else + { + throw new RuntimeException("State is WAIT, but current state is null!"); + } + } + 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 a aborted installation."); + } + if (result == MachineResult.NONE) + { + throw new RuntimeException("State machine in an undefined state."); + } + } } |