9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2026-01-04 15:31:37 +00:00

Refactor package to net.william278; update dependencies & stop shading internal modules

This commit is contained in:
William
2022-06-07 20:44:53 +01:00
parent 2f3b0f37e8
commit a76aecdd23
61 changed files with 255 additions and 253 deletions

View File

@@ -0,0 +1,19 @@
package net.william278.husksync.util;
import java.util.logging.Level;
/**
* Logger interface to allow for implementation of different logger platforms used by Bungee and Velocity
*/
public interface Logger {
void log(Level level, String message, Exception e);
void log(Level level, String message);
void info(String message);
void severe(String message);
void config(String message);
}

View File

@@ -0,0 +1,30 @@
package net.william278.husksync.util;
import java.util.HashMap;
public class MessageManager {
private static HashMap<String, String> messages = new HashMap<>();
public static void setMessages(HashMap<String, String> newMessages) {
messages = new HashMap<>(newMessages);
}
public static String getMessage(String messageId) {
return messages.get(messageId);
}
public static StringBuilder PLUGIN_INFORMATION = new StringBuilder().append("[HuskSync](#00fb9a bold) [| %proxy_brand% Version %proxy_version% (%bukkit_brand% v%bukkit_version%)](#00fb9a)\n")
.append("[%plugin_description%](gray)\n")
.append("[• Author:](white) [William278](gray show_text=&7Click to visit website open_url=https://william278.net)\n")
.append("[• Contributors:](white) [HarvelsX](gray show_text=&7Code)\n")
.append("[• Translators:](white) [Namiu/うにたろう](gray show_text=&7Japanese, ja-jp), [anchelthe](gray show_text=&7Spanish, es-es), [Ceddix](gray show_text=&7German, de-de), [小蔡](gray show_text=&7Traditional Chinese, zh-tw), [Ghost-chu](gray show_text=&7Simplified Chinese, zh-cn), [Thourgard](gray show_text=&7Ukrainian, uk-ua)\n")
.append("[• Plugin Info:](white) [[Link]](#00fb9a show_text=&7Click to open link open_url=https://github.com/WiIIiam278/HuskSync/)\n")
.append("[• Report Issues:](white) [[Link]](#00fb9a show_text=&7Click to open link open_url=https://github.com/WiIIiam278/HuskSync/issues)\n")
.append("[• Support Discord:](white) [[Link]](#00fb9a show_text=&7Click to join open_url=https://discord.gg/tVYhJfyDWG)");
public static StringBuilder PLUGIN_STATUS = new StringBuilder().append("[HuskSync](#00fb9a bold) [| Current system status:](#00fb9a)\n")
.append("[• Connected servers:](white) [%1%](#00fb9a)\n")
.append("[• Cached player data:](white) [%2%](#00fb9a)");
}

View File

@@ -0,0 +1,13 @@
package net.william278.husksync.util;
public interface ThrowSupplier<T> {
T get() throws Exception;
static <A> A get(ThrowSupplier<A> supplier) {
try {
return supplier.get();
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}

View File

@@ -0,0 +1,53 @@
package net.william278.husksync.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
public abstract class UpdateChecker {
private final static int SPIGOT_PROJECT_ID = 97144;
private final VersionUtils.Version currentVersion;
private VersionUtils.Version latestVersion;
public UpdateChecker(String currentVersion) {
this.currentVersion = VersionUtils.Version.of(currentVersion);
try {
final URL url = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + SPIGOT_PROJECT_ID);
URLConnection urlConnection = url.openConnection();
this.latestVersion = VersionUtils.Version.of(new BufferedReader(new InputStreamReader(urlConnection.getInputStream())).readLine());
} catch (IOException e) {
log(Level.WARNING, "Failed to check for updates: An IOException occurred.");
this.latestVersion = new VersionUtils.Version();
} catch (Exception e) {
log(Level.WARNING, "Failed to check for updates: An exception occurred.");
this.latestVersion = new VersionUtils.Version();
}
}
public boolean isUpToDate() {
return this.currentVersion.compareTo(latestVersion) >= 0;
}
public String getLatestVersion() {
return latestVersion.toString();
}
public String getCurrentVersion() {
return currentVersion.toString();
}
public abstract void log(Level level, String message);
public void logToConsole() {
if (!isUpToDate()) {
log(Level.WARNING, "A new version of HuskSync is available: Version "
+ latestVersion + " (Currently running: " + currentVersion + ")");
}
}
}

View File

@@ -0,0 +1,61 @@
package net.william278.husksync.util;
import java.util.Arrays;
public class VersionUtils {
private final static char META_SEPARATOR = '+';
private final static String VERSION_SEPARATOR = "\\.";
public static class Version implements Comparable<Version> {
public int[] versions = new int[]{};
public String metadata = "";
public Version() {
}
public Version(String version) {
this.parse(version);
}
public static Version of(String version) {
return new Version(version);
}
private void parse(String version) {
int metaIndex = version.indexOf(META_SEPARATOR);
if (metaIndex > 0) {
this.metadata = version.substring(metaIndex + 1);
version = version.substring(0, metaIndex);
}
String[] versions = version.split(VERSION_SEPARATOR);
this.versions = Arrays.stream(versions).mapToInt(Integer::parseInt).toArray();
}
@Override
public int compareTo(Version version) {
int length = Math.max(this.versions.length, version.versions.length);
for (int i = 0; i < length; i++) {
int a = i < this.versions.length ? this.versions[i] : 0;
int b = i < version.versions.length ? version.versions[i] : 0;
if (a < b) return -1;
if (a > b) return 1;
}
return 0;
}
@Override
public String toString() {
StringBuilder stringBuffer = new StringBuilder();
for (int version : this.versions) {
stringBuffer.append(version).append('.');
}
stringBuffer.deleteCharAt(stringBuffer.length() - 1);
return stringBuffer.append('+').append(this.metadata).toString();
}
}
}