Skip to content

Commit

Permalink
fix: addon-entity not always teleporting to furniture entity location
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Oct 14, 2024
1 parent 9e4419a commit bbbc645
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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<ClientboundSetEntityDataPacket>) {
fun bundlePacket(bonfireSize: Int) : ClientboundBundlePacket {
return ClientboundBundlePacket(listOf(addEntity, addons.elementAtOrNull(bonfireSize)))
data class BonfireAddonPacket(val addonEntity: Display.ItemDisplay, val addons: List<ClientboundSetEntityDataPacket>) {
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)
))
}
}

Expand All @@ -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<Bonfire>() ?: 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ class BonfireListener : Listener {

@EventHandler
fun AxiomManipulateEntityEvent.manipulateBonfire() {
val bonfireData = (entity as? ItemDisplay).takeUnless { entity.isDead }?.toGearyOrNull()?.get<Bonfire>() ?: return
val furniture = (entity as? ItemDisplay).takeUnless { entity.isDead } ?: return
val bonfireData = furniture.toGearyOrNull()?.get<Bonfire>() ?: return

bonfireData.bonfirePlayers.map { it.toOfflinePlayer() to it.toPlayer() }.forEach { (offline, online) ->
if (online != null) with(online.toGeary()) {
Expand All @@ -211,6 +212,7 @@ class BonfireListener : Listener {
decode<BonfireRespawn>()?.copy(bonfireLocation = entity.location)?.let { encode(it) }
}
}
BonfirePacketHelpers.sendAddonPacket(furniture)
}
}, bonfire.plugin)
}
Expand Down

0 comments on commit bbbc645

Please sign in to comment.