From de86fe16babb3ab8307d897f084323560f7653c4 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Thu, 13 Aug 2026 10:10:37 +0200 Subject: [PATCH] translation key for load errors and warnings --- src/main/kotlin/io/delilaheve/CustomAnvil.kt | 79 +++++++++++--------- src/main/resources/lang/en.yml | 29 +++++++ 2 files changed, 71 insertions(+), 37 deletions(-) create mode 100644 src/main/resources/lang/en.yml diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt index c88acfd3..8c284ef4 100644 --- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt +++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt @@ -7,8 +7,6 @@ import org.bukkit.plugin.java.JavaPlugin import xyz.alexcrea.cuanvil.api.event.CAConfigReadyEvent import xyz.alexcrea.cuanvil.api.event.CAEnchantRegistryReadyEvent import xyz.alexcrea.cuanvil.command.CustomAnvilCommand -import xyz.alexcrea.cuanvil.command.EditConfigExecutor -import xyz.alexcrea.cuanvil.command.ReloadExecutor import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.dependency.DependencyManager import xyz.alexcrea.cuanvil.dependency.MinecraftVersionUtil @@ -18,6 +16,7 @@ import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry import xyz.alexcrea.cuanvil.gui.config.MainConfigGui import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant import xyz.alexcrea.cuanvil.lang.Lang +import xyz.alexcrea.cuanvil.lang.Lang.translate import xyz.alexcrea.cuanvil.listener.AnvilCloseListener import xyz.alexcrea.cuanvil.listener.AnvilResultListener import xyz.alexcrea.cuanvil.listener.ChatEventListener @@ -109,11 +108,23 @@ open class CustomAnvil : JavaPlugin() { } } + /** + * Error Logging handler + */ + @JvmStatic fun logError(message: String, throwable: Throwable? = null, track: Boolean = true) { + instance.logger.log(Level.SEVERE, message, throwable) + addToLogQueue(message) + + if(track && throwable != null) { + MetricsUtil.trackError(throwable) + } + } } // stop plugin if we do not force a dirty start (true by default) // Return true if start was stopped private fun tryDirtyStart(): Boolean { + if(ConfigHolder.DEFAULT_CONFIG == null) return false if(!ConfigHolder.DEFAULT_CONFIG.config.getBoolean("dirty_start", false)) { Bukkit.getPluginManager().disablePlugin(this) return true @@ -124,6 +135,7 @@ open class CustomAnvil : JavaPlugin() { // stop plugin if we force a safe start (false by default) // Return true if start was stopped private fun trySafeStart(): Boolean { + if(ConfigHolder.DEFAULT_CONFIG == null) return false if(ConfigHolder.DEFAULT_CONFIG.config.getBoolean("safe_start", false)) { Bukkit.getPluginManager().disablePlugin(this) return true @@ -141,8 +153,7 @@ open class CustomAnvil : JavaPlugin() { if(!ConfigHolder.loadDefaultConfig()) throw RuntimeException("Error loading configuration file") } catch (e: Exception) { - logger.log(Level.SEVERE, "error occurred loading default configuration", e) - MetricsUtil.trackError(e) + logError("error occurred loading default configuration", e) if(tryDirtyStart()) return } @@ -150,16 +161,14 @@ open class CustomAnvil : JavaPlugin() { try { Lang.reload() } catch (e: Exception) { - logger.log(Level.SEVERE, "error occurred loading language file", e) - MetricsUtil.trackError(e) + logError("error occurred loading language file", e) if(tryDirtyStart()) return } try { legacyCheck() } catch (e: Exception) { - logger.log(Level.SEVERE, "error trying to check for legacy system", e) - MetricsUtil.trackError(e) + logError("error.load.legacy.failed".translate(), e) if(trySafeStart()) return } @@ -168,8 +177,7 @@ open class CustomAnvil : JavaPlugin() { try { CustomAnvilCommand(this) } catch (e: Exception) { - logger.log(Level.SEVERE, "error trying to register commands", e) - MetricsUtil.trackError(e) + logError("error.load.command-register".translate(), e) if(trySafeStart()) return } @@ -178,8 +186,7 @@ open class CustomAnvil : JavaPlugin() { try { DependencyManager.loadDependency() } catch (e: Exception) { - logger.log(Level.SEVERE, "error loading dependency compatibility", e) - MetricsUtil.trackError(e) + logError("error.load.compatibility".translate(), e) if(tryDirtyStart()) return } @@ -187,8 +194,7 @@ open class CustomAnvil : JavaPlugin() { try { registerListeners() } catch (e: Exception) { - logger.log(Level.SEVERE, "error registering listeners", e) - MetricsUtil.trackError(e) + logError("error.load.listeners".translate(), e) if(tryDirtyStart()) return } @@ -204,32 +210,22 @@ open class CustomAnvil : JavaPlugin() { MetricsUtil.shutdownMetrics() } - private fun loadEnchantmentSystemDirty() { - try { - loadEnchantmentSystem() - } catch (e: Exception) { - logger.log(Level.SEVERE, "error initializing enchantment system", e) - MetricsUtil.trackError(e) - tryDirtyStart() - } - } - private fun legacyCheck() { // Disable old plugin name if exist val potentialPlugin = Bukkit.getPluginManager().getPlugin("UnsafeEnchantsPlus") if (potentialPlugin != null) { Bukkit.getPluginManager().disablePlugin(potentialPlugin) - logger.warning("An old version of this plugin was detected") - logger.warning("Please note CustomAnvil is a more recent version of UnsafeEnchantsPlus") + logger.warning("warning.load.legacy.old-name.1".translate()) + logger.warning("warning.load.legacy.old-name.2".translate()) } val isPaper = PlatformUtil.isPaper if(!isPaper) { - logger.warning("It seems you are using spigot") - logger.warning("Please take notice that spigot is less supported than paper and derivatives") + logger.warning("warning.load.legacy.spigot.1".translate()) + logger.warning("warning.load.legacy.spigot.2".translate()) if(MinecraftVersionUtil.isTooNewForSpigot) { - logger.warning("If replace too expensive is not working this is likely because of spigot") - logger.warning("As native nms is not supported for spigot starting 26.1") + logger.warning("warning.load.legacy.spigot-old.1".translate()) + logger.warning("warning.load.legacy.spigot-old.1".translate()) } } @@ -242,13 +238,13 @@ open class CustomAnvil : JavaPlugin() { UpdateUtils.currentMinecraftVersion().toString()) .setFeatured(featured) .setOnError { - logger.log(Level.WARNING, "error trying to fetch latest update", it) + logger.log(Level.WARNING, "error.load.update.check-fail".translate(), it) } .checkVersion { latestVer: String? -> CustomAnvil.latestVer = latestVer if(latestVer == null || version.contains(latestVer)) return@checkVersion - logger.warning("An update may be available: $latestVer") + logger.warning("warning.load.update.available".translate(Pair("version", latestVer))) } } @@ -263,6 +259,15 @@ open class CustomAnvil : JavaPlugin() { server.pluginManager.registerEvents(AnvilCloseListener(DependencyManager.packetManager), this) } + private fun loadEnchantmentSystemDirty() { + try { + loadEnchantmentSystem() + } catch (e: Exception) { + logError("error.load.enchant-system".translate(), e) + tryDirtyStart() + } + } + private fun loadEnchantmentSystem(){ // Register enchantments CAEnchantmentRegistry.getInstance().registerBukkit() @@ -273,7 +278,7 @@ open class CustomAnvil : JavaPlugin() { // Load config if (!ConfigHolder.loadNonDefaultConfig()) { - logger.log(Level.SEVERE,"Plugin has an issue while trying to load non default config... exiting...") + logError("error.load.non-default-config".translate()) server.pluginManager.disablePlugin(this) return } @@ -327,15 +332,15 @@ open class CustomAnvil : JavaPlugin() { try { val configReader = FileReader(resourceFile) yamlConfig.load(configReader) - } catch (test: Exception) { + } catch (_: Exception) { if (hardFailSafe) { // This is important and may impact gameplay if it does not load. // Failsafe is to stop the plugin - logger.severe("Resource ${resourceFile.path} Could not be load or reload.") - logger.severe("Disabling plugin.") + logError("error.reload.resource.fail".translate(Pair("path", resourceFile.path))) + logError("error.reload.resource.hard-fail".translate()) Bukkit.getPluginManager().disablePlugin(this) } else { - logger.warning("Resource ${resourceFile.path} Could not be load or reload.") + logError("error.reload.resource.fail".translate(Pair("path", resourceFile.path))) } return null } diff --git a/src/main/resources/lang/en.yml b/src/main/resources/lang/en.yml new file mode 100644 index 00000000..d466dd53 --- /dev/null +++ b/src/main/resources/lang/en.yml @@ -0,0 +1,29 @@ +name: English + +warning: + load: + legacy: + old-name: + 1: "An old version of this plugin was detected" + 2: "Please note CustomAnvil is a more recent version of UnsafeEnchantsPlus" + spigot: + 1: "It seems you are using spigot" + 2: "Please take notice that spigot is less supported than paper and derivatives" + spigot-old: + 1: "If replace too expensive is not working this is likely because of spigot" + 2: "As native nms is not supported for spigot starting 26.1" + update.available: "An update may be available: %version" + +error: + load: + legacy.failed: "error trying to check for legacy system" + update.check-fail: "error trying to fetch latest update" + command-register: "error trying to register commands" + compatibility: "error loading dependency compatibility" + listeners: "error registering listeners" + enchant-system: "error initializing enchantment system" + non-default-config: "Plugin has an issue while trying to load non default config... exiting..." + reload: + resource: + fail: "Resource %path Could not be loaded or reloaded." + hard-fail: "Disabling plugin."