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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
package com.earth2me.essentials.update;
import com.earth2me.essentials.update.states.InstallationFinishedEvent;
import com.earth2me.essentials.update.states.StateMachine;
import com.earth2me.essentials.update.tasks.SelfUpdate;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;
public class UpdateProcess implements Listener
{
private transient Player currentPlayer;
private final transient Plugin plugin;
private final transient UpdateCheck updateCheck;
private transient StateMachine stateMachine;
public UpdateProcess(final Plugin plugin, final UpdateCheck updateCheck)
{
super();
this.plugin = plugin;
this.updateCheck = updateCheck;
}
public void registerEvents()
{
plugin.getServer().getPluginManager().registerEvents(this, 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 AbstractWorkListener(plugin, updateCheck.getNewVersionInfo())
{
@Override
public void onWorkAbort(final String message)
{
if (message != null && !message.isEmpty()
&& UpdateProcess.this.currentPlayer != null
&& UpdateProcess.this.currentPlayer.isOnline())
{
UpdateProcess.this.currentPlayer.sendMessage(message);
}
if (message != null && !message.isEmpty())
{
Bukkit.getLogger().log(Level.SEVERE, message);
}
UpdateProcess.this.currentPlayer = null;
}
@Override
public void onWorkDone(final String message)
{
if (message != null && !message.isEmpty()
&& UpdateProcess.this.currentPlayer != null
&& UpdateProcess.this.currentPlayer.isOnline())
{
UpdateProcess.this.currentPlayer.sendMessage(message);
}
if (message != null && !message.isEmpty())
{
Bukkit.getLogger().log(Level.INFO, message);
}
UpdateProcess.this.currentPlayer = null;
}
}).start();
return true;
}
if (updateCheck.getResult() == UpdateCheck.CheckResult.NEW_ESS_BUKKIT)
{
final String message = "Please update bukkit to version " + updateCheck.getNewBukkitVersion() + " before updating Essentials.";
if (currentPlayer != null)
{
currentPlayer.sendMessage(message);
}
Bukkit.getLogger().log(Level.INFO, message);
currentPlayer = null;
return true;
}
return false;
}
@EventHandler
public void onInstallationFinished(final InstallationFinishedEvent event)
{
UpdateProcess.this.currentPlayer = null;
}
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerChat(final PlayerChatEvent event)
{
if (event.getPlayer() == currentPlayer)
{
final StateMachine.MachineResult result = stateMachine.reactOnMessage(event.getMessage());
if (result == StateMachine.MachineResult.ABORT)
{
currentPlayer.sendMessage("Installation wizard aborted. You can restart it using /essentialsupdate.");
currentPlayer = null;
}
if (result == StateMachine.MachineResult.DONE)
{
startWork();
}
event.setCancelled(true);
return;
}
}
@EventHandler
public void onPlayerJoin(final PlayerJoinEvent event)
{
final Player player = event.getPlayer();
if (currentPlayer.getName().equals(player.getName()))
{
currentPlayer = player;
player.sendMessage("You quit the game, while the installation 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())
{
player.sendMessage("Hello " + player.getDisplayName());
player.sendMessage("Please type /essentialsupdate into the chat to start the installation of Essentials.");
}
if (player.hasPermission("essentials.update"))
{
final UpdateCheck.CheckResult result = updateCheck.getResult();
switch (result)
{
case NEW_ESS:
player.sendMessage("The new version " + updateCheck.getNewVersion().toString() + " for Essentials is available. Please type /essentialsupdate to update.");
break;
case NEW_BUKKIT:
player.sendMessage("Your bukkit version is not the recommended build for Essentials, please update to version " + updateCheck.getNewBukkitVersion() + ".");
break;
case NEW_ESS_BUKKIT:
player.sendMessage("There is a new version " + updateCheck.getNewVersion().toString() + " of Essentials for Bukkit " + updateCheck.getNewBukkitVersion());
break;
default:
}
}
}
public void onCommand(final CommandSender sender)
{
if (sender instanceof Player && sender.hasPermission("essentials.update"))
{
if (currentPlayer == null)
{
currentPlayer = (Player)sender;
if (selfUpdate())
{
return;
}
stateMachine = new StateMachine(plugin, currentPlayer, updateCheck);
final StateMachine.MachineResult result = stateMachine.askQuestion();
if (result == StateMachine.MachineResult.DONE)
{
startWork();
}
}
if (!currentPlayer.equals(sender))
{
sender.sendMessage("The player " + currentPlayer.getDisplayName() + " is already using the wizard.");
}
}
else
{
sender.sendMessage("Please run the command as op from in game.");
}
}
private void startWork()
{
currentPlayer.sendMessage("Installation wizard done. Starting installation.");
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
{
@Override
public void run()
{
stateMachine.startWork();
}
});
}
}
|