From 2b4e6eb8f46e4c04a6ca84b1ab2d356b4d106a26 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Fri, 2 Sep 2022 12:00:20 +0100 Subject: [PATCH] Improved grindstone --- .../mechanics/GrindstoneSupport.kt | 65 ++++++++++--------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/mechanics/GrindstoneSupport.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/mechanics/GrindstoneSupport.kt index e57a7545..e7116bee 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/mechanics/GrindstoneSupport.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/mechanics/GrindstoneSupport.kt @@ -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() - - 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() + + 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) }