add reload config command

This commit is contained in:
alexcrea 2024-02-10 22:59:30 +01:00
parent d9c7a9376b
commit 2f051a93e3
3 changed files with 70 additions and 9 deletions

View file

@ -4,6 +4,7 @@ import io.delilaheve.util.ConfigOptions
import org.bukkit.Bukkit
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.plugin.java.JavaPlugin
import xyz.alexcrea.command.ReloadExecutor
import xyz.alexcrea.group.EnchantConflictManager
import xyz.alexcrea.group.ItemGroupManager
import xyz.alexcrea.util.Metrics
@ -27,6 +28,11 @@ class UnsafeEnchants : JavaPlugin() {
const val bypassFusePermission = "ue.bypass.fuse"
// Permission string required to bypass enchantment conflicts test
const val bypassLevelPermission = "ue.bypass.level"
// Permission string required to reload the config
const val commandReloadPermission = "ue.command.reload"
// Command Name to reload the config
const val commandReloadName = "reloadunsafeenchants"
// Item Grouping Configuration file name
const val itemGroupingConfigFilePath = "item_groups.yml"
@ -60,10 +66,24 @@ class UnsafeEnchants : JavaPlugin() {
instance = this
// Load bstats
val metric = Metrics(this, bstatsPluginId)
saveDefaultConfig()
reloadAllConfigs()
addCustomMetric(metric)
// Add command to reload the plugin
val command = getCommand(commandReloadName)
command?.setExecutor(ReloadExecutor())
server.pluginManager.registerEvents(
AnvilEventListener(),
this
)
}
fun reloadAllConfigs(){
saveDefaultConfig()
// Load material grouping config
val itemGroupConfig = reloadResource(itemGroupingConfigFilePath) ?: return
// Read material groups from config
@ -73,17 +93,15 @@ class UnsafeEnchants : JavaPlugin() {
// Load enchantment conflicts config
val conflictConfig = reloadResource(enchantConflicConfigFilePath) ?: return
// Read conflicts from config and material group manager
conflictManager = EnchantConflictManager()
val conflictManager = EnchantConflictManager()
conflictManager.prepareConflicts(conflictConfig,itemGroupsManager)
// Load unit repair config
unitRepairConfig = reloadResource(unitRepairFilePath) ?: return
server.pluginManager.registerEvents(
AnvilEventListener(),
this
)
val unitRepairConfig = reloadResource(unitRepairFilePath) ?: return
// Set the global variable
UnsafeEnchants.conflictManager = conflictManager
UnsafeEnchants.unitRepairConfig = unitRepairConfig
}
private fun addCustomMetric(metric: Metrics) {
@ -96,6 +114,7 @@ class UnsafeEnchants : JavaPlugin() {
metric.addCustomChart(SimplePie("sacrifice_illegal_enchant_cost") {
ConfigOptions.sacrificeIllegalCost.toString()
})
}
private fun reloadResource(resourceName: String,

View file

@ -0,0 +1,35 @@
package xyz.alexcrea.command
import io.delilaheve.UnsafeEnchants
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
class ReloadExecutor : CommandExecutor {
override fun onCommand(sender: CommandSender, cmd: Command, cmdstr: String, args: Array<out String>): Boolean {
if(!sender.hasPermission(UnsafeEnchants.commandReloadPermission)) {
sender.sendMessage("§cYou do not have permission to reload the config")
return false
}
sender.sendMessage("§eReloading config...")
val commandSuccess = commandBody()
if(commandSuccess){
sender.sendMessage("§aConfig reloaded !")
}else{
sender.sendMessage("§cConfig was not able to be reloaded...")
}
return commandSuccess
}
/**
* Execute the command, return true if success or false otherwise
*/
private fun commandBody(): Boolean{
try {
UnsafeEnchants.instance.reloadAllConfigs()
return true
}catch (ignored: Exception){
return false
}
}
}

View file

@ -8,6 +8,10 @@ load: POSTWORLD
authors: [DelilahEve, alexcrea]
libraries:
- org.jetbrains.kotlin:kotlin-stdlib:1.6.21
commands:
reloadunsafeenchants:
description: Reload every config of this plugin
permission: ue.command.reload
permissions:
ue.unsafe:
default: true
@ -17,4 +21,7 @@ permissions:
description: Allow player to combine every "unsafe" enchants
ue.bypass.level:
default: false
description: Allow player to bypass max level limit
description: Allow player to bypass max level limit
ue.command.reload:
default: op
description: Allow administrator to reload the plugin's config