Skip to content

Commit 2014f0f

Browse files
v1.1.0 - Add SimpleUpdateChecker
1 parent 901d515 commit 2014f0f

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

build.gradle

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@ plugins {
33
}
44

55
group = 'org.example'
6-
version = '1.0-SNAPSHOT'
6+
version = '1.1.0'
77

88
repositories {
99
mavenCentral()
10+
maven {
11+
url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
12+
content {
13+
includeGroup 'org.bukkit'
14+
includeGroup 'org.spigotmc'
15+
}
16+
}
1017
}
1118

1219
java {
@@ -16,6 +23,8 @@ java {
1623
}
1724

1825
dependencies {
26+
compileOnly 'org.spigotmc:spigot-api:1.8.8-R0.1-SNAPSHOT'
27+
1928
testImplementation platform('org.junit:junit-bom:5.9.1')
2029
testImplementation 'org.junit.jupiter:junit-jupiter'
2130
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.tcoded.updatechecker;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.ChatColor;
5+
import org.bukkit.Server;
6+
import org.bukkit.command.ConsoleCommandSender;
7+
import org.bukkit.plugin.PluginDescriptionFile;
8+
import org.bukkit.plugin.java.JavaPlugin;
9+
10+
public class SimpleUpdateChecker {
11+
12+
public static void checkUpdate(JavaPlugin plugin, String prefix, int resourceId) {
13+
Server server = plugin.getServer();
14+
ConsoleCommandSender consoleSender = server.getConsoleSender();
15+
PluginDescriptionFile pluginDesc = plugin.getDescription();
16+
String pluginVersion = pluginDesc.getVersion();
17+
String pluginName = pluginDesc.getName();
18+
19+
server.getScheduler().runTaskAsynchronously(plugin, () -> {
20+
consoleSender.sendMessage(prefix + "Checking for updates...");
21+
22+
final UpdateResult result = new UpdateChecker(pluginVersion, resourceId).getResult();
23+
24+
int prioLevel = 0;
25+
String prioColor = ChatColor.AQUA.toString();
26+
String prioLevelName = "null";
27+
28+
switch (result.getType()) {
29+
case FAIL_SPIGOT:
30+
consoleSender.sendMessage(prefix + ChatColor.GOLD + "Warning: Could not contact Spigot to check if an update is available.");
31+
break;
32+
case UPDATE_LOW:
33+
prioLevel = 1;
34+
prioLevelName = "minor";
35+
break;
36+
case UPDATE_MEDIUM:
37+
prioLevel = 2;
38+
prioLevelName = "feature";
39+
prioColor = ChatColor.GOLD.toString();
40+
break;
41+
case UPDATE_HIGH:
42+
prioLevel = 3;
43+
prioLevelName = "MAJOR";
44+
prioColor = ChatColor.RED.toString();
45+
break;
46+
case DEV_BUILD:
47+
consoleSender.sendMessage(prefix + ChatColor.GOLD + "Warning: You are running an experimental/development build! Proceed with caution.");
48+
break;
49+
case NO_UPDATE:
50+
consoleSender.sendMessage(prefix + ChatColor.RESET + "You are running the latest version.");
51+
break;
52+
default:
53+
break;
54+
}
55+
56+
if (prioLevel > 0) {
57+
consoleSender.sendMessage( "\n" + prioColor +
58+
"===============================================================================\n" +
59+
"A " + prioLevelName + " update to " + pluginName + " is available!\n" +
60+
"Download at https://www.spigotmc.org/resources/" + pluginName.toLowerCase() + "." + resourceId + "/\n" +
61+
"(current: " + result.getCurrentVer() + ", latest: " + result.getLatestVer() + ")\n" +
62+
"===============================================================================\n");
63+
}
64+
65+
});
66+
}
67+
68+
}

0 commit comments

Comments
 (0)