From 4cd3ec5505fb63234390b25267fcaf85513f9cec Mon Sep 17 00:00:00 2001 From: jonah Date: Sat, 27 Jan 2024 06:38:05 -0500 Subject: [PATCH] Finish addons v1 and add AddonLogger --- src/com/dre/brewery/BreweryPlugin.java | 6 +++++ src/com/dre/brewery/api/addons/Addon.java | 4 ++- .../dre/brewery/api/addons/AddonLogger.java | 25 +++++++++++++++++++ .../dre/brewery/api/addons/AddonManager.java | 10 +++++++- 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 src/com/dre/brewery/api/addons/AddonLogger.java diff --git a/src/com/dre/brewery/BreweryPlugin.java b/src/com/dre/brewery/BreweryPlugin.java index 0304942..90e9c69 100644 --- a/src/com/dre/brewery/BreweryPlugin.java +++ b/src/com/dre/brewery/BreweryPlugin.java @@ -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); diff --git a/src/com/dre/brewery/api/addons/Addon.java b/src/com/dre/brewery/api/addons/Addon.java index 397b0ca..bc9dba4 100644 --- a/src/com/dre/brewery/api/addons/Addon.java +++ b/src/com/dre/brewery/api/addons/Addon.java @@ -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() { diff --git a/src/com/dre/brewery/api/addons/AddonLogger.java b/src/com/dre/brewery/api/addons/AddonLogger.java new file mode 100644 index 0000000..1f17f31 --- /dev/null +++ b/src/com/dre/brewery/api/addons/AddonLogger.java @@ -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 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)); + } +} diff --git a/src/com/dre/brewery/api/addons/AddonManager.java b/src/com/dre/brewery/api/addons/AddonManager.java index 374a961..217c48a 100644 --- a/src/com/dre/brewery/api/addons/AddonManager.java +++ b/src/com/dre/brewery/api/addons/AddonManager.java @@ -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> 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) {