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
|
package xyz.alexcrea.cuanvil.util
|
||||||
|
|
||||||
|
import org.bukkit.Material
|
||||||
import org.bukkit.inventory.meta.Damageable
|
import org.bukkit.inventory.meta.Damageable
|
||||||
|
|
||||||
// I LOVE support of old versions and needing to do modules like that
|
// 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
|
* @return max damage or int max if not set
|
||||||
*/
|
*/
|
||||||
fun getMaxDamage(meta: Damageable): Int {
|
fun getMaxDamage(type: Material, meta: Damageable): Int {
|
||||||
if(!meta.hasMaxDamage()) return Integer.MAX_VALUE
|
if(!meta.hasMaxDamage()) return type.maxDurability.toInt()
|
||||||
return meta.maxDamage
|
return meta.maxDamage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
package io.delilaheve.util
|
package io.delilaheve.util
|
||||||
|
|
||||||
|
import org.bukkit.Material
|
||||||
import org.bukkit.Material.ENCHANTED_BOOK
|
import org.bukkit.Material.ENCHANTED_BOOK
|
||||||
import org.bukkit.inventory.ItemStack
|
import org.bukkit.inventory.ItemStack
|
||||||
import org.bukkit.inventory.meta.Damageable
|
import org.bukkit.inventory.meta.Damageable
|
||||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment
|
|
||||||
import xyz.alexcrea.cuanvil.update.UpdateUtils
|
import xyz.alexcrea.cuanvil.update.UpdateUtils
|
||||||
import xyz.alexcrea.cuanvil.util.MaterialUtil.customType
|
import xyz.alexcrea.cuanvil.util.MaterialUtil.customType
|
||||||
import xyz.alexcrea.cuanvil.util.MaxDamageCheckerUtil
|
import xyz.alexcrea.cuanvil.util.MaxDamageCheckerUtil
|
||||||
|
|
@ -21,11 +21,11 @@ object ItemUtil {
|
||||||
*/
|
*/
|
||||||
fun ItemStack.isEnchantedBook() = type == ENCHANTED_BOOK
|
fun ItemStack.isEnchantedBook() = type == ENCHANTED_BOOK
|
||||||
|
|
||||||
private fun maxDamage(damageable: Damageable): Int {
|
private fun maxDamage(type: Material, damageable: Damageable): Int {
|
||||||
val ver = UpdateUtils.currentMinecraftVersion()
|
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,
|
first: ItemStack,
|
||||||
second: ItemStack
|
second: ItemStack
|
||||||
): Boolean {
|
): Boolean {
|
||||||
(itemMeta as? Damageable)?.let {
|
val meta = itemMeta
|
||||||
val durability = type.maxDurability.toInt()
|
if(meta !is Damageable) return false
|
||||||
val firstDamage = (first.itemMeta as? Damageable)?.damage ?: 0
|
|
||||||
if (firstDamage == 0) return false
|
|
||||||
|
|
||||||
val firstDurability = durability - firstDamage
|
val maxDamage = maxDamage(type, meta)
|
||||||
val secondDamage = (second.itemMeta as? Damageable)?.damage ?: 0
|
val damage = (first.itemMeta as? Damageable)?.damage ?: 0
|
||||||
val secondDurability = durability - secondDamage
|
if (damage == 0) return false
|
||||||
val combinedDurability = firstDurability + secondDurability
|
|
||||||
val newDurability = min(combinedDurability, durability)
|
|
||||||
|
|
||||||
val maxDamage = maxDamage(it)
|
val firstDurability = maxDamage - damage
|
||||||
it.damage = min(durability - newDurability, maxDamage)
|
val secondDamage = (second.itemMeta as? Damageable)?.damage ?: 0
|
||||||
itemMeta = it
|
val secondDurability = maxDamage - secondDamage
|
||||||
return true
|
val combinedDurability = firstDurability + secondDurability
|
||||||
}
|
val newDurability = min(combinedDurability, maxDamage)
|
||||||
return false
|
|
||||||
|
meta.damage = min(maxDamage - newDurability, maxDamage)
|
||||||
|
this.itemMeta = meta
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ItemStack.unitRepair(
|
fun ItemStack.unitRepair(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue