Skip to content

Commit

Permalink
Added Bungee Support
Browse files Browse the repository at this point in the history
  • Loading branch information
XDPXI committed Oct 17, 2024
1 parent d54f6a0 commit 513d31f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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/" }
}

Expand All @@ -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")
Expand All @@ -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") {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/dev/xdpxi/xdlib/plugin/bungee.java
Original file line number Diff line number Diff line change
@@ -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!");
}
}
73 changes: 73 additions & 0 deletions src/main/java/dev/xdpxi/xdlib/plugin/updateCheckerBungee.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
5 changes: 5 additions & 0 deletions src/main/resources/bungee.yml
Original file line number Diff line number Diff line change
@@ -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!

0 comments on commit 513d31f

Please sign in to comment.