diff --git a/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfireHelpers.kt b/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfireHelpers.kt index 389341e..d6ab4a8 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfireHelpers.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfireHelpers.kt @@ -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 @@ -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.forEachBonfire(action: (ItemDisplay) -> Unit): Unit { for (element in this.filterIsBonfire()) action(element) @@ -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() ?: return - when {// Set the base-furniture item to the correct state bonfire.bonfirePlayers.isEmpty() -> { this.brightness = toGearyOrNull()?.get()?.properties?.brightness @@ -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) } } diff --git a/src/main/kotlin/com/mineinabyss/bonfire/listeners/PlayerListener.kt b/src/main/kotlin/com/mineinabyss/bonfire/listeners/PlayerListener.kt index f96f04c..223535a 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/listeners/PlayerListener.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/listeners/PlayerListener.kt @@ -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 @@ -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 @@ -75,9 +73,8 @@ class PlayerListener : Listener { } @EventHandler - fun PlayerPostRespawnEvent.onBonfireRespawned() { - val bonfireRespawn = player.toGeary().get() ?: return - (bonfireRespawn.bonfireUuid.toEntity() as? ItemDisplay)?.updateBonfireState() + fun PlayerTrackEntityEvent.onTrackBonfire() { + (entity as? ItemDisplay)?.updateBonfireState() } @EventHandler(priority = EventPriority.HIGHEST) @@ -90,15 +87,6 @@ class PlayerListener : Listener { } } - @EventHandler - fun PlayerJoinEvent.onPlayerJoin() { - val bonfire = player.toGeary().get() ?: 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()