Finish addons v1 and add AddonLogger

This commit is contained in:
jonah 2024-01-27 06:38:05 -05:00
parent 7f9d14aaf3
commit 4cd3ec5505
4 changed files with 43 additions and 2 deletions

View File

@ -24,6 +24,7 @@
package com.dre.brewery;
import com.dre.brewery.api.addons.AddonManager;
import com.dre.brewery.commands.CommandManager;
import com.dre.brewery.commands.CommandUtil;
@ -57,6 +58,8 @@ import java.util.Map;
import java.util.function.Function;
public class BreweryPlugin extends JavaPlugin {
AddonManager addonManager = new AddonManager(this);
public static BreweryPlugin breweryPlugin;
public static boolean debug;
public static boolean useUUID;
@ -178,11 +181,14 @@ public class BreweryPlugin extends JavaPlugin {
}
}
addonManager.loadAddons();
this.log(this.getDescription().getName() + " enabled!");
}
@Override
public void onDisable() {
addonManager.unloadAddons();
// Disable listeners
HandlerList.unregisterAll(this);

View File

@ -5,9 +5,11 @@ import com.dre.brewery.BreweryPlugin;
public abstract class Addon {
protected final BreweryPlugin plugin;
protected final AddonLogger logger;
public Addon(BreweryPlugin plugin) {
public Addon(BreweryPlugin plugin, AddonLogger logger) {
this.plugin = plugin;
this.logger = logger;
}
public void onAddonEnable() {

View File

@ -0,0 +1,25 @@
package com.dre.brewery.api.addons;
import com.dre.brewery.utility.BUtil;
import org.bukkit.Bukkit;
public class AddonLogger {
private final String prefix;
public AddonLogger(Class<? extends Addon> addonUninstantiated) {
this.prefix = "[BreweryAddon: " + addonUninstantiated.getSimpleName() + "] ";
}
public void info(String message) {
Bukkit.getConsoleSender().sendMessage(BUtil.color(prefix + message));
}
public void warning(String message) {
Bukkit.getConsoleSender().sendMessage(BUtil.color("&e" + prefix + message));
}
public void severe(String message) {
Bukkit.getConsoleSender().sendMessage(BUtil.color("&c" + prefix + message));
}
}

View File

@ -31,6 +31,14 @@ public class AddonManager {
public void loadAddons() {
addons = getAllAddonClasses();
plugin.getLogger().info("Loaded " + addons.size() + " addons");
}
public void unloadAddons() {
for (Addon addon : addons) {
addon.onAddonDisable();
}
addons = null;
}
/**
@ -50,7 +58,7 @@ public class AddonManager {
for (CompletableFuture<Class<? extends Addon>> addonClass : addonClasses) {
addonClass.thenAccept(clazz -> {
try {
Addon addon = clazz.getConstructor(BreweryPlugin.class).newInstance(plugin);
Addon addon = clazz.getConstructor(BreweryPlugin.class, AddonLogger.class).newInstance(plugin, new AddonLogger(clazz));
addon.onAddonEnable();
addons.add(addon);
} catch (Exception e) {