diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/cannonCore/CannonCoreCommand.kt b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/cannonCore/CannonCoreCommand.kt index c38c6d63..303523e5 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/cannonCore/CannonCoreCommand.kt +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/cannonCore/CannonCoreCommand.kt @@ -21,7 +21,6 @@ package de.steamwar.bausystem.features.cannonCore import de.steamwar.bausystem.SWUtils import de.steamwar.command.SWCommand -import de.steamwar.linkage.Linked import org.bukkit.entity.Player object CannonCoreCommand : SWCommand("cannoncore") { diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/cannonCore/CannonCoreWand.kt b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/cannonCore/CannonCoreWand.kt index b92c16f7..9ef2e7bb 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/cannonCore/CannonCoreWand.kt +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/cannonCore/CannonCoreWand.kt @@ -19,16 +19,19 @@ package de.steamwar.bausystem.features.cannonCore +import de.steamwar.bausystem.BauSystem import de.steamwar.bausystem.utils.ItemUtils import de.steamwar.core.SWPlayer import de.steamwar.inventory.SWItem import org.bukkit.Material +import org.bukkit.entity.Player import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.player.PlayerItemHeldEvent import org.bukkit.inventory.ItemStack +import org.bukkit.plugin.java.JavaPlugin -object CannonCoreWand: Listener { +object CannonCoreWand : Listener { val wandId = "CANNON_CORE_WAND" val wandMaterial = Material.BREEZE_ROD @@ -41,10 +44,10 @@ object CannonCoreWand: Listener { val wand = SWItem(wandMaterial, title, lore, false, null) val item = wand.itemStack - return ItemUtils.setItem(item, wandId) + return ItemUtils.setItem(item, wandId) } - fun isWandItem(itemStack: ItemStack): Boolean { + fun isWandItem(itemStack: ItemStack?): Boolean { return ItemUtils.isItem(itemStack, wandId) } @@ -52,12 +55,21 @@ object CannonCoreWand: Listener { fun onPlayerEquip(event: PlayerItemHeldEvent) { val player = event.player val swPlayer = SWPlayer.of(event.player) - val item = player.inventory.getItem(event.newSlot) ?: return + val item = player.inventory.getItem(event.newSlot) - if(!isWandItem(item)) { + if (isWandItem(item)) { + scheduleDisplayUpdate(player) + } else { swPlayer.removeComponent(EmptyCannonCoreWandDisplay::class.java) } - else { + } + + private fun scheduleDisplayUpdate(player: Player) { + BauSystem.runTaskLater(JavaPlugin.getPlugin(BauSystem::class.java), Runnable { updateDisplay(player) }, 2) + } + + private fun updateDisplay(player: Player) { + if (isWandItem(player.inventory.itemInMainHand)) { EmptyCannonCoreWandDisplay(player) } } diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/cannonCore/EmptyCannonCoreWandDisplay.kt b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/cannonCore/EmptyCannonCoreWandDisplay.kt index 1a3a8740..52c22698 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/cannonCore/EmptyCannonCoreWandDisplay.kt +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/cannonCore/EmptyCannonCoreWandDisplay.kt @@ -26,10 +26,11 @@ import org.bukkit.entity.Player class EmptyCannonCoreWandDisplay : SWPlayer.Component { val coresDisplay: CannonCoresDisplay - val cursor: Cursor + var cursor: Cursor constructor(owner: Player) { coresDisplay = CannonCoresDisplay(owner) + SWPlayer.of(owner).setComponent(this) cursor = Cursor( coresDisplay.displayServer, owner, Material.GLASS, Material.COMMAND_BLOCK, @@ -39,11 +40,18 @@ class EmptyCannonCoreWandDisplay : SWPlayer.Component { ) { location, hitEntity, action -> print("Hello") } + } - SWPlayer.of(owner).setComponent(this) + override fun onMount(player: SWPlayer) { + coresDisplay.onMount(player) } override fun onUnmount(player: SWPlayer) { - player.removeComponent(cursor::class.java) + + player.getComponent(Cursor::class.java) + .filter { it === cursor } + .ifPresent { player.removeComponent(Cursor::class.java) } + + coresDisplay.onUnmount(player) } -} \ No newline at end of file +}