Attempt to switch from Enchantment to WrapperEnchantment

This commit is contained in:
alexcrea 2024-06-16 03:58:18 +02:00
parent 9f74c2cfff
commit 1eac81aef6
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
12 changed files with 63 additions and 104 deletions

View file

@ -2,8 +2,8 @@ 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
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
/**
* Config option accessors
@ -239,7 +239,7 @@ object ConfigOptions {
/**
* Get the given [enchantment]'s limit
*/
fun enchantLimit(enchantment: Enchantment): Int {
fun enchantLimit(enchantment: WrappedEnchantment): Int {
return enchantLimit(enchantment.enchantmentName)
}
@ -273,7 +273,7 @@ object ConfigOptions {
* it's source [isFromBook]
*/
fun enchantmentValue(
enchantment: Enchantment,
enchantment: WrappedEnchantment,
isFromBook: Boolean
): Int {
return enchantmentValue(enchantment.enchantmentName, isFromBook)
@ -309,22 +309,4 @@ object ConfigOptions {
return DEFAULT_ENCHANT_VALUE
}
/**
* Get an array of key of basic config options
*/
fun getBasicConfigKeys(): Array<String> {
return arrayOf(
DEFAULT_LIMIT_PATH,
CAP_ANVIL_COST,
MAX_ANVIL_COST,
REPLACE_TOO_EXPENSIVE,
ITEM_REPAIR_COST,
UNIT_REPAIR_COST,
ITEM_RENAME_COST,
SACRIFICE_ILLEGAL_COST,
REMOVE_ANVIL_COST_LIMIT
)
}
}

View file

@ -2,9 +2,9 @@ package io.delilaheve.util
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.enchant.WrappedEnchantment
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 Enchantment.enchantmentName: String
val WrappedEnchantment.enchantmentName: String
get() = key.key
/**
* Combine 2 sets of enchantments according to our configuration
*/
fun Map<Enchantment, Int>.combineWith(
other: Map<Enchantment, Int>,
fun Map<WrappedEnchantment, Int>.combineWith(
other: Map<WrappedEnchantment, Int>,
mat: Material,
player: HumanEntity
) = mutableMapOf<Enchantment, Int>().apply {
) = mutableMapOf<WrappedEnchantment, Int>().apply {
putAll(this@combineWith)
other.forEach { (enchantment, level) ->
// Get max level or 255 if player can bypass

View file

@ -1,11 +1,9 @@
package io.delilaheve.util
import io.delilaheve.CustomAnvil
import org.bukkit.Material.ENCHANTED_BOOK
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.Damageable
import org.bukkit.inventory.meta.EnchantmentStorageMeta
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
import kotlin.math.ceil
import kotlin.math.max
import kotlin.math.min
@ -23,44 +21,21 @@ object ItemUtil {
/**
* Find the enchantment map for this [ItemStack] and return it as a [MutableMap]
*/
fun ItemStack.findEnchantments() = if (isEnchantedBook()) {
(itemMeta as? EnchantmentStorageMeta)?.storedEnchants ?: emptyMap()
} else {
itemMeta?.enchants ?: emptyMap()
}
fun ItemStack.findEnchantments(): MutableMap<WrappedEnchantment, Int> = WrappedEnchantment.getEnchants(this)
/**
* Apply an [enchantments] map to this [ItemStack]
*/
fun ItemStack.setEnchantmentsUnsafe(enchantments: Map<Enchantment, Int>) {
if (isEnchantedBook()) {
/* For some god-forsaken reason, item meta is not mutable
* so, we have to get the instance, modify it, then set it
* back to the item... #BecauseMinecraft */
val bookMeta = (itemMeta as? EnchantmentStorageMeta)
bookMeta?.replaceEnchants(enchantments)
itemMeta = bookMeta
} else {
itemMeta?.enchants?.forEach { (enchant, _) ->
removeEnchantment(enchant)
}
addUnsafeEnchantments(enchantments)
}
}
fun ItemStack.setEnchantmentsUnsafe(enchantments: Map<WrappedEnchantment, Int>) {
WrappedEnchantment.clearEnchants(this)
/**
* Apply an [enchantments] map to this book
*/
private fun EnchantmentStorageMeta.replaceEnchants(
enchantments: Map<Enchantment, Int>
) {
storedEnchants.forEach { (enchant, _) ->
removeStoredEnchant(enchant)
}
enchantments.forEach { (enchant, level) ->
val added = addStoredEnchant(enchant, level, true)
CustomAnvil.log("${enchant.key} added to item? $added")
val meta = this.itemMeta ?: return
enchantments.forEach { (enchantment, level) ->
enchantment.addEnchantmentUnsafe(this, meta, level)
}
this.itemMeta = meta
}
/**

View file

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

View file

@ -5,6 +5,7 @@ import org.bukkit.Material
import org.bukkit.NamespacedKey
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.enchantments.Enchantment
import xyz.alexcrea.cuanvil.enchant.WrappedEnchantment
class EnchantConflictManager {
@ -27,12 +28,12 @@ class EnchantConflictManager {
// 1.20.5 compatibility TODO better update system
private val SWEEPING_EDGE_ENCHANT =
Enchantment.getByKey(NamespacedKey.minecraft("sweeping_edge")) ?:
Enchantment.SWEEPING_EDGE
WrappedEnchantment.getByKey(NamespacedKey.minecraft("sweeping_edge")) ?:
WrappedEnchantment.getByKey(Enchantment.SWEEPING_EDGE.key)
}
private lateinit var conflictMap: HashMap<Enchantment, ArrayList<EnchantConflictGroup>>
private lateinit var conflictMap: HashMap<WrappedEnchantment, ArrayList<EnchantConflictGroup>>
lateinit var conflictList: ArrayList<EnchantConflictGroup>
// Read and prepare all conflict
@ -58,14 +59,14 @@ class EnchantConflictManager {
}
}
fun addConflictToConflictMap(enchant: Enchantment, conflict: EnchantConflictGroup) {
fun addConflictToConflictMap(enchant: WrappedEnchantment, conflict: EnchantConflictGroup) {
if (!conflictMap.containsKey(enchant)) {
conflictMap[enchant] = ArrayList()
}
conflictMap[enchant]!!.add(conflict)
}
fun removeConflictFromMap(enchant: Enchantment, conflict: EnchantConflictGroup): Boolean {
fun removeConflictFromMap(enchant: WrappedEnchantment, conflict: EnchantConflictGroup): Boolean {
return conflictMap[enchant]!!.remove(conflict)
}
@ -100,7 +101,7 @@ class EnchantConflictManager {
return conflict
}
private fun getEnchantByName(enchantName: String): Enchantment? {
private fun getEnchantByName(enchantName: String): WrappedEnchantment? {
// Temporary solution for 1.20.5
when(enchantName){
@ -110,7 +111,7 @@ class EnchantConflictManager {
}
val enchantKey = NamespacedKey.minecraft(enchantName)
return Enchantment.getByKey(enchantKey)
return WrappedEnchantment.getByKey(enchantKey)
}
@ -151,7 +152,7 @@ class EnchantConflictManager {
return group
}
fun isConflicting(base: Set<Enchantment>, mat: Material, newEnchant: Enchantment): ConflictType {
fun isConflicting(base: Set<WrappedEnchantment>, mat: Material, newEnchant: WrappedEnchantment): ConflictType {
CustomAnvil.verboseLog("Testing conflict for ${newEnchant.key} on ${mat.key}")
val conflictList = conflictMap[newEnchant] ?: return ConflictType.NO_CONFLICT
CustomAnvil.verboseLog("Did not get skipped")