SkinChanger module

SkinChanger: Minecraft 26.3 cheat module, full source for every implementation (1 client: LiquidBounce (Kotlin)).

1 implementationRENDER

Clients with this module

Full module source: 1 implementation

Every implementation below is the complete module file, verbatim. Expand a client to read its code.

LiquidBounce (Kotlin)RENDERsrc/main/kotlin/net/ccbluex/liquidbounce/features/module/modules/render/ModuleSkinChanger.ktKotlin · 304 lines · 10.9 KB

Hides the player's real skin client-side (useful for videos).

SettingsForceOverrideModel
ModuleSkinChanger.ktKotlin · Minecraft 26.2 · 304 lines · 10.9 KB · complete file, verbatimOriginal ↗
1
/*
2
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
3
 *
4
 * Copyright (c) 2015 - 2026 CCBlueX
5
 *
6
 * LiquidBounce is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * LiquidBounce is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
18
 */
19
@file:OptIn(FlowPreview::class)
20
21
package net.ccbluex.liquidbounce.features.module.modules.render
22
23
import com.mojang.authlib.GameProfile
24
import com.mojang.authlib.yggdrasil.YggdrasilEnvironment
25
import kotlinx.coroutines.Dispatchers
26
import kotlinx.coroutines.FlowPreview
27
import kotlinx.coroutines.delay
28
import kotlinx.coroutines.flow.Flow
29
import kotlinx.coroutines.flow.MutableSharedFlow
30
import kotlinx.coroutines.flow.collect
31
import kotlinx.coroutines.flow.collectLatest
32
import kotlinx.coroutines.flow.combine
33
import kotlinx.coroutines.flow.debounce
34
import kotlinx.coroutines.flow.filter
35
import kotlinx.coroutines.launch
36
import kotlinx.coroutines.withContext
37
import net.ccbluex.liquidbounce.LiquidBounce
38
import net.ccbluex.liquidbounce.api.core.HttpClient
39
import net.ccbluex.liquidbounce.api.core.ioScope
40
import net.ccbluex.liquidbounce.api.core.renderScope
41
import net.ccbluex.liquidbounce.api.thirdparty.lookupUuidByName
42
import net.ccbluex.liquidbounce.api.thirdparty.mojang.model.ChangeSkinRequest
43
import net.ccbluex.liquidbounce.api.thirdparty.mojang.service.MinecraftServicesApi
44
import net.ccbluex.liquidbounce.config.gson.serializer.minecraft.accountType
45
import net.ccbluex.liquidbounce.config.types.group.ModeValueGroup
46
import net.ccbluex.liquidbounce.config.types.list.Tagged
47
import net.ccbluex.liquidbounce.event.SuspendHandlerBehavior
48
import net.ccbluex.liquidbounce.event.events.SessionEvent
49
import net.ccbluex.liquidbounce.event.suspendHandler
50
import net.ccbluex.liquidbounce.features.module.ClientModule
51
import net.ccbluex.liquidbounce.features.module.ModuleCategories
52
import net.ccbluex.liquidbounce.injection.mixins.authlib.MixinYggdrasilMinecraftSessionServiceAccessor
53
import net.ccbluex.liquidbounce.utils.client.chat
54
import net.ccbluex.liquidbounce.utils.client.inGame
55
import net.ccbluex.liquidbounce.utils.kotlin.Minecraft
56
import net.ccbluex.liquidbounce.utils.render.readNativeImage
57
import net.ccbluex.liquidbounce.utils.render.registerTexture
58
import net.minecraft.client.multiplayer.PlayerInfo
59
import net.minecraft.client.player.AbstractClientPlayer
60
import net.minecraft.core.ClientAsset
61
import net.minecraft.core.UUIDUtil
62
import net.minecraft.world.entity.player.PlayerModelType
63
import net.minecraft.world.entity.player.PlayerSkin
64
import okhttp3.MultipartBody
65
import okhttp3.RequestBody.Companion.asRequestBody
66
import okhttp3.RequestBody.Companion.toRequestBody
67
import java.io.IOException
68
import java.util.function.Supplier
69
import kotlin.time.Duration.Companion.seconds
70
71
object ModuleSkinChanger : ClientModule("SkinChanger", ModuleCategories.RENDER) {
72
73
    /**
74
     * Changes the player model by forcefully modifying
75
     * [AbstractClientPlayer.getSkin],
76
     * as PlayerListEntry is unreliable on some servers.
77
     */
78
    private val allowMixinAbstractClientPlayerEntity by boolean("ForceOverride", false)
79
80
    private val uploadSkin = boolean("UploadSkin", false)
81
82
    private val mode = choices("Mode", 0) {
83
        arrayOf(Mode.Online, Mode.File)
84
    }
85
86
    private val DEBOUNCE_DURATION = 3.seconds
87
88
    private val uploadSkinFlow = MutableSharedFlow<Unit>(replay = 0)
89
90
    init {
91
        ioScope.launch {
92
            // debounce skin uploads to prevent rapid calls
93
            uploadSkinFlow.debounce(DEBOUNCE_DURATION).filter { canUploadSkin() }.collectLatest {
94
                logger.info("Uploading skin...")
95
                mode.activeMode.uploadSkin()
96
            }
97
        }
98
99
        ioScope.launch {
100
            combine(uploadSkin.asStateFlow(), mode.asStateFlow()) { skin, mode ->
101
                if (skin) {
102
                    triggerUpload()
103
                }
104
            }.collect()
105
        }
106
    }
107
108
    private suspend fun waitUntilInGame() {
109
        while (!inGame) {
110
            delay(1.seconds)
111
        }
112
    }
113
114
    private inline fun <T> Flow<T>.debounceUntilInGame(crossinline action: suspend (T) -> Unit) {
115
        renderScope.launch {
116
            this@debounceUntilInGame.debounce(DEBOUNCE_DURATION).collectLatest {
117
                waitUntilInGame()
118
                try {
119
                    action(it)
120
                } catch (e: Exception) {
121
                    if (this@ModuleSkinChanger.running) {
122
                        chat("Unable to load custom skin because: ${e.message} (${e.javaClass.simpleName})")
123
                    }
124
                    logger.error("Unable to load custom skin", e)
125
                }
126
            }
127
        }
128
    }
129
130
    private sealed class Mode(name: String) : net.ccbluex.liquidbounce.config.types.group.Mode(name) {
131
        final override val parent: ModeValueGroup<*>
132
            get() = mode
133
134
        abstract val skinTextures: Supplier<PlayerSkin>?
135
136
        abstract suspend fun uploadSkin()
137
138
        object Online : Mode("Online") {
139
            private val username = text("Username", "LiquidBounce")
140
141
            init {
142
                username.asStateFlow().debounceUntilInGame { username ->
143
                    skinTextures = textureSupplier(username)
144
145
                    triggerUpload()
146
                }
147
            }
148
149
            override var skinTextures: Supplier<PlayerSkin>? = null
150
151
            private suspend fun textureSupplier(username: String): Supplier<PlayerSkin> {
152
                val profile = withContext(Dispatchers.IO) {
153
                    val uuid = lookupUuidByName(username) ?: UUIDUtil.createOfflinePlayerUUID(username)
154
                    mc.services.sessionService.fetchProfile(uuid, false)?.profile
155
                        ?: GameProfile(uuid, username)
156
                }
157
158
                return PlayerInfo.createSkinLookup(profile)
159
            }
160
161
            override suspend fun uploadSkin() {
162
                val uuid = lookupUuidByName(username.get()) ?: return
163
164
                val profile = withContext(Dispatchers.IO) {
165
                    mc.services.sessionService.fetchProfile(uuid, false)
166
                } ?: return
167
168
                val texture = mc.services.sessionService.unpackTextures(
169
                    mc.services.sessionService.getPackedTextures(profile.profile)
170
                )
171
                val skinTexture = texture.skin ?: return
172
                val variant = if (skinTexture.getMetadata("model") == "slim") {
173
                    PlayerModelType.SLIM
174
                } else {
175
                    PlayerModelType.WIDE
176
                }
177
178
                request {
179
                    changeSkin(ChangeSkinRequest(variant.variant, skinTexture.url))
180
                }
181
            }
182
        }
183
184
        object File : Mode("File"), ClientAsset.Texture {
185
            private val image = file("Image", supportedExtensions = setOf("png"))
186
187
            private val skinType by enumChoice("Model", ModelChoice.WIDE)
188
189
            private val identifier = LiquidBounce.identifier("skin-changer-from-file")
190
191
            override fun id() = identifier
192
193
            override fun texturePath() = identifier
194
195
            private enum class ModelChoice(
196
                override val tag: String,
197
                val type: PlayerModelType,
198
            ) : Tagged {
199
                SLIM("Slim", PlayerModelType.SLIM),
200
                WIDE("Default", PlayerModelType.WIDE),
201
            }
202
203
            override val skinTextures = Supplier {
204
                PlayerSkin(
205
                    this, // body
206
                    null, // cape
207
                    null, // elytra
208
                    skinType.type,
209
                    false,
210
                )
211
            }
212
213
            init {
214
                image.asStateFlow().filter { it.isFile }.debounceUntilInGame { file ->
215
                    // New texture will replace the old one
216
                    val nativeImage = withContext(Dispatchers.IO) {
217
                        file.readNativeImage()
218
                    }
219
220
                    withContext(Dispatchers.Minecraft) {
221
                        nativeImage.registerTexture(identifier)
222
                    }
223
224
                    triggerUpload()
225
                }
226
            }
227
228
            override suspend fun uploadSkin() {
229
                val file = image.get()
230
                if (!file.isFile) {
231
                    return
232
                }
233
234
                request {
235
                    uploadSkin(
236
                        skinType.type.variant.toRequestBody(HttpClient.MediaTypes.TEXT_PLAIN),
237
                        MultipartBody.Part.createFormData(
238
                            name = "file",
239
                            filename = "skin.png",
240
                            body = file.asRequestBody(HttpClient.MediaTypes.IMAGE_PNG)
241
                        )
242
                    )
243
                }
244
            }
245
        }
246
    }
247
248
    private val PlayerModelType.variant
249
        get() = when (this) {
250
            PlayerModelType.WIDE -> "classic"
251
            PlayerModelType.SLIM -> "slim"
252
        }
253
254
    val skinTextures: Supplier<PlayerSkin>? get() = mode.activeMode.skinTextures
255
256
    @JvmStatic
257
    fun shouldApplyChanges(): Boolean =
258
        running && allowMixinAbstractClientPlayerEntity
259
260
    @Suppress("unused")
261
    private val sessionHandler = suspendHandler<SessionEvent>(behavior = SuspendHandlerBehavior.CancelPrevious) {
262
        triggerUpload()
263
    }
264
265
    override suspend fun enabledEffect() {
266
        triggerUpload()
267
    }
268
269
    private suspend fun triggerUpload() {
270
        uploadSkinFlow.emit(Unit)
271
    }
272
273
    private inline fun request(block: MinecraftServicesApi.() -> Unit) {
274
        try {
275
            HttpClient.mojangApiClient.mcServicesApi.block()
276
        } catch (e: retrofit2.HttpException) {
277
            logger.error("Failed to upload skin: ${e.code()} ${e.message()}", e)
278
        } catch (e: IOException) {
279
            logger.error("Failed to upload skin", e)
280
        }
281
    }
282
283
    private fun canUploadSkin(): Boolean {
284
        if (!running || !uploadSkin.get() || mc.user.accountType == "legacy") {
285
            return false
286
        }
287
288
        val sessionService = mc.services.sessionService
289
        if (sessionService !is MixinYggdrasilMinecraftSessionServiceAccessor) {
290
            return false
291
        }
292
293
        val baseUrl = sessionService.baseUrl
294
        if (!baseUrl.startsWith(YggdrasilEnvironment.PROD.environment.sessionHost)) {
295
            // custom authentication endpoints are used
296
            // e.g. The Altening
297
            logger.info("Skipped skin upload as custom authentication endpoint is used: $baseUrl")
298
            return false
299
        }
300
301
        return true
302
    }
303
}
304