From 682ac8f9d317ba3131463ed5212c244c2175d7ba Mon Sep 17 00:00:00 2001 From: dragon-jpg Date: Fri, 7 Feb 2025 15:20:38 +0800 Subject: [PATCH] event notifier --- .../modules/impl/misc/EventNotifier.kt | 62 +++++++++++++++++++ .../sakura/managers/impl/ModuleManager.kt | 1 + .../resources/assets/sakura/lang/en_us.lang | 4 ++ .../resources/assets/sakura/lang/zh_cn.lang | 4 ++ 4 files changed, 71 insertions(+) create mode 100644 src/main/kotlin/dev/exceptionteam/sakura/features/modules/impl/misc/EventNotifier.kt diff --git a/src/main/kotlin/dev/exceptionteam/sakura/features/modules/impl/misc/EventNotifier.kt b/src/main/kotlin/dev/exceptionteam/sakura/features/modules/impl/misc/EventNotifier.kt new file mode 100644 index 0000000..ba092db --- /dev/null +++ b/src/main/kotlin/dev/exceptionteam/sakura/features/modules/impl/misc/EventNotifier.kt @@ -0,0 +1,62 @@ +package dev.exceptionteam.sakura.features.modules.impl.misc + +import dev.exceptionteam.sakura.events.NonNullContext +import dev.exceptionteam.sakura.events.impl.PacketEvents +import dev.exceptionteam.sakura.events.nonNullListener +import dev.exceptionteam.sakura.features.modules.Category +import dev.exceptionteam.sakura.features.modules.Module +import dev.exceptionteam.sakura.features.modules.impl.client.Language +import dev.exceptionteam.sakura.utils.ingame.ChatUtils +import net.minecraft.client.multiplayer.PlayerInfo +import net.minecraft.network.protocol.game.ClientboundPlayerInfoRemovePacket +import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket + +object EventNotifier: Module( + name = "event-notifier", + category = Category.MISC +) { + private val playerJoin by setting("player-join", true) + private val playerLeave by setting("player-leave", true) + + init { + nonNullListener { it -> + if (it.packet is ClientboundPlayerInfoUpdatePacket && playerJoin) { + val packet: ClientboundPlayerInfoUpdatePacket = it.packet + if (!packet.actions().contains(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER)) return@nonNullListener + sendJoinMessage(packet) + } + if (it.packet is ClientboundPlayerInfoRemovePacket && playerLeave) { + val packet: ClientboundPlayerInfoRemovePacket = it.packet + sendLeaveMessage(packet) + } + } + } + + private fun sendJoinMessage(packet: ClientboundPlayerInfoUpdatePacket) { + for (entry in packet.entries()) { + val player = entry.profile ?: continue + when (Language.language) { + Language.Languages.EN_US -> { + ChatUtils.sendMessage("Player ${player.name} joined!") + } + Language.Languages.ZH_CN -> { + ChatUtils.sendMessage("玩家 ${player.name} 加入了伺服器!") + } + } + } + } + + private fun NonNullContext.sendLeaveMessage(packet: ClientboundPlayerInfoRemovePacket) { + for (uuid in packet.profileIds) { + val leave: PlayerInfo = connection.getPlayerInfo(uuid) ?: continue + when (Language.language) { + Language.Languages.EN_US -> { + ChatUtils.sendMessage("Player ${leave.profile.name} leaved!") + } + Language.Languages.ZH_CN -> { + ChatUtils.sendMessage("Player ${leave.profile.name} 离开了伺服器!") + } + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/dev/exceptionteam/sakura/managers/impl/ModuleManager.kt b/src/main/kotlin/dev/exceptionteam/sakura/managers/impl/ModuleManager.kt index 2e0ec83..10b32c2 100644 --- a/src/main/kotlin/dev/exceptionteam/sakura/managers/impl/ModuleManager.kt +++ b/src/main/kotlin/dev/exceptionteam/sakura/managers/impl/ModuleManager.kt @@ -46,6 +46,7 @@ object ModuleManager { PacketEat, Disabler, FakePlayer, + EventNotifier, // Render NameTags, diff --git a/src/main/resources/assets/sakura/lang/en_us.lang b/src/main/resources/assets/sakura/lang/en_us.lang index d070854..21043e5 100644 --- a/src/main/resources/assets/sakura/lang/en_us.lang +++ b/src/main/resources/assets/sakura/lang/en_us.lang @@ -226,3 +226,7 @@ modules.array-list=Array List modules.array-list.text-color=Text Color modules.array-list.shadow=Shadow modules.array-list.scale=Scale + +modules.event-notifier=Event Notifier +modules.event-notifier.player-join=Player Join +modules.event-notifier.player-leave=Player Leave \ No newline at end of file diff --git a/src/main/resources/assets/sakura/lang/zh_cn.lang b/src/main/resources/assets/sakura/lang/zh_cn.lang index ef5c3b3..9746ced 100644 --- a/src/main/resources/assets/sakura/lang/zh_cn.lang +++ b/src/main/resources/assets/sakura/lang/zh_cn.lang @@ -234,3 +234,7 @@ modules.array-list=功能列表 modules.array-list.text-color=文字颜色 modules.array-list.shadow=阴影 modules.array-list.scale=缩放 + +modules.event-notifier=事件提醒器 +modules.event-notifier.player-join=玩家加入伺服器 +modules.event-notifier.player-leave=玩家离开伺服器 \ No newline at end of file