From 00507a1194adcbee7cc825407ef7cd29620216e0 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 17 Apr 2020 04:41:42 +1200 Subject: [PATCH] Use slightly different YAML loading, try fix an issue on a Chinese user's server --- .../libraryaddict/disguise/LibsDisguises.java | 2 +- .../disguise/utilities/LibsPremium.java | 2 +- .../reflection/ReflectionManager.java | 30 ++++++++++++++----- src/main/resources/plugin.yml | 2 +- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java index 1f68a432..410ce253 100644 --- a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java @@ -70,7 +70,7 @@ public class LibsDisguises extends JavaPlugin { saveResource("disguises.yml", false); } - YamlConfiguration pluginYml = ReflectionManager.getPluginYaml(getClassLoader()); + YamlConfiguration pluginYml = ReflectionManager.getPluginYAML(getClassLoader()); buildNumber = StringUtils.stripToNull(pluginYml.getString("build-number")); getLogger().info("Discovered nms version: " + ReflectionManager.getBukkitVersion()); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java b/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java index 7cf9bf17..88a24b78 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java @@ -128,7 +128,7 @@ public class LibsPremium { } // Fetch the plugin.yml from the jar file - YamlConfiguration config = ReflectionManager.getPluginYaml(cl); + YamlConfiguration config = ReflectionManager.getPluginYAML(cl); // No checks for null config as the correct error will be thrown on access Boolean premium; diff --git a/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java b/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java index 170857ea..f0283bc8 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java @@ -30,6 +30,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.*; +import java.net.URL; +import java.net.URLConnection; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -157,16 +159,28 @@ public class ReflectionManager { return true; } - public static YamlConfiguration getPluginYaml(ClassLoader loader) { - try (InputStream stream = loader.getResourceAsStream("plugin.yml")) { - YamlConfiguration config = new YamlConfiguration(); - config.loadFromString(new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines() - .collect(Collectors.joining("\n"))); + public static YamlConfiguration getPluginYAML(ClassLoader loader) { + try { + URL url = loader.getResource("plugin.yml"); - return config; + if (url == null) { + return null; + } else { + URLConnection connection = url.openConnection(); + connection.setUseCaches(false); + + try (InputStream stream = connection.getInputStream()) { + YamlConfiguration config = new YamlConfiguration(); + + config.loadFromString(new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines() + .collect(Collectors.joining("\n"))); + + return config; + } + } } - catch (IOException | InvalidConfigurationException e) { - e.printStackTrace(); + catch (IOException | InvalidConfigurationException ex) { + ex.printStackTrace(); } return null; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 1220e71d..ea8493a2 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -7,7 +7,7 @@ build-number: ${build.number} author: libraryaddict authors: [Byteflux, Navid K.] depend: [ProtocolLib] -api-version: 1.13 +api-version: '1.13' commands: libsdisguises: aliases: [libsdisg, ld]