summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/com/earth2me/essentials/update/UpdateCheck.java
blob: 29afc4d3f43d63fee5fb3b2bae21066f39e5bcd3 (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
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
200
201
202
package com.earth2me.essentials.update;

import java.io.File;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;


public class UpdateCheck
{
	private transient CheckResult result = CheckResult.UNKNOWN;
	private transient Version currentVersion;
	private transient Version newVersion = null;
	private transient int bukkitResult = 0;
	private transient UpdateFile updateFile;
	private final static int CHECK_INTERVAL = 20 * 60 * 60 * 6;
	private final transient Plugin plugin;
	private transient boolean essentialsInstalled;

	public UpdateCheck(final Plugin plugin)
	{
		this.plugin = plugin;
		updateFile = new UpdateFile(plugin);
		checkForEssentials();
	}

	private void checkForEssentials()
	{
		final PluginManager pluginManager = plugin.getServer().getPluginManager();
		final Plugin essentials = pluginManager.getPlugin("Essentials");
		essentialsInstalled = essentials != null;
		if (essentialsInstalled)
		{
			currentVersion = new Version(essentials.getDescription().getVersion());
		}
		else
		{
			if (new File(plugin.getDataFolder().getParentFile(), "Essentials.jar").exists())
			{
				Bukkit.getLogger().severe("Essentials.jar found, but not recognized by Bukkit. Broken download?");
			}
		}
	}

	public void scheduleUpdateTask()
	{
		plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable()
		{
			@Override
			public void run()
			{
				updateFile = new UpdateFile(plugin);
				checkForUpdates();
			}
		}, CHECK_INTERVAL, CHECK_INTERVAL);
	}

	public boolean isEssentialsInstalled()
	{
		return essentialsInstalled;
	}

	public CheckResult getResult()
	{
		return result;
	}

	public int getNewBukkitVersion()
	{
		return bukkitResult;
	}

	public VersionInfo getNewVersionInfo()
	{
		return updateFile.getVersions().get(newVersion);
	}


	public enum CheckResult
	{
		NEW_ESS, NEW_ESS_BUKKIT, NEW_BUKKIT, OK, UNKNOWN
	}

	public void checkForUpdates()
	{
		if (currentVersion == null)
		{
			return;
		}
		final Map<Version, VersionInfo> versions = updateFile.getVersions();
		final int bukkitVersion = getBukkitVersion();
		Version higher = null;
		Version found = null;
		Version lower = null;
		int bukkitHigher = 0;
		int bukkitLower = 0;
		for (Entry<Version, VersionInfo> entry : versions.entrySet())
		{
			final int minBukkit = entry.getValue().getMinBukkit();
			final int maxBukkit = entry.getValue().getMaxBukkit();
			if (minBukkit == 0 || maxBukkit == 0)
			{
				continue;
			}
			if (bukkitVersion <= maxBukkit)
			{
				if (bukkitVersion < minBukkit)
				{
					if (higher == null || higher.compareTo(entry.getKey()) < 0)
					{

						higher = entry.getKey();
						bukkitHigher = minBukkit;
					}
				}
				else
				{
					if (found == null || found.compareTo(entry.getKey()) < 0)
					{
						found = entry.getKey();
					}
				}
			}
			else
			{
				if (lower == null || lower.compareTo(entry.getKey()) < 0)
				{
					lower = entry.getKey();
					bukkitLower = minBukkit;
				}
			}
		}
		if (found != null)
		{
			if (found.compareTo(currentVersion) > 0)
			{
				result = CheckResult.NEW_ESS;
				newVersion = found;
			}
			else
			{
				result = CheckResult.OK;
			}
		}
		else if (higher != null)
		{
			if (higher.compareTo(currentVersion) > 0)
			{
				newVersion = higher;
				result = CheckResult.NEW_ESS_BUKKIT;
				bukkitResult = bukkitHigher;
			}
			else if (higher.compareTo(currentVersion) < 0)
			{
				result = CheckResult.UNKNOWN;
			}
			else
			{
				result = CheckResult.NEW_BUKKIT;
				bukkitResult = bukkitHigher;
			}
		}
		else if (lower != null)
		{
			if (lower.compareTo(currentVersion) > 0)
			{
				result = CheckResult.NEW_ESS_BUKKIT;
				newVersion = lower;
				bukkitResult = bukkitLower;
			}
			else if (lower.compareTo(currentVersion) < 0)
			{
				result = CheckResult.UNKNOWN;
			}
			else
			{
				result = CheckResult.NEW_BUKKIT;
				bukkitResult = bukkitLower;
			}
		}

	}

	private int getBukkitVersion()
	{
		final Matcher versionMatch = Pattern.compile("git-Bukkit-([0-9]+).([0-9]+).([0-9]+)-[0-9]+-[0-9a-z]+-b([0-9]+)jnks.*").matcher(plugin.getServer().getVersion());
		if (versionMatch.matches())
		{
			return Integer.parseInt(versionMatch.group(4));
		}
		throw new NumberFormatException("Bukkit Version changed!");
	}

	public Version getNewVersion()
	{
		return newVersion;
	}
}