The state machine now handles both manual updating and installation.

This commit is contained in:
snowleo 2011-10-26 22:14:24 +02:00
parent e8b8d26bdb
commit 72596decba
9 changed files with 260 additions and 40 deletions

View File

@ -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()))
{

View File

@ -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);
}

View File

@ -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))

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -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()

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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();
}
});
}
}