133 lines
6.6 KiB
Java
133 lines
6.6 KiB
Java
package net.t2code.t2codelib.SPIGOT.api.update;
|
|
|
|
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
|
|
import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain;
|
|
import net.t2code.t2codelib.SPIGOT.system.config.config.SelectLibConfig;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.plugin.Plugin;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
|
|
import java.util.HashMap;
|
|
|
|
public class T2CupdateAPI {
|
|
public static HashMap<String, T2CupdateObject> pluginVersions = new HashMap<>();
|
|
|
|
public static void join(Plugin plugin, String prefix, String perm, Player player, Integer spigotID, String discord) {
|
|
if (!SelectLibConfig.getUpdateCheckOnJoin()) {
|
|
return;
|
|
}
|
|
if (!player.hasPermission(perm) && !player.isOp()) {
|
|
return;
|
|
}
|
|
if (pluginVersions.get(plugin.getName()) == null) {
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
join(plugin, prefix, perm, player, spigotID, discord);
|
|
}
|
|
}.runTaskLaterAsynchronously(plugin, 20L);
|
|
return;
|
|
}
|
|
String publicVersion = pluginVersions.get(plugin.getName()).webData.getTag_name();
|
|
String pluginVersion = plugin.getDescription().getVersion();
|
|
if (pluginVersion.equals(publicVersion)) return;
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
sendUpdateMsg(prefix, spigotID, discord, plugin, player);
|
|
}
|
|
}.runTaskLaterAsynchronously(T2CodeLibMain.getPlugin(), 200L);
|
|
}
|
|
|
|
public static void sendUpdateMsg(String prefix, Integer spigotID, String discord, Plugin plugin, Player player) {
|
|
String publicVersion = pluginVersions.get(plugin.getName()).webData.getTag_name();
|
|
String pluginVersion = plugin.getDescription().getVersion();
|
|
if (publicVersion.equals("§4No public version found!")) {
|
|
return;
|
|
}
|
|
String st = "[prefix]<br>" +
|
|
"<click:open_url:'[link]'><hover:show_text:'<gold>You can download it here: <yellow>[link]</yellow></gold>'>[prefix] <gold>A new</gold> [value]<gold>version was found!</gold></hover></click><br>" +
|
|
"<click:open_url:'[link]'><hover:show_text:'<gold>You can download it here: <yellow>[link]</yellow></gold>'>[prefix] <red>[plv]</red> <gray>-></gray> <green>[puv]</green></hover></click><br>" +
|
|
"<click:open_url:'[dc]'><hover:show_text:'<yellow>[dc]</yellow>'>[prefix] <gold>You can find more information on Discord.</gold></hover></click><br>" +
|
|
"[prefix]";
|
|
String value = "";
|
|
if (publicVersion.toLowerCase().contains("dev") || publicVersion.toLowerCase().contains("beta") || publicVersion.toLowerCase().contains("snapshot")) {
|
|
if (publicVersion.toLowerCase().contains("dev")) {
|
|
value = "<dark_red>DEV </dark_red>";
|
|
}
|
|
if (publicVersion.toLowerCase().contains("beta")) {
|
|
value = "<green>BETA </green>";
|
|
}
|
|
if (publicVersion.toLowerCase().contains("snapshot")) {
|
|
value = "<yellow>SNAPSHOT </yellow>";
|
|
}
|
|
}
|
|
T2Csend.player(player, st.replace("[prefix]", prefix).replace("[value]", value).replace("[link]", "https://www.spigotmc.org/resources/" + spigotID)
|
|
.replace("[plv]", pluginVersion).replace("[puv]", publicVersion).replace("[dc]", discord));
|
|
}
|
|
|
|
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: §e" + data.getUpdateurl();
|
|
String s4 = "§6You can find more information on Discord: §e" + discord;
|
|
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";
|
|
}
|
|
if (publicVersion.toLowerCase().contains("beta")) {
|
|
s1 = "§6A new §2BETA§6 version was found!§r";
|
|
}
|
|
if (publicVersion.toLowerCase().contains("snapshot")) {
|
|
s1 = "§6A new §eSNAPSHOT§6 version was found!§r";
|
|
}
|
|
} else {
|
|
s1 = "§6A new version was found!§r";
|
|
}
|
|
String text = "<br>" + h + "<br>" + s1 + "<br>" + s2 + "<br>" + s3 + "<br>" + s4 + "<br>" + h;
|
|
T2Csend.console(text);
|
|
}
|
|
|
|
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)).getVersion((webData) -> {
|
|
T2CupdateObject update = new T2CupdateObject(
|
|
plugin.getName(),
|
|
plugin.getDescription().getVersion(),
|
|
webData,
|
|
false
|
|
);
|
|
pluginVersions.put(plugin.getName(), update);
|
|
if (!plugin.getDescription().getVersion().equalsIgnoreCase(webData.getTag_name())) {
|
|
if (!update.load) {
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
update.load = true;
|
|
sendUpdateMsg(prefix, webData, discord, plugin);
|
|
}
|
|
}.runTaskLaterAsynchronously(plugin, 600L);
|
|
} else sendUpdateMsg(prefix, webData, discord, plugin);
|
|
} else {
|
|
if (!update.load){
|
|
T2Csend.console(prefix + " §2No update found.");
|
|
update.load = true;
|
|
}
|
|
}
|
|
}, prefix, plugin.getDescription().getVersion(), finalRepoURL);
|
|
}
|
|
}.runTaskTimerAsynchronously(plugin, 0L, SelectLibConfig.getUpdateCheckTimeInterval() * 60 * 20L);
|
|
}
|
|
}
|