unit repair done

This commit is contained in:
alexcrea 2024-02-09 00:25:12 +01:00
parent 80468f6add
commit 4195add655
6 changed files with 230 additions and 65 deletions

View file

@ -17,6 +17,8 @@ object ConfigOptions {
private const val LIMIT_REPAIR_VALUE = "limit_repair_value"
// Path for level cost on item repair
private const val ITEM_REPAIR_COST = "item_repair_cost"
// Path for level cost on unit repair
private const val UNIT_REPAIR_COST = "unit_repair_cost"
// Path for level cost on item renaming
private const val ITEM_RENAME_COST = "item_rename_cost"
// Path for level cost on illegal enchantment on sacrifice
@ -41,6 +43,8 @@ object ConfigOptions {
private const val DEFAULT_LIMIT_REPAIR_VALUE = 39
// Default value for level cost on item repair
private const val DEFAULT_ITEM_REPAIR_COST = 2
// Default value for level cost per unit repair
private const val DEFAULT_UNIT_REPAIR_COST = 1
// Default value for level cost on item renaming
private const val DEFAULT_ITEM_RENAME_COST = 1
// Default value for level cost on illegal enchantment on sacrifice
@ -48,7 +52,7 @@ object ConfigOptions {
// Valid range for repair cost limit
private val REPAIR_LIMIT_RANGE = 1..39
// Valid range for repair cost
private val ITEM_REPAIR_COST_RANGE = 0..255
private val REPAIR_COST_RANGE = 0..255
// Valid range for rename cost
private val ITEM_RENAME_COST_RANGE = 0..255
// Valid range for illegal enchantment conflict cost
@ -102,10 +106,22 @@ object ConfigOptions {
return UnsafeEnchants.instance
.config
.getInt(ITEM_REPAIR_COST, DEFAULT_ITEM_REPAIR_COST)
.takeIf { it in ITEM_REPAIR_COST_RANGE }
.takeIf { it in REPAIR_COST_RANGE }
?: DEFAULT_ITEM_REPAIR_COST
}
/**
* Value of an item repair
*/
val unitRepairCost: Int
get() {
return UnsafeEnchants.instance
.config
.getInt(UNIT_REPAIR_COST, DEFAULT_UNIT_REPAIR_COST)
.takeIf { it in REPAIR_COST_RANGE }
?: DEFAULT_UNIT_REPAIR_COST
}
/**
* Value of an item rename
*/

View file

@ -31,17 +31,13 @@ object EnchantmentUtil {
other.forEach { (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) != ConflictType.NO_CONFLICT)){
this.remove(enchantment)
}
}else if(!keys.any { enchantment.conflictsWith(it) }){
this[enchantment] = level
// 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) != ConflictType.NO_CONFLICT)){
this.remove(enchantment)
}
}
// Enchantment already in result list
else{

View file

@ -1,13 +1,13 @@
package io.delilaheve.util
import io.delilaheve.UnsafeEnchants
import org.bukkit.Material.BOOK
import org.bukkit.Material.ENCHANTED_BOOK
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.Damageable
import org.bukkit.inventory.meta.EnchantmentStorageMeta
import org.bukkit.inventory.meta.ItemMeta
import kotlin.math.ceil
import kotlin.math.max
import kotlin.math.min
/**
@ -15,20 +15,15 @@ import kotlin.math.min
*/
object ItemUtil {
/**
* Check if this [ItemStack] is a [BOOK] or [ENCHANTED_BOOK]
*/
fun ItemStack.isBook() = type in listOf(BOOK, ENCHANTED_BOOK)
/**
* Check if this [ItemStack] is an [ENCHANTED_BOOK]
*/
private fun ItemStack.isEnchantedBook() = type == ENCHANTED_BOOK
fun ItemStack.isEnchantedBook() = type == ENCHANTED_BOOK
/**
* Find the enchantment map for this [ItemStack] and return it as a [MutableMap]
*/
fun ItemStack.findEnchantments() = if (isBook()) {
fun ItemStack.findEnchantments() = if (isEnchantedBook()) {
(itemMeta as? EnchantmentStorageMeta)?.storedEnchants ?: emptyMap()
} else {
itemMeta?.enchants ?: emptyMap()
@ -38,7 +33,7 @@ object ItemUtil {
* Apply an [enchantments] map to this [ItemStack]
*/
fun ItemStack.setEnchantmentsUnsafe(enchantments: Map<Enchantment, Int>) {
if (isBook()) {
if (isEnchantedBook()) {
/* For some god-forsaken reason, item meta is not mutable
* so, we have to get the instance, modify it, then set it
* back to the item... #BecauseMinecraft */
@ -88,18 +83,40 @@ object ItemUtil {
val combinedDurability = firstDurability + secondDurability
val newDurability = min(combinedDurability, durability)
it.damage = durability - newDurability
itemMeta = it as ItemMeta
itemMeta = it
return true
}
return false
}
fun ItemStack.unitRepair(
unitAmount: Int,
percentPerUnit: Double
): Int {
(itemMeta as? Damageable)?.let {
val durability = type.maxDurability.toInt()
val firstDamage = it.damage
if( firstDamage == 0) return 0
var unitCount = 0
var damage = firstDamage
while((unitCount < unitAmount) && (damage > 0)){
unitCount++
damage = ceil(firstDamage - durability*percentPerUnit*unitCount).toInt()
}
it.damage = max(damage, 0)
itemMeta = it
return unitCount
}
return 0
}
/**
* Check that this [ItemStack] can merge with the [other]
*
* The two items should either be the same type, or, the [other] is a book
*/
fun ItemStack.canMergeWith(
other: ItemStack
) = type == other.type || (other.isEnchantedBook())
other: ItemStack?
) = (other != null) && (type == other.type || (other.isEnchantedBook()))
}