diff --git a/src/main/kotlin/com/mineinabyss/bonfire/BonfireCommands.kt b/src/main/kotlin/com/mineinabyss/bonfire/BonfireCommands.kt index f5fc125..e9991b2 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/BonfireCommands.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/BonfireCommands.kt @@ -18,6 +18,7 @@ import com.mineinabyss.idofront.commands.execution.IdofrontCommandExecutor import com.mineinabyss.idofront.commands.extensions.actions.playerAction import com.mineinabyss.idofront.messaging.error import com.mineinabyss.idofront.messaging.info +import com.mineinabyss.idofront.messaging.observeLogger import com.mineinabyss.idofront.messaging.success import com.mineinabyss.idofront.nms.nbt.editOfflinePDC import com.mineinabyss.idofront.nms.nbt.getOfflinePDC @@ -51,7 +52,7 @@ class BonfireCommands : IdofrontCommandExecutor(), TabCompleter { } } "reload" { - actions { + actions(bonfire.logger) { bonfire.plugin.registerBonfireContext() sender.success("Bonfire configs have been reloaded!") } diff --git a/src/main/kotlin/com/mineinabyss/bonfire/BonfireContext.kt b/src/main/kotlin/com/mineinabyss/bonfire/BonfireContext.kt index 0561554..976c93a 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/BonfireContext.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/BonfireContext.kt @@ -2,10 +2,12 @@ package com.mineinabyss.bonfire import com.mineinabyss.bonfire.extensions.BonfireMessages import com.mineinabyss.idofront.di.DI +import com.mineinabyss.idofront.messaging.ComponentLogger val bonfire by DI.observe() interface BonfireContext { val plugin: BonfirePlugin val config: BonfireConfig val messages: BonfireMessages + val logger: ComponentLogger } diff --git a/src/main/kotlin/com/mineinabyss/bonfire/BonfirePlugin.kt b/src/main/kotlin/com/mineinabyss/bonfire/BonfirePlugin.kt index d14694c..e9c4517 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/BonfirePlugin.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/BonfirePlugin.kt @@ -9,6 +9,8 @@ import com.mineinabyss.geary.autoscan.autoscan import com.mineinabyss.geary.modules.geary import com.mineinabyss.idofront.config.config import com.mineinabyss.idofront.di.DI +import com.mineinabyss.idofront.messaging.ComponentLogger +import com.mineinabyss.idofront.messaging.observeLogger import com.mineinabyss.idofront.plugin.listeners import org.bukkit.plugin.java.JavaPlugin @@ -40,11 +42,7 @@ class BonfirePlugin : JavaPlugin() { override val plugin = this@BonfirePlugin override val config: BonfireConfig by config("config", dataFolder.toPath(), BonfireConfig()) override val messages: BonfireMessages by config("messages", dataFolder.toPath(), BonfireMessages()) + override val logger: ComponentLogger by plugin.observeLogger() }) } - - override fun onDisable() { - // Plugin shutdown logic -// ProtocolLibrary.getProtocolManager().removePacketListener(ChatPacketAdapter); - - }} +} diff --git a/src/main/kotlin/com/mineinabyss/bonfire/components/BonfireCooldown.kt b/src/main/kotlin/com/mineinabyss/bonfire/components/BonfireCooldown.kt new file mode 100644 index 0000000..ec29d8a --- /dev/null +++ b/src/main/kotlin/com/mineinabyss/bonfire/components/BonfireCooldown.kt @@ -0,0 +1,10 @@ +package com.mineinabyss.bonfire.components + +import com.mineinabyss.idofront.serialization.UUIDSerializer +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import java.util.* + +@Serializable +@SerialName("bonfire:cooldown") +data class BonfireCooldown(val bonfire: @Serializable(with = UUIDSerializer::class) UUID) diff --git a/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt b/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt index e023c44..b154d57 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt @@ -1,6 +1,7 @@ package com.mineinabyss.bonfire.listeners import com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent +import com.github.shynixn.mccoroutine.bukkit.launch import com.mineinabyss.blocky.api.BlockyFurnitures import com.mineinabyss.blocky.api.events.furniture.BlockyFurnitureBreakEvent import com.mineinabyss.blocky.api.events.furniture.BlockyFurnitureInteractEvent @@ -25,6 +26,7 @@ import com.mineinabyss.idofront.messaging.success import com.mineinabyss.idofront.nms.nbt.editOfflinePDC import com.mineinabyss.idofront.nms.nbt.getOfflinePDC import org.bukkit.Bukkit +import kotlinx.coroutines.delay import org.bukkit.entity.ItemDisplay import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority @@ -37,7 +39,6 @@ import kotlin.math.abs import kotlin.time.Duration.Companion.seconds class BonfireListener : Listener { - val cooldown = Cooldown(length = bonfire.config.bonfireInteractCooldown) private fun currentTime() = LocalDateTime.now().toInstant(ZoneOffset.UTC).epochSecond @@ -51,8 +52,7 @@ class BonfireListener : Listener { @EventHandler fun BlockyFurniturePlaceEvent.onBonfirePlace() { baseEntity.toGearyOrNull()?.with { bonfire: Bonfire -> - baseEntity.toGeary() - .setPersisting(bonfire.copy(bonfireOwner = player.uniqueId, bonfirePlayers = mutableListOf())) + baseEntity.toGeary().setPersisting(bonfire.copy(bonfireOwner = player.uniqueId, bonfirePlayers = mutableListOf())) baseEntity.toGeary().setPersisting(BonfireExpirationTime(0.seconds, currentTime())) baseEntity.updateBonfireState() } @@ -69,24 +69,13 @@ class BonfireListener : Listener { // Bonfire is lit, player is only registered player, player is unsetting // Since it is being unlit, set lastUnlitTimeStamp to currentTime bonfireData.bonfirePlayers.isNotEmpty() && bonfireData.bonfirePlayers.all { it == player.uniqueId } -> { - gearyEntity.setPersisting( - expiration.copy( - totalUnlitTime = expiration.totalUnlitTime, - lastUnlitTimeStamp = currentTime - ) - ) + gearyEntity.setPersisting(expiration.copy(totalUnlitTime = expiration.totalUnlitTime, lastUnlitTimeStamp = currentTime)) } // Bonfire was empty and player is attempting to set spawn // Check if Bonfires new totalUnlittime is greater than expiration time else -> { - val totalUnlitTime = - expiration.totalUnlitTime + (currentTime - expiration.lastUnlitTimeStamp).seconds - gearyEntity.setPersisting( - expiration.copy( - totalUnlitTime = totalUnlitTime, - lastUnlitTimeStamp = currentTime - ) - ) + val totalUnlitTime = expiration.totalUnlitTime + (currentTime - expiration.lastUnlitTimeStamp).seconds + gearyEntity.setPersisting(expiration.copy(totalUnlitTime = totalUnlitTime, lastUnlitTimeStamp = currentTime)) if (totalUnlitTime >= bonfireData.bonfireExpirationTime) { player.error(bonfire.messages.BONFIRE_EXPIRED) BlockyFurnitures.removeFurniture(baseEntity) @@ -99,18 +88,11 @@ class BonfireListener : Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) fun BlockyFurnitureInteractEvent.onBonfireInteract() { - if (!player.isSneaking) return + if (!player.isSneaking || player.toGeary().has()) return if (hand != EquipmentSlot.HAND || abs(0 - player.velocity.y) < 0.001) return - val gearyPlayer = player.toGeary() - val gearyBonfire = baseEntity.toGearyOrNull() ?: return - if (!Cooldown.isComplete(gearyPlayer, gearyBonfire)) { - isCancelled = true - return - } - Cooldown.start(gearyPlayer, gearyBonfire, cooldown) - - gearyBonfire.with { bonfireData: Bonfire -> + val gearyEntity = baseEntity.toGearyOrNull() + gearyEntity?.with { bonfireData: Bonfire -> when (player.uniqueId) { !in bonfireData.bonfirePlayers -> { if (bonfireData.bonfirePlayers.size >= bonfireData.maxPlayerCount && @@ -143,7 +125,13 @@ class BonfireListener : Listener { } baseEntity.updateBonfireState() - gearyBonfire.encodeComponentsTo(baseEntity) // Ensure data is saved to PDC + gearyEntity.encodeComponentsTo(baseEntity) // Ensure data is saved to PDC + + player.toGeary().set(BonfireCooldown(baseEntity.uniqueId)) + bonfire.plugin.launch { + delay(bonfire.config.bonfireInteractCooldown) + if (player.isOnline) player.toGeary().remove() + } } } @@ -158,6 +146,17 @@ class BonfireListener : Listener { } } + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + fun BlockyFurnitureInteractEvent.onBonfireCooldown() { + if (hand != EquipmentSlot.HAND || abs(0 - player.velocity.y) < 0.001) return + if (player.fallDistance > bonfire.config.minFallDist) return + + player.toGeary().with { cooldown: BonfireCooldown -> + if (cooldown.bonfire == baseEntity.uniqueId) isCancelled = true + else player.toGeary().remove() + } + } + private fun ItemDisplay.ensureSavedPlayersAreValid(bonfireData: Bonfire): Boolean { val validPlayers = bonfireData.bonfirePlayers.filter { val offlinePlayer = Bukkit.getOfflinePlayer(it) @@ -189,10 +188,12 @@ class BonfireListener : Listener { if (onlinePlayer != null) { onlinePlayer.toGeary().remove() onlinePlayer.toGeary().remove() + onlinePlayer.toGeary().remove() } else { p.editOfflinePDC { encode(BonfireRemoved()) remove() + remove() } } } diff --git a/src/main/kotlin/com/mineinabyss/bonfire/listeners/PlayerListener.kt b/src/main/kotlin/com/mineinabyss/bonfire/listeners/PlayerListener.kt index fe00c8d..023596a 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/listeners/PlayerListener.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/listeners/PlayerListener.kt @@ -5,6 +5,7 @@ import com.github.shynixn.mccoroutine.bukkit.launch import com.mineinabyss.blocky.helpers.GenericHelpers.toEntity import com.mineinabyss.bonfire.bonfire import com.mineinabyss.bonfire.components.Bonfire +import com.mineinabyss.bonfire.components.BonfireCooldown import com.mineinabyss.bonfire.components.BonfireRemoved import com.mineinabyss.bonfire.components.BonfireRespawn import com.mineinabyss.bonfire.extensions.isBonfire @@ -89,6 +90,7 @@ class PlayerListener : Listener { @EventHandler fun PlayerJoinEvent.onPlayerJoin() { + player.toGearyOrNull()?.remove() val bonfire = player.toGeary().get() ?: return val bonfireEntity = bonfire.bonfireUuid.toEntity() as? ItemDisplay ?: return com.mineinabyss.bonfire.bonfire.plugin.launch { @@ -98,6 +100,7 @@ class PlayerListener : Listener { } @EventHandler fun PlayerQuitEvent.onPlayerQuit() { + player.toGearyOrNull()?.remove() player.persistentDataContainer.remove() } }