diff options
author | snowleo <schneeleo@gmail.com> | 2011-10-26 22:14:24 +0200 |
---|---|---|
committer | snowleo <schneeleo@gmail.com> | 2011-10-26 22:14:24 +0200 |
commit | 72596decbae18e2f59fdd5f1fff78b304ca2e114 (patch) | |
tree | 973b898ef48e48f99e6ed6854737e1b79ad96f2d /EssentialsUpdate/src/com/earth2me/essentials | |
parent | e8b8d26bdbe935fff57ab3ded6902e576355f995 (diff) | |
download | Essentials-72596decbae18e2f59fdd5f1fff78b304ca2e114.tar Essentials-72596decbae18e2f59fdd5f1fff78b304ca2e114.tar.gz Essentials-72596decbae18e2f59fdd5f1fff78b304ca2e114.tar.lz Essentials-72596decbae18e2f59fdd5f1fff78b304ca2e114.tar.xz Essentials-72596decbae18e2f59fdd5f1fff78b304ca2e114.zip |
The state machine now handles both manual updating and installation.
Diffstat (limited to 'EssentialsUpdate/src/com/earth2me/essentials')
9 files changed, 260 insertions, 40 deletions
diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/EssentialsUpdate.java b/EssentialsUpdate/src/com/earth2me/essentials/update/EssentialsUpdate.java index d4ee6c0fc..6b380288a 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/EssentialsUpdate.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/EssentialsUpdate.java @@ -1,6 +1,7 @@ package com.earth2me.essentials.update; import com.earth2me.essentials.update.UpdateCheck.CheckResult; +import java.util.logging.Level; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -16,20 +17,20 @@ public class EssentialsUpdate extends JavaPlugin public void onEnable() { if (!getDataFolder().exists() && !getDataFolder().mkdirs() ) { - Bukkit.getLogger().severe("Could not create data folder:"+getDataFolder().getPath()); + Bukkit.getLogger().log(Level.SEVERE, "Could not create data folder: {0}", getDataFolder().getPath()); } essentialsHelp = new EssentialsHelp(this); essentialsHelp.registerEvents(); final UpdateCheck updateCheck = new UpdateCheck(this); + updateCheck.checkForUpdates(); updateProcess = new UpdateProcess(this, updateCheck); updateProcess.registerEvents(); - Bukkit.getLogger().info("EssentialsUpdate " + getDescription().getVersion() + " loaded."); + Bukkit.getLogger().log(Level.INFO, "EssentialsUpdate {0} loaded.", getDescription().getVersion()); if (updateCheck.isEssentialsInstalled()) { - updateCheck.checkForUpdates(); final Version myVersion = new Version(getDescription().getVersion()); if (updateCheck.getResult() == CheckResult.NEW_ESS && myVersion.equals(updateCheck.getNewVersion())) { diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateCheck.java b/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateCheck.java index dcda252a0..685700426 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateCheck.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateCheck.java @@ -71,12 +71,12 @@ public class UpdateCheck return result; } - int getNewBukkitVersion() + public int getNewBukkitVersion() { return bukkitResult; } - VersionInfo getNewVersionInfo() + public VersionInfo getNewVersionInfo() { return updateFile.getVersions().get(newVersion); } diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateProcess.java b/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateProcess.java index 393bf5aef..38fefb111 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateProcess.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateProcess.java @@ -1,7 +1,11 @@ package com.earth2me.essentials.update; +import com.earth2me.essentials.update.states.Changelog; +import com.earth2me.essentials.update.states.EssentialsChat; import com.earth2me.essentials.update.states.InstallationFinishedEvent; import com.earth2me.essentials.update.states.StateMachine; +import com.earth2me.essentials.update.states.UpdateOrInstallation; +import com.earth2me.essentials.update.tasks.SelfUpdate; import java.util.List; import java.util.logging.Level; import org.bukkit.Bukkit; @@ -51,6 +55,34 @@ public class UpdateProcess extends PlayerListener }, Priority.Normal, plugin); } + public boolean selfUpdate() + { + if (new Version(plugin.getDescription().getVersion()).compareTo(updateCheck.getNewVersion()) < 0) + { + if (currentPlayer != null) + { + currentPlayer.sendMessage("A newer version of EssentialsUpdate is found. Downloading new file and reloading server."); + } + Bukkit.getLogger().log(Level.INFO, "A newer version of EssentialsUpdate is found. Downloading new file and reloading server."); + new SelfUpdate(new WorkListener(plugin, updateCheck.getNewVersionInfo()) + { + @Override + public void onWorkAbort(final String message) + { + Bukkit.getLogger().log(Level.SEVERE, message); + } + + @Override + public void onWorkDone(final String message) + { + Bukkit.getLogger().log(Level.INFO, message); + } + }).start(); + return true; + } + return false; + } + @Override public void onPlayerChat(final PlayerChatEvent event) { @@ -109,7 +141,6 @@ public class UpdateProcess extends PlayerListener public void doAutomaticUpdate() { - final VersionInfo info = updateCheck.getNewVersionInfo(); final List<String> changelog = info.getChangelog(); Bukkit.getLogger().log(Level.INFO, "Essentials changelog {0}", updateCheck.getNewVersion().toString()); @@ -121,34 +152,22 @@ public class UpdateProcess extends PlayerListener downloader.start(); } - public void doManualUpdate() - { - } - public void onCommand(final CommandSender sender) { - if (sender instanceof Player && sender.hasPermission("essentials.install")) + if (sender instanceof Player && sender.hasPermission("essentials.update")) { if (currentPlayer == null) { currentPlayer = (Player)sender; - if (updateCheck.isEssentialsInstalled()) + if (selfUpdate()) { - doManualUpdate(); + return; } - else + stateMachine = new StateMachine(plugin, currentPlayer, updateCheck); + final StateMachine.MachineResult result = stateMachine.askQuestion(); + if (result == StateMachine.MachineResult.DONE) { - sender.sendMessage("Thank you for choosing Essentials."); - sender.sendMessage("The following installation wizard will guide you through the installation of Essentials."); - sender.sendMessage("Your answers will be saved for a later update."); - sender.sendMessage("Please answer the messages with yes or no, if not otherwise stated."); - sender.sendMessage("Write bye/exit/quit if you want to exit the wizard at anytime."); - stateMachine = new StateMachine(plugin, currentPlayer, updateCheck.getNewVersionInfo()); - final StateMachine.MachineResult result = stateMachine.askQuestion(); - if (result == StateMachine.MachineResult.DONE) - { - startWork(); - } + startWork(); } } if (!currentPlayer.equals(sender)) diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/UpdatesDownloader.java b/EssentialsUpdate/src/com/earth2me/essentials/update/UpdatesDownloader.java index d13d0d41b..95fdb2932 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/UpdatesDownloader.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/UpdatesDownloader.java @@ -28,7 +28,8 @@ public class UpdatesDownloader extends WorkListener implements Runnable if (iterator.hasNext()) { final Entry<String, ModuleInfo> entry = iterator.next(); - if (Bukkit.getPluginManager().getPlugin(entry.getKey()) == null) + final Plugin plugin = Bukkit.getPluginManager().getPlugin(entry.getKey()); + if (plugin == null) { run(); } diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/Changelog.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/Changelog.java new file mode 100644 index 000000000..36ae93c4d --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/Changelog.java @@ -0,0 +1,91 @@ +package com.earth2me.essentials.update.states; + +import com.earth2me.essentials.update.UpdateCheck; +import com.earth2me.essentials.update.VersionInfo; +import java.util.List; +import org.bukkit.entity.Player; + + +public class Changelog extends AbstractState +{ + private static final int CHANGES_PER_PAGE = 5; + private transient int page = 0; + private transient boolean confirmed = false; + private transient final List<String> changes; + private transient final int pages; + + public Changelog(final StateMap stateMap) + { + super(stateMap); + changes = getChanges(); + pages = changes.size() / CHANGES_PER_PAGE + (changes.size() % CHANGES_PER_PAGE > 0 ? 1 : 0); + } + + @Override + public AbstractState getNextState() + { + return confirmed ? getState(EssentialsChat.class) : this; + } + + @Override + public boolean guessAnswer() + { + if (pages == 0) + { + confirmed = true; + } + return confirmed; + } + + private List<String> getChanges() + { + final UpdateCheck updateCheck = getState(UpdateOrInstallation.class).getUpdateCheck(); + final VersionInfo versionInfo = updateCheck.getNewVersionInfo(); + return versionInfo.getChangelog(); + } + + @Override + public void askQuestion(final Player sender) + { + if (pages > 1) + { + sender.sendMessage("Changelog, page " + page + " of " + pages + ":"); + } + else + { + sender.sendMessage("Changelog:"); + } + for (int i = page * CHANGES_PER_PAGE; i < Math.min(page * CHANGES_PER_PAGE + CHANGES_PER_PAGE, changes.size()); i++) + { + sender.sendMessage(changes.get(i)); + } + if (pages > 1) + { + sender.sendMessage("Select a page by typing the numbers 1 to " + pages + " to view all changes and then type confirm to update Essentials."); + } + else + { + sender.sendMessage("Type confirm to update Essentials."); + } + } + + @Override + public boolean reactOnAnswer(final String answer) + { + if (answer.equalsIgnoreCase("confirm")) + { + confirmed = true; + return true; + } + if (answer.matches("[0-9]+")) + { + final int page = Integer.parseInt(answer); + if (page <= pages && page > 0) + { + this.page = page - 1; + return true; + } + } + return false; + } +} diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java index a8dd00a94..9ced4fc66 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java @@ -1,7 +1,7 @@ package com.earth2me.essentials.update.states; +import com.earth2me.essentials.update.UpdateCheck; import com.earth2me.essentials.update.WorkListener; -import com.earth2me.essentials.update.VersionInfo; import java.util.Iterator; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -19,16 +19,13 @@ public class StateMachine extends WorkListener implements Runnable 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(); + UpdateOrInstallation state = new UpdateOrInstallation(states, updateCheck); + current = states.put(UpdateOrInstallation.class, state); } public MachineResult askQuestion() diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMap.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMap.java index a2cf7b719..cca4223d6 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMap.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMap.java @@ -7,11 +7,6 @@ public class StateMap extends LinkedHashMap<Class<? extends AbstractState>, Abst { public StateMap() { - super(); - } - - public AbstractState add(AbstractState state) - { - return put(state.getClass(), state); + super(50); } } diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/UpdateOrInstallation.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/UpdateOrInstallation.java new file mode 100644 index 000000000..5671275f0 --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/UpdateOrInstallation.java @@ -0,0 +1,59 @@ +package com.earth2me.essentials.update.states; + +import com.earth2me.essentials.update.UpdateCheck; +import org.bukkit.entity.Player; + + +public class UpdateOrInstallation extends AbstractState +{ + private final transient UpdateCheck updateCheck; + private transient boolean update = false; + + public UpdateOrInstallation(final StateMap stateMap, final UpdateCheck updateCheck) + { + super(stateMap); + this.updateCheck = updateCheck; + } + + @Override + public boolean guessAnswer() + { + if (getUpdateCheck().isEssentialsInstalled()) { + update = true; + } + return update; + } + + @Override + public AbstractState getNextState() + { + return update ? getState(Changelog.class) : getState(EssentialsChat.class); + } + + @Override + public void askQuestion(final Player sender) + { + sender.sendMessage("Thank you for choosing Essentials."); + sender.sendMessage("The following installation wizard will guide you through the installation of Essentials."); + sender.sendMessage("Your answers will be saved for a later update."); + sender.sendMessage("Please answer the messages with yes or no, if not otherwise stated."); + sender.sendMessage("Write bye/exit/quit if you want to exit the wizard at anytime."); + sender.sendMessage("Type ok to continue..."); + } + + @Override + public boolean reactOnAnswer(final String answer) + { + return answer.equalsIgnoreCase("ok") || answer.equalsIgnoreCase("k") || answer.equalsIgnoreCase("continue"); + } + + public UpdateCheck getUpdateCheck() + { + return updateCheck; + } + + public boolean isUpdate() + { + return update; + } +} diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/tasks/SelfUpdate.java b/EssentialsUpdate/src/com/earth2me/essentials/update/tasks/SelfUpdate.java new file mode 100644 index 000000000..e9581b2cd --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/tasks/SelfUpdate.java @@ -0,0 +1,57 @@ +package com.earth2me.essentials.update.tasks; + +import com.earth2me.essentials.update.WorkListener; +import org.bukkit.Bukkit; + + +public class SelfUpdate extends WorkListener implements Task, Runnable +{ + private final transient WorkListener listener; + + public SelfUpdate(final WorkListener listener) + { + super(listener.getPlugin(), listener.getNewVersionInfo()); + this.listener = listener; + } + + @Override + public void onWorkAbort(final String message) + { + listener.onWorkAbort(message); + } + + @Override + public void onWorkDone(final String message) + { + listener.onWorkDone(message); + Bukkit.getScheduler().scheduleSyncDelayedTask(getPlugin(), new Runnable() + { + @Override + public void run() + { + Bukkit.getServer().reload(); + } + }); + } + + @Override + public void start() + { + Bukkit.getScheduler().scheduleSyncDelayedTask(getPlugin(), this); + } + + @Override + public void run() + { + Bukkit.getScheduler().scheduleAsyncDelayedTask(getPlugin(), new Runnable() { + + @Override + public void run() + { + new InstallModule(SelfUpdate.this, "EssentialsUpdate").start(); + } + }); + } + + +} |