-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
73
src/main/java/dev/xdpxi/xdlib/plugin/updateCheckerBungee.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |