Skip to content

Commit

Permalink
#585 Reduced amount of packets sent.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shynixn committed May 30, 2024
1 parent 0cdd9c3 commit 14c3e27
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "com.github.shynixn"
version = "9.7.0"
version = "9.7.1"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class ArmorstandEntityComponent(
private val plugin: Plugin,
val entityId: Int,
) : PhysicComponent {
private var lastVisibleVector3d = Vector3d()

init {
playerComponent.onSpawnMinecraft.add { player, location -> onPlayerSpawn(player, location) }
playerComponent.onRemoveMinecraft.add { player, _ -> onPlayerRemove(player) }
Expand Down Expand Up @@ -129,30 +131,41 @@ class ArmorstandEntityComponent(
return
}

for (player in players) {
packetService.sendPacketOutEntityVelocity(player, PacketOutEntityVelocity().also {
it.entityId = this.entityId
it.target = motion.toVector()
})
val hasDistanceChanged = lastVisibleVector3d.distance(position) > 0.1
val hasYawChanged = position.yaw.toFloat() != lastVisibleVector3d.yaw.toFloat()
val hasPitchChanged = position.pitch.toFloat() != lastVisibleVector3d.pitch.toFloat()
lastVisibleVector3d = position.copy()

packetService.sendPacketOutEntityTeleport(player, PacketOutEntityTeleport().also {
it.entityId = this.entityId
it.target = position.copy().addRelativeUp(petMeta.physics.groundOffset).toLocation()
})

if (parsedEntityType != null && parsedEntityType == EntityType.ARMOR_STAND) {
// It causes lag when sent to other entities.
packetService.sendPacketOutEntityMetadata(player, PacketOutEntityMetadata().also {
for (player in players) {
if (hasDistanceChanged || hasYawChanged) {
packetService.sendPacketOutEntityVelocity(player, PacketOutEntityVelocity().also {
it.entityId = this.entityId
it.armorStandHeadRotation = convertPitchToEulerAngle(position.pitch)
it.target = motion.toVector()
})
} else {
// Needed for some living entities other than armor stands.
packetService.sendPacketOutEntityHeadRotation(player, PacketOutEntityHeadRotation().also {

packetService.sendPacketOutEntityTeleport(player, PacketOutEntityTeleport().also {
it.entityId = this.entityId
it.yaw = position.yaw
it.target = position.copy().addRelativeUp(petMeta.physics.groundOffset).toLocation()
})
}

if (parsedEntityType != null && parsedEntityType == EntityType.ARMOR_STAND) {
if (hasPitchChanged) {
// It causes lag when sent to other entities.
packetService.sendPacketOutEntityMetadata(player, PacketOutEntityMetadata().also {
it.entityId = this.entityId
it.armorStandHeadRotation = convertPitchToEulerAngle(position.pitch)
})
}
} else {
if (hasYawChanged && !pet.isRiding()) {
// Needed for some living entities other than armor stands.
packetService.sendPacketOutEntityHeadRotation(player, PacketOutEntityHeadRotation().also {
it.entityId = this.entityId
it.yaw = position.yaw
})
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin-legacy.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: PetBlocks
version: 9.7.0
version: 9.7.1
author: Shynixn
main: com.github.shynixn.petblocks.PetBlocksPlugin
softdepend: [ PlaceholderAPI, HeadDatabase]
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: PetBlocks
version: 9.7.0
version: 9.7.1
author: Shynixn
main: com.github.shynixn.petblocks.PetBlocksPlugin
softdepend: [ PlaceholderAPI, HeadDatabase]
Expand Down

0 comments on commit 14c3e27

Please sign in to comment.