Skip to content

Commit 267a502

Browse files
committed
Merge remote-tracking branch 'origin/feature/renderer' into feature/renderer
2 parents 44f30d9 + 9cf8616 commit 267a502

File tree

15 files changed

+45
-43
lines changed

15 files changed

+45
-43
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.lambda.Lambda.gson
55
import com.lambda.context.SafeContext
66
import com.lambda.threading.runSafe
77
import com.lambda.util.Nameable
8+
import java.lang.reflect.Type
89
import kotlin.properties.Delegates
910
import kotlin.reflect.KProperty
1011

@@ -49,10 +50,12 @@ import kotlin.reflect.KProperty
4950
*
5051
* @property defaultValue The default value of the setting.
5152
* @property description A description of the setting.
53+
* @property type The type reflection of the setting.
5254
* @property visibility A function that determines whether the setting is visible.
5355
*/
5456
abstract class AbstractSetting<T : Any>(
5557
private val defaultValue: T,
58+
private val type: Type,
5659
val description: String,
5760
val visibility: () -> Boolean,
5861
) : Jsonable, Nameable {
@@ -74,10 +77,10 @@ abstract class AbstractSetting<T : Any>(
7477
}
7578

7679
override fun toJson(): JsonElement =
77-
gson.toJsonTree(value)
80+
gson.toJsonTree(value, type)
7881

7982
override fun loadFromJson(serialized: JsonElement) {
80-
value = gson.fromJson(serialized, value::class.java)
83+
value = gson.fromJson(serialized, type)
8184
}
8285

8386
fun onValueChange(block: SafeContext.(from: T, to: T) -> Unit) {
@@ -101,4 +104,4 @@ abstract class AbstractSetting<T : Any>(
101104
}
102105

103106
class ValueListener <T> (val requiresValueChange: Boolean, val execute: (from: T, to: T) -> Unit)
104-
}
107+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import com.lambda.util.Nameable
2222
import net.minecraft.block.Block
2323
import net.minecraft.util.math.BlockPos
2424
import java.awt.Color
25-
import java.lang.reflect.Type
2625

2726
/**
2827
* Represents a set of [AbstractSetting]s that are associated with the [name] of the [Configurable].

common/src/main/kotlin/com/lambda/config/settings/CharSetting.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lambda.config.settings
22

3+
import com.google.gson.reflect.TypeToken
34
import com.lambda.config.AbstractSetting
45

56
/**
@@ -17,6 +18,7 @@ class CharSetting(
1718
visibility: () -> Boolean,
1819
) : AbstractSetting<Char>(
1920
defaultValue,
21+
TypeToken.get(Char::class.java).type,
2022
description,
2123
visibility
22-
)
24+
)

common/src/main/kotlin/com/lambda/config/settings/NumericSetting.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lambda.config.settings
22

3+
import com.google.gson.reflect.TypeToken
34
import com.lambda.config.AbstractSetting
45
import com.lambda.util.math.MathUtils.roundToStep
56
import kotlin.reflect.KProperty
@@ -25,6 +26,7 @@ abstract class NumericSetting<T>(
2526
val unit: String,
2627
) : AbstractSetting<T>(
2728
value,
29+
TypeToken.get(value::class.java).type,
2830
description,
2931
visibility
3032
) where T : Number, T : Comparable<T> {
@@ -33,4 +35,4 @@ abstract class NumericSetting<T>(
3335
override operator fun setValue(thisRef: Any?, property: KProperty<*>, valueIn: T) {
3436
value = valueIn.coerceIn(range)
3537
}
36-
}
38+
}

common/src/main/kotlin/com/lambda/config/settings/StringSetting.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lambda.config.settings
22

3+
import com.google.gson.reflect.TypeToken
34
import com.lambda.config.AbstractSetting
45

56
/**
@@ -17,6 +18,7 @@ class StringSetting(
1718
visibility: () -> Boolean,
1819
) : AbstractSetting<String>(
1920
defaultValue,
21+
TypeToken.get(String::class.java).type,
2022
description,
2123
visibility
22-
)
24+
)

common/src/main/kotlin/com/lambda/config/settings/collections/ListSetting.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.lambda.config.settings.collections
22

33
import com.google.gson.JsonElement
4+
import com.google.gson.reflect.TypeToken
45
import com.lambda.Lambda.gson
56
import com.lambda.config.AbstractSetting
67
import java.lang.reflect.Type
@@ -13,6 +14,7 @@ class ListSetting<T : Any>(
1314
visibility: () -> Boolean,
1415
) : AbstractSetting<MutableList<T>>(
1516
defaultValue,
17+
type,
1618
description,
1719
visibility
1820
) {
Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
package com.lambda.config.settings.collections
22

3-
import com.google.gson.JsonElement
4-
import com.lambda.Lambda.gson
53
import com.lambda.config.AbstractSetting
64
import java.lang.reflect.Type
75

86
class MapSetting<K, V>(
97
override val name: String,
10-
private val defaultValue: MutableMap<K, V>,
11-
private val type: Type,
8+
defaultValue: MutableMap<K, V>,
9+
type: Type,
1210
description: String,
1311
visibility: () -> Boolean,
1412
) : AbstractSetting<MutableMap<K, V>>(
1513
defaultValue,
14+
type,
1615
description,
1716
visibility
18-
) {
19-
override fun loadFromJson(serialized: JsonElement) {
20-
value = gson.fromJson(serialized, type)
21-
}
22-
23-
override fun toJson(): JsonElement {
24-
value = defaultValue // Hack the Delegates.observable
25-
return gson.toJsonTree(value)
26-
}
27-
}
17+
)
Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
package com.lambda.config.settings.collections
22

3-
import com.google.gson.JsonElement
4-
import com.lambda.Lambda.gson
53
import com.lambda.config.AbstractSetting
64
import java.lang.reflect.Type
75

86
class SetSetting<T : Any>(
97
override val name: String,
10-
private val defaultValue: MutableSet<T>,
11-
private val type: Type,
8+
defaultValue: MutableSet<T>,
9+
type: Type,
1210
description: String,
1311
visibility: () -> Boolean,
1412
) : AbstractSetting<MutableSet<T>>(
1513
defaultValue,
14+
type,
1615
description,
1716
visibility
18-
) {
19-
override fun loadFromJson(serialized: JsonElement) {
20-
value = gson.fromJson(serialized, type)
21-
}
22-
23-
override fun toJson(): JsonElement {
24-
value = defaultValue // Hack the Delegates.observable
25-
return gson.toJsonTree(value)
26-
}
27-
}
17+
)

common/src/main/kotlin/com/lambda/config/settings/comparable/BooleanSetting.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lambda.config.settings.comparable
22

3+
import com.google.gson.reflect.TypeToken
34
import com.lambda.config.AbstractSetting
45

56
class BooleanSetting(
@@ -9,6 +10,7 @@ class BooleanSetting(
910
visibility: () -> Boolean,
1011
) : AbstractSetting<Boolean>(
1112
defaultValue,
13+
TypeToken.get(Boolean::class.java).type,
1214
description,
1315
visibility
14-
)
16+
)

common/src/main/kotlin/com/lambda/config/settings/comparable/EnumSetting.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lambda.config.settings.comparable
22

3+
import com.google.gson.reflect.TypeToken
34
import com.lambda.config.AbstractSetting
45

56
class EnumSetting<T : Enum<T>>(
@@ -9,6 +10,7 @@ class EnumSetting<T : Enum<T>>(
910
visibility: () -> Boolean,
1011
) : AbstractSetting<T>(
1112
defaultValue,
13+
TypeToken.get(defaultValue.declaringJavaClass).type,
1214
description,
1315
visibility,
1416
) {
@@ -17,4 +19,4 @@ class EnumSetting<T : Enum<T>>(
1719
fun next() {
1820
value = enumValues[((value.ordinal + 1) % enumValues.size)]
1921
}
20-
}
22+
}

0 commit comments

Comments
 (0)