From 6384817a9fc31327e2278e835d69d1e4e25b7c3a Mon Sep 17 00:00:00 2001 From: Boy Date: Sat, 2 Dec 2023 02:15:11 +0100 Subject: [PATCH] refactor: use mutable list for bonfirePlayers --- .../mineinabyss/bonfire/BonfireCommands.kt | 15 ++++++--------- .../mineinabyss/bonfire/components/Bonfire.kt | 2 +- .../bonfire/extensions/BonfireHelpers.kt | 8 +------- .../bonfire/listeners/BonfireListener.kt | 19 +++++++++---------- 4 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/main/kotlin/com/mineinabyss/bonfire/BonfireCommands.kt b/src/main/kotlin/com/mineinabyss/bonfire/BonfireCommands.kt index 2b5e4a3..cc4437e 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/BonfireCommands.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/BonfireCommands.kt @@ -88,17 +88,15 @@ class BonfireCommands : IdofrontCommandExecutor(), TabCompleter { bonfireEntity?.toGearyOrNull()?.get()?.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") } @@ -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()?.let { bonfire -> - bonfireEntity.toGeary() - .setPersisting(bonfire.copy(bonfirePlayers = bonfire.bonfirePlayers - offlinePlayer.uniqueId)) + bonfire.bonfirePlayers -= offlinePlayer.uniqueId if (bonfire.bonfirePlayers.isEmpty()) bonfireEntity.updateBonfireState() } } diff --git a/src/main/kotlin/com/mineinabyss/bonfire/components/Bonfire.kt b/src/main/kotlin/com/mineinabyss/bonfire/components/Bonfire.kt index 35f1f57..c68ef20 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/components/Bonfire.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/components/Bonfire.kt @@ -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, diff --git a/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfireHelpers.kt b/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfireHelpers.kt index a7e1dd9..8525f7c 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfireHelpers.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfireHelpers.kt @@ -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 @@ -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() == true @@ -47,8 +42,7 @@ fun Player.removeOldBonfire() { val bonfireRespawn = toGeary().get() ?: 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() ?: return@let - geary.setPersisting(bonfire.copy(bonfirePlayers = bonfire.bonfirePlayers - uniqueId)) + geary.get()?.let { it.bonfirePlayers -= uniqueId } } } } diff --git a/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt b/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt index 5e05d7e..b39fa67 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt @@ -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 @@ -86,14 +86,13 @@ class BonfireListener : Listener { if (!player.isSneaking || player.toGeary().has()) 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 @@ -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() player.toGeary().remove() - bonfire.config.respawnUnsetSound.run { + with(bonfire.config.respawnUnsetSound) { baseEntity.world.playSound(baseEntity.location, sound, volume, pitch) } player.error(bonfire.messages.BONFIRE_BREAK)