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

@ -29,6 +29,8 @@ class AnvilEventListener : Listener {
companion object {
// Anvil's output slot
private const val ANVIL_INPUT_LEFT = 0
private const val ANVIL_INPUT_RIGHT = 1
private const val ANVIL_OUTPUT_SLOT = 2
}
@ -38,19 +40,15 @@ class AnvilEventListener : Listener {
@EventHandler(priority = HIGHEST)
fun anvilCombineCheck(event: PrepareAnvilEvent) {
val inventory = event.inventory
val first = inventory.getItem(0) ?: return
val second = inventory.getItem(1) ?: return
val first = inventory.getItem(ANVIL_INPUT_LEFT) ?: return
val second = inventory.getItem(ANVIL_INPUT_RIGHT) ?: return
if (first.canMergeWith(second)) {
// Try to find player
val player = event.view.player
val newEnchants = first.findEnchantments()
.combineWith(second.findEnchantments(),player)
.combineWith(second.findEnchantments(), first.type, player)
val resultItem = first.clone()
resultItem.itemMeta?.let {
it.setDisplayName(inventory.renameText)
resultItem.itemMeta = it
}
resultItem.setEnchantmentsUnsafe(newEnchants)
var repairCost: Int
if (!first.isBook() && !second.isBook()) {
@ -61,17 +59,26 @@ class AnvilEventListener : Listener {
repairCost = resultItem.repairCost
}
// Test if nothing change and stop.
if(first == resultItem){
event.result = null
return
}
// Rename item and add renaming cost
resultItem.itemMeta?.let {
if(!it.displayName.contentEquals(inventory.renameText)){
it.setDisplayName(inventory.renameText)
resultItem.itemMeta = it
repairCost += 1
}
}
if (ConfigOptions.limitRepairCost) {
repairCost = min(repairCost, ConfigOptions.limitRepairValue)
}
// Set object only if allowed
if(itemAllowed(resultItem,player)){
event.result = resultItem
} else{
event.result = null
return
}
event.result = resultItem
/* Because Minecraft likes to have the final say in the repair cost displayed
* we need to wait for the event to end before overriding it, this ensures that
@ -94,29 +101,16 @@ class AnvilEventListener : Listener {
*/
@EventHandler(ignoreCancelled = true)
fun anvilExtractionCheck(event: InventoryClickEvent) {
val player = event.whoClicked as? Player ?: return
//val player = event.whoClicked as? Player ?: return
val inventory = event.inventory as? AnvilInventory ?: return
if (event.rawSlot != ANVIL_OUTPUT_SLOT) { return }
val output = inventory.getItem(ANVIL_OUTPUT_SLOT) ?: return
// Should be true most of the time
// But if permissions change in the anvil it can be false
if(!itemAllowed(output,player)){
// Is true if there was no change. probably when there are conflict
if(output == inventory.getItem(ANVIL_INPUT_LEFT)){
event.result = Event.Result.DENY
return
}
event.result = Event.Result.ALLOW
}
private fun itemAllowed(item: ItemStack, player: HumanEntity): Boolean{
if(player.hasPermission(UnsafeEnchants.bypassFusePermission)) return true
if(player.hasPermission(UnsafeEnchants.unsafePermission)){
if(UnsafeEnchants.conflictManager.isConflicting(item))
return false
}else if (item.findEnchantments().hasConflicts()){
return false
}
return true
}
}

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
*/