mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-08-28 08:25:56 +02:00
fix using durability for max damage
Some checks are pending
Java CI with Gradle / build (push) Waiting to run
Some checks are pending
Java CI with Gradle / build (push) Waiting to run
This commit is contained in:
parent
403c990083
commit
16e4f0a923
2 changed files with 21 additions and 21 deletions
|
|
@ -1,5 +1,6 @@
|
|||
package xyz.alexcrea.cuanvil.util
|
||||
|
||||
import org.bukkit.Material
|
||||
import org.bukkit.inventory.meta.Damageable
|
||||
|
||||
// I LOVE support of old versions and needing to do modules like that
|
||||
|
|
@ -10,8 +11,8 @@ object MaxDamageCheckerUtil {
|
|||
/**
|
||||
* @return max damage or int max if not set
|
||||
*/
|
||||
fun getMaxDamage(meta: Damageable): Int {
|
||||
if(!meta.hasMaxDamage()) return Integer.MAX_VALUE
|
||||
fun getMaxDamage(type: Material, meta: Damageable): Int {
|
||||
if(!meta.hasMaxDamage()) return type.maxDurability.toInt()
|
||||
return meta.maxDamage
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
package io.delilaheve.util
|
||||
|
||||
import org.bukkit.Material
|
||||
import org.bukkit.Material.ENCHANTED_BOOK
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import org.bukkit.inventory.meta.Damageable
|
||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
|
||||
import xyz.alexcrea.cuanvil.update.UpdateUtils
|
||||
import xyz.alexcrea.cuanvil.util.MaterialUtil.customType
|
||||
import xyz.alexcrea.cuanvil.util.MaxDamageCheckerUtil
|
||||
|
|
@ -21,11 +21,11 @@ object ItemUtil {
|
|||
*/
|
||||
fun ItemStack.isEnchantedBook() = type == ENCHANTED_BOOK
|
||||
|
||||
private fun maxDamage(damageable: Damageable): Int {
|
||||
private fun maxDamage(type: Material, damageable: Damageable): Int {
|
||||
val ver = UpdateUtils.currentMinecraftVersion()
|
||||
if(ver.major <= 1 && ver.minor <= 20 && ver.patch < 5) return Integer.MAX_VALUE
|
||||
if(ver.major <= 1 && ver.minor <= 20 && ver.patch < 5) return type.maxDurability.toInt()
|
||||
|
||||
return MaxDamageCheckerUtil.getMaxDamage(damageable)
|
||||
return MaxDamageCheckerUtil.getMaxDamage(type, damageable)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -37,23 +37,22 @@ object ItemUtil {
|
|||
first: ItemStack,
|
||||
second: ItemStack
|
||||
): Boolean {
|
||||
(itemMeta as? Damageable)?.let {
|
||||
val durability = type.maxDurability.toInt()
|
||||
val firstDamage = (first.itemMeta as? Damageable)?.damage ?: 0
|
||||
if (firstDamage == 0) return false
|
||||
val meta = itemMeta
|
||||
if(meta !is Damageable) return false
|
||||
|
||||
val firstDurability = durability - firstDamage
|
||||
val secondDamage = (second.itemMeta as? Damageable)?.damage ?: 0
|
||||
val secondDurability = durability - secondDamage
|
||||
val combinedDurability = firstDurability + secondDurability
|
||||
val newDurability = min(combinedDurability, durability)
|
||||
val maxDamage = maxDamage(type, meta)
|
||||
val damage = (first.itemMeta as? Damageable)?.damage ?: 0
|
||||
if (damage == 0) return false
|
||||
|
||||
val maxDamage = maxDamage(it)
|
||||
it.damage = min(durability - newDurability, maxDamage)
|
||||
itemMeta = it
|
||||
return true
|
||||
}
|
||||
return false
|
||||
val firstDurability = maxDamage - damage
|
||||
val secondDamage = (second.itemMeta as? Damageable)?.damage ?: 0
|
||||
val secondDurability = maxDamage - secondDamage
|
||||
val combinedDurability = firstDurability + secondDurability
|
||||
val newDurability = min(combinedDurability, maxDamage)
|
||||
|
||||
meta.damage = min(maxDamage - newDurability, maxDamage)
|
||||
this.itemMeta = meta
|
||||
return true
|
||||
}
|
||||
|
||||
fun ItemStack.unitRepair(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue