Skip to content

Commit 346cb31

Browse files
committed
Added update check #13
1 parent 6d26896 commit 346cb31

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/main/java/com/xxmicloxx/NoteBlockAPI/NoteBlockAPI.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.xxmicloxx.NoteBlockAPI;
22

3+
import java.io.IOException;
34
import java.util.ArrayList;
45
import java.util.Collections;
56
import java.util.HashMap;
@@ -15,6 +16,7 @@
1516
import org.bukkit.scheduler.BukkitRunnable;
1617

1718
import com.xxmicloxx.NoteBlockAPI.songplayer.SongPlayer;
19+
import com.xxmicloxx.NoteBlockAPI.utils.Updater;
1820

1921
/**
2022
* Main class; contains methods for playing and adjusting songs for players
@@ -110,7 +112,7 @@ public void onEnable() {
110112

111113
new NoteBlockPlayerMain().onEnable();
112114

113-
getServer().getScheduler().runTaskLater(this, new BukkitRunnable() {
115+
getServer().getScheduler().runTaskLater(this, new Runnable() {
114116

115117
@Override
116118
public void run() {
@@ -128,6 +130,21 @@ public void run() {
128130
}
129131
}
130132
}, 20*60);
133+
134+
getServer().getScheduler().runTaskLater(this, new Runnable() {
135+
136+
@Override
137+
public void run() {
138+
try {
139+
if (Updater.checkUpdate("19287", getDescription().getVersion())){
140+
Bukkit.getLogger().info(String.format("[%s] New update available!", plugin.getDescription().getName()));
141+
}
142+
} catch (IOException e) {
143+
Bukkit.getLogger().info(String.format("[%s] Cannot receive update from Spigot resource page!", plugin.getDescription().getName()));
144+
}
145+
}
146+
}, 20*10);
147+
131148
}
132149

133150
@Override
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.xxmicloxx.NoteBlockAPI.utils;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.net.MalformedURLException;
7+
import java.net.URL;
8+
import java.net.URLConnection;
9+
10+
public class Updater {
11+
12+
public static boolean checkUpdate(String resource, String actualVersion) throws MalformedURLException, IOException{
13+
Float version = getVersionNumber(actualVersion);
14+
15+
URLConnection con = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + resource).openConnection();
16+
String newVersionString = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
17+
int first = newVersionString.indexOf("(");
18+
String newVersion = newVersionString.substring(0, first);
19+
20+
Float newVer = getVersionNumber(newVersion);
21+
22+
return newVer > version;
23+
}
24+
25+
private static Float getVersionNumber(String version){
26+
String[] versionParts = version.split("\\.");
27+
String versionString = "0.";
28+
29+
for (String vpart : versionParts){
30+
if (vpart.length() < 2){
31+
versionString += "0";
32+
}
33+
versionString += vpart;
34+
}
35+
36+
return Float.parseFloat(versionString);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)