Skip to content

Commit cab0736

Browse files
committed
refactor: fix errors in Utils
[no ci]
1 parent fffc093 commit cab0736

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

mod/src/main/kotlin/gg/skytils/skytilsmod/features/impl/handlers/SpamHider.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import gg.skytils.event.EventPriority
3030
import gg.skytils.event.EventSubscriber
3131
import gg.skytils.event.impl.TickEvent
3232
import gg.skytils.event.impl.play.ActionBarReceivedEvent
33+
import gg.skytils.event.impl.play.ChatMessageReceivedEvent
3334
import gg.skytils.event.register
3435
import gg.skytils.skytilsmod.Skytils
3536
import gg.skytils.skytilsmod.Skytils.failPrefix
@@ -164,6 +165,7 @@ object SpamHider : EventSubscriber, PersistentSave(File(Skytils.modDir, "spamhid
164165
register(::onActionBarDisplay)
165166
register(::onChatPacket, EventPriority.Lowest)
166167
register(::onTick)
168+
register(::handleChat)
167169
}
168170

169171
fun onActionBarDisplay(event: ActionBarReceivedEvent) {
@@ -780,10 +782,19 @@ object SpamHider : EventSubscriber, PersistentSave(File(Skytils.modDir, "spamhid
780782

781783
private fun cancelChatPacket(event: PacketReceiveEvent<*>, addToSpam: Boolean) {
782784
if (event.packet !is ChatMessageS2CPacket) return
783-
Utils.cancelChatPacket(event)
785+
event.packet.unsignedContent?.let { messagesToHide.add(it) }
784786
if (addToSpam) newMessage(event.packet.unsignedContent?.formattedText)
785787
}
786788

789+
private fun handleChat(event: ChatMessageReceivedEvent) {
790+
if (event.message in messagesToHide) {
791+
messagesToHide.remove(event.message)
792+
event.cancelled = true
793+
}
794+
}
795+
796+
private val messagesToHide = mutableSetOf<Text>()
797+
787798
private fun newMessage(message: String?) {
788799
SpamHudElement.queueSpam(message ?: return)
789800
}

mod/src/main/kotlin/gg/skytils/skytilsmod/utils/Utils.kt

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
*/
1818
package gg.skytils.skytilsmod.utils
1919

20-
import dev.falsehonesty.asmhelper.AsmHelper
21-
import dev.falsehonesty.asmhelper.dsl.instructions.Descriptor
2220
import gg.essential.lib.caffeine.cache.Cache
2321
import gg.essential.universal.ChatColor
22+
import gg.essential.universal.UKeyboard
2423
import gg.essential.vigilance.Vigilant
2524
import gg.essential.vigilance.gui.settings.CheckboxComponent
2625
import gg.skytils.event.postSync
@@ -29,7 +28,6 @@ import gg.skytils.skytilsmod.Skytils
2928
import gg.skytils.skytilsmod.Skytils.mc
3029
import gg.skytils.skytilsmod._event.MainThreadPacketReceiveEvent
3130
import gg.skytils.skytilsmod._event.PacketReceiveEvent
32-
import gg.skytils.skytilsmod.asm.SkytilsTransformer
3331
import gg.skytils.skytilsmod.mixins.extensions.ExtensionEntity
3432
import gg.skytils.skytilsmod.mixins.transformers.accessors.AccessorWorldInfo
3533
import gg.skytils.skytilsmod.utils.NumberUtil.roundToPrecision
@@ -41,6 +39,7 @@ import kotlinx.coroutines.Job
4139
import kotlinx.coroutines.launch
4240
import net.minecraft.client.network.ClientPlayNetworkHandler
4341
import net.minecraft.client.option.GameOptions
42+
import net.minecraft.client.resource.language.I18n
4443
import net.minecraft.entity.Entity
4544
import net.minecraft.entity.LivingEntity
4645
import net.minecraft.entity.attribute.EntityAttributes
@@ -49,11 +48,11 @@ import net.minecraft.nbt.NbtList
4948
import net.minecraft.network.packet.s2c.play.GameMessageS2CPacket
5049
import net.minecraft.network.packet.s2c.play.ParticleS2CPacket
5150
import net.minecraft.particle.ParticleEffect
51+
import net.minecraft.sound.SoundEvent
52+
import net.minecraft.sound.SoundEvents
5253
import net.minecraft.text.Text
5354
import net.minecraft.util.*
5455
import net.minecraft.world.World
55-
import net.minecraftforge.client.event.ClientChatReceivedEvent
56-
import net.minecraftforge.common.MinecraftForge
5756
import org.objectweb.asm.tree.MethodInsnNode
5857
import java.awt.Color
5958
import java.io.File
@@ -109,9 +108,10 @@ object Utils {
109108
* https://github.com/BiscuitDevelopment/SkyblockAddons/blob/master/LICENSE
110109
* @author BiscuitDevelopment
111110
*/
112-
fun playLoudSound(sound: String?, pitch: Double) {
111+
fun playLoudSound(sound: String, pitch: Double) {
113112
shouldBypassVolume = true
114-
mc.player.playSound(sound, 1f, pitch.toFloat())
113+
val sound = SoundEvent.of(Identifier.of(sound))
114+
mc.player?.playSound(sound, 1f, pitch.toFloat())
115115
shouldBypassVolume = false
116116
}
117117

@@ -166,20 +166,6 @@ object Utils {
166166
} else run()
167167
}
168168

169-
/**
170-
* Cancels a chat packet and posts the chat event to the event bus if other mods need it
171-
* @param event packet to cancel
172-
*/
173-
fun cancelChatPacket(event: PacketReceiveEvent<*>) {
174-
if (event.packet !is GameMessageS2CPacket) return
175-
event.cancelled = true
176-
val packet = event.packet
177-
checkThreadAndQueue {
178-
postSync(MainThreadPacketReceiveEvent(packet))
179-
MinecraftForge.EVENT_BUS.post(ClientChatReceivedEvent(packet.type, packet.content))
180-
}
181-
}
182-
183169
fun timeFormat(seconds: Double): String {
184170
return if (seconds >= 60) {
185171
"${floor(seconds / 60).toInt()}m ${(seconds % 60).roundToPrecision(3)}s"
@@ -223,7 +209,13 @@ object Utils {
223209
}
224210

225211
fun getKeyDisplayStringSafe(keyCode: Int): String =
226-
runCatching { GameOptions.method_0_2345(keyCode) }.getOrNull() ?: "Key $keyCode"
212+
if (keyCode < 0) {
213+
"Button ${keyCode + 101}"
214+
} else if (keyCode < 256) {
215+
UKeyboard.getKeyName(keyCode)!!
216+
} else {
217+
"%c".format((keyCode - 256).toChar()).uppercase()
218+
}
227219
}
228220

229221
inline val Box.minVec: Vec3d

0 commit comments

Comments
 (0)