1
0
Fork 0

Destroy glass when it gets hit

This commit is contained in:
Moritz Ruth 2020-06-14 13:50:44 +02:00
parent 8c9dc40350
commit e6f1eec9e3
No known key found for this signature in database
GPG key ID: AFD57E23E753841B
3 changed files with 25 additions and 17 deletions

View file

@ -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

View file

@ -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")
}

View file

@ -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