Skip to content

Commit

Permalink
fix: prevent sneak pose while flying
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishon committed Jun 12, 2024
1 parent 06d7c84 commit bce9c43
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/main/java/dev/rishon/sync/handler/FileHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class FileHandler(private var instance: Sync) : IHandler {
var transferPackets: Boolean = false
var isUnderProxy: Boolean = false
var debug: Boolean = false
var playerChatFormat: String? = null

override fun init() {
handler = this;
Expand All @@ -46,6 +47,7 @@ class FileHandler(private var instance: Sync) : IHandler {
this.transferPackets = this.config?.getBoolean("allow-transfer-packets") == true
this.isUnderProxy = this.config?.getBoolean("is-under-proxy") == true
this.debug = this.config?.getBoolean("debug") == true
this.playerChatFormat = this.config?.getString("player-chat-format")
}

private fun handleTransferPackets() {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/dev/rishon/sync/jedis/packet/SneakPacket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import dev.rishon.sync.utils.LoggerUtil
import org.bukkit.Bukkit
import java.util.*

class SneakPacket(private val playerUUID: UUID, private val isSneaking: Boolean) : IPacket {
class SneakPacket(private val playerUUID: UUID, private val isSneaking: Boolean, private val isFlying: Boolean) :
IPacket {

override fun onReceive() {
LoggerUtil.debug("Received sneak packet for $playerUUID")
Expand All @@ -18,7 +19,7 @@ class SneakPacket(private val playerUUID: UUID, private val isSneaking: Boolean)

onlinePlayers.forEach { player ->
if (player.uniqueId == playerUUID) return@forEach
ClientSneakPlayerPacket.sendPacket(player, fakePlayer.second, isSneaking)
ClientSneakPlayerPacket.sendPacket(player, fakePlayer.second, isSneaking, isFlying)
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/dev/rishon/sync/listener/AnimationEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ class AnimationEvent : Listener {
val player = event.player
val uuid = player.uniqueId
val isSneaking = event.isSneaking
val isFlying = player.isFlying
val isSwimming = player.isSwimming
if (isSwimming) return
JedisManager.instance.sendPacket(SneakPacket(uuid, isSneaking))
JedisManager.instance.sendPacket(SneakPacket(uuid, isSneaking, isFlying))
}

@EventHandler(priority = EventPriority.MONITOR)
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/dev/rishon/sync/listener/AsyncChat.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package dev.rishon.sync.listener

import dev.rishon.sync.handler.FileHandler
import dev.rishon.sync.jedis.JedisManager
import dev.rishon.sync.jedis.packet.ChatPacket
import dev.rishon.sync.utils.ColorUtil
import io.papermc.paper.event.player.AsyncChatEvent
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.serializer.json.JSONComponentSerializer
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
Expand All @@ -13,16 +16,19 @@ class AsyncChat : Listener {

@EventHandler(priority = EventPriority.MONITOR)
private fun onAsyncChat(event: AsyncChatEvent) {
val player = event.player;
val renderedComponent: Component =
Component.text(player.name).append(Component.text(": ")).append(event.message())
val player = event.player
val fileHandler = FileHandler.handler

val format = fileHandler.playerChatFormat!!.replace("{player}", player.name).replace(
"{message}", LegacyComponentSerializer.legacySection().serialize(event.message())
)

val renderedComponent: Component = ColorUtil.translate(format)
event.renderer { _, _, _, _ -> return@renderer renderedComponent }

JedisManager.instance.sendPacket(
ChatPacket(
player.name,
JSONComponentSerializer.json().serialize(renderedComponent)
player.name, JSONComponentSerializer.json().serialize(renderedComponent)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ import org.bukkit.entity.Player
object ClientSneakPlayerPacket : IPacket {

@JvmStatic
fun sendPacket(player: Player, serverPlayer: ServerPlayer, isSneaking: Boolean) {
fun sendPacket(player: Player, serverPlayer: ServerPlayer, isSneaking: Boolean, isFlying: Boolean) {
val craftPlayer = player as CraftPlayer
val connection = craftPlayer.handle.connection
val pose: Pose = if (isSneaking) Pose.CROUCHING else Pose.STANDING
val flag: Byte = if (isSneaking) 0x02 else 0
val dataValueList: MutableList<DataValue<*>> = getDataWatcher(serverPlayer).nonDefaultValues ?: return
dataValueList.add(DataValue.create(EntityDataAccessor(6, EntityDataSerializers.POSE), pose))

if (!isFlying) dataValueList.add(
DataValue.create(
EntityDataAccessor(6, EntityDataSerializers.POSE), pose
)
)
dataValueList.add(DataValue.create(EntityDataAccessor(0, EntityDataSerializers.BYTE), flag))
connection.send(ClientboundSetEntityDataPacket(serverPlayer.id, dataValueList))
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ mysql:
username: ""
password: ""

player-chat-format: "<gray>{player}: <white>{message}"

player-data: # What is being saved to database to allow for syncing across instances

0 comments on commit bce9c43

Please sign in to comment.