13.0
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
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);
|
||||
}
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
package net.t2code.t2codelib.SPIGOT.api.update;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.Scanner;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class T2CupdateChecker {
|
||||
private JavaPlugin plugin;
|
||||
private int resourceId;
|
||||
|
||||
public T2CupdateChecker(JavaPlugin plugin, int resourceId) {
|
||||
this.plugin = plugin;
|
||||
this.resourceId = resourceId;
|
||||
}
|
||||
|
||||
public void getVersion(Consumer<String> consumer, String Prefix, String pluginVersion) {
|
||||
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;
|
||||
}
|
||||
inputStream.close();
|
||||
} catch (IOException var10) {
|
||||
T2CupdateObject update = new T2CupdateObject(
|
||||
plugin.getName(),
|
||||
pluginVersion,
|
||||
null
|
||||
);
|
||||
T2CupdateAPI.pluginVersions.put(plugin.getName(), update);
|
||||
this.plugin.getLogger().severe("§4 Cannot look for updates: " + var10.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package net.t2code.t2codelib.SPIGOT.api.update;
|
||||
|
||||
public class T2CupdateObject {
|
||||
|
||||
public String pluginName;
|
||||
public String pluginVersion;
|
||||
public String publicVersion;
|
||||
|
||||
public T2CupdateObject(String pluginName,
|
||||
String pluginVersion,
|
||||
String publicVersion ) {
|
||||
this.pluginName = pluginName;
|
||||
this.pluginVersion = pluginVersion;
|
||||
this.publicVersion = publicVersion;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user