mirror of
https://github.com/Auxilor/EcoEnchants.git
synced 2025-01-25 00:51:20 +01:00
Improved grindstone
This commit is contained in:
parent
c4590fb0eb
commit
2b4e6eb8f4
@ -22,6 +22,7 @@ import org.bukkit.inventory.ItemStack
|
||||
import org.bukkit.inventory.meta.Damageable
|
||||
import org.bukkit.inventory.meta.EnchantmentStorageMeta
|
||||
import java.util.*
|
||||
import javax.swing.text.html.HTML.Tag.P
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
@ -32,39 +33,37 @@ import kotlin.math.roundToInt
|
||||
class GrindstoneSupport(
|
||||
private val plugin: EcoPlugin
|
||||
) : Listener {
|
||||
@EventHandler
|
||||
fun onGrindstone(event: InventoryClickEvent) {
|
||||
val player = event.whoClicked as? Player ?: return
|
||||
|
||||
if (player.openInventory.topInventory.type != InventoryType.GRINDSTONE) {
|
||||
return
|
||||
}
|
||||
|
||||
if (event.slotType != InventoryType.SlotType.RESULT) {
|
||||
return
|
||||
}
|
||||
|
||||
val inventory = player.openInventory.topInventory as GrindstoneInventory
|
||||
|
||||
val topEnchants = inventory.getItem(0)?.fast()?.getEnchants(true) ?: emptyMap()
|
||||
val bottomEnchants = inventory.getItem(1)?.fast()?.getEnchants(true) ?: emptyMap()
|
||||
|
||||
val toKeep = mutableMapOf<Enchantment, Int>()
|
||||
|
||||
for ((enchant, level) in topEnchants) {
|
||||
if (enchant.wrap().type.noGrindstone) {
|
||||
toKeep[enchant] = level
|
||||
}
|
||||
}
|
||||
|
||||
for ((enchant, level) in bottomEnchants) {
|
||||
if (enchant.wrap().type.noGrindstone) {
|
||||
val current = toKeep[enchant] ?: 0
|
||||
toKeep[enchant] = max(level, current)
|
||||
}
|
||||
}
|
||||
val inventory = event.view.topInventory as? GrindstoneInventory ?: return
|
||||
|
||||
// Run everything later to await event completion
|
||||
plugin.scheduler.run {
|
||||
val topEnchants = inventory.getItem(0)?.fast()?.getEnchants(true) ?: emptyMap()
|
||||
val bottomEnchants = inventory.getItem(1)?.fast()?.getEnchants(true) ?: emptyMap()
|
||||
|
||||
if (topEnchants.isEmpty() && bottomEnchants.isEmpty()) {
|
||||
return@run
|
||||
}
|
||||
|
||||
val toKeep = mutableMapOf<Enchantment, Int>()
|
||||
|
||||
for ((enchant, level) in topEnchants) {
|
||||
println("Enchantment $enchant can grindstone? ${enchant.wrap().type.noGrindstone}")
|
||||
if (enchant.wrap().type.noGrindstone) {
|
||||
toKeep[enchant] = level
|
||||
}
|
||||
}
|
||||
|
||||
for ((enchant, level) in bottomEnchants) {
|
||||
if (enchant.wrap().type.noGrindstone) {
|
||||
val current = toKeep[enchant] ?: 0
|
||||
toKeep[enchant] = max(level, current)
|
||||
}
|
||||
}
|
||||
|
||||
val result = inventory.getItem(2)
|
||||
|
||||
if (result == null || event.isCancelled) {
|
||||
return@run
|
||||
}
|
||||
@ -76,10 +75,18 @@ class GrindstoneSupport(
|
||||
}
|
||||
|
||||
if (meta is EnchantmentStorageMeta) {
|
||||
for ((enchant, _) in meta.storedEnchants.toMap()) {
|
||||
meta.removeStoredEnchant(enchant)
|
||||
}
|
||||
|
||||
for ((enchant, level) in toKeep) {
|
||||
meta.addStoredEnchant(enchant, level, true)
|
||||
}
|
||||
} else {
|
||||
for ((enchant, _) in meta.enchants.toMap()) {
|
||||
meta.removeEnchant(enchant)
|
||||
}
|
||||
|
||||
for ((enchant, level) in toKeep) {
|
||||
meta.addEnchant(enchant, level, true)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user