Merge pull request #22 from oguzcanyilmazlar/miniontoolevent-item-mutable

Mutable Items through MinionToolEvent
This commit is contained in:
TomTom 2024-07-20 14:31:42 +02:00 committed by GitHub
commit 3f85df77e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 14 deletions

View File

@ -7,7 +7,7 @@ import org.bukkit.event.Event
import org.bukkit.event.HandlerList
import org.bukkit.inventory.ItemStack
class MinionToolEvent(minion: Minion, private val player: Player, private val newTool: ItemStack) : MinionEvent(minion), Cancellable {
class MinionToolEvent(minion: Minion, private val player: Player, var newTool: ItemStack, var oldTool: ItemStack) : MinionEvent(minion), Cancellable {
companion object {
private val handlerList = HandlerList()
@ -29,10 +29,6 @@ class MinionToolEvent(minion: Minion, private val player: Player, private val ne
return player
}
fun getNewTool(): ItemStack {
return newTool
}
override fun setCancelled(cancelled: Boolean) {
isCancelled = cancelled
}

View File

@ -69,19 +69,20 @@ class MinionInventoryListener : Listener {
player.sendMessage(StringUtils.formatToString(Messages.PREFIX() + Messages.WRONG_TOOL()))
return
}
val minionToolEvent = MinionToolEvent(minion, player, event.currentItem!!)
val minionToolEvent = MinionToolEvent(minion, player, event.currentItem!!.clone(), minion.getTool()!!.clone())
Bukkit.getPluginManager().callEvent(minionToolEvent)
if (minionToolEvent.isCancelled) return
if (minion.getTool()?.type != Material.AIR) {
val current = event.currentItem!!.clone()
val tool = minion.getTool()?.clone()
val current = minionToolEvent.newTool
val tool = minionToolEvent.oldTool
minion.setTool(current)
minion.updateArmour()
event.currentItem!!.amount = 0
event.clickedInventory!!.addItem(tool)
} else {
minion.setTool(event.currentItem!!)
minion.setTool(minionToolEvent.newTool)
event.currentItem!!.amount = 0
}
@ -95,18 +96,19 @@ class MinionInventoryListener : Listener {
player.sendMessage(StringUtils.formatToString(Messages.PREFIX() + Messages.ERROR_INVENTORY_FULL()))
return
}
val minionToolEvent = MinionToolEvent(minion, player, ItemStack(Material.AIR))
val tool = minion.getTool()?.clone() ?: return
val minionToolEvent = MinionToolEvent(minion, player, ItemStack(Material.AIR), tool)
Bukkit.getPluginManager().callEvent(minionToolEvent)
if (minionToolEvent.isCancelled) return
val tool = minion.getTool()?.clone() ?: return
minion.setTool(ItemStack(Material.AIR))
minion.updateArmour()
val toolMeta = tool.itemMeta ?: return
val toolMeta = minionToolEvent.oldTool.itemMeta ?: return
toolMeta.persistentDataContainer.remove(Keys.GUI)
tool.setItemMeta(toolMeta)
minionToolEvent.oldTool.setItemMeta(toolMeta)
player.inventory.addItem(tool)
player.inventory.addItem(minionToolEvent.oldTool)
minion.updateInventories()
return
}