Skip to content

Commit b68ba7b

Browse files
committed
Update checkers
1 parent 717a22e commit b68ba7b

File tree

5 files changed

+251
-33
lines changed

5 files changed

+251
-33
lines changed

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

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

55
group 'me.dkim19375'
6-
version '1.0.0'
6+
version '1.1.0'
77

88
repositories {
99
mavenCentral()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package me.dkim19375.dkim19375core.external;
2+
3+
import com.google.common.io.ByteStreams;
4+
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
6+
import com.google.gson.JsonParser;
7+
import org.bukkit.Bukkit;
8+
import org.bukkit.plugin.java.JavaPlugin;
9+
import org.bukkit.util.Consumer;
10+
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
import java.net.URL;
14+
15+
public class JsonUtils {
16+
private JsonUtils() {
17+
18+
}
19+
20+
public static void getJson(final Consumer<JsonObject> consumer, final Consumer<IOException> exception, URL url, JavaPlugin plugin) {
21+
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
22+
String string;
23+
try {
24+
InputStream inputStream = url.openStream();
25+
//noinspection UnstableApiUsage
26+
byte[] byteArray = ByteStreams.toByteArray(inputStream);
27+
string = new String(byteArray);
28+
} catch (IOException e) {
29+
exception.accept(e);
30+
return;
31+
}
32+
final JsonParser parse = new JsonParser();
33+
final JsonElement json = parse.parse(string);
34+
final JsonObject jsonObject = json.getAsJsonObject();
35+
consumer.accept(jsonObject);
36+
});
37+
}
38+
39+
public static void getJson(final Consumer<JsonObject> consumer, final Consumer<IOException> exception, String url, JavaPlugin plugin) {
40+
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
41+
String string;
42+
try {
43+
InputStream inputStream = new URL(url).openStream();
44+
//noinspection UnstableApiUsage
45+
byte[] byteArray = ByteStreams.toByteArray(inputStream);
46+
string = new String(byteArray);
47+
} catch (IOException e) {
48+
exception.accept(e);
49+
return;
50+
}
51+
final JsonParser parse = new JsonParser();
52+
final JsonElement json = parse.parse(string);
53+
final JsonObject jsonObject = json.getAsJsonObject();
54+
consumer.accept(jsonObject);
55+
});
56+
}
57+
}

src/main/java/me/dkim19375/dkim19375core/external/SpigotUpdateChecker.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package me.dkim19375.dkim19375core.external;
2+
3+
import com.google.gson.JsonElement;
4+
import org.bukkit.Bukkit;
5+
import org.bukkit.plugin.java.JavaPlugin;
6+
import org.bukkit.util.Consumer;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.net.URL;
11+
import java.util.Scanner;
12+
import java.util.concurrent.CompletableFuture;
13+
14+
public class UpdateChecker {
15+
private final String resourceId;
16+
private final URL url;
17+
private final JavaPlugin plugin;
18+
19+
public UpdateChecker(final String resourceId, final URL url, final JavaPlugin plugin) {
20+
this.resourceId = resourceId;
21+
this.url = url;
22+
this.plugin = plugin;
23+
}
24+
25+
public UpdateChecker(final String resourceId, final JavaPlugin plugin) {
26+
this.resourceId = resourceId;
27+
url = null;
28+
this.plugin = plugin;
29+
}
30+
31+
public UpdateChecker(final URL url, final JavaPlugin plugin) {
32+
resourceId = null;
33+
this.url = url;
34+
this.plugin = plugin;
35+
}
36+
37+
public void getSpigotVersion(final Consumer<String> version, final Consumer<IOException> exception) {
38+
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
39+
try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId).openStream();
40+
Scanner scanner = new Scanner(inputStream)) {
41+
if (scanner.hasNext()) {
42+
version.accept(scanner.next());
43+
}
44+
} catch (IOException e) {
45+
exception.accept(e);
46+
}
47+
});
48+
}
49+
public void getFromRaw(final Consumer<String> version, final Consumer<IOException> exception) {
50+
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
51+
try (InputStream inputStream = url.openStream();
52+
Scanner scanner = new Scanner(inputStream)) {
53+
if (scanner.hasNext()) {
54+
version.accept(scanner.next());
55+
}
56+
} catch (IOException e) {
57+
exception.accept(e);
58+
}
59+
});
60+
}
61+
62+
public void getFromGithubJson(final Consumer<String> version, final Consumer<IOException> exception) {
63+
JsonUtils.getJson((j) -> {
64+
JsonElement element = j.get("tag_name");
65+
String tag = element.getAsString();
66+
version.accept(tag);
67+
}, exception, url, plugin);
68+
}
69+
}

0 commit comments

Comments
 (0)