package vg.civcraft.mc.civmodcore.util;

import com.google.common.base.Strings;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.bukkit.plugin.java.PluginClassLoader;

public final class CivLogger extends Logger {

	private final String prefix;

	private CivLogger(final Logger logger, final String prefix) {
		super(logger.getName(), logger.getResourceBundleName());
		setParent(logger);
		this.prefix = prefix;
	}

	@Override
	public void log(final LogRecord record) {
		if (!Strings.isNullOrEmpty(this.prefix)) {
			record.setMessage("[" + this.prefix + "] " + record.getMessage());
		}
		super.log(record);
	}

	/**
	 * Creates a logger based on a given class. If the given class was loaded by a plugin, it will piggy back off that
	 * plugin's logger.
	 *
	 * @param clazz The class to base the logger on.
	 * @return Returns a new civ logger.
	 */
	public static CivLogger getLogger(final Class<?> clazz) {
		if (clazz == null) {
			return new CivLogger(Logger.getLogger(CivLogger.class.getSimpleName()), null);
		}
		final ClassLoader classLoader = clazz.getClassLoader();
		if (!(classLoader instanceof PluginClassLoader)) {
			return new CivLogger(Logger.getLogger(CivLogger.class.getSimpleName()), clazz.getSimpleName());
		}
		final var plugin = ((PluginClassLoader) classLoader).getPlugin();
		return new CivLogger(plugin.getLogger(), clazz.getSimpleName());
	}

}

@@ -142,16 +142,101 @@
 Logger;%0A
+import javax.annotation.Nonnull;%0Aimport org.apache.commons.lang3.reflect.FieldUtils;%0A
 import o
@@ -994,24 +994,33 @@
 r getLogger(
+@Nonnull 
 final Class%3C
@@ -1035,117 +1035,8 @@
 ) %7B%0A
-%09%09if (clazz == null) %7B%0A%09%09%09return new CivLogger(Logger.getLogger(CivLogger.class.getSimpleName()), null);%0A%09%09%7D%0A
 %09%09fi
@@ -1094,18 +1094,16 @@
 );%0A%09%09if 
-(!
 (classLo
@@ -1140,85 +1140,148 @@
 der)
-)
  %7B%0A%09%09%09
-return new CivLogger(Logg
+final var plugin = ((PluginClassLoader) classLoad
 er
+)
 .get
-Logger(CivLogger.class.getSimpleName
+Plugin();%0A%09%09%09if (plugin != null) %7B%0A%09%09%09%09return new CivLogger(plugin.getLogger
 ()
-)
 , cl
@@ -1308,77 +1308,377 @@
 ;%0A%09%09
+%09
 %7D%0A%09%09
-final var plugin = ((PluginClassLoader) classLoader).getPlugin();
+%09// Plugin has been constructed but not initialised yet%0A%09%09%09final var loaderLoggerField = FieldUtils.getDeclaredField(PluginClassLoader.class, %22logger%22, true);%0A%09%09%09try %7B%0A%09%09%09%09final var loaderLogger = (Logger) loaderLoggerField.get(classLoader);%0A%09%09%09%09return new CivLogger(loaderLogger, clazz.getSimpleName());%0A%09%09%09%7D%0A%09%09%09catch (final IllegalAccessException ignored) %7B%7D%0A%09%09%7D
 %0A%09%09r
@@ -1689,38 +1689,38 @@
 n new CivLogger(
-plugin
+Logger
 .getLogger(), cl
@@ -1706,32 +1706,63 @@
 ogger.getLogger(
+CivLogger.class.getSimpleName()
 ), clazz.getSimp

Fix CivLogger issue if the logger detects a plugin but the plugin is not enabled yet