From 513d31f5182056781f9885a1aa4df15bf93ff0ac Mon Sep 17 00:00:00 2001 From: Ztrolix Date: Thu, 17 Oct 2024 19:52:11 +1100 Subject: [PATCH] Added Bungee Support --- build.gradle | 5 ++ .../java/dev/xdpxi/xdlib/plugin/bungee.java | 21 ++++++ .../xdlib/plugin/updateCheckerBungee.java | 73 +++++++++++++++++++ src/main/resources/bungee.yml | 5 ++ 4 files changed, 104 insertions(+) create mode 100644 src/main/java/dev/xdpxi/xdlib/plugin/bungee.java create mode 100644 src/main/java/dev/xdpxi/xdlib/plugin/updateCheckerBungee.java create mode 100644 src/main/resources/bungee.yml diff --git a/build.gradle b/build.gradle index fbfd4d8..d0d7521 100644 --- a/build.gradle +++ b/build.gradle @@ -16,6 +16,7 @@ repositories { maven { url "https://maven.shedaniel.me/" } maven { url "https://api.modrinth.com/maven" } maven { url "https://maven.terraformersmc.com/releases/" } + maven { url "https://oss.sonatype.org/content/groups/public/" } maven { url "https://repo.papermc.io/repository/maven-public/" } } @@ -39,6 +40,7 @@ dependencies { minecraft("com.mojang:minecraft:1.21") mappings("net.fabricmc:yarn:1.21+build.9:v2") compileOnly("io.papermc.paper:paper-api:1.21.1-R0.1-SNAPSHOT") + compileOnly("net.md-5:bungeecord-api:1.21-R0.1-SNAPSHOT") modImplementation("net.fabricmc:fabric-loader:0.16.7") modImplementation("net.fabricmc.fabric-api:fabric-api:0.102.0+1.21") modImplementation("com.terraformersmc:modmenu:11.0.3") @@ -60,6 +62,9 @@ processResources { filesMatching('paper-plugin.yml') { expand props } + filesMatching('bungee.yml') { + expand props + } inputs.property "version", project.version filesMatching("fabric.mod.json") { diff --git a/src/main/java/dev/xdpxi/xdlib/plugin/bungee.java b/src/main/java/dev/xdpxi/xdlib/plugin/bungee.java new file mode 100644 index 0000000..288f7b6 --- /dev/null +++ b/src/main/java/dev/xdpxi/xdlib/plugin/bungee.java @@ -0,0 +1,21 @@ +package dev.xdpxi.xdlib.plugin; + +import net.md_5.bungee.api.plugin.Plugin; + +public final class bungee extends Plugin { + + @Override + public void onEnable() { + getLogger().info("[XDLib] - Enabling..."); + + updateCheckerBungee checker = new updateCheckerBungee(this); + checker.checkForUpdate(); + + getLogger().info("[XDLib] - Enabled!"); + } + + @Override + public void onDisable() { + getLogger().info("[XDLib] - Disabled!"); + } +} \ No newline at end of file diff --git a/src/main/java/dev/xdpxi/xdlib/plugin/updateCheckerBungee.java b/src/main/java/dev/xdpxi/xdlib/plugin/updateCheckerBungee.java new file mode 100644 index 0000000..abb122f --- /dev/null +++ b/src/main/java/dev/xdpxi/xdlib/plugin/updateCheckerBungee.java @@ -0,0 +1,73 @@ +package dev.xdpxi.xdlib.plugin; + +import dev.xdpxi.xdlib.api.plugin.chatUtils; +import org.bukkit.ChatColor; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +public class updateCheckerBungee { + private final bungee plugin; + + public updateCheckerBungee(bungee plugin) { + this.plugin = plugin; + } + + public void checkForUpdate() { + try { + HttpURLConnection connection = (HttpURLConnection) new URL("https://api.modrinth.com/v2/project/xdlib/version").openConnection(); + connection.setRequestMethod("GET"); + connection.setRequestProperty("User-Agent", "Mozilla/5.0"); + + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + StringBuilder response = new StringBuilder(); + String inputLine; + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + String latestVersion = parseLatestVersion(response.toString()); + + if (isVersionLower(plugin.getDescription().getVersion(), latestVersion)) { + plugin.getLogger().info("[XDLib] - An update is available!"); + chatUtils.sendMessageToAll(ChatColor.GOLD + "[XDLib] - An update is available!"); + } else { + plugin.getLogger().info("[XDLib] - No update available!"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private String parseLatestVersion(String jsonResponse) { + JSONArray versions = new JSONArray(jsonResponse); + if (versions.length() > 0) { + JSONObject latestVersionInfo = versions.getJSONObject(0); + return latestVersionInfo.getString("version_number"); + } + return null; + } + + private boolean isVersionLower(String currentVersion, String latestVersion) { + String[] currentParts = currentVersion.split("\\."); + String[] latestParts = latestVersion.split("\\."); + + for (int i = 0; i < Math.max(currentParts.length, latestParts.length); i++) { + int currentPart = i < currentParts.length ? Integer.parseInt(currentParts[i]) : 0; + int latestPart = i < latestParts.length ? Integer.parseInt(latestParts[i]) : 0; + + if (currentPart < latestPart) { + return true; + } else if (currentPart > latestPart) { + return false; + } + } + return false; + } +} \ No newline at end of file diff --git a/src/main/resources/bungee.yml b/src/main/resources/bungee.yml new file mode 100644 index 0000000..e18d0e5 --- /dev/null +++ b/src/main/resources/bungee.yml @@ -0,0 +1,5 @@ +name: XDLib +version: '2.2.2' +main: dev.xdpxi.xdlib.plugin.bungee +author: XDPXI +description: This is a library for many uses and is included as an player counter for XDPXI's mods and modpacks! \ No newline at end of file