Files
src
build.gradle.kts
manifest.kod
settings.gradle.kts
Code
9 379
src/main/kotlin/com/superiorluckyblock/listener/BlockProtectionListener.kt
package com.superiorluckyblock.listener
import com.bgsoftware.superiorskyblock.api.SuperiorSkyblockAPI
import com.superiorluckyblock.SuperiorLuckyBlockPlugin
import org.bukkit.GameMode
import org.bukkit.Material
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.block.BlockBreakEvent
import org.bukkit.event.block.BlockDamageEvent
import org.bukkit.event.block.BlockExplodeEvent
import org.bukkit.event.block.BlockPistonExtendEvent
import org.bukkit.event.block.BlockPistonRetractEvent
import org.bukkit.event.entity.EntityExplodeEvent
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.event.block.Action
class BlockProtectionListener(private val plugin: SuperiorLuckyBlockPlugin) : Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
fun onBlockBreak(event: BlockBreakEvent) {
if (!plugin.config.getBoolean("lucky-block.protection.prevent-break", true)) return
if (plugin.luckyBlockManager.isLuckyBlock(event.block)) {
event.isCancelled = true
event.player.sendMessage("§cYou cannot break the LuckyBlock! Right-click to use it.")
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
fun onBlockDamage(event: BlockDamageEvent) {
if (!plugin.config.getBoolean("lucky-block.protection.prevent-break", true)) return
if (plugin.luckyBlockManager.isLuckyBlock(event.block)) {
event.isCancelled = true
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
fun onEntityExplode(event: EntityExplodeEvent) {
if (!plugin.config.getBoolean("lucky-block.protection.prevent-explosion", true)) return
event.blockList().removeIf { block ->
plugin.luckyBlockManager.isLuckyBlock(block)
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
fun onBlockExplode(event: BlockExplodeEvent) {
if (!plugin.config.getBoolean("lucky-block.protection.prevent-explosion", true)) return
event.blockList().removeIf { block ->
plugin.luckyBlockManager.isLuckyBlock(block)
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
fun onPistonExtend(event: BlockPistonExtendEvent) {
if (!plugin.config.getBoolean("lucky-block.protection.prevent-piston", true)) return
for (block in event.blocks) {
if (plugin.luckyBlockManager.isLuckyBlock(block)) {
event.isCancelled = true
return
}
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
fun onPistonRetract(event: BlockPistonRetractEvent) {
if (!plugin.config.getBoolean("lucky-block.protection.prevent-piston", true)) return
for (block in event.blocks) {
if (plugin.luckyBlockManager.isLuckyBlock(block)) {
event.isCancelled = true
return
}
}
}
@EventHandler(priority = EventPriority.HIGH)
fun onPlayerInteract(event: PlayerInteractEvent) {
if (event.action != Action.RIGHT_CLICK_BLOCK) return
val block = event.clickedBlock ?: return
if (!plugin.luckyBlockManager.isLuckyBlock(block)) return
event.isCancelled = true
val player = event.player
val superiorPlayer = SuperiorSkyblockAPI.getPlayer(player)
val island = plugin.luckyBlockManager.getIslandByLuckyBlock(block) ?: return
if (!island.isMember(superiorPlayer)) {
player.sendMessage("§cYou must be a member of this island to use the LuckyBlock!")
return
}
if (!plugin.luckyBlockManager.canInteract(player.uniqueId, island)) {
val remaining = plugin.luckyBlockManager.getRemainingCooldown(player.uniqueId)
player.sendMessage("§cYou must wait $remaining seconds before using the LuckyBlock again!")
return
}
plugin.luckyBlockManager.setLastInteraction(player.uniqueId)
player.sendMessage("§aYou used the LuckyBlock!")
block.world.createExplosion(block.location, 0f, false, false)
plugin.server.scheduler.runTaskLater(plugin, Runnable {
if (block.type != Material.NOTE_BLOCK) {
block.type = Material.NOTE_BLOCK
}
}, 1L)
}
}