From 28a99d91ffc230be28b5b1c48be986b200cd3390 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 17 Apr 2020 14:08:21 +1200 Subject: [PATCH] Change how I grab plugin.yml from jars to fix an error with weird classloading --- .../libraryaddict/disguise/LibsDisguises.java | 2 +- .../disguise/utilities/LibsPremium.java | 2 +- .../reflection/ReflectionManager.java | 40 ++++++++----------- 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java index 410ce253..d2e25dd8 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(getFile()); 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 88a24b78..5012ad14 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(file); // 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 f0283bc8..d71a55d7 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java @@ -16,22 +16,17 @@ import me.libraryaddict.disguise.utilities.LibsPremium; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.bukkit.*; -import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.*; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.plugin.InvalidDescriptionException; import org.bukkit.potion.PotionEffect; import org.bukkit.util.Vector; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.*; 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; @@ -39,6 +34,8 @@ import java.util.Map; import java.util.Optional; import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; import java.util.stream.Collectors; public class ReflectionManager { @@ -159,27 +156,24 @@ public class ReflectionManager { return true; } - public static YamlConfiguration getPluginYAML(ClassLoader loader) { - try { - URL url = loader.getResource("plugin.yml"); + /** + * Copied from Bukkit + */ + public static YamlConfiguration getPluginYAML(File file) { + try (JarFile jar = new JarFile(file)) { + JarEntry entry = jar.getJarEntry("plugin.yml"); - if (url == null) { - return null; - } else { - URLConnection connection = url.openConnection(); - connection.setUseCaches(false); + try (InputStream stream = jar.getInputStream(entry)) { + YamlConfiguration config = new YamlConfiguration(); - try (InputStream stream = connection.getInputStream()) { - YamlConfiguration config = new YamlConfiguration(); + String configString = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines() + .collect(Collectors.joining("\n")); + config.loadFromString(configString); - config.loadFromString(new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines() - .collect(Collectors.joining("\n"))); - - return config; - } + return config; } } - catch (IOException | InvalidConfigurationException ex) { + catch (Exception ex) { ex.printStackTrace(); }