From a3b4b9ca980d1f64fd6e1d28a47e3b7dcc1eb009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sat, 22 Oct 2022 00:14:33 +0200 Subject: [PATCH] Implement logger --- .../wtf/beatrice/nounspicker/NounsPicker.java | 13 +++++-- .../nounspicker/utils/ConsoleLogger.java | 37 +++++++++++++++++++ src/main/resources/plugin.yml | 2 +- 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/main/java/wtf/beatrice/nounspicker/utils/ConsoleLogger.java diff --git a/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java b/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java index 71e7edf..3fd28f0 100644 --- a/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java +++ b/src/main/java/wtf/beatrice/nounspicker/NounsPicker.java @@ -4,17 +4,19 @@ import org.bukkit.Bukkit; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import wtf.beatrice.nounspicker.objects.PAPIManager; - -import java.util.logging.Level; +import wtf.beatrice.nounspicker.utils.ConsoleLogger; public class NounsPicker extends JavaPlugin { private static NounsPicker instance; private PluginManager pluginManager; + private final ConsoleLogger logger = new ConsoleLogger(getClass()); @Override public void onEnable() { + logger.log("Loading plugin!"); + pluginManager = getServer().getPluginManager(); instance = this; @@ -22,11 +24,14 @@ public class NounsPicker extends JavaPlugin // register PlaceholderAPI if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { new PAPIManager(this).register(); + logger.log("PlaceholderAPI integration loaded!"); } else { - + logger.err("Missing PlaceholderAPI! The plugin cannot continue."); + pluginManager.disablePlugin(this); + return; } - + logger.log("Plugin loaded!"); } public static NounsPicker getInstance() diff --git a/src/main/java/wtf/beatrice/nounspicker/utils/ConsoleLogger.java b/src/main/java/wtf/beatrice/nounspicker/utils/ConsoleLogger.java new file mode 100644 index 0000000..27485f9 --- /dev/null +++ b/src/main/java/wtf/beatrice/nounspicker/utils/ConsoleLogger.java @@ -0,0 +1,37 @@ +package wtf.beatrice.nounspicker.utils; + +import org.bukkit.Bukkit; +import wtf.beatrice.nounspicker.NounsPicker; + +import java.util.logging.Level; +import java.util.logging.Logger; + +public class ConsoleLogger +{ + private final String className; + private final Logger logger; + private final String format = "[%class%] %msg%"; + public ConsoleLogger(Class callingClass) + { + className = callingClass.getSimpleName(); + logger = Bukkit.getServer().getLogger(); + } + + public void log(String msg) + { + print(Level.INFO, msg); + } + public void err(String msg) + { + print(Level.SEVERE, msg); + } + + private void print(Level lvl, String msg) + { + String message = format.replace("%msg%", msg) + .replace("%class%", className); + + logger.log(lvl, message); + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index bad2647..f818ea5 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,7 +3,7 @@ version: 1.0 author: astro_bea main: wtf.beatrice.nounspicker.NounsPicker api-version: 1.19 -depend: +softdepend: - PlaceholderAPI commands: