diff --git a/build.gradle.kts b/build.gradle.kts index 7350b9bd..23d1bcdc 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -93,7 +93,9 @@ dependencies { compileOnly("com.sk89q.worldguard:worldguard-bukkit:7.0.7") compileOnly("com.sk89q.worldedit:worldedit-bukkit:7.3.0-SNAPSHOT") // https://dev.bukkit.org/projects/worldedit/files compileOnly("com.sk89q.worldedit:worldedit-core:7.3.0-SNAPSHOT") // https://dev.bukkit.org/projects/worldguard/files - compileOnly("com.griefcraft.lwc:LWCX:2.2.9-dev")// https://www.spigotmc.org/resources/lwc-extended.69551/history + compileOnly("com.griefcraft.lwc:LWCX:2.4.0") { // https://www.spigotmc.org/resources/lwc-extended.69551/history + exclude("com.google") + } compileOnly("com.github.TechFortress:GriefPrevention:16.18") // https://www.spigotmc.org/resources/griefprevention.1884/history compileOnly("de.diddiz:logblock:1.16.5.1") // https://www.spigotmc.org/resources/logblock.67333/history compileOnly("com.github.Slimefun:Slimefun4:RC-35") // https://github.com/Slimefun/Slimefun4/releases diff --git a/src/main/java/com/dre/brewery/api/addons/AddonManager.java b/src/main/java/com/dre/brewery/api/addons/AddonManager.java index e501acb2..83888a0b 100644 --- a/src/main/java/com/dre/brewery/api/addons/AddonManager.java +++ b/src/main/java/com/dre/brewery/api/addons/AddonManager.java @@ -167,10 +167,12 @@ public void loadAddon(File file) { Field loggerField = BreweryAddon.class.getDeclaredField("logger"); loggerField.setAccessible(true); Field fileManagerField = BreweryAddon.class.getDeclaredField("addonFileManager"); fileManagerField.setAccessible(true); Field addonConfigManagerField = BreweryAddon.class.getDeclaredField("addonConfigManager"); addonConfigManagerField.setAccessible(true); + Field addonFile = BreweryAddon.class.getDeclaredField("addonFile"); addonFile.setAccessible(true); loggerField.set(addon, new AddonLogger(addon.getAddonInfo())); fileManagerField.set(addon, new AddonFileManager(addon, file)); addonConfigManagerField.set(addon, new AddonConfigManager(addon)); + addonFile.set(addon, file); addon.getAddonLogger().info("Loading &a" + addon.getAddonInfo().name() + " &f-&a v" + addon.getAddonInfo().version() + " &fby &a" + addon.getAddonInfo().author()); diff --git a/src/main/java/com/dre/brewery/api/addons/BreweryAddon.java b/src/main/java/com/dre/brewery/api/addons/BreweryAddon.java index 732359ff..6ed9efa8 100644 --- a/src/main/java/com/dre/brewery/api/addons/BreweryAddon.java +++ b/src/main/java/com/dre/brewery/api/addons/BreweryAddon.java @@ -24,14 +24,21 @@ import com.dre.brewery.commands.CommandManager; import com.dre.brewery.utility.MinecraftVersion; import com.github.Anon8281.universalScheduler.scheduling.schedulers.TaskScheduler; +import com.google.common.reflect.ClassPath; import io.papermc.lib.PaperLib; import org.bukkit.Bukkit; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.jetbrains.annotations.NotNull; +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; /** * Entry and exit point for a BreweryX addon. Addon classes should be annotated with {@link AddonInfo}. @@ -81,6 +88,7 @@ public abstract class BreweryAddon { private AddonLogger logger; private AddonFileManager addonFileManager; private AddonConfigManager addonConfigManager; + private File addonFile; public void onAddonPreEnable() { @@ -133,6 +141,13 @@ public AddonLogger getAddonLogger() { return logger; } + + @NotNull + public File getAddonFile() { + return addonFile; + } + + /** * Register a listener with the server. * @param listener The listener to register @@ -246,4 +261,26 @@ public boolean isFolia() { public boolean isPaper() { return PaperLib.isPaper(); } + + + // Exposed Reflection API for addons. + + /** + * For addons to reflectively discover their own classes, they must reference their own Jar. + * @param packageName Package to search + * @return Set of classes in the package + */ + public Set> findClasses(String packageName) throws IOException { + URLClassLoader classLoader = new URLClassLoader( + new URL[] { getAddonFile().toURI().toURL() }, + this.getClass().getClassLoader() + ); + return ClassPath.from(classLoader) + .getAllClasses() + .stream() + .filter(clazz -> clazz.getPackageName() + .equalsIgnoreCase(packageName)) // should just be equals instead of equalsIgnoreCase probs + .map(ClassPath.ClassInfo::load) + .collect(Collectors.toSet()); + } }