From bbbc64565321d1f1e69c773858cb88b86bfbed3f Mon Sep 17 00:00:00 2001 From: Boy Date: Mon, 14 Oct 2024 23:22:03 +0200 Subject: [PATCH] fix: addon-entity not always teleporting to furniture entity location --- .../extensions/BonfirePacketHelpers.kt | 34 ++++++++++--------- .../bonfire/listeners/BonfireListener.kt | 4 ++- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfirePacketHelpers.kt b/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfirePacketHelpers.kt index 41576e3..02053ac 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfirePacketHelpers.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/extensions/BonfirePacketHelpers.kt @@ -7,6 +7,8 @@ import com.mineinabyss.bonfire.bonfire import com.mineinabyss.bonfire.components.Bonfire import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull import com.mineinabyss.geary.papermc.tracking.items.gearyItems +import com.mineinabyss.idofront.messaging.broadcast +import com.mineinabyss.idofront.nms.aliases.NMSEntity import com.mineinabyss.idofront.nms.aliases.toNMS import com.mineinabyss.idofront.time.ticks import it.unimi.dsi.fastutil.ints.IntList @@ -19,6 +21,7 @@ import net.minecraft.network.syncher.EntityDataSerializers import net.minecraft.network.syncher.SynchedEntityData import net.minecraft.world.entity.Display import net.minecraft.world.entity.EntityType +import org.bukkit.Location import org.bukkit.craftbukkit.entity.CraftPlayer import org.bukkit.craftbukkit.inventory.CraftItemStack import org.bukkit.entity.ItemDisplay @@ -27,9 +30,13 @@ import org.bukkit.entity.Player object BonfirePacketHelpers { data class BonfireAddonEntity(val furnitureUUID: FurnitureUUID, val addonEntity: Display.ItemDisplay) - data class BonfireAddonPacket(val addonEntity: Display.ItemDisplay, val addEntity: ClientboundAddEntityPacket, val addons: List) { - fun bundlePacket(bonfireSize: Int) : ClientboundBundlePacket { - return ClientboundBundlePacket(listOf(addEntity, addons.elementAtOrNull(bonfireSize))) + data class BonfireAddonPacket(val addonEntity: Display.ItemDisplay, val addons: List) { + fun bundlePacket(furniture: NMSEntity, bonfireSize: Int) : ClientboundBundlePacket { + addonEntity.teleportTo(furniture.x, furniture.y, furniture.z) + return ClientboundBundlePacket(listOf( + addonEntity.getAddEntityPacket(furniture.`moonrise$getTrackedEntity`().serverEntity), + addons.elementAtOrNull(bonfireSize) + )) } } @@ -43,29 +50,24 @@ object BonfirePacketHelpers { fun sendAddonPacket(furniture: ItemDisplay, player: Player) { if (!furniture.isBlockyFurniture) return - val plugin = bonfire.plugin val bonfire = furniture.toGearyOrNull()?.get() ?: return val nmsFurniture = furniture.toNMS() - val nmsWorld = nmsFurniture.level() - bonfireAddonPackets.computeIfAbsent(furniture.uniqueId) { + val bonfireAddonPacket = bonfireAddonPackets.computeIfAbsent(furniture.uniqueId) { val addonEntity = bonfireAddons.firstOrNull { it.furnitureUUID == furniture.uniqueId }?.addonEntity - ?: BonfireAddonEntity(furniture.uniqueId, Display.ItemDisplay(EntityType.ITEM_DISPLAY, nmsWorld)) - .apply(bonfireAddons::add).addonEntity.apply { teleportTo(furniture.x, furniture.y, furniture.z) } + ?: BonfireAddonEntity(furniture.uniqueId, Display.ItemDisplay(EntityType.ITEM_DISPLAY, nmsFurniture.level())) + .apply(bonfireAddons::add).addonEntity - val entityPacket = ClientboundAddEntityPacket(addonEntity, nmsFurniture.`moonrise$getTrackedEntity`().serverEntity) val metadataPackets = bonfire.addons.mapNotNull { addon -> val item = gearyItems.createItem(addon) ?: return@mapNotNull null ClientboundSetEntityDataPacket(addonEntity.id, listOf(SynchedEntityData.DataValue(23, EntityDataSerializers.ITEM_STACK, CraftItemStack.asNMSCopy(item))) ) } - BonfireAddonPacket(addonEntity, entityPacket, metadataPackets) - }.bundlePacket(bonfire.bonfirePlayers.size).let { - plugin.launch { - delay(2.ticks) - (player as CraftPlayer).handle.connection.send(it) - } - } + + BonfireAddonPacket(addonEntity, metadataPackets) + }.bundlePacket(nmsFurniture, bonfire.bonfirePlayers.size) + + (player as CraftPlayer).handle.connection.send(bonfireAddonPacket) } fun removeAddonPacket(furniture: ItemDisplay) { diff --git a/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt b/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt index b49d38d..0597839 100644 --- a/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt +++ b/src/main/kotlin/com/mineinabyss/bonfire/listeners/BonfireListener.kt @@ -201,7 +201,8 @@ class BonfireListener : Listener { @EventHandler fun AxiomManipulateEntityEvent.manipulateBonfire() { - val bonfireData = (entity as? ItemDisplay).takeUnless { entity.isDead }?.toGearyOrNull()?.get() ?: return + val furniture = (entity as? ItemDisplay).takeUnless { entity.isDead } ?: return + val bonfireData = furniture.toGearyOrNull()?.get() ?: return bonfireData.bonfirePlayers.map { it.toOfflinePlayer() to it.toPlayer() }.forEach { (offline, online) -> if (online != null) with(online.toGeary()) { @@ -211,6 +212,7 @@ class BonfireListener : Listener { decode()?.copy(bonfireLocation = entity.location)?.let { encode(it) } } } + BonfirePacketHelpers.sendAddonPacket(furniture) } }, bonfire.plugin) }