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 07f8b90
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ 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 All @@ -48,6 +57,8 @@ fun Player.removeOldBonfire() {
}
}

private val bonfireTrackingRadius = (Bukkit.getServer().simulationDistance * 16.0).pow(2)

/**
* Updates the bonfire state for all players.
*/
Expand All @@ -71,8 +82,8 @@ fun ItemDisplay.updateBonfireState() {
)

com.mineinabyss.bonfire.bonfire.plugin.launch(com.mineinabyss.bonfire.bonfire.plugin.minecraftDispatcher) {
delay(1.ticks)
bonfire.bonfirePlayers.mapNotNull { it.toPlayer() }.filter { it.world == world && it.location.distanceSquared(location) < (Bukkit.getServer().simulationDistance * 16.0).pow(2) }.forEach {
delay(3.ticks)
bonfire.bonfirePlayers.mapNotNull { it.toPlayer() }.filter { it.world.uid == world.uid && it.location.distanceSquared(location) < bonfireTrackingRadius }.forEach {
(it as CraftPlayer).handle.connection.send(metadataPacket)
}
}
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,8 @@ 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 All @@ -63,6 +65,7 @@ class PlayerListener : Listener {
player.info(bonfire.messages.BONFIRE_RESPAWNING)
player.teleportAsync(loc.toCenterLocation())
}

else -> {
player.error(bonfire.messages.BONFIRE_NOT_FOUND)
player.toGeary().remove<BonfireRespawn>()
Expand Down

0 comments on commit 07f8b90

Please sign in to comment.