Skip to content

Commit f455c4b

Browse files
committed
Color setting
1 parent fa5ce08 commit f455c4b

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

common/src/main/kotlin/com/lambda/Lambda.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import com.google.gson.Gson
44
import com.google.gson.GsonBuilder
55
import com.lambda.config.serializer.BlockPosSerializer
66
import com.lambda.config.serializer.BlockSerializer
7+
import com.lambda.config.serializer.ColorSerializer
78
import com.lambda.util.Eager
89
import net.minecraft.block.Block
910
import net.minecraft.client.MinecraftClient
1011
import net.minecraft.util.math.BlockPos
1112
import org.apache.logging.log4j.LogManager
1213
import org.apache.logging.log4j.Logger
1314
import org.reflections.Reflections
15+
import java.awt.Color
1416

1517
object Lambda {
1618
const val MOD_NAME = "Lambda"
@@ -23,6 +25,7 @@ object Lambda {
2325

2426
val gson: Gson = GsonBuilder()
2527
.setPrettyPrinting()
28+
.registerTypeAdapter(Color::class.java, ColorSerializer)
2629
.registerTypeAdapter(BlockPos::class.java, BlockPosSerializer)
2730
.registerTypeAdapter(Block::class.java, BlockSerializer)
2831
.create()

common/src/main/kotlin/com/lambda/config/Configurable.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import com.lambda.util.Nameable
1818
import net.minecraft.block.Block
1919
import net.minecraft.util.math.BlockPos
2020
import com.lambda.Lambda
21+
import com.lambda.config.settings.complex.ColorSetting
22+
import java.awt.Color
2123

2224
/**
2325
* Represents a set of [AbstractSetting]s that are associated with the [name] of the [Configurable].
@@ -356,6 +358,25 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
356358
settings.add(it)
357359
}
358360

361+
/**
362+
* Creates a [ColorSetting] with the provided parameters and adds it to the [settings].
363+
*
364+
* @param name The unique identifier for the setting.
365+
* @param defaultValue The default [Color] value of the setting.
366+
* @param visibility A lambda expression that determines the visibility status of the setting.
367+
* @param description A brief explanation of the setting's purpose and behavior.
368+
*
369+
* @return The created [ColorSetting].
370+
*/
371+
fun setting(
372+
name: String,
373+
defaultValue: Color,
374+
visibility: () -> Boolean = { true },
375+
description: String = "",
376+
) = ColorSetting(name, defaultValue, visibility, description).also {
377+
settings.add(it)
378+
}
379+
359380
/**
360381
* Creates a [BlockPosSetting] with the provided parameters and adds it to the [settings].
361382
*
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.lambda.config.serializer
2+
3+
import com.google.gson.*
4+
import java.awt.Color
5+
import java.lang.reflect.Type
6+
7+
object ColorSerializer : JsonSerializer<Color>, JsonDeserializer<Color> {
8+
override fun serialize(
9+
src: Color?,
10+
typeOfSrc: Type?,
11+
context: JsonSerializationContext?,
12+
): JsonElement =
13+
src?.let {
14+
it.rgb
15+
JsonPrimitive("${it.red},${it.green},${it.blue},${it.alpha}")
16+
} ?: JsonNull.INSTANCE
17+
18+
override fun deserialize(
19+
json: JsonElement?,
20+
typeOfT: Type?,
21+
context: JsonDeserializationContext?,
22+
): Color =
23+
json?.asString?.split(",")?.let {
24+
when (it.size) {
25+
3 -> Color(it[0].toInt(), it[1].toInt(), it[2].toInt())
26+
4 -> Color(it[0].toInt(), it[1].toInt(), it[2].toInt(), it[3].toInt())
27+
else -> throw JsonParseException("Invalid color format")
28+
}
29+
} ?: throw JsonParseException("Invalid color format")
30+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.lambda.config.settings.complex
2+
3+
import com.lambda.config.AbstractSetting
4+
import java.awt.Color
5+
6+
class ColorSetting(
7+
override val name: String,
8+
defaultValue: Color,
9+
visibility: () -> Boolean,
10+
description: String,
11+
) : AbstractSetting<Color>(
12+
defaultValue,
13+
visibility,
14+
description
15+
)

common/src/main/kotlin/com/lambda/module/modules/BoringModule.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.lambda.module.Module
77
import com.lambda.module.tag.ModuleTag
88
import com.lambda.util.KeyCode
99
import net.minecraft.util.math.BlockPos
10+
import java.awt.Color
1011

1112
object BoringModule : Module(
1213
name = "BoringModule",
@@ -15,6 +16,7 @@ object BoringModule : Module(
1516
defaultKeybind = KeyCode.Z
1617
) {
1718
private val superBoring by setting("Super Boring", false)
19+
private val boringColor by setting("Boring Color", Color.RED)
1820
private val boringValue by setting("Boring Value", 0.0, 0.1..5.0, 0.1)
1921
private val boringFriends by setting("Boring Friends List", listOf("Peter", "Paul", "Mary", "John", "Ringo"))
2022
private val boringEnemies by setting("Boring Enemies Set", setOf("Sauron", "Voldemort", "Darth Vader", "The Joker"))

0 commit comments

Comments
 (0)