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;
}
/**
* 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
import org.bukkit.Bukkit
import org.bukkit.configuration.file.FileConfiguration
import org.bukkit.plugin.Plugin
import xyz.alexcrea.cuanvil.dependency.protocolib.NoProtocoLib
import xyz.alexcrea.cuanvil.dependency.protocolib.PacketManager
import xyz.alexcrea.cuanvil.dependency.protocolib.ProtocoLibWrapper
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import java.io.File
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.event.inventory.PrepareAnvilEvent
import org.bukkit.plugin.Plugin
import xyz.alexcrea.cuanvil.api.EnchantmentApi
import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
import xyz.alexcrea.cuanvil.enchant.wrapped.CAEcoEnchant
import java.io.File
class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
@ -22,12 +21,9 @@ class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
}
fun registerEnchantments() {
val registery = CAEnchantmentRegistry.getInstance()
for (ecoEnchant in EcoEnchants.values()) {
val enchantments: CAEnchantment = CAEcoEnchant(ecoEnchant)
registery.unregister(registery.getByKey(ecoEnchant.enchantment.key)) // As eco enchants are considered real enchantment, we need to unregister it.
registery.register(enchantments)
EnchantmentApi.unregisterEnchantment(ecoEnchant.enchantment) // As eco enchants is loaded before ca, we need to unregister old "vanilla" enchant.
EnchantmentApi.registerEnchantment(ecoEnchant.enchantment)
}
}
@ -57,12 +53,7 @@ class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
doSave = true
config[testPath] = true
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
DependencyManager.writeDefaultConfig(defaultConfig, enchantment)
}
}

View file

@ -3,14 +3,18 @@ package xyz.alexcrea.cuanvil.dependency
import io.delilaheve.CustomAnvil
import me.athlaeos.enchantssquared.enchantments.CustomEnchant
import me.athlaeos.enchantssquared.managers.CustomEnchantManager
import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.inventory.ItemStack
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.enchant.CAEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
import xyz.alexcrea.cuanvil.enchant.wrapped.CAEnchantSquaredEnchantment
import xyz.alexcrea.cuanvil.group.IncludeGroup
import java.util.*
class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) {
@ -78,11 +82,7 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
// Write default level limit and xp cost
for (enchantment in esEnchantments) {
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
DependencyManager.writeDefaultConfig(defaultConfig, enchantment)
}
// Write groups and conflicts
@ -95,11 +95,6 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
// Save
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 !")
}
@ -107,60 +102,50 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
private fun writeMissingGroups(){
// 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)
val groupConfig = ConfigHolder.ITEM_GROUP_HOLDER.config
if(!groupConfig.isConfigurationSection("pickaxes")){
groupConfig["pickaxes.type"] = "include"
groupConfig["pickaxes.items"] = listOf("wooden_pickaxe", "stone_pickaxe", "iron_pickaxe", "diamond_pickaxe", "golden_pickaxe", "netherite_pickaxe")
}
val pickaxes = IncludeGroup("pickaxes")
pickaxes.addAll(Material.WOODEN_PICKAXE, Material.STONE_PICKAXE, Material.IRON_PICKAXE, Material.DIAMOND_PICKAXE, Material.GOLDEN_PICKAXE, Material.NETHERITE_PICKAXE)
MaterialGroupApi.addMaterialGroup(pickaxes)
if(!groupConfig.isConfigurationSection("shovels")){
groupConfig["shovels.type"] = "include"
groupConfig["shovels.items"] = listOf("wooden_shovel", "stone_shovel", "iron_shovel", "diamond_shovel", "golden_shovel", "netherite_shovel")
}
val shovels = IncludeGroup("shovels")
shovels.addAll(Material.WOODEN_SHOVEL, Material.STONE_SHOVEL, Material.IRON_SHOVEL, Material.DIAMOND_SHOVEL, Material.GOLDEN_SHOVEL, Material.NETHERITE_SHOVEL)
MaterialGroupApi.addMaterialGroup(shovels)
if(!groupConfig.isConfigurationSection("hoes")){
groupConfig["hoes.type"] = "include"
groupConfig["hoes.items"] = listOf("wooden_hoe", "stone_hoe", "iron_hoe", "diamond_hoe", "golden_hoe", "netherite_hoe")
}
val hoes = IncludeGroup("hoes")
hoes.addAll(Material.WOODEN_HOE, Material.STONE_HOE, Material.IRON_HOE, Material.DIAMOND_HOE, Material.GOLDEN_HOE, Material.NETHERITE_HOE)
MaterialGroupApi.addMaterialGroup(hoes)
if(!groupConfig.isConfigurationSection("shield")){
groupConfig["shield.type"] = "include"
groupConfig["shield.items"] = listOf("shield")
}
val shield = IncludeGroup("shield")
hoes.addToPolicy(Material.SHIELD)
MaterialGroupApi.addMaterialGroup(shield)
if(!groupConfig.isConfigurationSection("elytra")){
groupConfig["elytra.type"] = "include"
groupConfig["elytra.items"] = listOf("elytra")
}
val elytra = IncludeGroup("elytra")
hoes.addToPolicy(Material.ELYTRA)
MaterialGroupApi.addMaterialGroup(elytra)
if(!groupConfig.isConfigurationSection("trinkets")){
groupConfig["trinkets.type"] = "include"
groupConfig["trinkets.items"] = listOf("rotten_flesh")
}
val trinkets = IncludeGroup("trinkets")
hoes.addToPolicy(Material.ROTTEN_FLESH)
MaterialGroupApi.addMaterialGroup(trinkets)
}
private fun writeMaterialRestriction(esEnchantments: List<CAEnchantSquaredEnchantment>){
val conflictConfig = ConfigHolder.CONFLICT_HOLDER.config
for (enchantment in esEnchantments) {
val restrictionName = "restriction_${enchantment.key.key}"
if(!conflictConfig.isConfigurationSection(restrictionName)){
conflictConfig["$restrictionName.enchantments"] = listOf(enchantment.name)
val conflict = ConflictBuilder("restriction_${enchantment.key.key}")
// enchanted book is allowed in any case.
conflict.addExcludedGroup("enchanted_book")
// Get allowed groups
val listOfAllowed = ArrayList<String>()
listOfAllowed.add("enchanted_book") // enchanted book is allowed in any case.
for (esGroup in enchantment.enchant.compatibleItems) {
val caGroup = esGroupToCAGroup(esGroup)
if(caGroup == null){
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){
val conflictConfig = ConfigHolder.CONFLICT_HOLDER.config
val conflictPath = "${enchantment1.name}_with_${enchantment2.name}_conflict"
val conflict = ConflictBuilder("${enchantment1.name}_with_${enchantment2.name}_conflict")
if(!conflictConfig.isConfigurationSection(conflictPath)){
conflictConfig["$conflictPath.enchantments"] = listOf(enchantment1.name, enchantment2.name)
val empty: List<String> = Collections.emptyList()
conflictConfig["$conflictPath.notAffectedGroups"] = empty
conflictConfig["$conflictPath.maxEnchantmentBeforeConflict"] = 1
}
conflict.addEnchantment(enchantment1).addEnchantment(enchantment2)
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
* @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
* @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

View file

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

View file

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