T2CodeLib/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateCheckerGit.java

93 lines
3.9 KiB
Java

package net.t2code.t2codelib.SPIGOT.api.update;
import net.t2code.t2codelib.T2CupdateObject;
import net.t2code.t2codelib.T2CupdateWebData;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.function.Consumer;
public class T2CupdateCheckerGit {
private JavaPlugin plugin;
public T2CupdateCheckerGit(JavaPlugin plugin) {
this.plugin = plugin;
}
public void getVersion(Consumer<T2CupdateWebData> consumer, String pluginVersion, Integer spigotID, String URL, Boolean updateCheckOnJoin, Boolean seePreReleaseUpdates, Integer timeInterval) {
if (!plugin.isEnabled()) {
return;
}
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
try {
URL url = new URL(URL);
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
String data = "";
while ((inputLine = in.readLine()) != null)
data = inputLine;
in.close();
data = data.substring(1, data.length() - 1);
if (data.isEmpty()) {
consumer.accept(null);
return;
}
JSONObject obj = new JSONObject(data);
String UpdateName = obj.getString("name");
String tag_name = obj.getString("tag_name");
String body = obj.getString("body").replace("\n", "<br>").replace("\r", "").replace("'", "''").replace("**", "");
String updateurl = obj.getString("html_url");
boolean prerelease = obj.getBoolean("prerelease");
String date = obj.getString("published_at");
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
Date parsedDate = inputFormat.parse(date);
String formattedDate = outputFormat.format(parsedDate);
JSONArray downloadArray = obj.getJSONArray("assets");
String downloadURL;
if (downloadArray.isEmpty()) {
downloadURL = "https://www.spigotmc.org/resources/" + spigotID;
} else {
downloadURL = downloadArray.getJSONObject(0).getString("browser_download_url");
}
if (!prerelease) {
downloadURL = "https://www.spigotmc.org/resources/" + spigotID;
updateurl = "https://www.spigotmc.org/resources/" + spigotID;
}
T2CupdateWebData webData = new T2CupdateWebData(UpdateName, tag_name, body, updateurl, formattedDate, downloadURL, prerelease);
consumer.accept(webData);
} catch (Exception var10) {
Boolean load = false;
if (T2CupdateAPI.pluginVersions.containsKey(plugin.getName())) {
load = T2CupdateAPI.pluginVersions.get(plugin.getName()).load;
}
T2CupdateObject update = new T2CupdateObject(
plugin.getName(),
pluginVersion,
null,
load,
false,
updateCheckOnJoin
);
T2CupdateAPI.pluginVersions.put(plugin.getName(), update);
this.plugin.getLogger().severe("§4 Cannot look for updates: " + var10.getMessage());
}
});
}
}