last know xp bug fixed.

This commit is contained in:
alexcrea 2024-02-04 17:59:45 +01:00
parent b5a2ae67fd
commit 8d4248f1ea
6 changed files with 40 additions and 36 deletions

View file

@ -18,6 +18,7 @@ import org.bukkit.inventory.AnvilInventory
import org.bukkit.inventory.InventoryView.Property.REPAIR_COST
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.Repairable
import xyz.alexcrea.group.ConflictType
import kotlin.math.min
/**
@ -49,7 +50,7 @@ class AnvilEventListener : Listener {
val resultItem = first.clone()
resultItem.setEnchantmentsUnsafe(newEnchants)
var anvilCost = calculateCost(first, second, resultItem, player.hasPermission(UnsafeEnchants.bypassFusePermission))
var anvilCost = calculateCost(first, second, resultItem)
if (!first.isBook() && !second.isBook()) {
// we only need to be concerned with repair when neither item is a book
val repaired = resultItem.repairFrom(first, second)
@ -114,7 +115,7 @@ class AnvilEventListener : Listener {
* Function to calculate most of the xp requirement for the anvil fuse
* Change result work penalty for future use
*/
private fun calculateCost(left: ItemStack, right: ItemStack, result: ItemStack, bypassFuse: Boolean): Int{
private fun calculateCost(left: ItemStack, right: ItemStack, result: ItemStack): Int{
// Extracted From https://minecraft.fandom.com/wiki/Anvil_mechanics#Enchantment_equation
// Calculate work penality
val leftPenality = (left.itemMeta as? Repairable)?.repairCost ?: 0
@ -125,22 +126,26 @@ class AnvilEventListener : Listener {
var illegalPenalty = 0
val rightIsFormBook = right.isBook()
val resultEnchs = result.findEnchantments().keys
val resultEnchs = result.findEnchantments()
val resultEnchsKeys = HashSet(resultEnchs.keys)
for (enchantment in right.findEnchantments()) {
// count enchant as illegal enchant if it conflicts with another enchant or not in result
if(!bypassFuse && (
(enchantment.key !in resultEnchs) ||
UnsafeEnchants.conflictManager.isConflicting(resultEnchs,result.type,enchantment.key)
)){
// There may an issue when illegal enchant are trying to combine
// at least that what I think, but can't find why
illegalPenalty += ConfigOptions.sacrificeIllegalCost
if((enchantment.key !in resultEnchsKeys)){
resultEnchsKeys.add(enchantment.key)
val conflictType = UnsafeEnchants.conflictManager.isConflicting(resultEnchsKeys,result.type,enchantment.key)
resultEnchsKeys.remove(enchantment.key)
if(ConflictType.BIG_CONFLICT == conflictType){
illegalPenalty += ConfigOptions.sacrificeIllegalCost
}
continue
}
// We know "enchantment.key in resultEnchs" true
val resultLevel = resultEnchs[enchantment.key]!!
val enchantmentMultiplier = ConfigOptions.enchantmentValue(enchantment.key, rightIsFormBook)
val value = enchantment.value * enchantmentMultiplier
val value = resultLevel * enchantmentMultiplier
UnsafeEnchants.log("Value for ${enchantment.key.enchantmentName} level ${enchantment.value} is $value")
rightValue+=value

View file

@ -4,6 +4,7 @@ import io.delilaheve.UnsafeEnchants
import org.bukkit.Material
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.HumanEntity
import xyz.alexcrea.group.ConflictType
import kotlin.math.max
import kotlin.math.min
@ -34,7 +35,7 @@ object EnchantmentUtil {
// 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)){
(UnsafeEnchants.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)){
this.remove(enchantment)
}
}else if(!keys.any { enchantment.conflictsWith(it) }){
@ -45,7 +46,7 @@ object EnchantmentUtil {
// Enchantment already in result list
else{
// ... and they are conflicting
if(UnsafeEnchants.conflictManager.isConflicting(this.keys,mat,enchantment)
if((UnsafeEnchants.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)
&& !player.hasPermission(UnsafeEnchants.bypassFusePermission)){
return@forEach
}
@ -73,20 +74,4 @@ object EnchantmentUtil {
}
}
/**
* Check if a set of enchantments has any conflicts
*/
fun Map<Enchantment, Int>.hasConflicts() : Boolean {
forEach { (enchantment1, _) ->
val hasConflict = any { (enchantment2, _) ->
enchantment2.conflictsWith(enchantment1)
}
if (hasConflict) {
return true
}
}
return false
}
}