From e6f1eec9e3e852fe7a4e3cdd5fd8648cb83b574c Mon Sep 17 00:00:00 2001 From: Moritz Ruth Date: Sun, 14 Jun 2020 13:50:44 +0200 Subject: [PATCH] Destroy glass when it gets hit --- .../spigot_ttt/game/GameListener.kt | 8 ++++++++ .../moritzruth/spigot_ttt/game/GameManager.kt | 19 ++++++++++++++++--- .../game/items/impl/weapons/guns/Gun.kt | 15 +-------------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/de/moritzruth/spigot_ttt/game/GameListener.kt b/src/main/kotlin/de/moritzruth/spigot_ttt/game/GameListener.kt index 487b92f..ac25bbb 100644 --- a/src/main/kotlin/de/moritzruth/spigot_ttt/game/GameListener.kt +++ b/src/main/kotlin/de/moritzruth/spigot_ttt/game/GameListener.kt @@ -16,6 +16,7 @@ import org.bukkit.entity.Player import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority import org.bukkit.event.Listener +import org.bukkit.event.block.Action import org.bukkit.event.entity.EntityDamageByEntityEvent import org.bukkit.event.entity.EntityDamageEvent import org.bukkit.event.entity.PlayerDeathEvent @@ -110,6 +111,13 @@ object GameListener : Listener { event.deathMessage = null } + @EventHandler + fun onPlayerInteract(event: PlayerInteractEvent) { + if (event.player.inventory.itemInMainHand.type == Material.AIR && event.action == Action.LEFT_CLICK_BLOCK) { + GameManager.destroyBlock(event.clickedBlock!!) + } + } + @EventHandler(priority = EventPriority.LOWEST) fun onPlayerSwapHandItemsLowest(event: PlayerSwapHandItemsEvent) { event.isCancelled = true diff --git a/src/main/kotlin/de/moritzruth/spigot_ttt/game/GameManager.kt b/src/main/kotlin/de/moritzruth/spigot_ttt/game/GameManager.kt index 7d6694d..26f4bb1 100644 --- a/src/main/kotlin/de/moritzruth/spigot_ttt/game/GameManager.kt +++ b/src/main/kotlin/de/moritzruth/spigot_ttt/game/GameManager.kt @@ -14,9 +14,8 @@ import de.moritzruth.spigot_ttt.game.players.Role import de.moritzruth.spigot_ttt.plugin import de.moritzruth.spigot_ttt.utils.call import de.moritzruth.spigot_ttt.utils.teleportToWorldSpawn -import org.bukkit.GameRule -import org.bukkit.Location -import org.bukkit.Material +import org.bukkit.* +import org.bukkit.block.Block import kotlin.random.Random object GameManager { @@ -177,6 +176,20 @@ object GameManager { } } + fun destroyBlock(block: Block) { + if (phase != null && block.type.toString().contains("glass", true)) { + destroyedBlocks[block.location] = block.type + block.type = Material.AIR + world.playSound( + block.location, + Sound.BLOCK_GLASS_BREAK, + SoundCategory.BLOCKS, + 1F, + 1F + ) + } + } + fun ensurePhase(phase: GamePhase?) { if (this.phase !== phase) throw IllegalStateException("The game must be in $phase phase") } diff --git a/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/impl/weapons/guns/Gun.kt b/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/impl/weapons/guns/Gun.kt index c0109be..acc1ae3 100644 --- a/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/impl/weapons/guns/Gun.kt +++ b/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/impl/weapons/guns/Gun.kt @@ -88,20 +88,7 @@ abstract class Gun( if (rayTraceResult !== null) { val hitBlock = rayTraceResult.hitBlock - - if (hitBlock != null) { - if (hitBlock.type.toString().contains("glass", true)) { - GameManager.destroyedBlocks[hitBlock.location] = hitBlock.type - hitBlock.type = Material.AIR - GameManager.world.playSound( - hitBlock.location, - Sound.BLOCK_GLASS_BREAK, - SoundCategory.BLOCKS, - 1F, - 1F - ) - } - } + if (hitBlock != null) GameManager.destroyBlock(hitBlock) val entity = rayTraceResult.hitEntity