Make compatibility object with EcoEnchant and Enchant² use the api.

Abstract material group now have varargs "addAll" function.
This commit is contained in:
alexcrea 2024-07-08 23:24:39 +02:00
parent 1bd3328281
commit bcd8b6ae6e
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
7 changed files with 100 additions and 79 deletions

View file

@ -343,4 +343,13 @@ public class ConflictBuilder {
return clone; return clone;
} }
/**
* Register this conflict.
* Equivalent to {@link ConflictAPI#addConflict(ConflictBuilder)}
* @return True if successful.
*/
public boolean registerIfAbsent(){
return ConflictAPI.addConflict(this);
}
} }

View file

@ -1,10 +1,12 @@
package xyz.alexcrea.cuanvil.dependency package xyz.alexcrea.cuanvil.dependency
import org.bukkit.Bukkit import org.bukkit.Bukkit
import org.bukkit.configuration.file.FileConfiguration
import org.bukkit.plugin.Plugin import org.bukkit.plugin.Plugin
import xyz.alexcrea.cuanvil.dependency.protocolib.NoProtocoLib import xyz.alexcrea.cuanvil.dependency.protocolib.NoProtocoLib
import xyz.alexcrea.cuanvil.dependency.protocolib.PacketManager import xyz.alexcrea.cuanvil.dependency.protocolib.PacketManager
import xyz.alexcrea.cuanvil.dependency.protocolib.ProtocoLibWrapper import xyz.alexcrea.cuanvil.dependency.protocolib.ProtocoLibWrapper
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import java.io.File import java.io.File
object DependencyManager { object DependencyManager {
@ -43,4 +45,12 @@ object DependencyManager {
} }
fun writeDefaultConfig(defaultConfig: FileConfiguration, enchantment: CAEnchantment) {
defaultConfig["enchant_limits.${enchantment.key.key}"] = enchantment.defaultMaxLevel()
val rarity = enchantment.defaultRarity()
defaultConfig["enchant_values.${enchantment.key.key}.item"] = rarity.itemValue
defaultConfig["enchant_values.${enchantment.key.key}.book"] = rarity.bookValue
}
} }

View file

@ -5,10 +5,9 @@ import io.delilaheve.CustomAnvil
import org.bukkit.configuration.file.YamlConfiguration import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.plugin.Plugin import org.bukkit.plugin.Plugin
import xyz.alexcrea.cuanvil.api.EnchantmentApi
import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
import xyz.alexcrea.cuanvil.enchant.wrapped.CAEcoEnchant
import java.io.File import java.io.File
class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) { class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
@ -22,12 +21,9 @@ class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
} }
fun registerEnchantments() { fun registerEnchantments() {
val registery = CAEnchantmentRegistry.getInstance()
for (ecoEnchant in EcoEnchants.values()) { for (ecoEnchant in EcoEnchants.values()) {
val enchantments: CAEnchantment = CAEcoEnchant(ecoEnchant) EnchantmentApi.unregisterEnchantment(ecoEnchant.enchantment) // As eco enchants is loaded before ca, we need to unregister old "vanilla" enchant.
EnchantmentApi.registerEnchantment(ecoEnchant.enchantment)
registery.unregister(registery.getByKey(ecoEnchant.enchantment.key)) // As eco enchants are considered real enchantment, we need to unregister it.
registery.register(enchantments)
} }
} }
@ -57,12 +53,7 @@ class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
doSave = true doSave = true
config[testPath] = true config[testPath] = true
defaultConfig["enchant_limits.${enchantment.key.key}"] = enchantment.defaultMaxLevel() DependencyManager.writeDefaultConfig(defaultConfig, enchantment)
val rarity = enchantment.defaultRarity()
defaultConfig["enchant_values.${enchantment.key.key}.item"] = rarity.itemValue
defaultConfig["enchant_values.${enchantment.key.key}.book"] = rarity.bookValue
} }
} }

View file

