mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
add config holder.
This commit is contained in:
parent
3afe786b38
commit
643487e1a9
9 changed files with 271 additions and 69 deletions
|
|
@ -23,6 +23,7 @@ import org.bukkit.inventory.AnvilInventory
|
|||
import org.bukkit.inventory.InventoryView.Property.REPAIR_COST
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import org.bukkit.inventory.meta.Repairable
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||
import xyz.alexcrea.cuanvil.group.ConflictType
|
||||
import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair
|
||||
import kotlin.math.min
|
||||
|
|
@ -320,7 +321,7 @@ class AnvilEventListener : Listener {
|
|||
// count enchant as illegal enchant if it conflicts with another enchant or not in result
|
||||
if((enchantment.key !in resultEnchsKeys)){
|
||||
resultEnchsKeys.add(enchantment.key)
|
||||
val conflictType = CustomAnvil.conflictManager.isConflicting(resultEnchsKeys,result.type,enchantment.key)
|
||||
val conflictType = ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting(resultEnchsKeys,result.type,enchantment.key)
|
||||
resultEnchsKeys.remove(enchantment.key)
|
||||
|
||||
if(ConflictType.BIG_CONFLICT == conflictType){
|
||||
|
|
|
|||
|
|
@ -6,9 +6,8 @@ import org.bukkit.configuration.file.YamlConfiguration
|
|||
import org.bukkit.plugin.java.JavaPlugin
|
||||
import xyz.alexcrea.cuanvil.command.ReloadExecutor
|
||||
import xyz.alexcrea.cuanvil.command.TestExecutor
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||
import xyz.alexcrea.cuanvil.util.Metrics
|
||||
import xyz.alexcrea.cuanvil.group.EnchantConflictManager
|
||||
import xyz.alexcrea.cuanvil.group.ItemGroupManager
|
||||
import xyz.alexcrea.cuanvil.util.MetricsUtil
|
||||
import java.io.File
|
||||
import java.io.FileReader
|
||||
|
|
@ -37,17 +36,8 @@ class CustomAnvil : JavaPlugin() {
|
|||
// Test command name
|
||||
const val commandTestName = "test"
|
||||
|
||||
// Item Grouping Configuration file name
|
||||
const val itemGroupingConfigFilePath = "item_groups.yml"
|
||||
// Conflict Configuration file name
|
||||
const val enchantConflicConfigFilePath = "enchant_conflict.yml"
|
||||
// Unit Repair Configuration file name
|
||||
const val unitRepairFilePath = "unit_repair_item.yml"
|
||||
|
||||
// Current plugin instance
|
||||
lateinit var instance: CustomAnvil
|
||||
// Current item grouping configuration instance
|
||||
lateinit var conflictManager: EnchantConflictManager
|
||||
|
||||
// Configuration for unit repair
|
||||
lateinit var unitRepairConfig: YamlConfiguration
|
||||
|
|
@ -76,7 +66,9 @@ class CustomAnvil : JavaPlugin() {
|
|||
logger.warning("Please note CustomAnvil is a more recent version of UnsafeEnchantsPlus")
|
||||
}
|
||||
|
||||
val success = reloadAllConfigs(true)
|
||||
// Load config
|
||||
|
||||
val success = ConfigHolder.loadConfig();
|
||||
if(!success) return
|
||||
|
||||
// Load metrics
|
||||
|
|
@ -92,36 +84,8 @@ class CustomAnvil : JavaPlugin() {
|
|||
)
|
||||
}
|
||||
|
||||
fun reloadAllConfigs(hardFailSafe: Boolean): Boolean{
|
||||
saveDefaultConfig()
|
||||
reloadConfig()
|
||||
|
||||
// Load material grouping config
|
||||
val itemGroupConfig = reloadResource(itemGroupingConfigFilePath, hardFailSafe) ?: return false
|
||||
// Read material groups from config
|
||||
val itemGroupsManager = ItemGroupManager()
|
||||
itemGroupsManager.prepareGroups(itemGroupConfig)
|
||||
|
||||
// Load enchantment conflicts config
|
||||
val conflictConfig = reloadResource(enchantConflicConfigFilePath, hardFailSafe) ?: return false
|
||||
// Read conflicts from config and material group manager
|
||||
val conflictManager = EnchantConflictManager()
|
||||
conflictManager.prepareConflicts(conflictConfig,itemGroupsManager)
|
||||
|
||||
// Load unit repair config
|
||||
val unitRepairConfig = reloadResource(unitRepairFilePath, hardFailSafe) ?: return false
|
||||
|
||||
// Set the global variable
|
||||
CustomAnvil.conflictManager = conflictManager
|
||||
CustomAnvil.unitRepairConfig = unitRepairConfig
|
||||
|
||||
// Test if is default config
|
||||
MetricsUtil.testIfConfigIsDefault(config, itemGroupConfig, conflictConfig, unitRepairConfig)
|
||||
return true
|
||||
}
|
||||
|
||||
private fun reloadResource(resourceName: String,
|
||||
hardFailSafe:Boolean = true): YamlConfiguration?{
|
||||
fun reloadResource(resourceName: String,
|
||||
hardFailSafe:Boolean = true): YamlConfiguration?{
|
||||
// Save default resource
|
||||
val file = File(dataFolder,resourceName)
|
||||
if(!file.exists()){
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package io.delilaheve.util
|
|||
import io.delilaheve.CustomAnvil
|
||||
import io.delilaheve.util.EnchantmentUtil.enchantmentName
|
||||
import org.bukkit.enchantments.Enchantment
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||
|
||||
/**
|
||||
* Config option accessors
|
||||
|
|
@ -71,7 +72,7 @@ object ConfigOptions {
|
|||
*/
|
||||
private val defaultEnchantLimit: Int
|
||||
get() {
|
||||
return CustomAnvil.instance
|
||||
return ConfigHolder.DEFAULT_CONFIG
|
||||
.config
|
||||
.getInt(DEFAULT_LIMIT_PATH, DEFAULT_ENCHANT_LIMIT)
|
||||
}
|
||||
|
|
@ -81,7 +82,7 @@ object ConfigOptions {
|
|||
*/
|
||||
val limitRepairCost: Boolean
|
||||
get() {
|
||||
return CustomAnvil.instance
|
||||
return ConfigHolder.DEFAULT_CONFIG
|
||||
.config
|
||||
.getBoolean(LIMIT_REPAIR_COST, DEFAULT_LIMIT_REPAIR)
|
||||
}
|
||||
|
|
@ -91,7 +92,7 @@ object ConfigOptions {
|
|||
*/
|
||||
val limitRepairValue: Int
|
||||
get() {
|
||||
return CustomAnvil.instance
|
||||
return ConfigHolder.DEFAULT_CONFIG
|
||||
.config
|
||||
.getInt(LIMIT_REPAIR_VALUE, DEFAULT_LIMIT_REPAIR_VALUE)
|
||||
.takeIf { it in REPAIR_LIMIT_RANGE }
|
||||
|
|
@ -103,7 +104,7 @@ object ConfigOptions {
|
|||
*/
|
||||
val itemRepairCost: Int
|
||||
get() {
|
||||
return CustomAnvil.instance
|
||||
return ConfigHolder.DEFAULT_CONFIG
|
||||
.config
|
||||
.getInt(ITEM_REPAIR_COST, DEFAULT_ITEM_REPAIR_COST)
|
||||
.takeIf { it in REPAIR_COST_RANGE }
|
||||
|
|
@ -115,7 +116,7 @@ object ConfigOptions {
|
|||
*/
|
||||
val unitRepairCost: Int
|
||||
get() {
|
||||
return CustomAnvil.instance
|
||||
return ConfigHolder.DEFAULT_CONFIG
|
||||
.config
|
||||
.getInt(UNIT_REPAIR_COST, DEFAULT_UNIT_REPAIR_COST)
|
||||
.takeIf { it in REPAIR_COST_RANGE }
|
||||
|
|
@ -127,7 +128,7 @@ object ConfigOptions {
|
|||
*/
|
||||
val itemRenameCost: Int
|
||||
get() {
|
||||
return CustomAnvil.instance
|
||||
return ConfigHolder.DEFAULT_CONFIG
|
||||
.config
|
||||
.getInt(ITEM_RENAME_COST, DEFAULT_ITEM_RENAME_COST)
|
||||
.takeIf { it in ITEM_RENAME_COST_RANGE }
|
||||
|
|
@ -139,7 +140,7 @@ object ConfigOptions {
|
|||
*/
|
||||
val sacrificeIllegalCost: Int
|
||||
get() {
|
||||
return CustomAnvil.instance
|
||||
return ConfigHolder.DEFAULT_CONFIG
|
||||
.config
|
||||
.getInt(SACRIFICE_ILLEGAL_COST, DEFAULT_SACRIFICE_ILLEGAL_COST)
|
||||
.takeIf { it in SACRIFICE_ILLEGAL_COST_RANGE }
|
||||
|
|
@ -150,7 +151,7 @@ object ConfigOptions {
|
|||
*/
|
||||
val removeRepairLimit: Boolean
|
||||
get() {
|
||||
return CustomAnvil.instance
|
||||
return ConfigHolder.DEFAULT_CONFIG
|
||||
.config
|
||||
.getBoolean(REMOVE_REPAIR_LIMIT, DEFAULT_REMOVE_LIMIT)
|
||||
}
|
||||
|
|
@ -160,7 +161,7 @@ object ConfigOptions {
|
|||
*/
|
||||
val debugLog: Boolean
|
||||
get() {
|
||||
return CustomAnvil.instance
|
||||
return ConfigHolder.DEFAULT_CONFIG
|
||||
.config
|
||||
.getBoolean(DEBUG_LOGGING, DEFAULT_DEBUG_LOG)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import io.delilaheve.CustomAnvil
|
|||
import org.bukkit.Material
|
||||
import org.bukkit.enchantments.Enchantment
|
||||
import org.bukkit.entity.HumanEntity
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||
import xyz.alexcrea.cuanvil.group.ConflictType
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
|
@ -34,7 +35,7 @@ object EnchantmentUtil {
|
|||
// Add the enchantment if it doesn't have conflicts, or, if player is allowed to bypass enchantment restrictions
|
||||
this[enchantment] = level
|
||||
if(!player.hasPermission(CustomAnvil.bypassFusePermission) &&
|
||||
(CustomAnvil.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)){
|
||||
(ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)){
|
||||
this.remove(enchantment)
|
||||
}
|
||||
|
||||
|
|
@ -42,7 +43,7 @@ object EnchantmentUtil {
|
|||
// Enchantment already in result list
|
||||
else{
|
||||
// ... and they are conflicting
|
||||
if((CustomAnvil.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)
|
||||
if((ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)
|
||||
&& !player.hasPermission(CustomAnvil.bypassFusePermission)){
|
||||
return@forEach
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import io.delilaheve.CustomAnvil
|
|||
import org.bukkit.command.Command
|
||||
import org.bukkit.command.CommandExecutor
|
||||
import org.bukkit.command.CommandSender
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||
|
||||
class ReloadExecutor : CommandExecutor {
|
||||
override fun onCommand(sender: CommandSender, cmd: Command, cmdstr: String, args: Array<out String>): Boolean {
|
||||
|
|
@ -30,7 +31,7 @@ class ReloadExecutor : CommandExecutor {
|
|||
*/
|
||||
private fun commandBody(hardfail: Boolean): Boolean{
|
||||
try {
|
||||
return CustomAnvil.instance.reloadAllConfigs(hardfail)
|
||||
return ConfigHolder.reloadAllFromDisk(hardfail);
|
||||
}catch (e: Exception){
|
||||
e.printStackTrace()
|
||||
return false
|
||||
|
|
|
|||
|
|
@ -4,9 +4,7 @@ import io.delilaheve.CustomAnvil
|
|||
import org.bukkit.Material
|
||||
import org.bukkit.NamespacedKey
|
||||
import org.bukkit.configuration.ConfigurationSection
|
||||
import org.bukkit.configuration.file.YamlConfiguration
|
||||
import org.bukkit.enchantments.Enchantment
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
class EnchantConflictManager {
|
||||
|
||||
|
|
@ -29,7 +27,7 @@ class EnchantConflictManager {
|
|||
private lateinit var conflictMap: HashMap<Enchantment, ArrayList<EnchantConflictGroup>>
|
||||
|
||||
// Read and prepare all conflict
|
||||
fun prepareConflicts(config: YamlConfiguration, itemManager: ItemGroupManager){
|
||||
fun prepareConflicts(config: ConfigurationSection, itemManager: ItemGroupManager){
|
||||
conflictMap = HashMap()
|
||||
|
||||
val keys = config.getKeys(false)
|
||||
|
|
@ -140,7 +138,7 @@ class EnchantConflictManager {
|
|||
|
||||
}
|
||||
|
||||
enum class ConflictType(){
|
||||
enum class ConflictType{
|
||||
NO_CONFLICT,
|
||||
SMALL_CONFLICT,
|
||||
BIG_CONFLICT
|
||||
|
|
|
|||
|
|
@ -3,9 +3,7 @@ package xyz.alexcrea.cuanvil.group
|
|||
import io.delilaheve.CustomAnvil
|
||||
import org.bukkit.Material
|
||||
import org.bukkit.configuration.ConfigurationSection
|
||||
import org.bukkit.configuration.file.YamlConfiguration
|
||||
import java.util.*
|
||||
import kotlin.collections.HashMap
|
||||
|
||||
class ItemGroupManager {
|
||||
|
||||
|
|
@ -23,7 +21,7 @@ class ItemGroupManager {
|
|||
private lateinit var groupMap : HashMap<String, AbstractMaterialGroup>
|
||||
|
||||
// Read and create material groups
|
||||
fun prepareGroups(config: YamlConfiguration){
|
||||
fun prepareGroups(config: ConfigurationSection){
|
||||
groupMap = HashMap()
|
||||
|
||||
val keys = config.getKeys(false)
|
||||
|
|
@ -35,7 +33,7 @@ class ItemGroupManager {
|
|||
}
|
||||
|
||||
// Create group by key
|
||||
private fun createGroup(config: YamlConfiguration,
|
||||
private fun createGroup(config: ConfigurationSection,
|
||||
keys: Set<String>,
|
||||
key: String): AbstractMaterialGroup {
|
||||
val groupSection = config.getConfigurationSection(key)!!
|
||||
|
|
@ -60,7 +58,7 @@ class ItemGroupManager {
|
|||
// Read Group elements
|
||||
private fun readGroup(group: AbstractMaterialGroup,
|
||||
groupSection: ConfigurationSection,
|
||||
config: YamlConfiguration,
|
||||
config: ConfigurationSection,
|
||||
keys: Set<String>){
|
||||
// Read material to include in this group policy
|
||||
val materialList = groupSection.getStringList(MATERIAL_LIST_PATH)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
package xyz.alexcrea.cuanvil.util
|
||||
|
||||
import io.delilaheve.CustomAnvil
|
||||
import org.bukkit.configuration.ConfigurationSection
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||
|
||||
object UnitRepairUtil {
|
||||
|
||||
|
|
@ -19,15 +19,14 @@ object UnitRepairUtil {
|
|||
other: ItemStack?
|
||||
): Double? {
|
||||
if(other == null) return null
|
||||
val config = CustomAnvil.unitRepairConfig
|
||||
val config = ConfigHolder.UNIT_REPAIR_HOLDER.config
|
||||
// Get configuration section if exist
|
||||
val otherName = other.type.name.uppercase()
|
||||
var section = config.getConfigurationSection(otherName)
|
||||
if(section == null){
|
||||
section = config.getConfigurationSection(otherName.lowercase())
|
||||
if(section == null) {
|
||||
return null
|
||||
}
|
||||
if(section == null) return null
|
||||
|
||||
}
|
||||
// Get repair amount
|
||||
var userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue