From e7ce84e7fc8211131fb95226432cdf6de816a8eb Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Mon, 20 Jul 2020 18:55:37 -0400 Subject: [PATCH] cleaned up fileutil and expansion manager --- .../expansion/ExpansionManager.java | 317 ++++++++++-------- .../me/clip/placeholderapi/util/FileUtil.java | 124 ++++--- 2 files changed, 229 insertions(+), 212 deletions(-) diff --git a/src/main/java/me/clip/placeholderapi/expansion/ExpansionManager.java b/src/main/java/me/clip/placeholderapi/expansion/ExpansionManager.java index faa1f68..e1eb341 100644 --- a/src/main/java/me/clip/placeholderapi/expansion/ExpansionManager.java +++ b/src/main/java/me/clip/placeholderapi/expansion/ExpansionManager.java @@ -28,186 +28,207 @@ import me.clip.placeholderapi.util.FileUtil; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.event.Listener; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; -import java.lang.reflect.Constructor; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.logging.Level; -public final class ExpansionManager { - private final PlaceholderAPIPlugin plugin; +public final class ExpansionManager +{ - public ExpansionManager(PlaceholderAPIPlugin instance) { - plugin = instance; + @NotNull + private final File folder; + @NotNull + private final PlaceholderAPIPlugin plugin; - File f = new File(PlaceholderAPIPlugin.getInstance().getDataFolder(), "expansions"); - if (!f.exists()) { - f.mkdirs(); - } - } + public ExpansionManager(@NotNull final PlaceholderAPIPlugin plugin) + { + this.plugin = plugin; + this.folder = new File(plugin.getDataFolder(), "expansions"); - public PlaceholderExpansion getRegisteredExpansion(String name) { - for (Entry hook : PlaceholderAPI.getPlaceholders().entrySet()) { - if (hook.getValue() instanceof PlaceholderExpansion) { - if (name.equalsIgnoreCase(hook.getKey())) { - return (PlaceholderExpansion) hook.getValue(); - } - } - } + if (!this.folder.exists() && !folder.mkdirs()) + { + plugin.getLogger().log(Level.WARNING, "failed to create expansions folder!"); + } + } - return null; - } + public PlaceholderExpansion getRegisteredExpansion(String name) + { + for (Entry hook : PlaceholderAPI.getPlaceholders().entrySet()) + { + if (hook.getValue() instanceof PlaceholderExpansion) + { + if (name.equalsIgnoreCase(hook.getKey())) + { + return (PlaceholderExpansion) hook.getValue(); + } + } + } - public boolean registerExpansion(PlaceholderExpansion expansion) { - if (expansion == null || expansion.getIdentifier() == null) { - return false; - } + return null; + } - if (expansion instanceof Configurable) { - Map defaults = ((Configurable) expansion).getDefaults(); - String pre = "expansions." + expansion.getIdentifier() + "."; - FileConfiguration cfg = plugin.getConfig(); - boolean save = false; + public boolean registerExpansion(@NotNull final PlaceholderExpansion expansion) + { + if (expansion.getIdentifier() == null) + { + return false; + } - if (defaults != null) { - for (Entry entries : defaults.entrySet()) { - if (entries.getKey() == null || entries.getKey().isEmpty()) { - continue; - } + if (expansion instanceof Configurable) + { + Map defaults = ((Configurable) expansion).getDefaults(); + String pre = "expansions." + expansion.getIdentifier() + "."; + FileConfiguration cfg = plugin.getConfig(); + boolean save = false; - if (entries.getValue() == null) { - if (cfg.contains(pre + entries.getKey())) { - save = true; - cfg.set(pre + entries.getKey(), null); - } - } else { - if (!cfg.contains(pre + entries.getKey())) { - save = true; - cfg.set(pre + entries.getKey(), entries.getValue()); - } - } - } - } + if (defaults != null) + { + for (Entry entries : defaults.entrySet()) + { + if (entries.getKey() == null || entries.getKey().isEmpty()) + { + continue; + } - if (save) { - plugin.saveConfig(); - plugin.reloadConfig(); - } - } + if (entries.getValue() == null) + { + if (cfg.contains(pre + entries.getKey())) + { + save = true; + cfg.set(pre + entries.getKey(), null); + } + } + else + { + if (!cfg.contains(pre + entries.getKey())) + { + save = true; + cfg.set(pre + entries.getKey(), entries.getValue()); + } + } + } + } - if (expansion instanceof VersionSpecific) { - VersionSpecific nms = (VersionSpecific) expansion; - if (!nms.isCompatibleWith(PlaceholderAPIPlugin.getServerVersion())) { - plugin.getLogger() - .info( - "Your server version is not compatible with expansion: " + expansion.getIdentifier() - + " version: " + expansion.getVersion()); - return false; - } - } + if (save) + { + plugin.saveConfig(); + plugin.reloadConfig(); + } + } - if (!expansion.canRegister()) { - return false; - } + if (expansion instanceof VersionSpecific) + { + VersionSpecific nms = (VersionSpecific) expansion; + if (!nms.isCompatibleWith(PlaceholderAPIPlugin.getServerVersion())) + { + plugin.getLogger() + .info( + "Your server version is not compatible with expansion: " + expansion.getIdentifier() + + " version: " + expansion.getVersion()); + return false; + } + } - if (!expansion.register()) { - return false; - } + if (!expansion.canRegister() || !expansion.register()) + { + return false; + } - if (expansion instanceof Listener) { - Listener l = (Listener) expansion; - Bukkit.getPluginManager().registerEvents(l, plugin); - } + if (expansion instanceof Listener) + { + Bukkit.getPluginManager().registerEvents(((Listener) expansion), plugin); + } - plugin.getLogger().info("Successfully registered expansion: " + expansion.getIdentifier()); + plugin.getLogger().info("Successfully registered expansion: " + expansion.getIdentifier()); - if (expansion instanceof Taskable) { - ((Taskable) expansion).start(); - } + if (expansion instanceof Taskable) + { + ((Taskable) expansion).start(); + } - if (plugin.getExpansionCloud() != null) { - CloudExpansion ce = plugin.getExpansionCloud().getCloudExpansion(expansion.getIdentifier()); + if (plugin.getExpansionCloud() != null) + { + final CloudExpansion cloudExpansion = plugin.getExpansionCloud().getCloudExpansion(expansion.getIdentifier()); - if (ce != null) { - ce.setHasExpansion(true); - if (!ce.getLatestVersion().equals(expansion.getVersion())) { - ce.setShouldUpdate(true); - } - } - } + if (cloudExpansion != null) + { + cloudExpansion.setHasExpansion(true); + if (!cloudExpansion.getLatestVersion().equals(expansion.getVersion())) + { + cloudExpansion.setShouldUpdate(true); + } + } + } - return true; - } + return true; + } - public PlaceholderExpansion registerExpansion(String fileName) { - List> subs = FileUtil.getClasses("expansions", fileName, PlaceholderExpansion.class); - if (subs == null || subs.isEmpty()) { - return null; - } + @Nullable + public PlaceholderExpansion registerExpansion(@NotNull final String fileName) + { + final List> subs = FileUtil.getClasses(folder, PlaceholderExpansion.class, fileName); + if (subs.isEmpty()) + { + return null; + } - // only register the first instance found as an expansion jar should only have 1 class - // extending PlaceholderExpansion - PlaceholderExpansion ex = createInstance(subs.get(0)); - if (registerExpansion(ex)) { - return ex; - } + // only register the first instance found as an expansion jar should only have 1 class + // extending PlaceholderExpansion + final PlaceholderExpansion expansion = createInstance(subs.get(0)); + if (expansion != null && registerExpansion(expansion)) + { + return expansion; + } - return null; - } + return null; + } - public void registerAllExpansions() { - if (plugin == null) { - return; - } + public void registerAllExpansions() + { + final List<@NotNull Class> subs = FileUtil.getClasses(folder, PlaceholderExpansion.class); + if (subs.isEmpty()) + { + return; + } - List> subs = FileUtil.getClasses("expansions", null, PlaceholderExpansion.class); - if (subs == null || subs.isEmpty()) { - return; - } + for (final Class clazz : subs) + { + final PlaceholderExpansion expansion = createInstance(clazz); + if (expansion == null) + { + continue; + } - for (Class klass : subs) { - PlaceholderExpansion ex = createInstance(klass); - if (ex != null) { - try { - registerExpansion(ex); - } catch (Exception e) { - plugin.getLogger().info("Couldn't register " + ex.getIdentifier() + " expansion"); - e.printStackTrace(); - } - } - } - } + try + { + registerExpansion(expansion); + } + catch (final Exception ex) + { + plugin.getLogger().log(Level.WARNING, "Couldn't register " + expansion.getIdentifier() + " expansion", ex); + } + } + } - private PlaceholderExpansion createInstance(Class klass) { - if (klass == null) { - return null; - } + @Nullable + private PlaceholderExpansion createInstance(@NotNull final Class clazz) + { + try + { + return clazz.getDeclaredConstructor().newInstance(); + } + catch (final Throwable ex) + { + plugin.getLogger().log(Level.SEVERE, "Failed to load placeholder expansion from class: " + clazz.getName(), ex); + } - PlaceholderExpansion ex = null; - if (!PlaceholderExpansion.class.isAssignableFrom(klass)) { - return null; - } + return null; + } - try { - Constructor[] c = klass.getConstructors(); - if (c.length == 0) { - ex = (PlaceholderExpansion) klass.newInstance(); - } else { - for (Constructor con : c) { - if (con.getParameterTypes().length == 0) { - ex = (PlaceholderExpansion) klass.newInstance(); - break; - } - } - } - } catch (Throwable t) { - plugin.getLogger() - .severe("Failed to init placeholder expansion from class: " + klass.getName()); - plugin.getLogger().severe(t.getMessage()); - } - - return ex; - } } diff --git a/src/main/java/me/clip/placeholderapi/util/FileUtil.java b/src/main/java/me/clip/placeholderapi/util/FileUtil.java index 84eb613..0acb44e 100644 --- a/src/main/java/me/clip/placeholderapi/util/FileUtil.java +++ b/src/main/java/me/clip/placeholderapi/util/FileUtil.java @@ -20,89 +20,85 @@ */ package me.clip.placeholderapi.util; -import me.clip.placeholderapi.PlaceholderAPIPlugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.FilenameFilter; +import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarInputStream; -public class FileUtil { +public class FileUtil +{ - public static List> getClasses(String folder, Class type) { - return getClasses(folder, null, type); - } + @NotNull + public static List<@NotNull Class> getClasses(@NotNull final File folder, @NotNull final Class clazz) + { + return getClasses(folder, clazz, null); + } - public static List> getClasses(String folder, String fileName, Class type) { - List> list = new ArrayList<>(); + @NotNull + public static List<@NotNull Class> getClasses(@NotNull final File folder, @NotNull final Class clazz, @Nullable final String target) + { + if (!folder.exists()) + { + return Collections.emptyList(); + } - try { - File f = new File(PlaceholderAPIPlugin.getInstance().getDataFolder(), folder); - if (!f.exists()) { - return list; - } + try + { + final FilenameFilter filter = + (dir, name) -> name.endsWith(".jar") && (target == null || name.replace(".jar", "").equalsIgnoreCase(target.replace(".jar", ""))); - FilenameFilter fileNameFilter = (dir, name) -> { - if (fileName != null) { - return name.endsWith(".jar") && name.replace(".jar", "") - .equalsIgnoreCase(fileName.replace(".jar", "")); - } + final File[] jars = folder.listFiles(filter); + if (jars == null) + { + return Collections.emptyList(); + } - return name.endsWith(".jar"); - }; + final List<@NotNull Class> list = new ArrayList<>(); - File[] jars = f.listFiles(fileNameFilter); - if (jars == null) { - return list; - } + for (File file : jars) + { + gather(file.toURI().toURL(), clazz, list); + } - for (File file : jars) { - list = gather(file.toURI().toURL(), list, type); - } + return list; + } + catch (Throwable t) + { + // THIS SHOULD NOT BE EATEN LIKE THIS. + } - return list; - } catch (Throwable t) { - } + return Collections.emptyList(); + } - return null; - } + private static void gather(@NotNull final URL jar, @NotNull final Class clazz, @NotNull final List<@NotNull Class> list) throws IOException, ClassNotFoundException + { + try (final URLClassLoader loader = new URLClassLoader(new URL[]{jar}, clazz.getClassLoader()); final JarInputStream stream = new JarInputStream(jar.openStream())) + { + JarEntry entry; + while ((entry = stream.getNextJarEntry()) != null) + { + final String name = entry.getName(); + if (name == null || name.isEmpty() || !name.endsWith(".class")) + { + continue; + } - private static List> gather(URL jar, List> list, Class clazz) { - if (list == null) { - list = new ArrayList<>(); - } + final Class loaded = loader.loadClass(name.substring(0, name.lastIndexOf('.')).replace('/', '.')); + if (clazz.isAssignableFrom(loaded)) + { + list.add(loaded.asSubclass(clazz)); + } + } + } + } - try (URLClassLoader cl = new URLClassLoader(new URL[]{jar}, clazz.getClassLoader()); - JarInputStream jis = new JarInputStream(jar.openStream())) { - - while (true) { - JarEntry j = jis.getNextJarEntry(); - if (j == null) { - break; - } - - String name = j.getName(); - if (name == null || name.isEmpty()) { - continue; - } - - if (name.endsWith(".class")) { - name = name.replace("/", "."); - String cname = name.substring(0, name.lastIndexOf(".class")); - - Class c = cl.loadClass(cname); - if (clazz.isAssignableFrom(c)) { - list.add(c); - } - } - } - } catch (Throwable t) { - } - - return list; - } }