mirror of
https://github.com/rockyhawk64/CommandPanels.git
synced 2025-11-18 07:14:17 +01:00
Updater using plugin.yml
This commit is contained in:
parent
f24a8057c7
commit
189539241f
@ -1,6 +1,5 @@
|
||||
package me.rockyhawk.commandpanels.updater;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import me.rockyhawk.commandpanels.CommandPanels;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
@ -12,12 +11,9 @@ import org.bukkit.scheduler.BukkitRunnable;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class Updater implements Listener {
|
||||
//if this is set to something, it will download that version on restart
|
||||
//can be a version number, 'latest' or 'cancel'
|
||||
@ -25,7 +21,6 @@ public class Updater implements Listener {
|
||||
public String cachedLatestVersion = "null";
|
||||
|
||||
CommandPanels plugin;
|
||||
|
||||
public Updater(CommandPanels pl) {
|
||||
this.plugin = pl;
|
||||
}
|
||||
@ -54,7 +49,7 @@ public class Updater implements Listener {
|
||||
//refresh latest version
|
||||
getLatestVersion(sendMessages);
|
||||
|
||||
if (plugin.getDescription().getVersion().contains("-")) {
|
||||
if(plugin.getDescription().getVersion().contains("SNAPSHOT")){
|
||||
if(sendMessages) {
|
||||
Bukkit.getConsoleSender().sendMessage("[CommandPanels]" + ChatColor.GREEN + " Running a custom version.");
|
||||
}
|
||||
@ -78,54 +73,33 @@ public class Updater implements Listener {
|
||||
}
|
||||
|
||||
public String getLatestVersion(boolean sendMessages){
|
||||
// Default to current version if the cached version is null
|
||||
//check for null
|
||||
if(cachedLatestVersion.equals("null")){
|
||||
cachedLatestVersion = plugin.getDescription().getVersion();
|
||||
}
|
||||
|
||||
//using an array allows editing while still being final
|
||||
new BukkitRunnable(){
|
||||
public void run(){
|
||||
HttpURLConnection connection;
|
||||
try {
|
||||
// Connect to the MC_Versions.json file
|
||||
connection = (HttpURLConnection) new URL("https://raw.githubusercontent.com/rockyhawk64/CommandPanels/latest/MC_Versions.json").openConnection();
|
||||
connection.setConnectTimeout(5000); // 5 seconds timeout
|
||||
connection.setReadTimeout(5000); // 5 seconds read timeout
|
||||
connection = (HttpURLConnection) new URL("https://raw.githubusercontent.com/rockyhawk64/CommandPanels/latest/resource/plugin.yml").openConnection();
|
||||
connection.setConnectTimeout(5000); // 5 seconds
|
||||
connection.setReadTimeout(5000); // 5 seconds
|
||||
connection.connect();
|
||||
|
||||
// Read the response into a String
|
||||
StringBuilder response = new StringBuilder();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
response.append(line);
|
||||
}
|
||||
|
||||
// Parse the JSON response using Gson
|
||||
Gson gson = new Gson();
|
||||
JsonObject jsonResponse = gson.fromJson(response.toString(), JsonObject.class);
|
||||
|
||||
// Get the Minecraft version from the current server version
|
||||
// Eg "1.21.5-25-def0532 (MC: 1.21.5)"
|
||||
String mcVersion = Bukkit.getVersion().split("\\s")[2].replace(")","");
|
||||
|
||||
// Check if the Minecraft version exists in the JSON
|
||||
if (jsonResponse.has(mcVersion)) {
|
||||
JsonArray versions = jsonResponse.getAsJsonArray(mcVersion);
|
||||
if (versions.size() > 0) { //Gson from older MC versions does not have isEmpty()
|
||||
// Get the latest plugin version
|
||||
cachedLatestVersion = versions.get(0).getAsString();
|
||||
}
|
||||
} else {
|
||||
Bukkit.getConsoleSender().sendMessage("[CommandPanels]" + ChatColor.RED + " No updates found for Minecraft version " + mcVersion);
|
||||
}
|
||||
cachedLatestVersion = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine().split("\\s")[1];
|
||||
connection.disconnect();
|
||||
} catch (IOException e) {
|
||||
Bukkit.getConsoleSender().sendMessage("[CommandPanels]" + ChatColor.RED + " Could not access GitHub to check for updates.");
|
||||
} catch (IOException ignore) {
|
||||
Bukkit.getConsoleSender().sendMessage("[CommandPanels]" + ChatColor.RED + " Could not access github.");
|
||||
}
|
||||
}
|
||||
}.runTaskAsynchronously(plugin);
|
||||
|
||||
if(cachedLatestVersion.contains("-")){
|
||||
if(sendMessages) {
|
||||
Bukkit.getConsoleSender().sendMessage("[CommandPanels]" + ChatColor.RED + " Cannot check for update.");
|
||||
}
|
||||
}
|
||||
return cachedLatestVersion;
|
||||
}
|
||||
|
||||
@ -210,5 +184,6 @@ public class Updater implements Listener {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user