Skip to content

Commit

Permalink
refactor: use mutable list for bonfirePlayers
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Dec 2, 2023
1 parent 986c73d commit 6384817
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 27 deletions.
15 changes: 6 additions & 9 deletions src/main/kotlin/com/mineinabyss/bonfire/BonfireCommands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,15 @@ class BonfireCommands : IdofrontCommandExecutor(), TabCompleter {

bonfireEntity?.toGearyOrNull()?.get<Bonfire>()?.let { bonfire ->
when {
offlinePlayer.uniqueId in bonfire.bonfirePlayers -> return@thenAccept sender.error("Player is already registered to this bonfire")
bonfire.bonfirePlayers.size >= bonfire.maxPlayerCount -> return@thenAccept sender.error(
"Bonfire is full"
)

offlinePlayer.uniqueId in bonfire.bonfirePlayers ->
sender.error("Player is already registered to this bonfire")
bonfire.bonfirePlayers.size >= bonfire.maxPlayerCount ->
sender.error("Bonfire is full")
else -> {
offlinePlayer.editOfflinePDC {
encode(BonfireRespawn(bonfireEntity.uniqueId, bonfireEntity.location))
}
bonfireEntity.toGeary()
.setPersisting(bonfire.copy(bonfirePlayers = bonfire.bonfirePlayers + offlinePlayer.uniqueId))
bonfire.bonfirePlayers += offlinePlayer.uniqueId
bonfireEntity.updateBonfireState()
sender.success("Set respawn point for ${offlinePlayer.name} to $x $y $z in $worldName")
}
Expand Down Expand Up @@ -128,8 +126,7 @@ class BonfireCommands : IdofrontCommandExecutor(), TabCompleter {
respawn.bonfireLocation.world.getChunkAtAsync(respawn.bonfireLocation).thenAccept {
val bonfireEntity = Bukkit.getEntity(respawn.bonfireUuid) as? ItemDisplay
bonfireEntity?.toGeary()?.get<Bonfire>()?.let { bonfire ->
bonfireEntity.toGeary()
.setPersisting(bonfire.copy(bonfirePlayers = bonfire.bonfirePlayers - offlinePlayer.uniqueId))
bonfire.bonfirePlayers -= offlinePlayer.uniqueId
if (bonfire.bonfirePlayers.isEmpty()) bonfireEntity.updateBonfireState()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import kotlin.time.Duration
@SerialName("bonfire:bonfire")
data class Bonfire(
val bonfireOwner: @Serializable(UUIDSerializer::class) UUID? = null,
val bonfirePlayers: List<@Serializable(UUIDSerializer::class) UUID> = emptyList(),
val bonfirePlayers: MutableList<@Serializable(UUIDSerializer::class) UUID> = mutableListOf(),
val maxPlayerCount: Int = bonfire.config.maxPlayerCount,
val bonfireExpirationTime: @Serializable(with = DurationSerializer::class) Duration = bonfire.config.bonfireExpirationTime,
val states: BonfireStates,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import com.comphenix.protocol.events.PacketContainer
import com.github.shynixn.mccoroutine.bukkit.launch
import com.github.shynixn.mccoroutine.bukkit.minecraftDispatcher
import com.github.shynixn.mccoroutine.bukkit.ticks
import com.mineinabyss.blocky.api.BlockyFurnitures
import com.mineinabyss.blocky.components.core.BlockyFurniture
import com.mineinabyss.blocky.helpers.FurniturePacketHelpers.ITEM_DISPLAY_ITEMSTACK_ID
import com.mineinabyss.bonfire.bonfire
import com.mineinabyss.bonfire.components.Bonfire
import com.mineinabyss.bonfire.components.BonfireRespawn
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull
import com.mineinabyss.geary.papermc.tracking.items.gearyItems
import com.mineinabyss.idofront.entities.toPlayer
import com.mineinabyss.idofront.messaging.broadcast
import com.mineinabyss.protocolburrito.dsl.sendTo
import kotlinx.coroutines.delay
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket
Expand All @@ -27,8 +24,6 @@ import org.bukkit.entity.Entity
import org.bukkit.entity.ItemDisplay
import org.bukkit.entity.Player
import kotlin.math.pow
import kotlin.math.sqrt
import kotlin.time.Duration.Companion.seconds

val Entity.isBonfire: Boolean
get() = this is ItemDisplay && this.toGearyOrNull()?.has<Bonfire>() == true
Expand All @@ -47,8 +42,7 @@ fun Player.removeOldBonfire() {
val bonfireRespawn = toGeary().get<BonfireRespawn>() ?: return
bonfireRespawn.bonfireLocation.world.getChunkAtAsync(bonfireRespawn.bonfireLocation).thenAccept { chunk ->
chunk.entities.find { it.isBonfire && it.uniqueId == bonfireRespawn.bonfireUuid }?.toGearyOrNull()?.let { geary ->
val bonfire = geary.get<Bonfire>() ?: return@let
geary.setPersisting(bonfire.copy(bonfirePlayers = bonfire.bonfirePlayers - uniqueId))
geary.get<Bonfire>()?.let { it.bonfirePlayers -= uniqueId }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import com.mineinabyss.blocky.api.events.furniture.BlockyFurnitureInteractEvent
import com.mineinabyss.blocky.api.events.furniture.BlockyFurniturePlaceEvent
import com.mineinabyss.bonfire.bonfire
import com.mineinabyss.bonfire.components.*
import com.mineinabyss.bonfire.extensions.*
import com.mineinabyss.bonfire.extensions.canBreakBonfire
import com.mineinabyss.bonfire.extensions.isBonfire
import com.mineinabyss.bonfire.extensions.removeOldBonfire
import com.mineinabyss.bonfire.extensions.updateBonfireState
import com.mineinabyss.geary.helpers.with
import com.mineinabyss.geary.papermc.datastore.encode
import com.mineinabyss.geary.papermc.datastore.remove
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull
import com.mineinabyss.idofront.entities.toOfflinePlayer
import com.mineinabyss.idofront.messaging.broadcast
import com.mineinabyss.idofront.messaging.broadcastVal
import com.mineinabyss.idofront.messaging.error
import com.mineinabyss.idofront.messaging.success
import com.mineinabyss.idofront.nms.nbt.editOfflinePDC
import kotlinx.coroutines.delay
import org.bukkit.block.BlockFace
import org.bukkit.entity.ItemDisplay
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
Expand Down Expand Up @@ -86,14 +86,13 @@ class BonfireListener : Listener {
if (!player.isSneaking || player.toGeary().has<BonfireCooldown>()) return
if (hand != EquipmentSlot.HAND || abs(0 - player.velocity.y) < 0.001) return

val gearyEntity = baseEntity.toGearyOrNull() ?: return
gearyEntity.with { bonfireData: Bonfire ->
baseEntity.toGearyOrNull()?.with { bonfireData: Bonfire ->
when (player.uniqueId) {
!in bonfireData.bonfirePlayers -> {
if (bonfireData.bonfirePlayers.size >= bonfireData.maxPlayerCount) player.error(bonfire.messages.BONFIRE_FULL)
else {
gearyEntity.setPersisting(bonfireData.copy(bonfirePlayers = bonfireData.bonfirePlayers + player.uniqueId))
bonfire.config.respawnSetSound.run {
bonfireData.bonfirePlayers += player.uniqueId
with(bonfire.config.respawnSetSound) {
baseEntity.world.playSound(baseEntity.location, sound, volume, pitch)
}
// Load old bonfire and remove player from it if it exists
Expand All @@ -106,10 +105,10 @@ class BonfireListener : Listener {
}

in bonfireData.bonfirePlayers -> {
gearyEntity.setPersisting(bonfireData.copy(bonfirePlayers = bonfireData.bonfirePlayers - player.uniqueId))
bonfireData.bonfirePlayers -= player.uniqueId
player.toGeary().remove<BonfireRespawn>()
player.toGeary().remove<BonfireEffectArea>()
bonfire.config.respawnUnsetSound.run {
with(bonfire.config.respawnUnsetSound) {
baseEntity.world.playSound(baseEntity.location, sound, volume, pitch)
}
player.error(bonfire.messages.BONFIRE_BREAK)
Expand Down

0 comments on commit 6384817

Please sign in to comment.