Small fixes
This commit is contained in:
parent
5d8a0139ab
commit
acadac3c2a
3 changed files with 96 additions and 66 deletions
|
@ -75,41 +75,15 @@ public final class ThemeParkPlus extends JavaPlugin {
|
||||||
|
|
||||||
Bukkit.getLogger().info("[ThemeParkPlus] Checking license...");
|
Bukkit.getLogger().info("[ThemeParkPlus] Checking license...");
|
||||||
|
|
||||||
|
final String license = config.getFile().getString("License");
|
||||||
if (config.getFile().contains("License")) {
|
if (config.getFile().contains("License")) {
|
||||||
Bukkit.getLogger().info("[ThemeParkPlus] Licence code: " + config.getFile().getString("License"));
|
Bukkit.getLogger().info("[ThemeParkPlus] Licence code: " + license);
|
||||||
} else {
|
} else {
|
||||||
Bukkit.getLogger().severe("[ThemeParkPlus] Licence code unknown! Please change the config.yml!");
|
Bukkit.getLogger().severe("[ThemeParkPlus] Licence code unknown! Please change the config.yml!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
new License(this, "TP", config.getFile().getString("License"));
|
new License(this, "TP", license);
|
||||||
|
|
||||||
if (getSConfig().getFile().getBoolean("UpdateChecker.Enabled")) {
|
|
||||||
UpdateManager updateManager = new UpdateManager(this, 6, UpdateManager.CheckType.SBDPLUGINS);
|
|
||||||
|
|
||||||
updateManager.handleResponse((versionResponse, version) -> {
|
|
||||||
if (versionResponse == UpdateManager.VersionResponse.FOUND_NEW) {
|
|
||||||
Bukkit.getLogger().warning("[ThemeParkPlus] There is a new version available! Current: " + this.getDescription().getVersion() + " New: " + version.get());
|
|
||||||
if (getSConfig().getFile().getBoolean("UpdateChecker.DownloadOnUpdate")) {
|
|
||||||
Bukkit.getLogger().info("[ThemeParkPlus] Trying to download the update...");
|
|
||||||
|
|
||||||
updateManager.handleDownloadResponse((downloadResponse, fileName) -> {
|
|
||||||
if (downloadResponse == UpdateManager.DownloadResponse.DONE) {
|
|
||||||
Bukkit.getLogger().info("[ThemeParkPlus] Update downloaded! If you restart your server, it will be loaded. Filename: " + fileName);
|
|
||||||
} else if (downloadResponse == UpdateManager.DownloadResponse.ERROR) {
|
|
||||||
Bukkit.getLogger().severe("[ThemeParkPlus] Something went wrong when trying downloading the latest version.");
|
|
||||||
} else if (downloadResponse == UpdateManager.DownloadResponse.UNAVAILABLE) {
|
|
||||||
Bukkit.getLogger().warning("[ThemeParkPlus] Unable to download the latest version.");
|
|
||||||
}
|
|
||||||
}).runUpdate();
|
|
||||||
}
|
|
||||||
} else if (versionResponse == UpdateManager.VersionResponse.LATEST) {
|
|
||||||
Bukkit.getLogger().info("[ThemeParkPlus] You are running the latest version [" + this.getDescription().getVersion() + "]!");
|
|
||||||
} else if (versionResponse == UpdateManager.VersionResponse.UNAVAILABLE) {
|
|
||||||
Bukkit.getLogger().severe("[ThemeParkPlus] Unable to perform an update check.");
|
|
||||||
}
|
|
||||||
}).check();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Bukkit.getPluginManager().getPlugin("ThemePark") == null) {
|
if (Bukkit.getPluginManager().getPlugin("ThemePark") == null) {
|
||||||
Bukkit.getLogger().severe("[ThemeParkPlus] Missing ThemePark! Please install it first.");
|
Bukkit.getLogger().severe("[ThemeParkPlus] Missing ThemePark! Please install it first.");
|
||||||
|
@ -176,6 +150,44 @@ public final class ThemeParkPlus extends JavaPlugin {
|
||||||
Bukkit.getLogger().info("[ThemeParkPlus] Loading metrics (can be disabled in the config of bStats)...");
|
Bukkit.getLogger().info("[ThemeParkPlus] Loading metrics (can be disabled in the config of bStats)...");
|
||||||
new Metrics(this, 5023);
|
new Metrics(this, 5023);
|
||||||
|
|
||||||
|
if (getSConfig().getFile().getBoolean("UpdateChecker.Enabled")) {
|
||||||
|
UpdateManager updateManager = new UpdateManager(this, 2, license);
|
||||||
|
|
||||||
|
updateManager.handleResponse((versionResponse, version) -> {
|
||||||
|
switch (versionResponse) {
|
||||||
|
case FOUND_NEW:
|
||||||
|
Bukkit.getLogger().warning("[ThemeParkPlus] There is a new version available! Current: " + this.getDescription().getVersion() + " New: " + version.get());
|
||||||
|
if (getSConfig().getFile().getBoolean("UpdateChecker.DownloadOnUpdate")) {
|
||||||
|
Bukkit.getLogger().info("[ThemeParkPlus] Trying to download the update. This could take some time...");
|
||||||
|
|
||||||
|
updateManager.handleDownloadResponse((downloadResponse, fileName) -> {
|
||||||
|
switch (downloadResponse) {
|
||||||
|
case DONE:
|
||||||
|
Bukkit.getLogger().info("[ThemeParkPlus] Update downloaded! If you restart your server, it will be loaded. Filename: " + fileName);
|
||||||
|
break;
|
||||||
|
case ERROR:
|
||||||
|
Bukkit.getLogger().severe("[ThemeParkPlus] Something went wrong when trying downloading the latest version.");
|
||||||
|
break;
|
||||||
|
case UNAVAILABLE:
|
||||||
|
Bukkit.getLogger().warning("[ThemeParkPlus] Unable to download the latest version.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}).runUpdate();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case LATEST:
|
||||||
|
Bukkit.getLogger().info("[ThemeParkPlus] You are running the latest version [" + this.getDescription().getVersion() + "]!");
|
||||||
|
break;
|
||||||
|
case THIS_NEWER:
|
||||||
|
Bukkit.getLogger().info("[ThemeParkPlus] You are running a newer version [" + this.getDescription().getVersion() + "]! This is probably fine.");
|
||||||
|
break;
|
||||||
|
case UNAVAILABLE:
|
||||||
|
Bukkit.getLogger().severe("[ThemeParkPlus] Unable to perform an update check.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}).check();
|
||||||
|
}
|
||||||
|
|
||||||
Bukkit.getLogger().info("[ThemeParkPlus] Plugin enabled!");
|
Bukkit.getLogger().info("[ThemeParkPlus] Plugin enabled!");
|
||||||
Bukkit.getLogger().info("[ThemeParkPlus] -------------------------------");
|
Bukkit.getLogger().info("[ThemeParkPlus] -------------------------------");
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package nl.sbdeveloper.themeparkplus.sbutils;
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
import nl.sbdeveloper.themeparkplus.ThemeParkPlus;
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
@ -25,39 +24,54 @@ import java.util.function.BiConsumer;
|
||||||
* Update class for SBDevelopment
|
* Update class for SBDevelopment
|
||||||
* @author Stijn [SBDeveloper]
|
* @author Stijn [SBDeveloper]
|
||||||
* @since 05-03-2020
|
* @since 05-03-2020
|
||||||
* @version 1.6
|
* @version 2.0 [26-12-2020] - This class supports the v2 Update API
|
||||||
*
|
*
|
||||||
* © Stijn Bannink <stijnbannink23@gmail.com> - All rights reserved.
|
* <p>© Stijn Bannink [stijnbannink23@gmail.com] - All rights reserved.</p>
|
||||||
*/
|
*/
|
||||||
public class UpdateManager {
|
public class UpdateManager {
|
||||||
|
|
||||||
private static final String SPIGOT_API = "https://api.spigotmc.org/legacy/update.php?resource=%d";
|
private static final String SPIGOT_API = "https://api.spigotmc.org/legacy/update.php?resource=%d";
|
||||||
private static final String SPIGOT_DOWNLOAD = "http://api.spiget.org/v2/resources/%s/download";
|
private static final String SPIGOT_DOWNLOAD = "http://api.spiget.org/v2/resources/%s/download";
|
||||||
|
|
||||||
/* Port 4000 is now legacy, 4443 has a SSL cert */
|
private static final String SBDPLUGINS_API = "https://updates.sbdplugins.nl/api/v2/plugins/%d";
|
||||||
/* As of 24-05-2020, using the legacy port because of SSL errors */
|
private static final String SBDPLUGINS_DOWNLOAD = "https://updates.sbdplugins.nl/api/v2/download/%d";
|
||||||
private static final String SBDPLUGINS_API = "http://updates.sbdplugins.nl:4000/api/resources/%d";
|
|
||||||
private static final String SBDPLUGINS_DOWNLOAD = "http://updates.sbdplugins.nl:4000/api/download/%d";
|
|
||||||
|
|
||||||
private final Plugin plugin;
|
private final Plugin plugin;
|
||||||
private final Version currentVersion;
|
private final Version currentVersion;
|
||||||
private final int resourceID;
|
private final int resourceID;
|
||||||
private final CheckType type;
|
private final CheckType type;
|
||||||
|
private final String license;
|
||||||
|
|
||||||
private BiConsumer<VersionResponse, Version> versionResponse;
|
private BiConsumer<VersionResponse, Version> versionResponse;
|
||||||
private BiConsumer<DownloadResponse, String> downloadResponse;
|
private BiConsumer<DownloadResponse, String> downloadResponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new UpdateManager
|
* Construct a new UpdateManager for Spigot
|
||||||
*
|
*
|
||||||
* @param plugin The javaplugin (Main class)
|
* @param plugin The javaplugin (Main class)
|
||||||
* @param resourceID The resourceID on spigot/sbdplugins
|
* @param resourceID The resourceID on spigot/sbdplugins
|
||||||
* @param type The check type
|
|
||||||
*/
|
*/
|
||||||
public UpdateManager(@NotNull Plugin plugin, int resourceID, CheckType type) {
|
public UpdateManager(@NotNull Plugin plugin, int resourceID) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.currentVersion = new Version(plugin.getDescription().getVersion());
|
this.currentVersion = new Version(plugin.getDescription().getVersion());
|
||||||
this.resourceID = resourceID;
|
this.resourceID = resourceID;
|
||||||
this.type = type;
|
this.type = CheckType.SPIGOT;
|
||||||
|
this.license = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a new UpdateManager for SBDPlugins
|
||||||
|
*
|
||||||
|
* @param plugin The javaplugin (Main class)
|
||||||
|
* @param resourceID The resourceID on spigot/sbdplugins
|
||||||
|
* @param license The license for the download
|
||||||
|
*/
|
||||||
|
public UpdateManager(@NotNull Plugin plugin, int resourceID, String license) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.currentVersion = new Version(plugin.getDescription().getVersion());
|
||||||
|
this.resourceID = resourceID;
|
||||||
|
this.type = CheckType.SBDPLUGINS;
|
||||||
|
this.license = license;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -89,7 +103,7 @@ public class UpdateManager {
|
||||||
|
|
||||||
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||||
} else if (type == CheckType.SBDPLUGINS) {
|
} else if (type == CheckType.SBDPLUGINS) {
|
||||||
HttpURLConnection con = (HttpURLConnection) new URL(String.format(SBDPLUGINS_API, this.resourceID)).openConnection();
|
HttpsURLConnection con = (HttpsURLConnection) new URL(String.format(SBDPLUGINS_API, this.resourceID)).openConnection();
|
||||||
con.setRequestMethod("GET");
|
con.setRequestMethod("GET");
|
||||||
con.setRequestProperty("User-Agent", "Mozilla/5.0");
|
con.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||||
|
|
||||||
|
@ -98,7 +112,7 @@ public class UpdateManager {
|
||||||
|
|
||||||
if (in == null) return;
|
if (in == null) return;
|
||||||
|
|
||||||
String version = null;
|
String version;
|
||||||
|
|
||||||
String inputLine;
|
String inputLine;
|
||||||
StringBuilder response = new StringBuilder();
|
StringBuilder response = new StringBuilder();
|
||||||
|
@ -113,10 +127,10 @@ public class UpdateManager {
|
||||||
JsonArray array = parser.parse(response.toString()).getAsJsonArray();
|
JsonArray array = parser.parse(response.toString()).getAsJsonArray();
|
||||||
|
|
||||||
version = array.get(0).getAsJsonObject().get("name").getAsString();
|
version = array.get(0).getAsJsonObject().get("name").getAsString();
|
||||||
} else if (type == CheckType.SBDPLUGINS) {
|
} else {
|
||||||
JsonObject object = parser.parse(response.toString()).getAsJsonObject();
|
JsonObject object = parser.parse(response.toString()).getAsJsonObject();
|
||||||
|
|
||||||
version = object.get("data").getAsJsonObject().get("version").getAsString();
|
version = object.get("version").getAsString();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (version == null) return;
|
if (version == null) return;
|
||||||
|
@ -134,7 +148,7 @@ public class UpdateManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void runUpdate() {
|
public void runUpdate() {
|
||||||
File pluginFile = getPluginFile();// /plugins/XXX.jar
|
File pluginFile = getPluginFile(); // /plugins/XXX.jar
|
||||||
if (pluginFile == null) {
|
if (pluginFile == null) {
|
||||||
this.downloadResponse.accept(DownloadResponse.ERROR, null);
|
this.downloadResponse.accept(DownloadResponse.ERROR, null);
|
||||||
Bukkit.getLogger().info("Pluginfile is null");
|
Bukkit.getLogger().info("Pluginfile is null");
|
||||||
|
@ -154,11 +168,12 @@ public class UpdateManager {
|
||||||
ReadableByteChannel channel;
|
ReadableByteChannel channel;
|
||||||
try {
|
try {
|
||||||
//https://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java
|
//https://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java
|
||||||
HttpURLConnection connection;
|
int response;
|
||||||
|
InputStream stream;
|
||||||
if (type == CheckType.SBDPLUGINS) {
|
if (type == CheckType.SBDPLUGINS) {
|
||||||
connection = (HttpURLConnection) new URL(String.format(SBDPLUGINS_DOWNLOAD, this.resourceID)).openConnection();
|
HttpURLConnection connection = (HttpURLConnection) new URL(String.format(SBDPLUGINS_DOWNLOAD, this.resourceID)).openConnection();
|
||||||
|
|
||||||
String urlParameters = "license=" + ThemeParkPlus.getSConfig().getFile().getString("License") + "&port=" + Bukkit.getPort();
|
String urlParameters = "license=" + license + "&port=" + Bukkit.getPort();
|
||||||
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
|
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
|
||||||
int postDataLength = postData.length;
|
int postDataLength = postData.length;
|
||||||
|
|
||||||
|
@ -172,27 +187,31 @@ public class UpdateManager {
|
||||||
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
|
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
|
||||||
wr.write(postData);
|
wr.write(postData);
|
||||||
wr.close();
|
wr.close();
|
||||||
|
|
||||||
|
response = connection.getResponseCode();
|
||||||
|
stream = connection.getInputStream();
|
||||||
} else {
|
} else {
|
||||||
connection = (HttpURLConnection) new URL(String.format(SPIGOT_DOWNLOAD, this.resourceID)).openConnection();
|
HttpsURLConnection connection = (HttpsURLConnection) new URL(String.format(SPIGOT_DOWNLOAD, this.resourceID)).openConnection();
|
||||||
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
|
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||||
|
|
||||||
|
response = connection.getResponseCode();
|
||||||
|
stream = connection.getInputStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (connection.getResponseCode() != 200) {
|
if (response != 200) {
|
||||||
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
|
||||||
|
|
||||||
String inputLine;
|
String inputLine;
|
||||||
StringBuilder response = new StringBuilder();
|
StringBuilder responsestr = new StringBuilder();
|
||||||
while ((inputLine = in.readLine()) != null) {
|
while ((inputLine = in.readLine()) != null) {
|
||||||
response.append(inputLine);
|
responsestr.append(inputLine);
|
||||||
}
|
}
|
||||||
in.close();
|
in.close();
|
||||||
|
|
||||||
Bukkit.getLogger().info(response.toString());
|
throw new RuntimeException("Download returned status #" + response, new Throwable(responsestr.toString()));
|
||||||
|
|
||||||
throw new RuntimeException("Download returned status #" + connection.getResponseCode());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
channel = Channels.newChannel(connection.getInputStream());
|
channel = Channels.newChannel(stream);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Bukkit.getScheduler().runTask(this.plugin, () -> this.downloadResponse.accept(DownloadResponse.ERROR, null));
|
Bukkit.getScheduler().runTask(this.plugin, () -> this.downloadResponse.accept(DownloadResponse.ERROR, null));
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -243,7 +262,7 @@ public class UpdateManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum CheckType {
|
private enum CheckType {
|
||||||
SPIGOT, SBDPLUGINS
|
SPIGOT, SBDPLUGINS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -260,13 +279,13 @@ public class UpdateManager {
|
||||||
|
|
||||||
public static class Version {
|
public static class Version {
|
||||||
|
|
||||||
private String version;
|
private final String version;
|
||||||
|
|
||||||
public final String get() {
|
public final String get() {
|
||||||
return this.version;
|
return this.version;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Version(String version) {
|
private Version(String version) {
|
||||||
if(version == null)
|
if(version == null)
|
||||||
throw new IllegalArgumentException("Version can not be null");
|
throw new IllegalArgumentException("Version can not be null");
|
||||||
if(!version.matches("[0-9]+(\\.[0-9]+)*"))
|
if(!version.matches("[0-9]+(\\.[0-9]+)*"))
|
||||||
|
@ -274,7 +293,7 @@ public class UpdateManager {
|
||||||
this.version = version;
|
this.version = version;
|
||||||
}
|
}
|
||||||
|
|
||||||
public VersionResponse check(@NotNull Version that) {
|
private VersionResponse check(@NotNull Version that) {
|
||||||
String[] thisParts = this.get().split("\\.");
|
String[] thisParts = this.get().split("\\.");
|
||||||
String[] thatParts = that.get().split("\\.");
|
String[] thatParts = that.get().split("\\.");
|
||||||
|
|
||||||
|
|
|
@ -88,12 +88,12 @@ public class License implements Listener {
|
||||||
try {
|
try {
|
||||||
response = sendGETRequestJSON(url);
|
response = sendGETRequestJSON(url);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
disable("Couldn't send the request.");
|
disable("Something went wrong while sending the request.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response == null) {
|
if (response == null) {
|
||||||
disable("Couldn't send the request.");
|
disable("Something went wrong while sending the request. Did you even fill out a license?");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,12 +175,12 @@ public class License implements Listener {
|
||||||
try {
|
try {
|
||||||
response = sendGETRequestJSON(url);
|
response = sendGETRequestJSON(url);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
disable("Couldn't send the activate request.");
|
disable("Something went wrong while sending the request.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response == null) {
|
if (response == null) {
|
||||||
disable("Couldn't send the activate request.");
|
disable("Something went wrong while sending the request. Did you even fill out a license?");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -241,7 +241,6 @@ public class License implements Listener {
|
||||||
con.setRequestProperty("Authorization", "Basic " + authStringEnc);
|
con.setRequestProperty("Authorization", "Basic " + authStringEnc);
|
||||||
int code = con.getResponseCode();
|
int code = con.getResponseCode();
|
||||||
if (code == 404) {
|
if (code == 404) {
|
||||||
disable("404_error");
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue