1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
package com.earth2me.essentials.update.states;
import com.earth2me.essentials.update.AbstractWorkListener;
import com.earth2me.essentials.update.UpdateCheck;
import java.util.Iterator;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public class StateMachine extends AbstractWorkListener implements Runnable
{
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 UpdateCheck updateCheck)
{
super(plugin, updateCheck.getNewVersionInfo());
this.player = player;
states.clear();
final UpdateOrInstallation state = new UpdateOrInstallation(states, updateCheck);
current = states.put(UpdateOrInstallation.class, state);
}
public MachineResult askQuestion()
{
try
{
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;
}
}
catch (RuntimeException ex)
{
player.sendMessage(ex.getMessage());
finish();
result = MachineResult.ABORT;
}
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();
Bukkit.getScheduler().scheduleAsyncDelayedTask(getPlugin(), this);
}
@Override
public void run()
{
if (!iterator.hasNext())
{
Bukkit.getScheduler().scheduleSyncDelayedTask(getPlugin(), new Runnable()
{
@Override
public void run()
{
if (StateMachine.this.player.isOnline())
{
StateMachine.this.player.sendMessage("Installation done. Reloading server.");
}
finish();
Bukkit.getServer().reload();
}
});
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);
}
Bukkit.getScheduler().scheduleAsyncDelayedTask(getPlugin(), StateMachine.this);
}
});
}
private void finish()
{
current = null;
iterator = null;
states.clear();
getPlugin().getServer().getPluginManager().callEvent(new InstallationFinishedEvent());
}
public void resumeInstallation(final Player player)
{
this.player = player;
if (result == MachineResult.WAIT)
{
if (current == null)
{
throw new RuntimeException("State is WAIT, but current state is null!");
}
current.askQuestion(player);
}
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 an aborted installation.");
}
if (result == MachineResult.NONE)
{
throw new RuntimeException("State machine in an undefined state.");
}
}
}
|