Files
src
build.gradle.kts
manifest.kod
settings.gradle.kts
Code
13 702
src/main/kotlin/com/guns/listener/GunListener.kt
package com.guns.listener
import com.guns.GunsPlugin
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.TextDecoration
import org.bukkit.Particle
import org.bukkit.Sound
import org.bukkit.entity.Arrow
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.entity.EntityDamageByEntityEvent
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.event.block.Action
import org.bukkit.event.entity.PlayerDeathEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.persistence.PersistentDataType
class GunListener(private val plugin: GunsPlugin) : Listener {
@EventHandler(priority = EventPriority.HIGH)
fun onInteract(event: PlayerInteractEvent) {
val action = event.action
if (action != Action.RIGHT_CLICK_AIR && action != Action.RIGHT_CLICK_BLOCK) return
val player = event.player
val item = player.inventory.itemInMainHand
val gunType = plugin.gunManager.getGunType(item) ?: return
event.isCancelled = true
if (!player.hasPermission("guns.use")) {
player.sendActionBar(
Component.text("You don't have permission to use guns.")
.color(NamedTextColor.RED)
)
return
}
if (plugin.gunManager.isGunOnCooldown(player.uniqueId, gunType)) {
val remaining = plugin.gunManager.getCooldownRemaining(player.uniqueId, gunType)
val seconds = String.format("%.1f", remaining / 1000.0)
player.sendActionBar(
Component.text("⏳ Reloading... ")
.color(NamedTextColor.YELLOW)
.decoration(TextDecoration.BOLD, true)
.append(Component.text("${seconds}s").color(NamedTextColor.WHITE).decoration(TextDecoration.BOLD, false))
)
player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_BASS, 0.5f, 0.8f)
return
}
if (!plugin.bulletManager.hasBullet(player)) {
player.sendActionBar(
Component.text("✗ No bullets! Craft with gunpowder in a ")
.color(NamedTextColor.RED)
.decoration(TextDecoration.BOLD, true)
.append(Component.text("+ shape").color(NamedTextColor.YELLOW).decoration(TextDecoration.BOLD, true))
)
player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_BASS, 1.0f, 0.5f)
return
}
plugin.bulletManager.consumeBullet(player)
plugin.gunManager.shoot(player, gunType)
val bulletsLeft = plugin.bulletManager.countBullets(player)
player.sendActionBar(
Component.text("🔫 ")
.color(NamedTextColor.WHITE)
.append(
Component.text("${gunType.displayName} ")
.color(gunType.nameColor)
.decoration(TextDecoration.BOLD, true)
)
.append(
Component.text("Bullets: $bulletsLeft")
.color(NamedTextColor.GRAY)
.decoration(TextDecoration.BOLD, false)
)
)
}
@EventHandler(priority = EventPriority.HIGH)
fun onEntityDamage(event: EntityDamageByEntityEvent) {
val damager = event.damager
if (damager !is Arrow) return
val pdc = damager.persistentDataContainer
val damage = pdc.get(plugin.gunManager.projectileDamageKey, PersistentDataType.DOUBLE) ?: return
event.damage = damage
val hitEntity = event.entity
hitEntity.world.spawnParticle(
Particle.DAMAGE_INDICATOR,
hitEntity.location.add(0.0, 1.0, 0.0),
6,
0.2, 0.3, 0.2,
0.0
)
if (hitEntity is LivingEntity && hitEntity !is Player) {
hitEntity.world.playSound(hitEntity.location, Sound.ENTITY_ARROW_HIT_PLAYER, 0.5f, 1.2f)
}
}
@EventHandler
fun onPlayerDeath(event: PlayerDeathEvent) {
val player = event.entity
if (plugin.scopeManager.isScoped(player.uniqueId)) {
plugin.scopeManager.disableScope(player)
}
}
@EventHandler
fun onPlayerQuit(event: PlayerQuitEvent) {
val player = event.player
if (plugin.scopeManager.isScoped(player.uniqueId)) {
plugin.scopeManager.disableScope(player)
}
plugin.gunManager.removeCooldown(player.uniqueId)
}
}