Skip to content

Commit 2353dad

Browse files
authored
feat: shorten
1 parent 5eba010 commit 2353dad

File tree

1 file changed

+48
-55
lines changed

1 file changed

+48
-55
lines changed
Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,99 @@
11
package net.potato.tuff;
22

3-
import com.google.gson.JsonObject;
4-
import com.google.gson.JsonParser;
3+
import com.google.gson.*;
54
import org.bukkit.Bukkit;
65
import org.bukkit.plugin.java.JavaPlugin;
76
import org.bukkit.scheduler.BukkitRunnable;
8-
9-
import java.io.File;
10-
import java.io.FileOutputStream;
11-
import java.io.InputStream;
12-
import java.io.InputStreamReader;
13-
import java.net.HttpURLConnection;
14-
import java.net.URL;
15-
import java.nio.channels.Channels;
16-
import java.nio.channels.ReadableByteChannel;
7+
import java.io.*;
8+
import java.net.*;
9+
import java.nio.channels.*;
1710

1811
public class Updater {
1912

20-
private final JavaPlugin plugin;
21-
private final String currentVersion;
13+
private final JavaPlugin p;
14+
private final String cv;
2215

23-
public Updater(JavaPlugin plugin) {
24-
this.plugin = plugin;
25-
this.currentVersion = plugin.getDescription().getVersion();
16+
public Updater(JavaPlugin pl) {
17+
p = pl;
18+
cv = pl.getDescription().getVersion();
2619
}
2720

2821
public void scheduleCheck() {
29-
if (!plugin.getConfig().getBoolean("updater.enabled", true)) return;
30-
long interval = plugin.getConfig().getLong("updater.check-interval-minutes", 60) * 60 * 20;
22+
if (!p.getConfig().getBoolean("updater.enabled", true)) return;
23+
long i = p.getConfig().getLong("updater.check-interval-minutes", 60) * 60 * 20;
3124
new BukkitRunnable() {
3225
@Override
3326
public void run() {
34-
checkForUpdates();
27+
cfu();
3528
}
36-
}.runTaskTimerAsynchronously(plugin, 0L, interval);
29+
}.runTaskTimerAsynchronously(p, 0L, i);
3730
}
3831

39-
private void checkForUpdates() {
40-
String localLatestVersion = currentVersion;
32+
private void cfu() {
33+
String llv = cv;
4134

42-
String versionUrl = "https://verytuffautoupdater.netlify.app/version-remote.json";
35+
String vu = "https://verytuffautoupdater.netlify.app/version-remote.json";
4336

4437
try {
45-
URL url = new URL(versionUrl);
46-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
47-
connection.setConnectTimeout(5000);
38+
URL u = new URL(vu);
39+
HttpURLConnection c = (HttpURLConnection) u.openConnection();
40+
c.setConnectTimeout(5000);
4841

49-
if (connection.getResponseCode() == 200) {
50-
InputStreamReader reader = new InputStreamReader(connection.getInputStream());
51-
JsonObject versionInfo = JsonParser.parseReader(reader).getAsJsonObject();
52-
reader.close();
42+
if (c.getResponseCode() == 200) {
43+
InputStreamReader r = new InputStreamReader(c.getInputStream());
44+
JsonObject vi = new JsonParser().parse(r).getAsJsonObject();
45+
r.close();
5346

54-
String webLatestVersion = versionInfo.get("latestVersion").getAsString();
47+
String wlv = vi.get("latestVersion").getAsString();
5548

56-
if (isNewer(webLatestVersion, currentVersion)) {
57-
plugin.getLogger().info("A new version is available: " + webLatestVersion + "! Downloading...");
58-
String downloadUrl = versionInfo.get("downloadUrl").getAsString();
59-
downloadUpdate(downloadUrl);
49+
if (in(wlv, cv)) {
50+
p.getLogger().info("A new version is available: " + wlv + "! Downloading...");
51+
String du = vi.get("downloadUrl").getAsString();
52+
du(du);
6053
} else {
61-
plugin.getLogger().info("You are running the latest version (" + currentVersion + ").");
54+
p.getLogger().info("You are running the latest version (" + cv + ").");
6255
}
6356
} else {
64-
plugin.getLogger().warning("Could not check for updates. (Response code: " + connection.getResponseCode() + ")");
57+
p.getLogger().warning("Could not check for updates. (Response code: " + c.getResponseCode() + ")");
6558
}
6659
} catch (Exception e) {
67-
plugin.getLogger().warning("An error occurred while checking for updates: " + e.getMessage());
60+
p.getLogger().warning("An error occurred while checking for updates: " + e.getMessage());
6861
}
6962
}
7063

71-
private boolean isNewer(String latest, String current) {
72-
return !latest.trim().equalsIgnoreCase(current);
64+
private boolean in(String l, String c) {
65+
return !l.trim().equalsIgnoreCase(c);
7366
}
7467

75-
private void downloadUpdate(String url) {
68+
private void du(String u) {
7669
try {
77-
File updateFolder = plugin.getServer().getUpdateFolderFile();
78-
if (!updateFolder.exists()) {
79-
updateFolder.mkdirs();
70+
File uf = p.getServer().getUpdateFolderFile();
71+
if (!uf.exists()) {
72+
uf.mkdirs();
8073
}
81-
File downloadTarget = new File(updateFolder, plugin.getName() + ".jar");
74+
File dt = new File(uf, p.getName() + ".jar");
8275

83-
URL downloadUrl = new URL(url);
84-
ReadableByteChannel rbc = Channels.newChannel(downloadUrl.openStream());
85-
FileOutputStream fos = new FileOutputStream(downloadTarget);
76+
URL dl = new URL(u);
77+
ReadableByteChannel rbc = Channels.newChannel(dl.openStream());
78+
FileOutputStream fos = new FileOutputStream(dt);
8679
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
8780
fos.close();
8881
rbc.close();
8982

90-
plugin.getLogger().info("Successfully downloaded the new version to the 'update' folder.");
91-
plugin.getLogger().info("The update will be applied on the next server restart.");
83+
p.getLogger().info("Successfully downloaded the new version to the 'update' folder.");
84+
p.getLogger().info("The update will be applied on the next server restart.");
9285

93-
if (plugin.getConfig().getBoolean("updater.restart-on-update", false)) {
94-
plugin.getLogger().info("Restarting server to apply the update...");
86+
if (p.getConfig().getBoolean("updater.restart-on-update", false)) {
87+
p.getLogger().info("Restarting server to apply the update...");
9588
new BukkitRunnable() {
9689
@Override
9790
public void run() {
9891
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "restart");
9992
}
100-
}.runTask(plugin);
93+
}.runTask(p);
10194
}
10295
} catch (Exception e) {
103-
plugin.getLogger().severe("The update download failed: " + e.getMessage());
96+
p.getLogger().severe("The update download failed: " + e.getMessage());
10497
}
10598
}
106-
}
99+
}

0 commit comments

Comments
 (0)