add xp rename cost and fix non vanilla anvil beavior. but keep some protection to avoid useless fuse.

This commit is contained in:
alexcrea 2024-02-03 13:25:18 +01:00
parent 760cdef6ad
commit bd28c5b71c
4 changed files with 115 additions and 68 deletions

View file

@ -1,6 +1,7 @@
package io.delilaheve.util
import io.delilaheve.UnsafeEnchants
import org.bukkit.Material
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.HumanEntity
import kotlin.math.max
@ -21,43 +22,58 @@ object EnchantmentUtil {
* Combine 2 sets of enchantments according to our configuration
*/
fun Map<Enchantment, Int>.combineWith(
other: Map<Enchantment, Int>, player: HumanEntity
other: Map<Enchantment, Int>,
mat: Material,
player: HumanEntity
) = mutableMapOf<Enchantment, Int>().apply {
putAll(this@combineWith)
other.forEach { (enchantment, level) ->
when {
// Enchantment not yet in result list
!containsKey(enchantment) -> {
// Add the enchantment if it doesn't have conflicts, or, if we're allowing unsafe enchantments
if (!keys.any { enchantment.conflictsWith(it) } || ConfigOptions.allowUnsafe) {
this[enchantment] = level
// Enchantment not yet in result list
if (!containsKey(enchantment)) {
if(player.hasPermission(UnsafeEnchants.unsafePermission)){
// Add the enchantment if it doesn't have conflicts, or, if player is allowed to bypass enchantment restrictions
this[enchantment] = level
if(!player.hasPermission(UnsafeEnchants.bypassFusePermission) &&
UnsafeEnchants.conflictManager.isConflicting(this.keys,mat,enchantment)){
this.remove(enchantment)
}
}else if(!keys.any { enchantment.conflictsWith(it) }){
this[enchantment] = level
}
// Enchantment already in result list...
else -> when {
// ... and they're not the same level
this[enchantment] != other[enchantment] -> {
val newLevel = max(this[enchantment] ?: 0, other[enchantment] ?: 0)
// apply the greater of the two if non-zero
if (newLevel > 0) { this[enchantment] = newLevel }
}
// ... and they're the same level
else -> {
// try to increase the enchantment level by 1
var newLevel = this[enchantment]?.plus(1) ?: 0
val maxLevel = if(player.hasPermission(UnsafeEnchants.bypassLevelPermission)){
255
}else{
ConfigOptions.enchantLimit(enchantment)
}
newLevel = min(newLevel, maxLevel)
if (newLevel > 0) { this[enchantment] = newLevel }
}
// Enchantment already in result list
else{
// ... and they are conflicting
if(UnsafeEnchants.conflictManager.isConflicting(this.keys,mat,enchantment)
&& !player.hasPermission(UnsafeEnchants.bypassFusePermission)){
return@forEach
}
// ... and they're not the same level
if(this[enchantment] != other[enchantment]){
val newLevel = max(this[enchantment] ?: 0, other[enchantment] ?: 0)
// apply the greater of the two if non-zero
if (newLevel > 0) { this[enchantment] = newLevel }
}
// ... and they're the same level
else {
// try to increase the enchantment level by 1
var newLevel = this[enchantment]!! +1
// Get max level or 255 if player can bypass
val maxLevel = if(player.hasPermission(UnsafeEnchants.bypassLevelPermission)){
255
}else{
ConfigOptions.enchantLimit(enchantment)
}
newLevel = min(newLevel, maxLevel)
if (newLevel > 0) { this[enchantment] = newLevel }
}
}
}
}
/**
* Check if a set of enchantments has any conflicts
*/