hibiscus/src/main/kotlin/codes/som/hibiscus/util/netmoving/NetworkMovingEvent.kt

65 lines
2.4 KiB
Kotlin

package codes.som.hibiscus.util.netmoving
import codes.som.hibiscus.Hibiscus
import codes.som.hibiscus.api.event.TypedListener
import codes.som.hibiscus.events.NetworkMovingEvent
import codes.som.hibiscus.events.SendPacketEvent
import codes.som.hibiscus.mc
import codes.som.hibiscus.mixins.MixinExtClientPlayerEntity
import codes.som.hibiscus.util.ext.requireExtension
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket
class NetworkMovingDispatcher : TypedListener<SendPacketEvent>(SendPacketEvent::class.java) {
override fun on(event: SendPacketEvent) {
val packet = event.packet
if (packet is PlayerMoveC2SPacket) {
val player = mc.player!!
requireExtension<MixinExtClientPlayerEntity>(player)
val movingEvent = NetworkMovingEvent(
packet.getX(player.x), packet.getY(player.y), packet.getZ(player.z),
packet.getYaw(player.yaw), packet.getPitch(player.pitch), packet.isOnGround
)
Hibiscus.bus.fire(movingEvent)
if (movingEvent.cancelled)
event.cancel()
val moving =
movingEvent.x != player.lastX || movingEvent.y != player.lastBaseY || movingEvent.z != player.lastZ
val rotating = movingEvent.yaw != player.lastYaw || movingEvent.pitch != player.lastPitch
player.lastX = movingEvent.x
player.lastBaseY = movingEvent.y
player.lastZ = movingEvent.z
player.lastYaw = movingEvent.yaw
player.lastPitch = movingEvent.pitch
event.packet = when {
moving && rotating -> PlayerMoveC2SPacket.Full(
movingEvent.x,
movingEvent.y,
movingEvent.z,
movingEvent.yaw,
movingEvent.pitch,
movingEvent.onGround
)
moving -> PlayerMoveC2SPacket.PositionAndOnGround(
movingEvent.x,
movingEvent.y,
movingEvent.z,
movingEvent.onGround
)
rotating -> PlayerMoveC2SPacket.LookAndOnGround(
movingEvent.yaw,
movingEvent.pitch,
movingEvent.onGround
)
else -> PlayerMoveC2SPacket.OnGroundOnly(movingEvent.onGround)
}
}
}
}