mirror of
https://github.com/essentials/Essentials.git
synced 2025-10-01 16:46:51 +02:00
If the installation is already running in background after the wizard, it will not stop, if the player quits the game.
171 lines
3.6 KiB
Java
171 lines
3.6 KiB
Java
package com.earth2me.essentials.update.states;
|
|
|
|
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;
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
|
|
public class StateMachine extends WorkListener
|
|
{
|
|
public enum MachineResult
|
|
{
|
|
ABORT, WAIT, DONE, NONE
|
|
}
|
|
private final transient StateMap states = new StateMap();
|
|
private transient AbstractState current;
|
|
private transient Player player;
|
|
private transient MachineResult result = MachineResult.NONE;
|
|
|
|
public StateMachine(final Plugin plugin, final Player player, final VersionInfo newVersionInfo)
|
|
{
|
|
super(plugin, newVersionInfo);
|
|
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();
|
|
}
|
|
|
|
public MachineResult askQuestion()
|
|
{
|
|
while (current.guessAnswer())
|
|
{
|
|
current = current.getNextState();
|
|
if (current == null)
|
|
{
|
|
result = MachineResult.DONE;
|
|
break;
|
|
}
|
|
}
|
|
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())
|
|
{
|
|
finish();
|
|
result = MachineResult.ABORT;
|
|
}
|
|
else
|
|
{
|
|
result = MachineResult.DONE;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
current = next;
|
|
askQuestion();
|
|
}
|
|
return result;
|
|
}
|
|
private transient Iterator<AbstractState> iterator;
|
|
|
|
public void startWork()
|
|
{
|
|
iterator = states.values().iterator();
|
|
callStateWork();
|
|
}
|
|
|
|
private void callStateWork()
|
|
{
|
|
if (!iterator.hasNext())
|
|
{
|
|
if (player.isOnline())
|
|
{
|
|
player.sendMessage("Installation done.");
|
|
}
|
|
finish();
|
|
return;
|
|
}
|
|
final AbstractState state = iterator.next();
|
|
state.doWork(this);
|
|
}
|
|
|
|
@Override
|
|
public void onWorkAbort(final String message)
|
|
{
|
|
finish();
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(getPlugin(), new Runnable()
|
|
{
|
|
@Override
|
|
public void run()
|
|
{
|
|
if (message != null && !message.isEmpty() && StateMachine.this.player.isOnline())
|
|
{
|
|
StateMachine.this.player.sendMessage(message);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onWorkDone(final String message)
|
|
{
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(getPlugin(), new Runnable()
|
|
{
|
|
@Override
|
|
public void run()
|
|
{
|
|
if (message != null && !message.isEmpty() && StateMachine.this.player.isOnline())
|
|
{
|
|
StateMachine.this.player.sendMessage(message);
|
|
}
|
|
StateMachine.this.callStateWork();
|
|
}
|
|
});
|
|
}
|
|
|
|
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.");
|
|
}
|
|
}
|
|
}
|