mirror of
https://github.com/EssentialsX/Essentials.git
synced 2024-08-26 15:32:50 +02:00
Cleanly resume the installation, if the player quits the game and reconnects
If the installation is already running in background after the wizard, it will not stop, if the player quits the game.
This commit is contained in:
parent
7f825750f0
commit
48f8eb9788
|
@ -1,10 +1,13 @@
|
||||||
package com.earth2me.essentials.update;
|
package com.earth2me.essentials.update;
|
||||||
|
|
||||||
|
import com.earth2me.essentials.update.states.InstallationFinishedEvent;
|
||||||
import com.earth2me.essentials.update.states.StateMachine;
|
import com.earth2me.essentials.update.states.StateMachine;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
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.Priority;
|
||||||
import org.bukkit.event.Event.Type;
|
import org.bukkit.event.Event.Type;
|
||||||
import org.bukkit.event.player.PlayerChatEvent;
|
import org.bukkit.event.player.PlayerChatEvent;
|
||||||
|
@ -32,6 +35,18 @@ public class UpdateProcess extends PlayerListener
|
||||||
final PluginManager pluginManager = plugin.getServer().getPluginManager();
|
final PluginManager pluginManager = plugin.getServer().getPluginManager();
|
||||||
pluginManager.registerEvent(Type.PLAYER_QUIT, this, Priority.Low, plugin);
|
pluginManager.registerEvent(Type.PLAYER_QUIT, this, Priority.Low, plugin);
|
||||||
pluginManager.registerEvent(Type.PLAYER_CHAT, this, Priority.Lowest, 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
|
@Override
|
||||||
|
@ -58,6 +73,13 @@ public class UpdateProcess extends PlayerListener
|
||||||
public void onPlayerJoin(final PlayerJoinEvent event)
|
public void onPlayerJoin(final PlayerJoinEvent event)
|
||||||
{
|
{
|
||||||
final Player player = event.getPlayer();
|
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())
|
if (player.hasPermission("essentials.update") && !updateCheck.isEssentialsInstalled())
|
||||||
{
|
{
|
||||||
player.sendMessage("Hello " + player.getDisplayName());
|
player.sendMessage("Hello " + player.getDisplayName());
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,11 +12,12 @@ public class StateMachine extends WorkListener
|
||||||
{
|
{
|
||||||
public enum MachineResult
|
public enum MachineResult
|
||||||
{
|
{
|
||||||
ABORT, WAIT, DONE
|
ABORT, WAIT, DONE, NONE
|
||||||
}
|
}
|
||||||
private final transient StateMap states = new StateMap();
|
private final transient StateMap states = new StateMap();
|
||||||
private transient AbstractState current;
|
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 VersionInfo newVersionInfo)
|
||||||
{
|
{
|
||||||
|
@ -37,29 +38,43 @@ public class StateMachine extends WorkListener
|
||||||
current = current.getNextState();
|
current = current.getNextState();
|
||||||
if (current == null)
|
if (current == null)
|
||||||
{
|
{
|
||||||
return MachineResult.DONE;
|
result = MachineResult.DONE;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
current.askQuestion(player);
|
if (current != null)
|
||||||
return MachineResult.WAIT;
|
{
|
||||||
|
if (player.isOnline())
|
||||||
|
{
|
||||||
|
current.askQuestion(player);
|
||||||
|
}
|
||||||
|
result = MachineResult.WAIT;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MachineResult reactOnMessage(final String message)
|
public MachineResult reactOnMessage(final String message)
|
||||||
{
|
{
|
||||||
|
result = MachineResult.NONE;
|
||||||
final AbstractState next = current.reactOnAnswer(player, message);
|
final AbstractState next = current.reactOnAnswer(player, message);
|
||||||
if (next == null)
|
if (next == null)
|
||||||
{
|
{
|
||||||
if (current.isAbortion())
|
if (current.isAbortion())
|
||||||
{
|
{
|
||||||
return MachineResult.ABORT;
|
finish();
|
||||||
|
result = MachineResult.ABORT;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return MachineResult.DONE;
|
result = MachineResult.DONE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
current = next;
|
else
|
||||||
return askQuestion();
|
{
|
||||||
|
current = next;
|
||||||
|
askQuestion();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
private transient Iterator<AbstractState> iterator;
|
private transient Iterator<AbstractState> iterator;
|
||||||
|
|
||||||
|
@ -77,6 +92,7 @@ public class StateMachine extends WorkListener
|
||||||
{
|
{
|
||||||
player.sendMessage("Installation done.");
|
player.sendMessage("Installation done.");
|
||||||
}
|
}
|
||||||
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final AbstractState state = iterator.next();
|
final AbstractState state = iterator.next();
|
||||||
|
@ -86,6 +102,7 @@ public class StateMachine extends WorkListener
|
||||||
@Override
|
@Override
|
||||||
public void onWorkAbort(final String message)
|
public void onWorkAbort(final String message)
|
||||||
{
|
{
|
||||||
|
finish();
|
||||||
Bukkit.getScheduler().scheduleSyncDelayedTask(getPlugin(), new Runnable()
|
Bukkit.getScheduler().scheduleSyncDelayedTask(getPlugin(), new Runnable()
|
||||||
{
|
{
|
||||||
@Override
|
@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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user