Skip to content

Commit

Permalink
fix: spectator debug-mode & code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Jul 11, 2024
1 parent 57f757a commit 03491e4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity>.forEachBonfire(action: (ItemDisplay) -> Unit): Unit {
for (element in this.filterIsBonfire()) action(element)
}
fun Array<Entity>.forEachBonfire(action: (ItemDisplay) -> Unit): Unit {
for (element in this.filterIsBonfire()) action(element)
}
fun Iterable<Entity>.filterIsBonfire() = mapNotNull { it.takeIf { it.isBonfire } as? ItemDisplay }
fun Array<Entity>.filterIsBonfire() = mapNotNull { it.takeIf { it.isBonfire } as? ItemDisplay }

val Entity.isBonfire: Boolean
get() = this is ItemDisplay && this.toGearyOrNull()?.has<Bonfire>() == true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ class BonfireListener : Listener {
*/
@EventHandler
fun EntityRemoveFromWorldEvent.onRemoveBonfire() {
if (!entity.isDead) return
val bonfireData = (entity as? ItemDisplay)?.toGearyOrNull()?.get<Bonfire>() ?: return
val bonfireData = (entity as? ItemDisplay).takeIf { entity.isDead }?.toGearyOrNull()?.get<Bonfire>() ?: return

BlockyFurnitures.removeFurniture(entity as ItemDisplay)

Expand Down
18 changes: 5 additions & 13 deletions src/main/kotlin/com/mineinabyss/bonfire/listeners/DebugListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -45,30 +47,20 @@ class DebugListener : Listener {

@EventHandler
fun PlayerToggleSneakEvent.onDebugToggle() {
if (!player.toGeary().has<BonfireDebug>()) return
player.getNearbyEntities(16.0, 16.0, 16.0).filterIsInstance<ItemDisplay>().filter { it.isBonfire }.forEach {
if (player.toGeary().has<BonfireDebug>()) player.getNearbyEntities(16.0, 16.0, 16.0).forEachBonfire {
if (isSneaking) player.sendDebugTextDisplay(it)
else removeDebugTextDisplay(player)
}
}

@EventHandler
fun PlayerGameModeChangeEvent.onDebugToggle() {
if (!player.toGeary().has<BonfireDebug>()) return
player.getNearbyEntities(16.0, 16.0, 16.0).filterIsInstance<ItemDisplay>().filter { it.isBonfire }.forEach {
if (player.toGeary().has<BonfireDebug>()) 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<BonfireDebug>()) return
player.getNearbyEntities(16.0, 16.0, 16.0).filterIsInstance<ItemDisplay>().filter { it.isBonfire }.forEach {
player.sendDebugTextDisplay(it)
}
}

private val debugIdMap = mutableMapOf<UUID, MutableMap<FurnitureUUID, Int>>()
private fun Player.sendDebugTextDisplay(baseEntity: ItemDisplay) {
val entityIds = debugIdMap.computeIfAbsent(uniqueId) { mutableMapOf(baseEntity.uniqueId to Entity.nextEntityId()) }
Expand All @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ class FixUntrackedBonfiresListener : Listener {

@EventHandler
fun ChunkLoadEvent.onAddToWorld() {
chunk.entities.forEach { entity ->
if (entity !is ItemDisplay) return@forEach
chunk.entities.filterIsInstance<ItemDisplay>().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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,7 +48,7 @@ class PlayerListener : Listener {
val loc = bonfireRespawn.bonfireLocation

loc.world.getChunkAtAsyncUrgently(loc).thenAccept { chunk ->
val bonfireEntity = chunk.entities.filterIsInstance<ItemDisplay>().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<Bonfire>() ?: return@thenAccept

when {
Expand Down

0 comments on commit 03491e4

Please sign in to comment.