New Updater

This commit is contained in:
Jkobs 2022-11-10 11:28:55 +01:00
parent 14c520722a
commit 8034d8852a
7 changed files with 106 additions and 58 deletions

13
pom.xml
View File

@ -147,5 +147,18 @@
<artifactId>adventure-platform-bukkit</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
</dependency>
</dependencies>
</project>

View File

@ -82,7 +82,7 @@ public class T2Ctemplate {
public static void sendInfo(CommandSender sender, Plugin plugin, int spigotID, String discord, Boolean premiumVerified, String text) {
String pluginVersion = plugin.getDescription().getVersion();
String publicVersion = T2CupdateAPI.pluginVersions.get(plugin.getName()).publicVersion;
String publicVersion = T2CupdateAPI.pluginVersions.get(plugin.getName()).webData.getTag_name();
boolean update = !publicVersion.equalsIgnoreCase(pluginVersion);
boolean player = sender instanceof Player;

View File

@ -29,7 +29,7 @@ public class T2CupdateAPI {
}.runTaskLaterAsynchronously(plugin, 20L);
return;
}
String publicVersion = pluginVersions.get(plugin.getName()).publicVersion;
String publicVersion = pluginVersions.get(plugin.getName()).webData.getTag_name();
String pluginVersion = plugin.getDescription().getVersion();
if (pluginVersion.equals(publicVersion)) return;
new BukkitRunnable() {
@ -41,7 +41,7 @@ public class T2CupdateAPI {
}
public static void sendUpdateMsg(String prefix, Integer spigotID, String discord, Plugin plugin, Player player) {
String publicVersion = pluginVersions.get(plugin.getName()).publicVersion;
String publicVersion = pluginVersions.get(plugin.getName()).webData.getTag_name();
String pluginVersion = plugin.getDescription().getVersion();
if (publicVersion.equals("§4No public version found!")) {
return;
@ -67,15 +67,16 @@ public class T2CupdateAPI {
.replace("[plv]", pluginVersion).replace("[puv]", publicVersion).replace("[dc]", discord));
}
public static void sendUpdateMsg(String prefix, Integer spigot, String discord, Plugin plugin) {
String publicVersion = pluginVersions.get(plugin.getName()).publicVersion;
public static void sendUpdateMsg(String prefix, T2CupdateWebData data, String discord, Plugin plugin) {
String publicVersion = pluginVersions.get(plugin.getName()).webData.getTag_name();
String pluginVersion = plugin.getDescription().getVersion();
String h = "§4=========== " + prefix + " §4===========";
String s1 = "";
String s2 = "§6Your version: §c" + pluginVersion + " §7- §6Current version: §a" + publicVersion;
String s3 = "§6You can download it here: §ehttps://www.spigotmc.org/resources/" + spigot;
String s3 = "§6You can download it here: §e" + data.getUpdateurl();
String s4 = "§6You can find more information on Discord: §e" + discord;
if (publicVersion.toLowerCase().contains("dev") || publicVersion.toLowerCase().contains("beta") || publicVersion.toLowerCase().contains("snapshot")) {
if (data.isPrerelease()) {
s1 = "§6A new §4Pre-Release§6 version was found!§r";
if (publicVersion.toLowerCase().contains("dev")) {
s1 = "§6A new §4DEV§6 version was found!§r";
}
@ -92,35 +93,39 @@ public class T2CupdateAPI {
T2Csend.console(text);
}
public static void onUpdateCheck(Plugin plugin, String prefix, int spigotID, String discord) {
public static void onUpdateCheck(Plugin plugin, String prefix, String discord, String RepoURL) {
if(!RepoURL.contains("?limit=1")){
RepoURL = RepoURL + "?limit=1";
}
String finalRepoURL = RepoURL;
new BukkitRunnable() {
@Override
public void run() {
(new T2CupdateChecker((JavaPlugin) plugin, spigotID)).getVersion((update_version) -> {
(new T2CupdateChecker((JavaPlugin) plugin)).getVersion((webData) -> {
T2CupdateObject update = new T2CupdateObject(
plugin.getName(),
plugin.getDescription().getVersion(),
update_version,
webData,
false
);
pluginVersions.put(plugin.getName(), update);
if (!plugin.getDescription().getVersion().equalsIgnoreCase(update_version)) {
if (!plugin.getDescription().getVersion().equalsIgnoreCase(webData.getTag_name())) {
if (!update.load) {
new BukkitRunnable() {
@Override
public void run() {
update.load = true;
sendUpdateMsg(prefix, spigotID, discord, plugin);
sendUpdateMsg(prefix, webData, discord, plugin);
}
}.runTaskLaterAsynchronously(plugin, 600L);
} else sendUpdateMsg(prefix, spigotID, discord, plugin);
} else sendUpdateMsg(prefix, webData, discord, plugin);
} else {
if (!update.load){
T2Csend.console(prefix + " §2No update found.");
update.load = true;
}
}
}, prefix, plugin.getDescription().getVersion());
}, prefix, plugin.getDescription().getVersion(), finalRepoURL);
}
}.runTaskTimerAsynchronously(plugin, 0L, SelectLibConfig.getUpdateCheckTimeInterval() * 60 * 20L);
}

View File

@ -2,62 +2,63 @@ package net.t2code.t2codelib.SPIGOT.api.update;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URL;
import java.util.Scanner;
import java.net.URLConnection;
import java.util.function.Consumer;
public class T2CupdateChecker {
private JavaPlugin plugin;
private int resourceId;
public T2CupdateChecker(JavaPlugin plugin, int resourceId) {
public T2CupdateChecker(JavaPlugin plugin) {
this.plugin = plugin;
this.resourceId = resourceId;
}
public void getVersion(Consumer<String> consumer, String Prefix, String pluginVersion) {
public void getVersion(Consumer<T2CupdateWebData> consumer, String Prefix, String pluginVersion, String URL) {
if (!plugin.isEnabled()) {
return;
}
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
try {
InputStream inputStream = (new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId)).openStream();
try {
Scanner scanner = new Scanner(inputStream);
try {
if (scanner.hasNext()) {
consumer.accept(scanner.next());
}
} catch (Throwable var8) {
try {
scanner.close();
} catch (Throwable var7) {
var8.addSuppressed(var7);
}
throw var8;
}
scanner.close();
} catch (Throwable var9) {
if (inputStream != null) {
try {
inputStream.close();
} catch (Throwable var6) {
var9.addSuppressed(var6);
}
}
throw var9;
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;
}
inputStream.close();
} catch (IOException var10) {
JSONObject obj = new JSONObject(data);
String UpdateName = obj.getString("name");
String tag_name = obj.getString("tag_name");
String body = obj.getString("body");
String updateurl = obj.getString("url");
boolean prerelease = obj.getBoolean("prerelease");
String published_at = obj.getString("published_at");
JSONArray downloadArray = obj.getJSONArray("assets");
String downloadURL;
if (downloadArray.isEmpty()) {
downloadURL = "";
} else {
downloadURL = downloadArray.getJSONObject(0).getString("browser_download_url");
}
T2CupdateWebData webData = new T2CupdateWebData(UpdateName, tag_name, body, updateurl, published_at, downloadURL, prerelease);
consumer.accept(webData);
} catch (Exception var10) {
Boolean load = false;
if (T2CupdateAPI.pluginVersions.containsKey(plugin.getName())){
if (T2CupdateAPI.pluginVersions.containsKey(plugin.getName())) {
load = T2CupdateAPI.pluginVersions.get(plugin.getName()).load;
}
T2CupdateObject update = new T2CupdateObject(
plugin.getName(),
pluginVersion,
@ -69,4 +70,5 @@ public class T2CupdateChecker {
}
});
}
}

View File

@ -5,19 +5,16 @@ public class T2CupdateObject {
public String pluginName;
public String pluginVersion;
public String publicVersion;
public String changeLog;
public T2CupdateWebData webData;
public Boolean load;
public T2CupdateObject(String pluginName,
String pluginVersion,
String publicVersion ,
String changeLog ,
T2CupdateWebData webData ,
Boolean load) {
this.pluginName = pluginName;
this.pluginVersion = pluginVersion;
this.publicVersion = publicVersion;
this.changeLog = changeLog;
this.webData = webData;
this.load=load;
}
}

View File

@ -0,0 +1,31 @@
package net.t2code.t2codelib.SPIGOT.api.update;
import lombok.Getter;
public class T2CupdateWebData {
@Getter
private String UpdateName;
@Getter
private String tag_name;
@Getter
private String body;
@Getter
private String updateurl;
@Getter
private String published_at;
@Getter
private String downloadURL;
@Getter
private boolean prerelease;
public T2CupdateWebData(String updateName, String tag_name, String body, String updateurl, String published_at, String downloadURL, boolean prerelease) {
UpdateName = updateName;
this.tag_name = tag_name;
this.body = body;
this.updateurl = updateurl;
this.published_at = published_at;
this.downloadURL = downloadURL;
this.prerelease = prerelease;
}
}

View File

@ -90,7 +90,7 @@ public final class T2CodeLibMain extends JavaPlugin {
SelectLibConfig.onSelect();
SelectLibMsg.onSelect();
T2CupdateAPI.onUpdateCheck(plugin, prefix, Util.getSpigotID(), Util.getDiscord());
T2CupdateAPI.onUpdateCheck(plugin, prefix, Util.getDiscord(), "https://git.t2code.net/api/v1/repos/JaTiTV/T2CodeLib/releases?limit=1");
Metrics.Bstats(plugin, Util.getBstatsID());
if (SelectLibConfig.getBungee()){
Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "t2c:bcmd");