summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/com/earth2me/essentials/update/tasks/InstallModule.java
blob: 66eeb380d5a0385ac715630becec9a036851881b (plain)
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
package com.earth2me.essentials.update.tasks;

import com.earth2me.essentials.update.GetFile;
import com.earth2me.essentials.update.ModuleInfo;
import com.earth2me.essentials.update.VersionInfo;
import com.earth2me.essentials.update.AbstractWorkListener;
import java.io.File;
import java.net.URL;
import java.util.logging.Level;
import org.bukkit.Bukkit;


public class InstallModule implements Runnable, Task
{
	protected final transient AbstractWorkListener listener;
	private final transient String moduleName;
	private final transient String fileName;

	public InstallModule(final AbstractWorkListener listener, final String moduleName)
	{
		this(listener, moduleName, moduleName + ".jar");
	}

	public InstallModule(final AbstractWorkListener listener, final String moduleName, final String fileName)
	{
		this.listener = listener;
		this.moduleName = moduleName;
		this.fileName = fileName;
	}

	@Override
	public void start()
	{
		Bukkit.getScheduler().scheduleAsyncDelayedTask(listener.getPlugin(), this);
	}

	@Override
	public void run()
	{
		final VersionInfo info = listener.getNewVersionInfo();
		final ModuleInfo module = info.getModules().get(moduleName);
		if (module == null)
		{
			listener.onWorkAbort("Module " + moduleName + " not found in VersionInfo.");
			return;
		}
		try
		{
			final URL downloadUrl = module.getUrl();
			final GetFile getFile = new GetFile(downloadUrl);
			getFile.saveTo(new File(listener.getPlugin().getServer().getUpdateFolderFile(), fileName), module.getHash());
			listener.onWorkDone("Module " + moduleName + " downloaded.");
		}
		catch (Exception ex)
		{
			Bukkit.getLogger().log(Level.SEVERE, "Failed to download module " + moduleName + " to " + fileName, ex);
			listener.onWorkAbort("An error occured, please check your server log.");
			return;
		}
	}
}