Skip to content

Commit

Permalink
fix: properly send lit/unlit state to players
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Oct 11, 2024
1 parent 70e521d commit 8157ec3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.mineinabyss.bonfire.extensions

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.components.core.BlockyFurniture
import com.mineinabyss.bonfire.bonfire
import com.mineinabyss.bonfire.components.Bonfire
import com.mineinabyss.bonfire.components.BonfireRemoved
import com.mineinabyss.bonfire.components.BonfireRespawn
Expand All @@ -15,14 +15,12 @@ import kotlinx.coroutines.delay
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket
import net.minecraft.network.syncher.EntityDataSerializers
import net.minecraft.network.syncher.SynchedEntityData
import org.bukkit.Bukkit
import org.bukkit.craftbukkit.entity.CraftPlayer
import org.bukkit.craftbukkit.inventory.CraftItemStack
import org.bukkit.entity.Display
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)
Expand Down Expand Up @@ -56,15 +54,13 @@ fun Player.removeOldBonfire() {
}
}

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

/**
* Updates the bonfire state for all players.
*/
fun ItemDisplay.updateBonfireState() {
val plugin = bonfire.plugin
val bonfire = toGearyOrNull()?.get<Bonfire>() ?: return


when {// Set the base-furniture item to the correct state
bonfire.bonfirePlayers.isEmpty() -> {
this.brightness = toGearyOrNull()?.get<BlockyFurniture>()?.properties?.brightness
Expand All @@ -80,9 +76,9 @@ fun ItemDisplay.updateBonfireState() {
listOf(SynchedEntityData.DataValue(23, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(stateItem)))
)

com.mineinabyss.bonfire.bonfire.plugin.launch(com.mineinabyss.bonfire.bonfire.plugin.minecraftDispatcher) {
delay(3.ticks)
bonfire.bonfirePlayers.mapNotNull { it.toPlayer() }.filter { it.world.uid == world.uid && it.location.distanceSquared(location) < bonfireTrackingRadius }.forEach {
plugin.launch {
delay(2.ticks)
bonfire.bonfirePlayers.mapNotNull { it.toPlayer()?.takeIf( { p -> p.canSee(this@updateBonfireState) }) }.forEach {
(it as CraftPlayer).handle.connection.send(metadataPacket)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.mineinabyss.bonfire.listeners

import com.destroystokyo.paper.event.player.PlayerPostRespawnEvent
import com.github.shynixn.mccoroutine.bukkit.launch
import com.mineinabyss.blocky.helpers.GenericHelpers.toEntity
import com.mineinabyss.bonfire.bonfire
import com.mineinabyss.bonfire.components.Bonfire
import com.mineinabyss.bonfire.components.BonfireRemoved
Expand All @@ -16,7 +14,7 @@ import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull
import com.mineinabyss.idofront.entities.rightClicked
import com.mineinabyss.idofront.messaging.error
import com.mineinabyss.idofront.messaging.info
import com.mineinabyss.idofront.time.ticks
import io.papermc.paper.event.player.PlayerTrackEntityEvent
import kotlinx.coroutines.delay
import org.bukkit.block.Block
import org.bukkit.block.BlockFace
Expand Down Expand Up @@ -75,9 +73,8 @@ class PlayerListener : Listener {
}

@EventHandler
fun PlayerPostRespawnEvent.onBonfireRespawned() {
val bonfireRespawn = player.toGeary().get<BonfireRespawn>() ?: return
(bonfireRespawn.bonfireUuid.toEntity() as? ItemDisplay)?.updateBonfireState()
fun PlayerTrackEntityEvent.onTrackBonfire() {
(entity as? ItemDisplay)?.updateBonfireState()
}

@EventHandler(priority = EventPriority.HIGHEST)
Expand All @@ -90,15 +87,6 @@ class PlayerListener : Listener {
}
}

@EventHandler
fun PlayerJoinEvent.onPlayerJoin() {
val bonfire = player.toGeary().get<BonfireRespawn>() ?: return
val bonfireEntity = bonfire.bonfireUuid.toEntity() as? ItemDisplay ?: return
com.mineinabyss.bonfire.bonfire.plugin.launch {
delay(3.ticks)
bonfireEntity.updateBonfireState()
}
}
@EventHandler
fun PlayerQuitEvent.onPlayerQuit() {
player.persistentDataContainer.remove<BonfireRemoved>()
Expand Down

0 comments on commit 8157ec3

Please sign in to comment.