T2CodeLib/src/main/java/net/t2code/t2codelib/BUNGEE/api/update/T2CBupdateAPI.java

145 lines
6.9 KiB
Java

package net.t2code.t2codelib.BUNGEE.api.update;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.Plugin;
import net.t2code.t2codelib.BUNGEE.api.messages.T2CBsend;
import net.t2code.t2codelib.T2CupdateObject;
import net.t2code.t2codelib.T2CupdateWebData;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
public class T2CBupdateAPI {
public static HashMap<String, T2CupdateObject> bungeePluginVersins = new HashMap<>();
public static void sendUpdateMsg(String Prefix, String Spigot, String Discord, String foundVersion, String update_version) {
T2CBsend.console("§4=========== " + Prefix + " §4===========");
T2CBsend.console("§6A new version was found!");
T2CBsend.console("§6Your version: §c" + foundVersion + " §7- §6Current version: §a" + update_version);
T2CBsend.console("§6You can download it here: §e" + Spigot);
T2CBsend.console("§6You can find more information on Discord: §e" + Discord);
T2CBsend.console("§4=========== " + Prefix + " §4===========");
}
public static void sendUpdateMsg(String Prefix, String Spigot, String Discord, String foundVersion, String update_version, CommandSender sender) {
T2CBsend.sender(sender, "§4=========== " + Prefix + " §4===========");
T2CBsend.sender(sender, "§6A new version was found!");
T2CBsend.sender(sender, "§6Your version: §c" + foundVersion + " §7- §6Current version: §a" + update_version);
T2CBsend.sender(sender, "§6You can download it here: §e" + Spigot);
T2CBsend.sender(sender, "§6You can find more information on Discord: §e" + Discord);
T2CBsend.sender(sender, "§4=========== " + Prefix + " §4===========");
}
private static Boolean noUpdate = true;
private static String pluginVersion;
public static void onUpdateCheckTimer(Plugin plugin, String Prefix, String Spigot, String Discord, Integer SpigotID, String url) {
ProxyServer.getInstance().getScheduler().schedule(plugin, new Runnable() {
public void run() {
(new T2CBupdateAPI(plugin, SpigotID)).getVersion((update_version) -> {
pluginVersion = plugin.getDescription().getVersion();
T2CupdateObject update = new T2CupdateObject(
plugin.getDescription().getName(),
plugin.getDescription().getVersion(),
update_version,
false,
!plugin.getDescription().getVersion().equals(update_version.getVersion())
);
bungeePluginVersins.put(plugin.getDescription().getName(), update);
if (!pluginVersion.replace("_Bungee", "").equalsIgnoreCase(update_version.getVersion())) {
sendUpdateMsg(Prefix, Spigot, Discord, pluginVersion, update_version.getVersion());
noUpdate = true;
} else {
if (noUpdate) {
T2CBsend.console(Prefix + " §2No update found.");
noUpdate = false;
}
}
}, pluginVersion, SpigotID, url);
}
}, 0, 20 * 60 * 60L, TimeUnit.SECONDS);
}
private Plugin plugin;
private int resourceId;
public T2CBupdateAPI(Plugin plugin, int resourceId) {
this.plugin = plugin;
this.resourceId = resourceId;
}
public void getVersion(Consumer<T2CupdateWebData> consumer, String pluginVersion, Integer spigotID, String URL) {
BungeeCord.getInstance().getScheduler().runAsync(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("'", "''");
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 (bungeePluginVersins.containsKey(plugin.getDescription().getName())) {
load = bungeePluginVersins.get(plugin.getDescription().getName()).load;
}
T2CupdateObject update = new T2CupdateObject(
plugin.getDescription().getName(),
pluginVersion,
null,
load,
false
);
bungeePluginVersins.put(plugin.getDescription().getName(), update);
this.plugin.getLogger().severe("§4 Cannot look for updates: " + var10.getMessage());
}
});
}
}