Renamed WrappedEnchantment to CAEnchantment (CustomAnvilEnchantment).

created an interface out of CAEnchantment and moved the implementation to CAEnchantmentBase.
This commit is contained in:
alexcrea 2024-06-20 13:08:48 +02:00
parent fa4752ea67
commit dafe595c5b
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
20 changed files with 575 additions and 463 deletions

View file

@ -8,7 +8,7 @@ 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.enchant.WrappedEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
import xyz.alexcrea.cuanvil.gui.config.MainConfigGui
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant
import xyz.alexcrea.cuanvil.listener.ChatEventListener
@ -94,7 +94,7 @@ class CustomAnvil : JavaPlugin() {
DependencyManager.loadDependency()
// Register enchantments
WrappedEnchantment.registerEnchantments()
CAEnchantmentRegistry.getInstance().registerStartupEnchantments()
// Load chat listener
chatListener = ChatEventListener()

View file

@ -3,7 +3,7 @@ package io.delilaheve.util
import io.delilaheve.CustomAnvil
import io.delilaheve.util.EnchantmentUtil.enchantmentName
import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
/**
* Config option accessors
@ -239,7 +239,7 @@ object ConfigOptions {
/**
* Get the given [enchantment]'s limit
*/
fun enchantLimit(enchantment: WrappedEnchantment): Int {
fun enchantLimit(enchantment: CAEnchantment): Int {
return enchantLimit(enchantment.enchantmentName)
}
@ -273,7 +273,7 @@ object ConfigOptions {
* it's source [isFromBook]
*/
fun enchantmentValue(
enchantment: WrappedEnchantment,
enchantment: CAEnchantment,
isFromBook: Boolean
): Int {
return enchantmentValue(enchantment.enchantmentName, isFromBook)
@ -307,7 +307,7 @@ object ConfigOptions {
return enchantmentValue("sweeping", isFromBook)
}
val enchantment = WrappedEnchantment.getByName(enchantmentName)
val enchantment = CAEnchantment.getByName(enchantmentName)
if(enchantment != null){
val rarity = enchantment.defaultRarity()

View file

@ -4,7 +4,7 @@ import io.delilaheve.CustomAnvil
import org.bukkit.Material
import org.bukkit.entity.HumanEntity
import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import xyz.alexcrea.cuanvil.group.ConflictType
import kotlin.math.max
import kotlin.math.min
@ -17,17 +17,17 @@ object EnchantmentUtil {
/**
* Enchantment name without namespace
*/
val WrappedEnchantment.enchantmentName: String
val CAEnchantment.enchantmentName: String
get() = key.key
/**
* Combine 2 sets of enchantments according to our configuration
*/
fun Map<WrappedEnchantment, Int>.combineWith(
other: Map<WrappedEnchantment, Int>,
fun Map<CAEnchantment, Int>.combineWith(
other: Map<CAEnchantment, Int>,
mat: Material,
player: HumanEntity
) = mutableMapOf<WrappedEnchantment, Int>().apply {
) = mutableMapOf<CAEnchantment, Int>().apply {
putAll(this@combineWith)
other.forEach { (enchantment, level) ->
if(!enchantment.isAllowed(player)) return@forEach

View file

@ -3,7 +3,7 @@ package io.delilaheve.util
import org.bukkit.Material.ENCHANTED_BOOK
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.Damageable
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import kotlin.math.ceil
import kotlin.math.max
import kotlin.math.min
@ -21,13 +21,13 @@ object ItemUtil {
/**
* Find the enchantment map for this [ItemStack] and return it as a [MutableMap]
*/
fun ItemStack.findEnchantments(): MutableMap<WrappedEnchantment, Int> = WrappedEnchantment.getEnchants(this)
fun ItemStack.findEnchantments(): MutableMap<CAEnchantment, Int> = CAEnchantment.getEnchants(this)
/**
* Apply an [enchantments] map to this [ItemStack]
*/
fun ItemStack.setEnchantmentsUnsafe(enchantments: Map<WrappedEnchantment, Int>) {
WrappedEnchantment.clearEnchants(this)
fun ItemStack.setEnchantmentsUnsafe(enchantments: Map<CAEnchantment, Int>) {
CAEnchantment.clearEnchants(this)
//TODO maybe faster methode to add vanilla enchantment. maybe move this function to wrapped enchantment
enchantments.forEach { (enchantment, level) ->

View file

@ -8,10 +8,10 @@ import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.inventory.ItemStack
import org.bukkit.plugin.Plugin
import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
import xyz.alexcrea.cuanvil.enchant.wrapped.EnchantSquaredEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
import xyz.alexcrea.cuanvil.enchant.wrapped.CAEnchantSquaredEnchantment
import java.util.*
import kotlin.collections.ArrayList
class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) {
@ -30,12 +30,16 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
fun registerEnchantments(){
for (enchant in CustomEnchantManager.getInstance().allEnchants.values) {
WrappedEnchantment.register(EnchantSquaredEnchantment(enchant))
CAEnchantmentRegistry.getInstance().register(
CAEnchantSquaredEnchantment(
enchant
)
)
}
}
fun getEnchantmentsSquared(item: ItemStack, enchantments: MutableMap<WrappedEnchantment, Int>) {
fun getEnchantmentsSquared(item: ItemStack, enchantments: MutableMap<CAEnchantment, Int>) {
val customEnchants = CustomEnchantManager.getInstance().getItemsEnchantsFromPDC(item)
customEnchants.forEach{
@ -51,8 +55,8 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
fun getKeyFromEnchant(enchant: CustomEnchant): NamespacedKey{
return NamespacedKey.fromString(enchant.type.lowercase(Locale.getDefault()), this.enchantmentSquaredPlugin)!!
}
private fun getWrappedEnchant(enchant: CustomEnchant): WrappedEnchantment{
return WrappedEnchantment.getByKey(getKeyFromEnchant(enchant))!!
private fun getWrappedEnchant(enchant: CustomEnchant): CAEnchantment {
return CAEnchantment.getByKey(getKeyFromEnchant(enchant))!!
}
@ -65,9 +69,9 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
CustomAnvil.instance.logger.info("Preparing configuration for Enchantment Squared...")
// Prepare enchantments
val esEnchantments = ArrayList<EnchantSquaredEnchantment>()
val esEnchantments = ArrayList<CAEnchantSquaredEnchantment>()
CustomEnchantManager.getInstance().allEnchants.forEach { (_, enchant) ->
esEnchantments.add(getWrappedEnchant(enchant) as EnchantSquaredEnchantment)
esEnchantments.add(getWrappedEnchant(enchant) as CAEnchantSquaredEnchantment)
}
// Write default level limit and xp cost
@ -134,7 +138,7 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
}
private fun writeMaterialRestriction(esEnchantments: List<EnchantSquaredEnchantment>){
private fun writeMaterialRestriction(esEnchantments: List<CAEnchantSquaredEnchantment>){
val conflictConfig = ConfigHolder.CONFLICT_HOLDER.config
for (enchantment in esEnchantments) {
val restrictionName = "restriction_${enchantment.key.key}"
@ -158,9 +162,9 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
}
}
private fun writeEnchantmentConflicts(esEnchantments: List<EnchantSquaredEnchantment>){
val otherEnchants = ArrayList<WrappedEnchantment>()
otherEnchants.addAll(WrappedEnchantment.values())
private fun writeEnchantmentConflicts(esEnchantments: List<CAEnchantSquaredEnchantment>){
val otherEnchants = ArrayList<CAEnchantment>()
otherEnchants.addAll(CAEnchantmentRegistry.getInstance().values())
for (enchantment in esEnchantments) {
otherEnchants.remove(enchantment)
@ -174,7 +178,7 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin)
}
}
private fun writeConflict(enchantment1: WrappedEnchantment, enchantment2: WrappedEnchantment){
private fun writeConflict(enchantment1: CAEnchantment, enchantment2: CAEnchantment){
val conflictConfig = ConfigHolder.CONFLICT_HOLDER.config
val conflictPath = "${enchantment1.name}_with_${enchantment2.name}_conflict"

View file

@ -2,7 +2,7 @@ package xyz.alexcrea.cuanvil.group
import io.delilaheve.CustomAnvil
import org.bukkit.Material
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
class EnchantConflictGroup(
private val name: String,
@ -10,13 +10,13 @@ class EnchantConflictGroup(
var minBeforeBlock: Int
) {
private val enchantments = HashSet<WrappedEnchantment>()
private val enchantments = HashSet<CAEnchantment>()
fun addEnchantment(enchant: WrappedEnchantment) {
fun addEnchantment(enchant: CAEnchantment) {
enchantments.add(enchant)
}
fun allowed(enchants: Set<WrappedEnchantment>, mat: Material): Boolean {
fun allowed(enchants: Set<CAEnchantment>, mat: Material): Boolean {
if (enchantments.size < minBeforeBlock) {
CustomAnvil.verboseLog("Conflicting bc of to many enchantments")
return true
@ -44,11 +44,11 @@ class EnchantConflictGroup(
return this.cantConflict
}
fun getEnchants(): HashSet<WrappedEnchantment> {
fun getEnchants(): HashSet<CAEnchantment> {
return enchantments
}
fun setEnchants(enchants: Set<WrappedEnchantment>) {
fun setEnchants(enchants: Set<CAEnchantment>) {
enchantments.clear()
enchantments.addAll(enchants)
}

View file

@ -5,7 +5,8 @@ import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.enchantments.Enchantment
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry
class EnchantConflictManager {
@ -28,8 +29,8 @@ class EnchantConflictManager {
// 1.20.5 compatibility TODO better update system
private val SWEEPING_EDGE_ENCHANT =
WrappedEnchantment.getByKey(NamespacedKey.minecraft("sweeping_edge")) ?:
WrappedEnchantment.getByKey(Enchantment.SWEEPING_EDGE.key)
CAEnchantment.getByKey(NamespacedKey.minecraft("sweeping_edge")) ?:
CAEnchantment.getByKey(Enchantment.SWEEPING_EDGE.key)
}
@ -40,7 +41,7 @@ class EnchantConflictManager {
conflictList = ArrayList()
// Clear conflict if exist
for (enchant in WrappedEnchantment.values()) {
for (enchant in CAEnchantmentRegistry.getInstance().values()) {
enchant.clearConflict()
}
@ -84,7 +85,7 @@ class EnchantConflictManager {
}
conflict.addEnchantment(enchant)
}
if (conflict.getEnchants().size == 0) {
if (conflict.getEnchants().isEmpty()) {
if (!futureUse) { //TODO future use will be deprecated once the new update system is finished
CustomAnvil.instance.logger.warning("Conflict $conflictName do not have valid enchantment, it will not do anything")
}
@ -93,7 +94,7 @@ class EnchantConflictManager {
return conflict
}
private fun getEnchantByName(enchantName: String): WrappedEnchantment? {
private fun getEnchantByName(enchantName: String): CAEnchantment? {
// Temporary solution for 1.20.5
when(enchantName){
@ -102,7 +103,7 @@ class EnchantConflictManager {
}
}
return WrappedEnchantment.getByName(enchantName)
return CAEnchantment.getByName(enchantName)
}
@ -143,7 +144,7 @@ class EnchantConflictManager {
return group
}
fun isConflicting(base: Set<WrappedEnchantment>, mat: Material, newEnchant: WrappedEnchantment): ConflictType {
fun isConflicting(base: Set<CAEnchantment>, mat: Material, newEnchant: CAEnchantment): ConflictType {
CustomAnvil.verboseLog("Testing conflict for ${newEnchant.key} on ${mat.key}")
val conflictList = newEnchant.conflicts;