Create a more straightforward way for interacting with unstable methods

This commit is contained in:
ME1312 2018-07-28 22:09:47 -04:00
parent ae1a9b990d
commit 319618fe71
No known key found for this signature in database
GPG Key ID: FEFFE2F698E88FA8
16 changed files with 126 additions and 40 deletions

View File

@ -150,6 +150,7 @@ public abstract class SubCreator {
public String toString() { public String toString() {
YAMLSection tinfo = new YAMLSection(); YAMLSection tinfo = new YAMLSection();
tinfo.set("enabled", isEnabled()); tinfo.set("enabled", isEnabled());
tinfo.set("name", getName());
tinfo.set("display", getDisplayName()); tinfo.set("display", getDisplayName());
tinfo.set("icon", getIcon()); tinfo.set("icon", getIcon());
tinfo.set("type", getType().toString()); tinfo.set("type", getType().toString());

View File

@ -26,8 +26,7 @@ public final class Launch {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
System.setProperty("apple.laf.useScreenMenuBar", "true"); System.setProperty("apple.laf.useScreenMenuBar", "true");
Container<Boolean> bungee = new Container<Boolean>(false); if (Util.getDespiteException(() -> Class.forName("net.md_5.bungee.BungeeCord") != null, false)) {
if (Util.isException(() -> bungee.set(Class.forName("net.md_5.bungee.BungeeCord") != null)) && !bungee.get()) {
System.out.println(""); System.out.println("");
System.out.println("*******************************************"); System.out.println("*******************************************");
System.out.println("*** Error: BungeeCord.jar Doesn't Exist ***"); System.out.println("*** Error: BungeeCord.jar Doesn't Exist ***");
@ -100,8 +99,7 @@ public final class Launch {
if (!options.has("noconsole")) { if (!options.has("noconsole")) {
try { try {
Container<Boolean> proprietary = new Container<Boolean>(false); if (!Util.getDespiteException(() -> Class.forName("io.github.waterfallmc.waterfall.console.WaterfallConsole") != null, false)) {
if (!Util.isException(() -> proprietary.set(Class.forName("io.github.waterfallmc.waterfall.console.WaterfallConsole") != null)) && proprietary.get()) {
Class.forName("io.github.waterfallmc.waterfall.console.WaterfallConsole").getMethod("readCommands").invoke(null); Class.forName("io.github.waterfallmc.waterfall.console.WaterfallConsole").getMethod("readCommands").invoke(null);
} else { } else {
plugin.canSudo = true; plugin.canSudo = true;

View File

@ -11,6 +11,9 @@ import java.util.zip.ZipOutputStream;
*/ */
public final class Util { public final class Util {
private Util(){} private Util(){}
public interface ExceptionReturnRunnable<R> {
R run() throws Throwable;
}
public interface ExceptionRunnable { public interface ExceptionRunnable {
void run() throws Throwable; 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 <R> Variable Type
* @return Returns value or default depending on if an exception is thrown
*/
public static <R> R getDespiteException(ExceptionReturnRunnable<R> runnable, R def) {
try {
return runnable.run();
} catch (Throwable e) {
return def;
}
}
/** /**
* Determines if an Exception will occur * Determines if an Exception will occur
* *

View File

@ -675,15 +675,14 @@ public final class SubAPI {
*/ */
public Version[] getGameVersion() { public Version[] getGameVersion() {
if (GAME_VERSION == null) { if (GAME_VERSION == null) {
Container<Boolean> valid = new Container<Boolean>(false);
if (System.getProperty("subservers.minecraft.version", "").length() > 0) { if (System.getProperty("subservers.minecraft.version", "").length() > 0) {
return new Version[]{new Version(System.getProperty("subservers.minecraft.version"))}; 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<Version> versions = new LinkedList<Version>(); List<Version> versions = new LinkedList<Version>();
for (String version : ProtocolConstants.SUPPORTED_VERSIONS) versions.add(new Version(version)); for (String version : ProtocolConstants.SUPPORTED_VERSIONS) versions.add(new Version(version));
Collections.sort(versions); Collections.sort(versions);
return versions.toArray(new Version[versions.size()]); 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(); String raw = plugin.getGameVersion();
if (raw.contains("-") || raw.contains(",")) { if (raw.contains("-") || raw.contains(",")) {
List<Version> versions = new LinkedList<Version>(); List<Version> versions = new LinkedList<Version>();

View File

@ -8,6 +8,9 @@ import java.util.*;
*/ */
public final class Util { public final class Util {
private Util(){} private Util(){}
public interface ExceptionReturnRunnable<R> {
R run() throws Throwable;
}
public interface ExceptionRunnable { public interface ExceptionRunnable {
void run() throws Throwable; 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 <R> Variable Type
* @return Returns value or default depending on if an exception is thrown
*/
public static <R> R getDespiteException(ExceptionReturnRunnable<R> runnable, R def) {
try {
return runnable.run();
} catch (Throwable e) {
return def;
}
}
/** /**
* Determines if an Exception will occur * Determines if an Exception will occur
* *
@ -140,8 +159,6 @@ public final class Util {
} }
} }
/** /**
* Delete a Directory * Delete a Directory
* *

View File

@ -37,7 +37,7 @@ public class SubCreator {
private YAMLSection raw; private YAMLSection raw;
private ServerType type; private ServerType type;
private ServerTemplate(YAMLSection raw) { public ServerTemplate(YAMLSection raw) {
this.raw = raw; this.raw = raw;
this.type = (Util.isException(() -> ServerType.valueOf(raw.getRawString("type").toUpperCase())))?ServerType.valueOf(raw.getRawString("type").toUpperCase()):ServerType.CUSTOM; 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() { public String toString() {
YAMLSection tinfo = new YAMLSection(); YAMLSection tinfo = new YAMLSection();
tinfo.set("enabled", isEnabled()); tinfo.set("enabled", isEnabled());
tinfo.set("name", getName());
tinfo.set("display", getDisplayName()); tinfo.set("display", getDisplayName());
tinfo.set("icon", getIcon()); tinfo.set("icon", getIcon());
tinfo.set("type", getType().toString()); tinfo.set("type", getType().toString());

View File

@ -92,10 +92,7 @@ public final class SubCommand implements CommandExecutor {
plugin.subdata.sendPacket(new PacketDownloadServerList(null, null, data -> { plugin.subdata.sendPacket(new PacketDownloadServerList(null, null, data -> {
int i = 0; int i = 0;
boolean sent = false; boolean sent = false;
Container<Boolean> spigot = new Container<Boolean>(false); if (!Util.getDespiteException(() -> Class.forName("org.spigotmc.SpigotConfig") != null, false) && sender instanceof Player) {
if (!Util.isException(() -> {
if (Class.forName("org.spigotmc.SpigotConfig") != null) spigot.set(true);
}) && spigot.get() && 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")); 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) { if (data.getSection("groups").getKeys().size() > 0) {
sender.sendMessage(plugin.api.getLang("SubServers", "Command.List.Group-Header")); sender.sendMessage(plugin.api.getLang("SubServers", "Command.List.Group-Header"));

View File

@ -8,6 +8,9 @@ import java.util.*;
*/ */
public final class Util { public final class Util {
private Util(){} private Util(){}
public interface ExceptionReturnRunnable<R> {
R run() throws Throwable;
}
public interface ExceptionRunnable { public interface ExceptionRunnable {
void run() throws Throwable; 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 <R> Variable Type
* @return Returns value or default depending on if an exception is thrown
*/
public static <R> R getDespiteException(ExceptionReturnRunnable<R> runnable, R def) {
try {
return runnable.run();
} catch (Throwable e) {
return def;
}
}
/** /**
* Determines if an Exception will occur * Determines if an Exception will occur
* *
@ -140,8 +159,6 @@ public final class Util {
} }
} }
/** /**
* Delete a Directory * Delete a Directory
* *

View File

@ -36,7 +36,7 @@ public class SubCreator {
private YAMLSection raw; private YAMLSection raw;
private ServerType type; private ServerType type;
private ServerTemplate(YAMLSection raw) { public ServerTemplate(YAMLSection raw) {
this.raw = raw; this.raw = raw;
this.type = (Util.isException(() -> ServerType.valueOf(raw.getRawString("type").toUpperCase())))?ServerType.valueOf(raw.getRawString("type").toUpperCase()):ServerType.CUSTOM; 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() { public String toString() {
YAMLSection tinfo = new YAMLSection(); YAMLSection tinfo = new YAMLSection();
tinfo.set("enabled", isEnabled()); tinfo.set("enabled", isEnabled());
tinfo.set("name", getName());
tinfo.set("display", getDisplayName()); tinfo.set("display", getDisplayName());
tinfo.set("icon", getIcon()); tinfo.set("icon", getIcon());
tinfo.set("type", getType().toString()); tinfo.set("type", getType().toString());

View File

@ -9,6 +9,7 @@ import net.ME1312.SubServers.Host.Library.NamedContainer;
import net.ME1312.SubServers.Host.Library.UniversalFile; import net.ME1312.SubServers.Host.Library.UniversalFile;
import net.ME1312.SubServers.Host.Library.Util; import net.ME1312.SubServers.Host.Library.Util;
import net.ME1312.SubServers.Host.Library.Version.Version; 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.PacketExCreateServer;
import net.ME1312.SubServers.Host.Network.Packet.PacketOutExLogMessage; import net.ME1312.SubServers.Host.Network.Packet.PacketOutExLogMessage;
import net.ME1312.SubServers.Host.ExHost; import net.ME1312.SubServers.Host.ExHost;
@ -23,7 +24,6 @@ import java.io.*;
import java.net.URL; import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.*; import java.util.*;
import java.util.regex.Pattern;
/** /**
* Internal SubCreator Class * Internal SubCreator Class
@ -32,7 +32,7 @@ public class SubCreator {
private ExHost host; private ExHost host;
private TreeMap<String, NamedContainer<Thread, NamedContainer<SubLogger, Process>>> thread; private TreeMap<String, NamedContainer<Thread, NamedContainer<SubLogger, Process>>> thread;
public static class ServerTemplate { public static class ServerTemplate extends net.ME1312.SubServers.Host.Network.API.SubCreator.ServerTemplate {
private String name; private String name;
private String nick = null; private String nick = null;
private boolean enabled; private boolean enabled;
@ -51,7 +51,7 @@ public class SubCreator {
* @param options Configuration Options * @param options Configuration Options
*/ */
public ServerTemplate(String name, boolean enabled, String icon, File directory, YAMLSection build, YAMLSection 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); if (name.contains(" ")) throw new InvalidTemplateException("Template names cannot have spaces: " + name);
this.name = name; this.name = name;
this.enabled = enabled; this.enabled = enabled;
@ -165,17 +165,16 @@ public class SubCreator {
public YAMLSection getConfigOptions() { public YAMLSection getConfigOptions() {
return options; return options;
} }
}
public enum ServerType {
SPIGOT,
VANILLA,
FORGE,
SPONGE,
CUSTOM;
@Override private static YAMLSection toRaw(String name, boolean enabled, String icon, File directory, YAMLSection build, YAMLSection options) {
public String toString() { if (Util.isNull(name, enabled, directory, build, options)) throw new NullPointerException();
return super.toString().substring(0, 1).toUpperCase()+super.toString().substring(1).toLowerCase(); 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;
} }
} }

View File

@ -11,6 +11,9 @@ import java.util.zip.ZipOutputStream;
*/ */
public final class Util { public final class Util {
private Util(){} private Util(){}
public interface ExceptionReturnRunnable<R> {
R run() throws Throwable;
}
public interface ExceptionRunnable { public interface ExceptionRunnable {
void run() throws Throwable; 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 <R> Variable Type
* @return Returns value or default depending on if an exception is thrown
*/
public static <R> R getDespiteException(ExceptionReturnRunnable<R> runnable, R def) {
try {
return runnable.run();
} catch (Throwable e) {
return def;
}
}
/** /**
* Determines if an Exception will occur * Determines if an Exception will occur
* *

View File

@ -36,7 +36,7 @@ public class SubCreator {
private YAMLSection raw; private YAMLSection raw;
private ServerType type; private ServerType type;
private ServerTemplate(YAMLSection raw) { public ServerTemplate(YAMLSection raw) {
this.raw = raw; this.raw = raw;
this.type = (Util.isException(() -> ServerType.valueOf(raw.getRawString("type").toUpperCase())))? ServerType.valueOf(raw.getRawString("type").toUpperCase()): ServerType.CUSTOM; 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() { public String toString() {
YAMLSection tinfo = new YAMLSection(); YAMLSection tinfo = new YAMLSection();
tinfo.set("enabled", isEnabled()); tinfo.set("enabled", isEnabled());
tinfo.set("name", getName());
tinfo.set("display", getDisplayName()); tinfo.set("display", getDisplayName());
tinfo.set("icon", getIcon()); tinfo.set("icon", getIcon());
tinfo.set("type", getType().toString()); tinfo.set("type", getType().toString());

View File

@ -26,8 +26,7 @@ public final class Launch {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
System.setProperty("apple.laf.useScreenMenuBar", "true"); System.setProperty("apple.laf.useScreenMenuBar", "true");
Container<Boolean> bungee = new Container<Boolean>(false); if (Util.getDespiteException(() -> Class.forName("net.md_5.bungee.BungeeCord") != null, false)) {
if (Util.isException(() -> bungee.set(Class.forName("net.md_5.bungee.BungeeCord") != null)) && !bungee.get()) {
System.out.println(""); System.out.println("");
System.out.println("*******************************************"); System.out.println("*******************************************");
System.out.println("*** Error: BungeeCord.jar Doesn't Exist ***"); System.out.println("*** Error: BungeeCord.jar Doesn't Exist ***");
@ -100,8 +99,7 @@ public final class Launch {
if (!options.has("noconsole")) { if (!options.has("noconsole")) {
try { try {
Container<Boolean> proprietary = new Container<Boolean>(false); if (!Util.getDespiteException(() -> Class.forName("io.github.waterfallmc.waterfall.console.WaterfallConsole") != null, false)) {
if (!Util.isException(() -> proprietary.set(Class.forName("io.github.waterfallmc.waterfall.console.WaterfallConsole") != null)) && proprietary.get()) {
Class.forName("io.github.waterfallmc.waterfall.console.WaterfallConsole").getMethod("readCommands").invoke(null); Class.forName("io.github.waterfallmc.waterfall.console.WaterfallConsole").getMethod("readCommands").invoke(null);
} else { } else {
String line; String line;

View File

@ -8,7 +8,10 @@ import java.util.*;
*/ */
public final class Util { public final class Util {
private Util(){} private Util(){}
public interface ExceptionRunnable { public interface ExceptionReturnRunnable<R> {
R run() throws Throwable;
}
public interface ExceptionRunnable<R> {
void run() throws Throwable; void run() throws Throwable;
} }
public interface ReturnRunnable<R> { public interface ReturnRunnable<R> {
@ -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 <R> Variable Type
* @return Returns value or default depending on if an exception is thrown
*/
public static <R> R getDespiteException(ExceptionReturnRunnable<R> runnable, R def) {
try {
return runnable.run();
} catch (Throwable e) {
return def;
}
}
/** /**
* Determines if an Exception will occur * Determines if an Exception will occur
* *

View File

@ -36,7 +36,7 @@ public class SubCreator {
private YAMLSection raw; private YAMLSection raw;
private ServerType type; private ServerType type;
private ServerTemplate(YAMLSection raw) { public ServerTemplate(YAMLSection raw) {
this.raw = raw; this.raw = raw;
this.type = (Util.isException(() -> ServerType.valueOf(raw.getRawString("type").toUpperCase())))? ServerType.valueOf(raw.getRawString("type").toUpperCase()): ServerType.CUSTOM; 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() { public String toString() {
YAMLSection tinfo = new YAMLSection(); YAMLSection tinfo = new YAMLSection();
tinfo.set("enabled", isEnabled()); tinfo.set("enabled", isEnabled());
tinfo.set("name", getName());
tinfo.set("display", getDisplayName()); tinfo.set("display", getDisplayName());
tinfo.set("icon", getIcon()); tinfo.set("icon", getIcon());
tinfo.set("type", getType().toString()); tinfo.set("type", getType().toString());

View File

@ -357,15 +357,14 @@ public final class SubAPI {
*/ */
public Version[] getGameVersion() { public Version[] getGameVersion() {
if (GAME_VERSION == null) { if (GAME_VERSION == null) {
Container<Boolean> valid = new Container<Boolean>(false);
if (System.getProperty("subservers.minecraft.version", "").length() > 0) { if (System.getProperty("subservers.minecraft.version", "").length() > 0) {
return new Version[]{new Version(System.getProperty("subservers.minecraft.version"))}; 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<Version> versions = new LinkedList<Version>(); List<Version> versions = new LinkedList<Version>();
for (String version : ProtocolConstants.SUPPORTED_VERSIONS) versions.add(new Version(version)); for (String version : ProtocolConstants.SUPPORTED_VERSIONS) versions.add(new Version(version));
Collections.sort(versions); Collections.sort(versions);
return versions.toArray(new Version[versions.size()]); 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(); String raw = plugin.getGameVersion();
if (raw.contains("-") || raw.contains(",")) { if (raw.contains("-") || raw.contains(",")) {
List<Version> versions = new LinkedList<Version>(); List<Version> versions = new LinkedList<Version>();