Skip to content

Commit fb14525

Browse files
committed
String identifier conversion
1 parent fffc05a commit fb14525

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

common/src/main/kotlin/com/lambda/network/CapeManager.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import com.lambda.util.FileUtils.downloadIfNotPresent
3737
import com.lambda.util.FileUtils.ifNotExists
3838
import com.lambda.util.FileUtils.isOlderThan
3939
import com.lambda.util.FolderRegister.capes
40+
import com.lambda.util.StringUtils.asIdentifier
4041
import com.lambda.util.extension.resolveFile
4142
import kotlinx.coroutines.runBlocking
4243
import net.minecraft.client.texture.NativeImage.read
@@ -57,7 +58,7 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
5758
private val images = capes.walk()
5859
.filter { it.extension == "png" }
5960
.associate { it.nameWithoutExtension to NativeImageBackedTexture({ it.nameWithoutExtension }, read(it.inputStream())) }
60-
.onEach { (key, value) -> mc.textureManager.registerTexture(key.toIdentifier(), value) }
61+
.onEach { (key, value) -> mc.textureManager.registerTexture(key.asIdentifier, value) }
6162

6263
private val fetchQueue = mutableListOf<UUID>()
6364

@@ -100,7 +101,7 @@ object CapeManager : ConcurrentHashMap<UUID, String>(), Loadable {
100101
.downloadIfNotPresent(cape.url).getOrNull()
101102
?.readBytes() ?: return@runIO
102103

103-
mc.textureManager.registerTexture(cape.id.toIdentifier(), NativeImageBackedTexture({ cape.id }, TextureUtils.readImage(bytes)))
104+
mc.textureManager.registerTexture(cape.id.asIdentifier, NativeImageBackedTexture({ cape.id }, TextureUtils.readImage(bytes)))
104105

105106
put(uuid, cape.id)
106107
}.invokeOnCompletion { block(it) }

common/src/main/kotlin/com/lambda/sound/LambdaSound.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@
1717

1818
package com.lambda.sound
1919

20-
import com.lambda.sound.SoundManager.toIdentifier
20+
import com.lambda.util.StringUtils.asIdentifier
2121
import net.minecraft.sound.SoundEvent
2222
import net.minecraft.util.Identifier
2323

2424
enum class LambdaSound(val id: Identifier) {
25-
BUTTON_CLICK("button_click".toIdentifier()),
25+
BUTTON_CLICK("button_click".asIdentifier),
2626

27-
BOOLEAN_SETTING_ON("bool_on".toIdentifier()),
28-
BOOLEAN_SETTING_OFF("bool_off".toIdentifier()),
27+
BOOLEAN_SETTING_ON("bool_on".asIdentifier),
28+
BOOLEAN_SETTING_OFF("bool_off".asIdentifier),
2929

30-
MODULE_ON("module_on".toIdentifier()),
31-
MODULE_OFF("module_off".toIdentifier()),
30+
MODULE_ON("module_on".asIdentifier),
31+
MODULE_OFF("module_off".asIdentifier),
3232

33-
SETTINGS_OPEN("settings_open".toIdentifier()),
34-
SETTINGS_CLOSE("settings_close".toIdentifier());
33+
SETTINGS_OPEN("settings_open".asIdentifier),
34+
SETTINGS_CLOSE("settings_close".asIdentifier);
3535

3636
val event: SoundEvent = SoundEvent.of(id)
3737
}

common/src/main/kotlin/com/lambda/sound/SoundManager.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717

1818
package com.lambda.sound
1919

20-
import com.lambda.Lambda
2120
import com.lambda.Lambda.mc
2221
import com.lambda.util.math.random
2322
import net.minecraft.client.sound.PositionedSoundInstance
2423
import net.minecraft.sound.SoundEvent
25-
import net.minecraft.util.Identifier
2624

2725
object SoundManager {
2826
fun playSound(event: SoundEvent, pitch: Double = 1.0) {
@@ -38,6 +36,4 @@ object SoundManager {
3836
PositionedSoundInstance.master(event, actualPitch.toFloat())
3937
)
4038
}
41-
42-
fun String.toIdentifier() = Identifier.of(Lambda.MOD_ID, this)
4339
}

common/src/main/kotlin/com/lambda/util/StringUtils.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
package com.lambda.util
1919

20+
import com.lambda.Lambda
2021
import com.lambda.Lambda.gson
22+
import net.minecraft.util.Identifier
2123
import java.security.MessageDigest
2224
import kotlin.io.encoding.Base64
2325
import kotlin.io.encoding.ExperimentalEncodingApi
@@ -37,6 +39,17 @@ object StringUtils {
3739
*/
3840
fun String.capitalize() = replaceFirstChar { it.titlecase() }
3941

42+
/**
43+
* Returns a Minecraft [net.minecraft.util.Identifier] from a string with the given namespace
44+
*/
45+
fun String.toIdentifier(namespace: String = Lambda.MOD_ID): Identifier =
46+
Identifier.of(namespace, this)
47+
48+
/**
49+
* Returns a [Lambda] Minecraft [Identifier] from a string
50+
*/
51+
val String.asIdentifier: Identifier get() = toIdentifier()
52+
4053
/**
4154
* Find similar strings in a set of words.
4255
*

0 commit comments

Comments
 (0)