/
ClientMumbleLinkMod.kt
208 lines (175 loc) · 7.61 KB
/
ClientMumbleLinkMod.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.skaggsm.mumblelinkmod.client
import com.skaggsm.jmumblelink.MumbleLink
import com.skaggsm.jmumblelink.MumbleLinkImpl
import com.skaggsm.mumblelinkmod.client.ClientConfig.AutoLaunchOption
import com.skaggsm.mumblelinkmod.main.MainConfig
import com.skaggsm.mumblelinkmod.main.MainMumbleLinkMod
import com.skaggsm.mumblelinkmod.main.MainMumbleLinkMod.LOG
import com.skaggsm.mumblelinkmod.main.MainMumbleLinkMod.SERIALIZER
import com.skaggsm.mumblelinkmod.main.MainMumbleLinkMod.createSettings
import com.skaggsm.mumblelinkmod.main.SendMumbleURL
import io.github.fablabsmc.fablabs.api.fiber.v1.serialization.FiberSerialization
import io.github.fablabsmc.fablabs.api.fiber.v1.tree.ConfigBranch
import io.github.fablabsmc.fablabs.api.fiber.v1.tree.ConfigTree
import io.github.fablabsmc.fablabs.impl.fiber.tree.ConfigBranchImpl
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import net.fabricmc.api.ClientModInitializer
import net.fabricmc.api.EnvType.CLIENT
import net.fabricmc.api.Environment
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking
import net.fabricmc.fabric.api.networking.v1.PacketSender
import net.minecraft.client.MinecraftClient
import net.minecraft.client.network.ClientPlayNetworkHandler
import net.minecraft.network.PacketByteBuf
import org.lwjgl.system.Platform
import java.awt.Desktop
import java.awt.GraphicsEnvironment
import java.net.URI
import java.net.URISyntaxException
import java.nio.file.Files
import java.nio.file.StandardOpenOption.CREATE
import java.nio.file.StandardOpenOption.READ
import java.nio.file.StandardOpenOption.WRITE
import kotlin.io.path.div
/**
* Created by Mitchell Skaggs on 5/12/2019.
*/
@Environment(CLIENT)
object ClientMumbleLinkMod : ClientModInitializer {
// Config files
private val configFile = MainMumbleLinkMod.configFolder / "fabric-mumblelink-mod-client.json"
// Configs
lateinit var config: ClientConfig
lateinit var configTree: ConfigBranch
lateinit var unionConfigTree: ConfigBranch
private var mumble: MumbleLink? = null
private fun channelHandler(
minecraftClient: MinecraftClient,
clientPlayNetworkHandler: ClientPlayNetworkHandler,
packetByteBuf: PacketByteBuf,
packetSender: PacketSender
) {
if (config.clientAutoLaunchOption == AutoLaunchOption.IGNORE) return
val voipClient = packetByteBuf.readEnumConstant(MainConfig.VoipClient::class.java)
val userinfo = packetByteBuf.readString().ifEmpty { null }
val host = packetByteBuf.readString().ifEmpty { null }
val port = packetByteBuf.readInt()
val path = packetByteBuf.readString().ifEmpty { null }
val query = packetByteBuf.readString().ifEmpty { null }
val fragment = packetByteBuf.readString().ifEmpty { null }
try {
val uri = URI(voipClient.scheme, userinfo, host, port, path, query, fragment)
ensureNotHeadless()
Desktop.getDesktop().browse(uri)
} catch (e: URISyntaxException) {
LOG.warn("Ignoring invalid VoIP client URI \"${e.input}\"")
} catch (e: UnsupportedOperationException) {
LOG.warn("Unable to use the \"BROWSE\" intent to open your VoIP client automatically! Check that you aren't using a headless or server JVM.")
}
}
/**
* Runs after [MainMumbleLinkMod.onInitialize].
*/
override fun onInitializeClient() {
setupConfig()
setupEvents()
}
private fun setupConfig() {
config = ClientConfig()
configTree = ConfigTree.builder().applyFromPojo(config, createSettings()).withName("client").build()
unionConfigTree = ConfigBranchImpl("union", null)
unionConfigTree.items.add(configTree)
unionConfigTree.items.add(MainMumbleLinkMod.configTree)
if (Files.notExists(configFile)) {
serialize()
}
// Verify save worked
deserialize()
}
fun serialize() {
FiberSerialization.serialize(
configTree,
Files.newOutputStream(configFile, WRITE, CREATE),
SERIALIZER
)
}
private fun deserialize() {
FiberSerialization.deserialize(
configTree,
Files.newInputStream(configFile, READ),
SERIALIZER
)
}
private fun setupEvents() {
ClientPlayNetworking.registerGlobalReceiver(SendMumbleURL.ID, ClientMumbleLinkMod::channelHandler)
ClientTickEvents.START_CLIENT_TICK.register(
ClientTickEvents.StartTick {
val world = it.world
val player = it.player
if (world != null && player != null) {
val mumble = ensureLinked()
val camPos = player.getCameraPosVec(1F).toLHArray
val camDir = player.rotationVecClient.toLHArray
val camTop = floatArrayOf(0f, 1f, 0f)
// Make people in other dimensions far away so that they're muted.
val yAxisAdjuster = (world.registryKey.value.stableHash % 2048) * config.clientDimensionYAxisAdjust
camPos[1] += yAxisAdjuster
mumble.uiVersion = 2
mumble.uiTick++
mumble.avatarPosition = camPos
mumble.avatarFront = camDir
mumble.avatarTop = camTop
mumble.name = "Minecraft"
mumble.cameraPosition = camPos
mumble.cameraFront = camDir
mumble.cameraTop = camTop
mumble.identity = Json.encodeToString(Identity(world, player))
mumble.context = "Context"
mumble.description = "A Minecraft mod that provides position data to VoIP clients."
} else {
ensureClosed()
}
}
)
}
private fun ensureLinked(): MumbleLink {
var localMumble = mumble
if (localMumble != null) {
return localMumble
}
LOG.info("Linking to VoIP client...")
localMumble = MumbleLinkImpl()
mumble = localMumble
LOG.info("Linked")
return localMumble
}
private fun ensureClosed() {
if (mumble != null) {
LOG.info("Unlinking from VoIP client...")
mumble?.close()
mumble = null
LOG.info("Unlinked")
}
}
private fun ensureNotHeadless() {
if (GraphicsEnvironment.isHeadless()) {
LOG.warn("Unable to unset headless earlier (are you using macOS?), doing it with nasty reflection now!")
val headlessField = GraphicsEnvironment::class.java.getDeclaredField("headless")
headlessField.isAccessible = true
headlessField[null] = false
}
}
init {
// Many mods assume java.awt.headless=true on macOS because they accidentally use AWT classes that triggers JNI stuff on classload if not headless.
// That JNI stuff fails on Mac because of course it does, so we skip settings java.awt.headless=false now and set it on-demand later (hopefully after the mods have already triggered the AWT JNI code).
if (Platform.get() == Platform.MACOSX) {
LOG.warn("macOS needs java.awt.headless=true right now, so we'll set it later with a reflection hack!")
} else {
// If not on macOS isn't loaded, we can just set it here and skip the hassle later.
// Required to open URIs
System.setProperty("java.awt.headless", "false")
}
}
}