Skip to content

Commit

Permalink
refactor: revert Geary Cooldown change
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed May 12, 2024
1 parent 69062b9 commit 2102937
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 35 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/com/mineinabyss/bonfire/BonfireCommands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,7 +52,7 @@ class BonfireCommands : IdofrontCommandExecutor(), TabCompleter {
}
}
"reload" {
actions {
actions(bonfire.logger) {
bonfire.plugin.registerBonfireContext()
sender.success("Bonfire configs have been reloaded!")
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/mineinabyss/bonfire/BonfireContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<BonfireContext>()
interface BonfireContext {
val plugin: BonfirePlugin
val config: BonfireConfig
val messages: BonfireMessages
val logger: ComponentLogger
}
10 changes: 4 additions & 6 deletions src/main/kotlin/com/mineinabyss/bonfire/BonfirePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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);

}}
}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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()
}
Expand All @@ -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)
Expand All @@ -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<BonfireCooldown>()) 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 &&
Expand Down Expand Up @@ -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<BonfireCooldown>()
}
}
}

Expand All @@ -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<BonfireCooldown>()
}
}

private fun ItemDisplay.ensureSavedPlayersAreValid(bonfireData: Bonfire): Boolean {
val validPlayers = bonfireData.bonfirePlayers.filter {
val offlinePlayer = Bukkit.getOfflinePlayer(it)
Expand Down Expand Up @@ -189,10 +188,12 @@ class BonfireListener : Listener {
if (onlinePlayer != null) {
onlinePlayer.toGeary().remove<BonfireEffectArea>()
onlinePlayer.toGeary().remove<BonfireRespawn>()
onlinePlayer.toGeary().remove<BonfireCooldown>()
} else {
p.editOfflinePDC {
encode(BonfireRemoved())
remove<BonfireRespawn>()
remove<BonfireCooldown>()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -89,6 +90,7 @@ class PlayerListener : Listener {

@EventHandler
fun PlayerJoinEvent.onPlayerJoin() {
player.toGearyOrNull()?.remove<BonfireCooldown>()
val bonfire = player.toGeary().get<BonfireRespawn>() ?: return
val bonfireEntity = bonfire.bonfireUuid.toEntity() as? ItemDisplay ?: return
com.mineinabyss.bonfire.bonfire.plugin.launch {
Expand All @@ -98,6 +100,7 @@ class PlayerListener : Listener {
}
@EventHandler
fun PlayerQuitEvent.onPlayerQuit() {
player.toGearyOrNull()?.remove<BonfireCooldown>()
player.persistentDataContainer.remove<BonfireRemoved>()
}
}

0 comments on commit 2102937

Please sign in to comment.