@ -3,14 +3,18 @@ package xyz.alexcrea.cuanvil.dependency
import io.delilaheve.CustomAnvil import io.delilaheve.CustomAnvil
import me.athlaeos.enchantssquared.enchantments.CustomEnchant import me.athlaeos.enchantssquared.enchantments.CustomEnchant
import me.athlaeos.enchantssquared.managers.CustomEnchantManager import me.athlaeos.enchantssquared.managers.CustomEnchantManager
import org.bukkit.Material
import org.bukkit.NamespacedKey import org.bukkit.NamespacedKey
import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.inventory.ItemStack import org.bukkit.inventory.ItemStack
import org.bukkit.plugin.Plugin import org.bukkit.plugin.Plugin
import xyz.alexcrea.cuanvil.api.ConflictBuilder
import xyz.alexcrea.cuanvil.api.MaterialGroupApi
import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
import xyz.alexcrea.cuanvil.enchant.wrapped.CAEnchantSquaredEnchantment import xyz.alexcrea.cuanvil.enchant.wrapped.CAEnchantSquaredEnchantment
import xyz.alexcrea.cuanvil.group.IncludeGroup
import java.util.* import java.util.*
class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) { class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) {
@ -78,11 +82,7 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
// Write default level limit and xp cost // Write default level limit and xp cost
for (enchantment in esEnchantments) { for (enchantment in esEnchantments) {
defaultConfig["enchant_limits.${enchantment.key.key}"] = enchantment.defaultMaxLevel() DependencyManager.writeDefaultConfig(defaultConfig, enchantment)
val rarity = enchantment.defaultRarity()
defaultConfig["enchant_values.${enchantment.key.key}.item"] = rarity.itemValue
defaultConfig["enchant_values.${enchantment.key.key}.book"] = rarity.bookValue
} }
// Write groups and conflicts // Write groups and conflicts
@ -95,11 +95,6 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
// Save // Save
ConfigHolder.DEFAULT_CONFIG.saveToDisk(true) ConfigHolder.DEFAULT_CONFIG.saveToDisk(true)
ConfigHolder.ITEM_GROUP_HOLDER.saveToDisk(true)
ConfigHolder.CONFLICT_HOLDER.saveToDisk(true)
// Reload
ConfigHolder.ITEM_GROUP_HOLDER.reload()
CustomAnvil.instance.logger.info("Enchantment Squared should now work as expected !") CustomAnvil.instance.logger.info("Enchantment Squared should now work as expected !")
} }
@ -107,60 +102,50 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
private fun writeMissingGroups(){ private fun writeMissingGroups(){
// Write group that do not exist on custom anvil. // Write group that do not exist on custom anvil.
// (Tools group regroup most of the tool items. I did not create a seperated group for theses) // (Tools group regroup most of the tool items. I did not create a seperated group for theses)
val groupConfig = ConfigHolder.ITEM_GROUP_HOLDER.config val pickaxes = IncludeGroup("pickaxes")
if(!groupConfig.isConfigurationSection("pickaxes")){ pickaxes.addAll(Material.WOODEN_PICKAXE, Material.STONE_PICKAXE, Material.IRON_PICKAXE, Material.DIAMOND_PICKAXE, Material.GOLDEN_PICKAXE, Material.NETHERITE_PICKAXE)
groupConfig["pickaxes.type"] = "include" MaterialGroupApi.addMaterialGroup(pickaxes)
groupConfig["pickaxes.items"] = listOf("wooden_pickaxe", "stone_pickaxe", "iron_pickaxe", "diamond_pickaxe", "golden_pickaxe", "netherite_pickaxe")
}
if(!groupConfig.isConfigurationSection("shovels")){ val shovels = IncludeGroup("shovels")
groupConfig["shovels.type"] = "include" shovels.addAll(Material.WOODEN_SHOVEL, Material.STONE_SHOVEL, Material.IRON_SHOVEL, Material.DIAMOND_SHOVEL, Material.GOLDEN_SHOVEL, Material.NETHERITE_SHOVEL)
groupConfig["shovels.items"] = listOf("wooden_shovel", "stone_shovel", "iron_shovel", "diamond_shovel", "golden_shovel", "netherite_shovel") MaterialGroupApi.addMaterialGroup(shovels)
}
if(!groupConfig.isConfigurationSection("hoes")){ val hoes = IncludeGroup("hoes")
groupConfig["hoes.type"] = "include" hoes.addAll(Material.WOODEN_HOE, Material.STONE_HOE, Material.IRON_HOE, Material.DIAMOND_HOE, Material.GOLDEN_HOE, Material.NETHERITE_HOE)
groupConfig["hoes.items"] = listOf("wooden_hoe", "stone_hoe", "iron_hoe", "diamond_hoe", "golden_hoe", "netherite_hoe") MaterialGroupApi.addMaterialGroup(hoes)
}
if(!groupConfig.isConfigurationSection("shield")){ val shield = IncludeGroup("shield")
groupConfig["shield.type"] = "include" hoes.addToPolicy(Material.SHIELD)
groupConfig["shield.items"] = listOf("shield") MaterialGroupApi.addMaterialGroup(shield)
}
if(!groupConfig.isConfigurationSection("elytra")){ val elytra = IncludeGroup("elytra")
groupConfig["elytra.type"] = "include" hoes.addToPolicy(Material.ELYTRA)
groupConfig["elytra.items"] = listOf("elytra") MaterialGroupApi.addMaterialGroup(elytra)
}
if(!groupConfig.isConfigurationSection("trinkets")){ val trinkets = IncludeGroup("trinkets")
groupConfig["trinkets.type"] = "include" hoes.addToPolicy(Material.ROTTEN_FLESH)
groupConfig["trinkets.items"] = listOf("rotten_flesh") MaterialGroupApi.addMaterialGroup(trinkets)
}
} }
private fun writeMaterialRestriction(esEnchantments: List<CAEnchantSquaredEnchantment>){ private fun writeMaterialRestriction(esEnchantments: List<CAEnchantSquaredEnchantment>){
val conflictConfig = ConfigHolder.CONFLICT_HOLDER.config
for (enchantment in esEnchantments) { for (enchantment in esEnchantments) {
val restrictionName = "restriction_${enchantment.key.key}" val conflict = ConflictBuilder("restriction_${enchantment.key.key}")
if(!conflictConfig.isConfigurationSection(restrictionName)){
conflictConfig["$restrictionName.enchantments"] = listOf(enchantment.name)
// Get allowed groups // enchanted book is allowed in any case.
val listOfAllowed = ArrayList<String>() conflict.addExcludedGroup("enchanted_book")
listOfAllowed.add("enchanted_book") // enchanted book is allowed in any case.
for (esGroup in enchantment.enchant.compatibleItems) { // Get allowed groups
val caGroup = esGroupToCAGroup(esGroup) for (esGroup in enchantment.enchant.compatibleItems) {
if(caGroup == null){ val caGroup = esGroupToCAGroup(esGroup)
CustomAnvil.instance.logger.info("Could not find equivalent custom anvil group for $esGroup") if(caGroup == null){
continue CustomAnvil.instance.logger.info("Could not find equivalent custom anvil group for $esGroup")
} continue
listOfAllowed.add(caGroup)
} }
conflictConfig["$restrictionName.notAffectedGroups"] = listOfAllowed conflict.addExcludedGroup(caGroup)
} }
conflict.registerIfAbsent()
} }
} }
@ -181,18 +166,12 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
} }
private fun writeConflict(enchantment1: CAEnchantment, enchantment2: CAEnchantment){ private fun writeConflict(enchantment1: CAEnchantment, enchantment2: CAEnchantment){
val conflictConfig = ConfigHolder.CONFLICT_HOLDER.config val conflict = ConflictBuilder("${enchantment1.name}_with_${enchantment2.name}_conflict")
val conflictPath = "${enchantment1.name}_with_${enchantment2.name}_conflict"
if(!conflictConfig.isConfigurationSection(conflictPath)){ conflict.addEnchantment(enchantment1).addEnchantment(enchantment2)
conflictConfig["$conflictPath.enchantments"] = listOf(enchantment1.name, enchantment2.name)
val empty: List<String> = Collections.emptyList()
conflictConfig["$conflictPath.notAffectedGroups"] = empty
conflictConfig["$conflictPath.maxEnchantmentBeforeConflict"] = 1
}
conflict.setMaxBeforeConflict(1);
conflict.registerIfAbsent()
} }
/** /**

View file

@ -25,13 +25,37 @@ abstract class AbstractMaterialGroup(private val name: String) {
/** /**
* Push a material to this group to follow this group policy * Push a material to this group to follow this group policy
* @return this instance.
*/ */
abstract fun addToPolicy(mat: Material) abstract fun addToPolicy(mat: Material): AbstractMaterialGroup
/**
* Push a list of material to this group to follow this group policy
* @return this instance.
*/
fun addAll(vararg materials: Material): AbstractMaterialGroup {
for (material in materials) {
addToPolicy(material)
}
return this
}
/** /**
* Push a group to this group to follow this group policy * Push a group to this group to follow this group policy
* @return this instance.
*/ */
abstract fun addToPolicy(other: AbstractMaterialGroup) abstract fun addToPolicy(other: AbstractMaterialGroup): AbstractMaterialGroup
/**
* Push a list of group to this group to follow this group policy
* @return this instance.
*/
fun addAll(vararg otherList: AbstractMaterialGroup): AbstractMaterialGroup {
for (group in otherList) {
addToPolicy(group)
}
return this
}
/** /**
* Get the group contained material as a set * Get the group contained material as a set

View file

@ -20,14 +20,18 @@ class ExcludeGroup(name: String) : AbstractMaterialGroup(name) {
return false return false
} }
override fun addToPolicy(mat: Material) { override fun addToPolicy(mat: Material): ExcludeGroup {
includedMaterial.remove(mat) includedMaterial.remove(mat)
groupItems.remove(mat) groupItems.remove(mat)
return this
} }
override fun addToPolicy(other: AbstractMaterialGroup) { override fun addToPolicy(other: AbstractMaterialGroup): ExcludeGroup {
includedGroup.add(other) includedGroup.add(other)
groupItems.removeAll(other.getMaterials()) groupItems.removeAll(other.getMaterials())
return this
} }
override fun setGroups(groups: MutableSet<AbstractMaterialGroup>) { override fun setGroups(groups: MutableSet<AbstractMaterialGroup>) {

View file

@ -20,14 +20,18 @@ class IncludeGroup(name: String) : AbstractMaterialGroup(name) {
return false return false
} }
override fun addToPolicy(mat: Material) { override fun addToPolicy(mat: Material): IncludeGroup {
includedMaterial.add(mat) includedMaterial.add(mat)
groupItems.add(mat) groupItems.add(mat)
return this
} }
override fun addToPolicy(other: AbstractMaterialGroup) { override fun addToPolicy(other: AbstractMaterialGroup): IncludeGroup {
includedGroup.add(other) includedGroup.add(other)
groupItems.addAll(other.getMaterials()) groupItems.addAll(other.getMaterials())
return this
} }
override fun setGroups(groups: MutableSet<AbstractMaterialGroup>) { override fun setGroups(groups: MutableSet<AbstractMaterialGroup>) {