Version verification
get
https://groupez.dev/api
/v1/resource/version/:id
Get resource version

Example

You will be able to check if the version of the plugin is the same as the version on the site. You can then warn users to update the plugin.
1
public class VersionChecker implements Listener {
2
3
private final String URL_API = "https://groupez.dev/api/v1/resource/version/%s";
4
private final String URL_RESOURCE = "https://groupez.dev/resources/%s";
5
private final Plugin plugin;
6
private final int pluginID;
7
private boolean useLastVersion = false;
8
9
/**
10
* Class constructor
11
*
12
* @param plugin
13
* @param pluginID
14
*/
15
public VersionChecker(Plugin plugin, int pluginID) {
16
super();
17
this.plugin = plugin;
18
this.pluginID = pluginID;
19
}
20
21
/**
22
* Allows to check if the plugin version is up to date.
23
*/
24
public void useLastVersion() {
25
26
Bukkit.getPluginManager().registerEvents(this, this.plugin); // Register
27
// event
28
29
String pluginVersion = plugin.getDescription().getVersion();
30
AtomicBoolean atomicBoolean = new AtomicBoolean();
31
this.getVersion(version -> {
32
33
long ver = Long.valueOf(version.replace(".", ""));
34
long plVersion = Long.valueOf(pluginVersion.replace(".", ""));
35
atomicBoolean.set(plVersion >= ver);
36
this.useLastVersion = atomicBoolean.get();
37
if (atomicBoolean.get())
38
Logger.info("No update available.");
39
else {
40
Logger.info("New update available. Your version: " + pluginVersion + ", latest version: " + version);
41
Logger.info("Download plugin here: " + String.format(URL_RESOURCE, this.pluginID));
42
}
43
});
44
45
}
46
47
@EventHandler
48
public void onConnect(PlayerJoinEvent event) {
49
final Player player = event.getPlayer();
50
if (!useLastVersion && event.getPlayer().hasPermission("zplugin.notifs")) {
51
new BukkitRunnable() {
52
@Override
53
public void run() {
54
String prefix = "GroupeZ ";
55
player.sendMessage(prefix + "§cYou do not use the latest version of the plugin! Thank you for taking the latest version to avoid any risk of problem!");
56
player.sendMessage(prefix + "§fDownload plugin here: §a" + String.format(URL_RESOURCE, pluginID));
57
}
58
}.runTaskLater(plugin, 20 * 2);
59
}
60
}
61
62
/**
63
* Get version by plugin id
64
*
65
* @param consumer
66
* - Do something after
67
*/
68
public void getVersion(Consumer<String> consumer) {
69
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
70
final String apiURL = String.format(URL_API, this.pluginID);
71
try {
72
URL url = new URL(apiURL);
73
URLConnection hc = url.openConnection();
74
hc.setRequestProperty("User-Agent", "Mozilla/5.0");
75
Scanner scanner = new Scanner(hc.getInputStream());
76
if (scanner.hasNext())
77
consumer.accept(scanner.next());
78
scanner.close();
79
80
} catch (IOException exception) {
81
this.plugin.getLogger().info("Cannot look for updates: " + exception.getMessage());
82
}
83
});
84
}
85
86
}
Copied!
Last modified 6mo ago