From 03491e4d2a754623861f718769f67c708af96450 Mon Sep 17 00:00:00 2001 From: Boy Date: Thu, 11 Jul 2024 16:24:42 +0200 Subject: [PATCH] fix: spectator debug-mode & code cleanup --- .../bonfire/extensions/BonfireHelpers.kt | 8 ++++++++ .../bonfire/listeners/BonfireListener.kt | 3 +-- .../bonfire/listeners/DebugListener.kt | 18 +++++------------- .../listeners/FixUntrackedBonfiresListener.kt | 5 ++--- .../bonfire/listeners/PlayerListener.kt | 3 ++- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfireHelpers.kt b/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfireHelpers.kt index e5fb922..ce1a5b4 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfireHelpers.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfireHelpers.kt @@ -24,6 +24,14 @@ import org.bukkit.entity.Entity import org.bukkit.entity.ItemDisplay import org.bukkit.entity.Player import kotlin.math.pow +fun Iterable.forEachBonfire(action: (ItemDisplay) -> Unit): Unit { + for (element in this.filterIsBonfire()) action(element) +} +fun Array.forEachBonfire(action: (ItemDisplay) -> Unit): Unit { + for (element in this.filterIsBonfire()) action(element) +} +fun Iterable.filterIsBonfire() = mapNotNull { it.takeIf { it.isBonfire } as? ItemDisplay } +fun Array.filterIsBonfire() = mapNotNull { it.takeIf { it.isBonfire } as? ItemDisplay } val Entity.isBonfire: Boolean get() = this is ItemDisplay && this.toGearyOrNull()?.has() == true diff --git a/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt b/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt index 7c07996..b470de7 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt @@ -180,8 +180,7 @@ class BonfireListener : Listener { */ @EventHandler fun EntityRemoveFromWorldEvent.onRemoveBonfire() { - if (!entity.isDead) return - val bonfireData = (entity as? ItemDisplay)?.toGearyOrNull()?.get() ?: return + val bonfireData = (entity as? ItemDisplay).takeIf { entity.isDead }?.toGearyOrNull()?.get() ?: return BlockyFurnitures.removeFurniture(entity as ItemDisplay) diff --git a/src/main/kotlin/com/mineinabyss/bonfire/listeners/DebugListener.kt b/src/main/kotlin/com/mineinabyss/bonfire/listeners/DebugListener.kt index 7887d38..8d36e9f 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/listeners/DebugListener.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/listeners/DebugListener.kt @@ -7,6 +7,8 @@ import com.mineinabyss.blocky.helpers.GenericHelpers.toBlockCenterLocation import com.mineinabyss.bonfire.bonfire import com.mineinabyss.bonfire.components.Bonfire import com.mineinabyss.bonfire.components.BonfireDebug +import com.mineinabyss.bonfire.extensions.filterIsBonfire +import com.mineinabyss.bonfire.extensions.forEachBonfire import com.mineinabyss.bonfire.extensions.isBonfire import com.mineinabyss.geary.papermc.tracking.entities.toGeary import com.mineinabyss.idofront.entities.toOfflinePlayer @@ -45,8 +47,7 @@ class DebugListener : Listener { @EventHandler fun PlayerToggleSneakEvent.onDebugToggle() { - if (!player.toGeary().has()) return - player.getNearbyEntities(16.0, 16.0, 16.0).filterIsInstance().filter { it.isBonfire }.forEach { + if (player.toGeary().has()) player.getNearbyEntities(16.0, 16.0, 16.0).forEachBonfire { if (isSneaking) player.sendDebugTextDisplay(it) else removeDebugTextDisplay(player) } @@ -54,21 +55,12 @@ class DebugListener : Listener { @EventHandler fun PlayerGameModeChangeEvent.onDebugToggle() { - if (!player.toGeary().has()) return - player.getNearbyEntities(16.0, 16.0, 16.0).filterIsInstance().filter { it.isBonfire }.forEach { + if (player.toGeary().has()) player.getNearbyEntities(16.0, 16.0, 16.0).forEachBonfire { if (newGameMode == GameMode.SPECTATOR) player.sendDebugTextDisplay(it) else removeDebugTextDisplay(player) } } - @EventHandler - fun PlayerMoveEvent.onDebugToggle() { - if (player.gameMode != GameMode.SPECTATOR || !player.toGeary().has()) return - player.getNearbyEntities(16.0, 16.0, 16.0).filterIsInstance().filter { it.isBonfire }.forEach { - player.sendDebugTextDisplay(it) - } - } - private val debugIdMap = mutableMapOf>() private fun Player.sendDebugTextDisplay(baseEntity: ItemDisplay) { val entityIds = debugIdMap.computeIfAbsent(uniqueId) { mutableMapOf(baseEntity.uniqueId to Entity.nextEntityId()) } @@ -85,7 +77,7 @@ class DebugListener : Listener { do { this@sendDebugTextDisplay.sendDebugText(baseEntity, entityId) delay(1.seconds) - } while (isSneaking) + } while (isSneaking || gameMode == GameMode.SPECTATOR) removeDebugTextDisplay(this@sendDebugTextDisplay) } } diff --git a/src/main/kotlin/com/mineinabyss/bonfire/listeners/FixUntrackedBonfiresListener.kt b/src/main/kotlin/com/mineinabyss/bonfire/listeners/FixUntrackedBonfiresListener.kt index e05594a..7a5e6a6 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/listeners/FixUntrackedBonfiresListener.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/listeners/FixUntrackedBonfiresListener.kt @@ -21,10 +21,9 @@ class FixUntrackedBonfiresListener : Listener { @EventHandler fun ChunkLoadEvent.onAddToWorld() { - chunk.entities.forEach { entity -> - if (entity !is ItemDisplay) return@forEach + chunk.entities.filterIsInstance().forEach { entity -> if (entity.persistentDataContainer.hasComponentsEncoded) return@forEach - val displayItemPDC = entity.itemStack?.itemMeta?.persistentDataContainer ?: return + val displayItemPDC = entity.itemStack.itemMeta?.persistentDataContainer ?: return val itemPrefabs = displayItemPDC.decodePrefabs() if (bonfireItemKey in itemPrefabs || bonfireLitItemKey in itemPrefabs) { diff --git a/src/main/kotlin/com/mineinabyss/bonfire/listeners/PlayerListener.kt b/src/main/kotlin/com/mineinabyss/bonfire/listeners/PlayerListener.kt index fe00c8d..612055b 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/listeners/PlayerListener.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/listeners/PlayerListener.kt @@ -7,6 +7,7 @@ import com.mineinabyss.bonfire.bonfire import com.mineinabyss.bonfire.components.Bonfire import com.mineinabyss.bonfire.components.BonfireRemoved import com.mineinabyss.bonfire.components.BonfireRespawn +import com.mineinabyss.bonfire.extensions.filterIsBonfire import com.mineinabyss.bonfire.extensions.isBonfire import com.mineinabyss.bonfire.extensions.updateBonfireState import com.mineinabyss.geary.papermc.datastore.remove @@ -47,7 +48,7 @@ class PlayerListener : Listener { val loc = bonfireRespawn.bonfireLocation loc.world.getChunkAtAsyncUrgently(loc).thenAccept { chunk -> - val bonfireEntity = chunk.entities.filterIsInstance().find { it.isBonfire && it.uniqueId == bonfireRespawn.bonfireUuid } ?: return@thenAccept + val bonfireEntity = chunk.entities.filterIsBonfire().find { it.uniqueId == bonfireRespawn.bonfireUuid } ?: return@thenAccept val bonfireData = bonfireEntity.toGeary().get() ?: return@thenAccept when {