124 lines
6.3 KiB
Java
124 lines
6.3 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()).publicVersion;
|
|
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()).publicVersion;
|
|
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, Integer spigot, String discord, Plugin plugin) {
|
|
String publicVersion = pluginVersions.get(plugin.getName()).publicVersion;
|
|
String pluginVersion = plugin.getDescription().getVersion();
|
|
T2Csend.console("§4=========== " + prefix + " §4===========");
|
|
if (publicVersion.toLowerCase().contains("dev") || publicVersion.toLowerCase().contains("beta") || publicVersion.toLowerCase().contains("snapshot")) {
|
|
if (publicVersion.toLowerCase().contains("dev")) {
|
|
T2Csend.console("§6A new §4DEV§6 version was found!");
|
|
}
|
|
if (publicVersion.toLowerCase().contains("beta")) {
|
|
T2Csend.console("§6A new §2BETA§6 version was found!");
|
|
}
|
|
if (publicVersion.toLowerCase().contains("snapshot")) {
|
|
T2Csend.console("§6A new §eSNAPSHOT§6 version was found!");
|
|
}
|
|
} else {
|
|
T2Csend.console("§6A new version was found!");
|
|
}
|
|
T2Csend.console("§6Your version: §c" + pluginVersion + " §7- §6Current version: §a" + publicVersion);
|
|
T2Csend.console("§6You can download it here: §ehttps://www.spigotmc.org/resources/" + spigot);
|
|
T2Csend.console("§6You can find more information on Discord: §e" + discord);
|
|
T2Csend.console("§4=========== " + prefix + " §4===========");
|
|
}
|
|
|
|
private static Boolean load = false;
|
|
|
|
public static void onUpdateCheck(Plugin plugin, String prefix, int spigotID, String discord) {
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
(new T2CupdateChecker((JavaPlugin) plugin, spigotID)).getVersion((update_version) -> {
|
|
T2CupdateObject update = new T2CupdateObject(
|
|
plugin.getName(),
|
|
plugin.getDescription().getVersion(),
|
|
update_version
|
|
);
|
|
pluginVersions.put(plugin.getName(), update);
|
|
if (!plugin.getDescription().getVersion().equalsIgnoreCase(update_version)) {
|
|
if (!load) {
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
load = true;
|
|
sendUpdateMsg(prefix, spigotID, discord, plugin);
|
|
}
|
|
}.runTaskLaterAsynchronously(plugin, 600L);
|
|
} else sendUpdateMsg(prefix, spigotID, discord, plugin);
|
|
} else {
|
|
T2Csend.console(prefix + " §2No update found.");
|
|
}
|
|
}, prefix, plugin.getDescription().getVersion());
|
|
}
|
|
}.runTaskTimerAsynchronously(plugin, 0L, SelectLibConfig.getUpdateCheckTimeInterval() * 60 * 20L);
|
|
}
|
|
}
|