From 319618fe71cfafb966229bfb8942fef0b2d9a71d Mon Sep 17 00:00:00 2001 From: ME1312 Date: Sat, 28 Jul 2018 22:09:47 -0400 Subject: [PATCH] Create a more straightforward way for interacting with unstable methods --- .../SubServers/Bungee/Host/SubCreator.java | 1 + .../net/ME1312/SubServers/Bungee/Launch.java | 6 ++--- .../SubServers/Bungee/Library/Util.java | 19 ++++++++++++++ .../net/ME1312/SubServers/Bungee/SubAPI.java | 5 ++-- .../Client/Bukkit/Library/Util.java | 21 ++++++++++++++-- .../Client/Bukkit/Network/API/SubCreator.java | 3 ++- .../SubServers/Client/Bukkit/SubCommand.java | 5 +--- .../Client/Sponge/Library/Util.java | 21 ++++++++++++++-- .../Client/Sponge/Network/API/SubCreator.java | 3 ++- .../Host/Executable/SubCreator.java | 25 +++++++++---------- .../ME1312/SubServers/Host/Library/Util.java | 19 ++++++++++++++ .../Host/Network/API/SubCreator.java | 3 ++- .../net/ME1312/SubServers/Sync/Launch.java | 6 ++--- .../ME1312/SubServers/Sync/Library/Util.java | 21 +++++++++++++++- .../Sync/Network/API/SubCreator.java | 3 ++- .../net/ME1312/SubServers/Sync/SubAPI.java | 5 ++-- 16 files changed, 126 insertions(+), 40 deletions(-) diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/SubCreator.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/SubCreator.java index e6c8154e..cc02bbb9 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/SubCreator.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/SubCreator.java @@ -150,6 +150,7 @@ public abstract class SubCreator { public String toString() { YAMLSection tinfo = new YAMLSection(); tinfo.set("enabled", isEnabled()); + tinfo.set("name", getName()); tinfo.set("display", getDisplayName()); tinfo.set("icon", getIcon()); tinfo.set("type", getType().toString()); diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Launch.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Launch.java index 9afbceaa..d8805c88 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Launch.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Launch.java @@ -26,8 +26,7 @@ public final class Launch { public static void main(String[] args) throws Exception { System.setProperty("apple.laf.useScreenMenuBar", "true"); - Container bungee = new Container(false); - if (Util.isException(() -> bungee.set(Class.forName("net.md_5.bungee.BungeeCord") != null)) && !bungee.get()) { + if (Util.getDespiteException(() -> Class.forName("net.md_5.bungee.BungeeCord") != null, false)) { System.out.println(""); System.out.println("*******************************************"); System.out.println("*** Error: BungeeCord.jar Doesn't Exist ***"); @@ -100,8 +99,7 @@ public final class Launch { if (!options.has("noconsole")) { try { - Container proprietary = new Container(false); - if (!Util.isException(() -> proprietary.set(Class.forName("io.github.waterfallmc.waterfall.console.WaterfallConsole") != null)) && proprietary.get()) { + if (!Util.getDespiteException(() -> Class.forName("io.github.waterfallmc.waterfall.console.WaterfallConsole") != null, false)) { Class.forName("io.github.waterfallmc.waterfall.console.WaterfallConsole").getMethod("readCommands").invoke(null); } else { plugin.canSudo = true; diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Util.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Util.java index a22b28e5..b28e079c 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Util.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Util.java @@ -11,6 +11,9 @@ import java.util.zip.ZipOutputStream; */ public final class Util { private Util(){} + public interface ExceptionReturnRunnable { + R run() throws Throwable; + } public interface ExceptionRunnable { void run() throws Throwable; } @@ -128,6 +131,22 @@ public final class Util { } } + /** + * Get a variable from a method which may throw an exception + * + * @param runnable Runnable + * @param def Default value when an exception is thrown + * @param Variable Type + * @return Returns value or default depending on if an exception is thrown + */ + public static R getDespiteException(ExceptionReturnRunnable runnable, R def) { + try { + return runnable.run(); + } catch (Throwable e) { + return def; + } + } + /** * Determines if an Exception will occur * diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/SubAPI.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/SubAPI.java index 7cc1d004..79c4bc1a 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/SubAPI.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/SubAPI.java @@ -675,15 +675,14 @@ public final class SubAPI { */ public Version[] getGameVersion() { if (GAME_VERSION == null) { - Container valid = new Container(false); if (System.getProperty("subservers.minecraft.version", "").length() > 0) { return new Version[]{new Version(System.getProperty("subservers.minecraft.version"))}; - } else if (!Util.isException(() -> valid.set(ProtocolConstants.SUPPORTED_VERSIONS != null)) && valid.get()) { + } else if (!Util.getDespiteException(() -> ProtocolConstants.SUPPORTED_VERSIONS != null, false)) { List versions = new LinkedList(); for (String version : ProtocolConstants.SUPPORTED_VERSIONS) versions.add(new Version(version)); Collections.sort(versions); return versions.toArray(new Version[versions.size()]); - } else if (!Util.isException(() -> valid.set(plugin.getGameVersion() != null)) && valid.get()) { + } else if (!Util.getDespiteException(() -> plugin.getGameVersion() != null, false)) { String raw = plugin.getGameVersion(); if (raw.contains("-") || raw.contains(",")) { List versions = new LinkedList(); diff --git a/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/Library/Util.java b/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/Library/Util.java index aae7f146..c837a070 100644 --- a/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/Library/Util.java +++ b/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/Library/Util.java @@ -8,6 +8,9 @@ import java.util.*; */ public final class Util { private Util(){} + public interface ExceptionReturnRunnable { + R run() throws Throwable; + } public interface ExceptionRunnable { void run() throws Throwable; } @@ -125,6 +128,22 @@ public final class Util { } } + /** + * Get a variable from a method which may throw an exception + * + * @param runnable Runnable + * @param def Default value when an exception is thrown + * @param Variable Type + * @return Returns value or default depending on if an exception is thrown + */ + public static R getDespiteException(ExceptionReturnRunnable runnable, R def) { + try { + return runnable.run(); + } catch (Throwable e) { + return def; + } + } + /** * Determines if an Exception will occur * @@ -140,8 +159,6 @@ public final class Util { } } - - /** * Delete a Directory * diff --git a/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/Network/API/SubCreator.java b/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/Network/API/SubCreator.java index 631aed40..1e311e1c 100644 --- a/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/Network/API/SubCreator.java +++ b/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/Network/API/SubCreator.java @@ -37,7 +37,7 @@ public class SubCreator { private YAMLSection raw; private ServerType type; - private ServerTemplate(YAMLSection raw) { + public ServerTemplate(YAMLSection raw) { this.raw = raw; this.type = (Util.isException(() -> ServerType.valueOf(raw.getRawString("type").toUpperCase())))?ServerType.valueOf(raw.getRawString("type").toUpperCase()):ServerType.CUSTOM; } @@ -91,6 +91,7 @@ public class SubCreator { public String toString() { YAMLSection tinfo = new YAMLSection(); tinfo.set("enabled", isEnabled()); + tinfo.set("name", getName()); tinfo.set("display", getDisplayName()); tinfo.set("icon", getIcon()); tinfo.set("type", getType().toString()); diff --git a/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/SubCommand.java b/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/SubCommand.java index b53d8b81..8d7c9794 100644 --- a/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/SubCommand.java +++ b/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/SubCommand.java @@ -92,10 +92,7 @@ public final class SubCommand implements CommandExecutor { plugin.subdata.sendPacket(new PacketDownloadServerList(null, null, data -> { int i = 0; boolean sent = false; - Container spigot = new Container(false); - if (!Util.isException(() -> { - if (Class.forName("org.spigotmc.SpigotConfig") != null) spigot.set(true); - }) && spigot.get() && sender instanceof Player) { + if (!Util.getDespiteException(() -> Class.forName("org.spigotmc.SpigotConfig") != null, false) && sender instanceof Player) { net.md_5.bungee.api.chat.TextComponent div = new net.md_5.bungee.api.chat.TextComponent(plugin.api.getLang("SubServers", "Command.List.Divider")); if (data.getSection("groups").getKeys().size() > 0) { sender.sendMessage(plugin.api.getLang("SubServers", "Command.List.Group-Header")); diff --git a/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/Library/Util.java b/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/Library/Util.java index 7947c980..bc0f394d 100644 --- a/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/Library/Util.java +++ b/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/Library/Util.java @@ -8,6 +8,9 @@ import java.util.*; */ public final class Util { private Util(){} + public interface ExceptionReturnRunnable { + R run() throws Throwable; + } public interface ExceptionRunnable { void run() throws Throwable; } @@ -125,6 +128,22 @@ public final class Util { } } + /** + * Get a variable from a method which may throw an exception + * + * @param runnable Runnable + * @param def Default value when an exception is thrown + * @param Variable Type + * @return Returns value or default depending on if an exception is thrown + */ + public static R getDespiteException(ExceptionReturnRunnable runnable, R def) { + try { + return runnable.run(); + } catch (Throwable e) { + return def; + } + } + /** * Determines if an Exception will occur * @@ -140,8 +159,6 @@ public final class Util { } } - - /** * Delete a Directory * diff --git a/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/Network/API/SubCreator.java b/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/Network/API/SubCreator.java index 3115aae9..5860c759 100644 --- a/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/Network/API/SubCreator.java +++ b/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/Network/API/SubCreator.java @@ -36,7 +36,7 @@ public class SubCreator { private YAMLSection raw; private ServerType type; - private ServerTemplate(YAMLSection raw) { + public ServerTemplate(YAMLSection raw) { this.raw = raw; this.type = (Util.isException(() -> ServerType.valueOf(raw.getRawString("type").toUpperCase())))?ServerType.valueOf(raw.getRawString("type").toUpperCase()):ServerType.CUSTOM; } @@ -90,6 +90,7 @@ public class SubCreator { public String toString() { YAMLSection tinfo = new YAMLSection(); tinfo.set("enabled", isEnabled()); + tinfo.set("name", getName()); tinfo.set("display", getDisplayName()); tinfo.set("icon", getIcon()); tinfo.set("type", getType().toString()); diff --git a/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubCreator.java b/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubCreator.java index 97301d1b..8e859e65 100644 --- a/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubCreator.java +++ b/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubCreator.java @@ -9,6 +9,7 @@ import net.ME1312.SubServers.Host.Library.NamedContainer; import net.ME1312.SubServers.Host.Library.UniversalFile; import net.ME1312.SubServers.Host.Library.Util; import net.ME1312.SubServers.Host.Library.Version.Version; +import net.ME1312.SubServers.Host.Network.API.SubCreator.ServerType; import net.ME1312.SubServers.Host.Network.Packet.PacketExCreateServer; import net.ME1312.SubServers.Host.Network.Packet.PacketOutExLogMessage; import net.ME1312.SubServers.Host.ExHost; @@ -23,7 +24,6 @@ import java.io.*; import java.net.URL; import java.nio.charset.Charset; import java.util.*; -import java.util.regex.Pattern; /** * Internal SubCreator Class @@ -32,7 +32,7 @@ public class SubCreator { private ExHost host; private TreeMap>> thread; - public static class ServerTemplate { + public static class ServerTemplate extends net.ME1312.SubServers.Host.Network.API.SubCreator.ServerTemplate { private String name; private String nick = null; private boolean enabled; @@ -51,7 +51,7 @@ public class SubCreator { * @param options Configuration Options */ public ServerTemplate(String name, boolean enabled, String icon, File directory, YAMLSection build, YAMLSection options) { - if (Util.isNull(name, enabled, directory, build, options)) throw new NullPointerException(); + super(toRaw(name, enabled, icon, directory, build, options)); if (name.contains(" ")) throw new InvalidTemplateException("Template names cannot have spaces: " + name); this.name = name; this.enabled = enabled; @@ -165,17 +165,16 @@ public class SubCreator { public YAMLSection getConfigOptions() { return options; } - } - public enum ServerType { - SPIGOT, - VANILLA, - FORGE, - SPONGE, - CUSTOM; - @Override - public String toString() { - return super.toString().substring(0, 1).toUpperCase()+super.toString().substring(1).toLowerCase(); + private static YAMLSection toRaw(String name, boolean enabled, String icon, File directory, YAMLSection build, YAMLSection options) { + if (Util.isNull(name, enabled, directory, build, options)) throw new NullPointerException(); + YAMLSection tinfo = new YAMLSection(); + tinfo.set("enabled", enabled); + tinfo.set("name", name); + tinfo.set("display", name); + tinfo.set("icon", icon); + tinfo.set("type", (build.contains("Server-Type"))?ServerType.valueOf(build.getRawString("Server-Type").toUpperCase()):ServerType.CUSTOM); + return tinfo; } } diff --git a/SubServers.Host/src/net/ME1312/SubServers/Host/Library/Util.java b/SubServers.Host/src/net/ME1312/SubServers/Host/Library/Util.java index 4881ec54..d82db9a8 100644 --- a/SubServers.Host/src/net/ME1312/SubServers/Host/Library/Util.java +++ b/SubServers.Host/src/net/ME1312/SubServers/Host/Library/Util.java @@ -11,6 +11,9 @@ import java.util.zip.ZipOutputStream; */ public final class Util { private Util(){} + public interface ExceptionReturnRunnable { + R run() throws Throwable; + } public interface ExceptionRunnable { void run() throws Throwable; } @@ -128,6 +131,22 @@ public final class Util { } } + /** + * Get a variable from a method which may throw an exception + * + * @param runnable Runnable + * @param def Default value when an exception is thrown + * @param Variable Type + * @return Returns value or default depending on if an exception is thrown + */ + public static R getDespiteException(ExceptionReturnRunnable runnable, R def) { + try { + return runnable.run(); + } catch (Throwable e) { + return def; + } + } + /** * Determines if an Exception will occur * diff --git a/SubServers.Host/src/net/ME1312/SubServers/Host/Network/API/SubCreator.java b/SubServers.Host/src/net/ME1312/SubServers/Host/Network/API/SubCreator.java index 4f84def3..65e84df1 100644 --- a/SubServers.Host/src/net/ME1312/SubServers/Host/Network/API/SubCreator.java +++ b/SubServers.Host/src/net/ME1312/SubServers/Host/Network/API/SubCreator.java @@ -36,7 +36,7 @@ public class SubCreator { private YAMLSection raw; private ServerType type; - private ServerTemplate(YAMLSection raw) { + public ServerTemplate(YAMLSection raw) { this.raw = raw; this.type = (Util.isException(() -> ServerType.valueOf(raw.getRawString("type").toUpperCase())))? ServerType.valueOf(raw.getRawString("type").toUpperCase()): ServerType.CUSTOM; } @@ -90,6 +90,7 @@ public class SubCreator { public String toString() { YAMLSection tinfo = new YAMLSection(); tinfo.set("enabled", isEnabled()); + tinfo.set("name", getName()); tinfo.set("display", getDisplayName()); tinfo.set("icon", getIcon()); tinfo.set("type", getType().toString()); diff --git a/SubServers.Sync/src/net/ME1312/SubServers/Sync/Launch.java b/SubServers.Sync/src/net/ME1312/SubServers/Sync/Launch.java index d54eb65d..cfe9ba82 100644 --- a/SubServers.Sync/src/net/ME1312/SubServers/Sync/Launch.java +++ b/SubServers.Sync/src/net/ME1312/SubServers/Sync/Launch.java @@ -26,8 +26,7 @@ public final class Launch { public static void main(String[] args) throws Exception { System.setProperty("apple.laf.useScreenMenuBar", "true"); - Container bungee = new Container(false); - if (Util.isException(() -> bungee.set(Class.forName("net.md_5.bungee.BungeeCord") != null)) && !bungee.get()) { + if (Util.getDespiteException(() -> Class.forName("net.md_5.bungee.BungeeCord") != null, false)) { System.out.println(""); System.out.println("*******************************************"); System.out.println("*** Error: BungeeCord.jar Doesn't Exist ***"); @@ -100,8 +99,7 @@ public final class Launch { if (!options.has("noconsole")) { try { - Container proprietary = new Container(false); - if (!Util.isException(() -> proprietary.set(Class.forName("io.github.waterfallmc.waterfall.console.WaterfallConsole") != null)) && proprietary.get()) { + if (!Util.getDespiteException(() -> Class.forName("io.github.waterfallmc.waterfall.console.WaterfallConsole") != null, false)) { Class.forName("io.github.waterfallmc.waterfall.console.WaterfallConsole").getMethod("readCommands").invoke(null); } else { String line; diff --git a/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Util.java b/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Util.java index b55a6753..f4e8d47f 100644 --- a/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Util.java +++ b/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Util.java @@ -8,7 +8,10 @@ import java.util.*; */ public final class Util { private Util(){} - public interface ExceptionRunnable { + public interface ExceptionReturnRunnable { + R run() throws Throwable; + } + public interface ExceptionRunnable { void run() throws Throwable; } public interface ReturnRunnable { @@ -125,6 +128,22 @@ public final class Util { } } + /** + * Get a variable from a method which may throw an exception + * + * @param runnable Runnable + * @param def Default value when an exception is thrown + * @param Variable Type + * @return Returns value or default depending on if an exception is thrown + */ + public static R getDespiteException(ExceptionReturnRunnable runnable, R def) { + try { + return runnable.run(); + } catch (Throwable e) { + return def; + } + } + /** * Determines if an Exception will occur * diff --git a/SubServers.Sync/src/net/ME1312/SubServers/Sync/Network/API/SubCreator.java b/SubServers.Sync/src/net/ME1312/SubServers/Sync/Network/API/SubCreator.java index 9fa1574a..243fbeb1 100644 --- a/SubServers.Sync/src/net/ME1312/SubServers/Sync/Network/API/SubCreator.java +++ b/SubServers.Sync/src/net/ME1312/SubServers/Sync/Network/API/SubCreator.java @@ -36,7 +36,7 @@ public class SubCreator { private YAMLSection raw; private ServerType type; - private ServerTemplate(YAMLSection raw) { + public ServerTemplate(YAMLSection raw) { this.raw = raw; this.type = (Util.isException(() -> ServerType.valueOf(raw.getRawString("type").toUpperCase())))? ServerType.valueOf(raw.getRawString("type").toUpperCase()): ServerType.CUSTOM; } @@ -90,6 +90,7 @@ public class SubCreator { public String toString() { YAMLSection tinfo = new YAMLSection(); tinfo.set("enabled", isEnabled()); + tinfo.set("name", getName()); tinfo.set("display", getDisplayName()); tinfo.set("icon", getIcon()); tinfo.set("type", getType().toString()); diff --git a/SubServers.Sync/src/net/ME1312/SubServers/Sync/SubAPI.java b/SubServers.Sync/src/net/ME1312/SubServers/Sync/SubAPI.java index a7ac2c71..690567cb 100644 --- a/SubServers.Sync/src/net/ME1312/SubServers/Sync/SubAPI.java +++ b/SubServers.Sync/src/net/ME1312/SubServers/Sync/SubAPI.java @@ -357,15 +357,14 @@ public final class SubAPI { */ public Version[] getGameVersion() { if (GAME_VERSION == null) { - Container valid = new Container(false); if (System.getProperty("subservers.minecraft.version", "").length() > 0) { return new Version[]{new Version(System.getProperty("subservers.minecraft.version"))}; - } else if (!Util.isException(() -> valid.set(ProtocolConstants.SUPPORTED_VERSIONS != null)) && valid.get()) { + } else if (!Util.getDespiteException(() -> ProtocolConstants.SUPPORTED_VERSIONS != null, false)) { List versions = new LinkedList(); for (String version : ProtocolConstants.SUPPORTED_VERSIONS) versions.add(new Version(version)); Collections.sort(versions); return versions.toArray(new Version[versions.size()]); - } else if (!Util.isException(() -> valid.set(plugin.getGameVersion() != null)) && valid.get()) { + } else if (!Util.getDespiteException(() -> plugin.getGameVersion() != null, false)) { String raw = plugin.getGameVersion(); if (raw.contains("-") || raw.contains(",")) { List versions = new LinkedList();