Skip to content

Commit

Permalink
Feat: Ensure players on bonfire are still valid when checking a full …
Browse files Browse the repository at this point in the history
…bonfire, or checking if player can remove
  • Loading branch information
0ffz committed Feb 1, 2024
1 parent 8aab0de commit 0fc6d12
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kotlinx.serialization.Serializable
@Serializable
data class BonfireMessages(
val BONFIRE_REMOVED: String = "<red>Your respawn point was unset because the bonfire was broken by the owner",
val BONFIRE_BREAK_DENIED: String = "<red>You cannot break this bonfire, unkindled one",
val BONFIRE_BREAK_DENIED: String = "<red>You cannot break this bonfire, others are using it and you are not the owner",
val BONFIRE_BREAK: String = "<red>Respawn point has been removed",
val BONFIRE_FULL: String = "<red>This bonfire is full",
val BONFIRE_EXPIRED: String = "<red>The bonfire has expired and turned to ash",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.mineinabyss.bonfire.extensions.removeOldBonfire
import com.mineinabyss.bonfire.extensions.updateBonfireState
import com.mineinabyss.geary.helpers.with
import com.mineinabyss.geary.papermc.bridge.conditions.Cooldown
import com.mineinabyss.geary.papermc.datastore.decode
import com.mineinabyss.geary.papermc.datastore.encode
import com.mineinabyss.geary.papermc.datastore.encodeComponentsTo
import com.mineinabyss.geary.papermc.datastore.remove
Expand All @@ -22,6 +23,8 @@ import com.mineinabyss.idofront.entities.toOfflinePlayer
import com.mineinabyss.idofront.messaging.error
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 org.bukkit.entity.ItemDisplay
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
Expand Down Expand Up @@ -101,16 +104,18 @@ class BonfireListener : Listener {

val gearyPlayer = player.toGeary()
val gearyBonfire = baseEntity.toGearyOrNull() ?: return
if (!Cooldown.isComplete(gearyPlayer, gearyBonfire, cooldown)) {
if (!Cooldown.isComplete(gearyPlayer, gearyBonfire)) {
isCancelled = true
return
}
Cooldown.start(gearyPlayer, gearyBonfire)
Cooldown.start(gearyPlayer, gearyBonfire, cooldown)

gearyBonfire.with { bonfireData: Bonfire ->
when (player.uniqueId) {
!in bonfireData.bonfirePlayers -> {
if (bonfireData.bonfirePlayers.size >= bonfireData.maxPlayerCount) player.error(bonfire.messages.BONFIRE_FULL)
if (bonfireData.bonfirePlayers.size >= bonfireData.maxPlayerCount &&
baseEntity.ensureSavedPlayersAreValid(bonfireData) // Will double-check that the players are still valid, returns true if size # of players didn't change
) player.error(bonfire.messages.BONFIRE_FULL)
else {
// Load old bonfire and remove player from it if it exists
player.removeOldBonfire()
Expand Down Expand Up @@ -145,13 +150,28 @@ class BonfireListener : Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
fun BlockyFurnitureBreakEvent.onBreakBonfire() {
baseEntity.toGearyOrNull()?.with { bonfireData: Bonfire ->
baseEntity.ensureSavedPlayersAreValid(bonfireData)
if (!player.canBreakBonfire(bonfireData)) {
player.error(bonfire.messages.BONFIRE_BREAK_DENIED)
isCancelled = true
}
}
}

private fun ItemDisplay.ensureSavedPlayersAreValid(bonfireData: Bonfire): Boolean {
val validPlayers = bonfireData.bonfirePlayers.filter {
val offlinePlayer = Bukkit.getOfflinePlayer(it)
val respawn = when {
offlinePlayer.isOnline -> offlinePlayer.player?.toGearyOrNull()?.get<BonfireRespawn>()
else -> offlinePlayer.getOfflinePDC()?.decode<BonfireRespawn>()
}
respawn?.bonfireUuid == this.uniqueId
}
bonfireData.bonfirePlayers.clear()
bonfireData.bonfirePlayers.addAll(validPlayers)
return validPlayers.size == bonfireData.bonfirePlayers.size
}

/**
* When a bonfire is marked for removal, either via commands or being broken in any way
* The below listener will handle completely removing it and all assosiacted playerdata
Expand Down

0 comments on commit 0fc6d12

Please sign in to comment.