Skip to content

Commit 11d7fed

Browse files
committed
Disable receiving transcriptions from client-muted players
1 parent 386218c commit 11d7fed

File tree

6 files changed

+71
-2
lines changed

6 files changed

+71
-2
lines changed

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ val architecturyVersion = when (mcData.version.rawVersion) {
7373
dependencies {
7474
implementation("de.maxhenkel.voicechat:voicechat-api:${project.property("voicechat_api_version")}")
7575
compileOnly("su.plo.voice.api:server:${project.property("plasmo_api_version")}")
76+
compileOnly("su.plo.voice.api:client:${project.property("plasmo_api_version")}")
7677

7778
modApi("dev.architectury:architectury-${mcData.loader.friendlyString}:$architecturyVersion")
7879

@@ -98,6 +99,7 @@ dependencies {
9899
else if (!mcData.isNeoForge) {
99100
modRuntimeOnly("maven.modrinth:plasmo-voice:${mcData.loader.friendlyString}-${if (mcData.version != MinecraftVersion.VERSION_1_21_1) mcData.version else "1.21"}-${project.property("plasmo_version")}")
100101
runtimeOnly("su.plo.voice.api:server:${project.property("plasmo_api_version")}")
102+
runtimeOnly("su.plo.voice.api:client:${project.property("plasmo_api_version")}")
101103
}
102104

103105
val clothConfigVersion = when(mcData.version.rawVersion) {

src/main/kotlin/xyz/bluspring/unitytranslate/client/gui/TranscriptBox.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import net.minecraft.world.entity.player.Player
1212
import xyz.bluspring.unitytranslate.Language
1313
import xyz.bluspring.unitytranslate.UnityTranslate
1414
import xyz.bluspring.unitytranslate.client.UnityTranslateClient
15+
import xyz.bluspring.unitytranslate.compat.voicechat.UTVoiceChatCompat
1516
import xyz.bluspring.unitytranslate.events.TranscriptEvents
1617
import xyz.bluspring.unitytranslate.transcript.Transcript
1718
import java.util.*
@@ -147,6 +148,9 @@ data class TranscriptBox(
147148
}
148149

149150
fun updateTranscript(source: Player, text: String, language: Language, index: Int, updateTime: Long, incomplete: Boolean) {
151+
if (!UTVoiceChatCompat.isPlayerAudible(source))
152+
return
153+
150154
if (this.transcripts.any { it.player.uuid == source.uuid && it.index == index }) {
151155
val transcript = this.transcripts.first { it.player.uuid == source.uuid && it.index == index }
152156

src/main/kotlin/xyz/bluspring/unitytranslate/compat/voicechat/PlasmoVoiceChatCompat.kt

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package xyz.bluspring.unitytranslate.compat.voicechat
22

33
import com.google.inject.Inject
4+
import net.minecraft.client.Minecraft
45
import net.minecraft.server.level.ServerPlayer
6+
import net.minecraft.world.entity.player.Player
57
import su.plo.voice.api.addon.AddonInitializer
68
import su.plo.voice.api.addon.AddonLoaderScope
79
import su.plo.voice.api.addon.annotation.Addon
10+
import su.plo.voice.api.client.PlasmoVoiceClient
11+
import su.plo.voice.api.client.event.connection.VoicePlayerUpdateEvent
12+
import su.plo.voice.api.event.EventSubscribe
813
import su.plo.voice.api.server.PlasmoVoiceServer
914
import su.plo.voice.api.server.audio.capture.ProximityServerActivationHelper
15+
import su.plo.voice.api.server.event.mute.PlayerVoiceMutedEvent
16+
import xyz.bluspring.unitytranslate.UnityTranslate
17+
import xyz.bluspring.unitytranslate.client.UnityTranslateClient
1018

1119
@Addon(
1220
id = "pv-unitytranslate-compat",
@@ -19,17 +27,38 @@ class PlasmoVoiceChatCompat : AddonInitializer {
1927
@Inject
2028
lateinit var voiceServer: PlasmoVoiceServer
2129

30+
@Inject
31+
lateinit var voiceClient: PlasmoVoiceClient
32+
2233
private var proximityHelper: ProximityServerActivationHelper? = null
2334

2435
override fun onAddonInitialize() {
2536
instance = this
2637
}
2738

39+
@EventSubscribe
40+
fun onVoiceUpdateEvent(event: VoicePlayerUpdateEvent) {
41+
if (event.player.playerId != Minecraft.getInstance().player?.uuid)
42+
return
43+
44+
if (UnityTranslate.config.client.muteTranscriptWhenVoiceChatMuted) {
45+
if (event.player.isVoiceDisabled) {
46+
UnityTranslateClient.shouldTranscribe = false
47+
} else if (!event.player.isVoiceDisabled && !(event.player.isMuted || event.player.isMicrophoneMuted)) {
48+
UnityTranslateClient.shouldTranscribe = true
49+
} else if (!event.player.isVoiceDisabled && (event.player.isMuted || event.player.isMicrophoneMuted)) {
50+
UnityTranslateClient.shouldTranscribe = false
51+
}
52+
}
53+
}
54+
2855
companion object {
2956
lateinit var instance: PlasmoVoiceChatCompat
3057

3158
fun init() {
32-
PlasmoVoiceServer.getAddonsLoader().load(PlasmoVoiceChatCompat())
59+
val compat = PlasmoVoiceChatCompat()
60+
PlasmoVoiceServer.getAddonsLoader().load(compat)
61+
PlasmoVoiceClient.getAddonsLoader().load(compat)
3362
}
3463

3564
fun getNearbyPlayers(source: ServerPlayer): List<ServerPlayer> {
@@ -47,6 +76,13 @@ class PlasmoVoiceChatCompat : AddonInitializer {
4776
}
4877
}
4978

79+
fun isPlayerAudible(source: Player): Boolean {
80+
val connection = instance.voiceClient.serverConnection.orElse(null) ?: return false
81+
val vcPlayer = connection.getPlayerById(source.uuid).orElse(null) ?: return false
82+
83+
return !vcPlayer.isMuted && !vcPlayer.isMicrophoneMuted && !vcPlayer.isVoiceDisabled
84+
}
85+
5086
fun playerSharesGroup(player: ServerPlayer, other: ServerPlayer): Boolean {
5187
// TODO: Support pv-addon-groups
5288
return false

src/main/kotlin/xyz/bluspring/unitytranslate/compat/voicechat/SimpleVoiceChatCompat.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package xyz.bluspring.unitytranslate.compat.voicechat
22

33
import de.maxhenkel.voicechat.api.ForgeVoicechatPlugin
44
import de.maxhenkel.voicechat.api.Group
5+
import de.maxhenkel.voicechat.api.VoicechatClientApi
56
import de.maxhenkel.voicechat.api.VoicechatPlugin
67
import de.maxhenkel.voicechat.api.VoicechatServerApi
8+
import de.maxhenkel.voicechat.api.events.ClientVoicechatInitializationEvent
79
import de.maxhenkel.voicechat.api.events.EventRegistration
810
import de.maxhenkel.voicechat.api.events.MicrophoneMuteEvent
911
import de.maxhenkel.voicechat.api.events.VoicechatDisableEvent
1012
import de.maxhenkel.voicechat.api.events.VoicechatServerStartedEvent
1113
import net.minecraft.server.level.ServerPlayer
14+
import net.minecraft.world.entity.player.Player
1215
import xyz.bluspring.unitytranslate.UnityTranslate
1316
import xyz.bluspring.unitytranslate.client.UnityTranslateClient
1417

@@ -42,10 +45,15 @@ class SimpleVoiceChatCompat : VoicechatPlugin {
4245
registration.registerEvent(VoicechatServerStartedEvent::class.java) {
4346
voiceChatServer = it.voicechat
4447
}
48+
49+
registration.registerEvent(ClientVoicechatInitializationEvent::class.java) {
50+
voiceChatClient = it.voicechat
51+
}
4552
}
4653

4754
companion object {
4855
lateinit var voiceChatServer: VoicechatServerApi
56+
lateinit var voiceChatClient: VoicechatClientApi
4957

5058
fun getNearbyPlayers(source: ServerPlayer): List<ServerPlayer> {
5159
if (isPlayerDeafened(source))
@@ -72,6 +80,11 @@ class SimpleVoiceChatCompat : VoicechatPlugin {
7280
return firstGroup.id == secondGroup.id
7381
}
7482

83+
fun isPlayerAudible(player: Player): Boolean {
84+
// FIXME: SVC doesn't provide an easy way of detecting this...
85+
return true
86+
}
87+
7588
fun isPlayerDeafened(player: ServerPlayer): Boolean {
7689
return voiceChatServer.getConnectionOf(player.uuid)?.isDisabled == true
7790
}

src/main/kotlin/xyz/bluspring/unitytranslate/compat/voicechat/UTVoiceChatCompat.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package xyz.bluspring.unitytranslate.compat.voicechat
22

3+
import net.minecraft.client.Minecraft
34
import net.minecraft.server.level.ServerPlayer
5+
import net.minecraft.world.entity.player.Player
46
import net.minecraft.world.level.GameType
57
import xyz.bluspring.unitytranslate.UnityTranslate
68

@@ -38,6 +40,17 @@ object UTVoiceChatCompat {
3840
false
3941
}
4042

43+
fun isPlayerAudible(player: Player): Boolean {
44+
if (player == Minecraft.getInstance().player)
45+
return true
46+
47+
return if (usesSimpleVoiceChat)
48+
SimpleVoiceChatCompat.isPlayerAudible(player)
49+
else if (usesPlasmoVoice)
50+
PlasmoVoiceChatCompat.isPlayerAudible(player)
51+
else false
52+
}
53+
4154
fun areBothSpectator(player: ServerPlayer, other: ServerPlayer): Boolean {
4255
if (player.gameMode.gameModeForPlayer == GameType.SPECTATOR && other.gameMode.gameModeForPlayer == GameType.SPECTATOR)
4356
return true

src/main/kotlin/xyz/bluspring/unitytranslate/network/UTClientNetworking.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import net.minecraft.client.Minecraft
1515
import xyz.bluspring.unitytranslate.Language
1616
import xyz.bluspring.unitytranslate.UnityTranslate
1717
import xyz.bluspring.unitytranslate.client.UnityTranslateClient
18+
import xyz.bluspring.unitytranslate.compat.voicechat.UTVoiceChatCompat
1819
import xyz.bluspring.unitytranslate.events.TranscriptEvents
1920
import xyz.bluspring.unitytranslate.transcript.Transcript
2021
import java.util.*
@@ -96,7 +97,7 @@ object UTClientNetworking {
9697
val box = boxes.firstOrNull { it.language == language }
9798
box?.updateTranscript(source, text, sourceLanguage, index, updateTime, false)
9899

99-
if (box == null) {
100+
if (box == null && UTVoiceChatCompat.isPlayerAudible(ctx.player)) {
100101
TranscriptEvents.UPDATE.invoker().onTranscriptUpdate(Transcript(index, source, text, language, updateTime, false), language)
101102
}
102103
}

0 commit comments

Comments
 (0)