Skip to content

Commit

Permalink
need to expose more addon api
Browse files Browse the repository at this point in the history
  • Loading branch information
Jsinco committed Dec 14, 2024
1 parent cfa8663 commit e7cbc85
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/dre/brewery/api/addons/AddonManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/dre/brewery/api/addons/BreweryAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -81,6 +88,7 @@ public abstract class BreweryAddon {
private AddonLogger logger;
private AddonFileManager addonFileManager;
private AddonConfigManager addonConfigManager;
private File addonFile;


public void onAddonPreEnable() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<Class<?>> 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());
}
}

0 comments on commit e7cbc85

Please sign in to comment.