Skip to content

Commit d726bae

Browse files
committed
Refactor!: Mutable settings
1 parent 9434c39 commit d726bae

File tree

10 files changed

+111
-23
lines changed

10 files changed

+111
-23
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.lambda.core.Loader
99
import com.lambda.gui.impl.clickgui.windows.TagWindow
1010
import com.lambda.module.tag.ModuleTag
1111
import com.lambda.util.KeyCode
12+
import com.mojang.authlib.GameProfile
1213
import net.minecraft.block.Block
1314
import net.minecraft.client.MinecraftClient
1415
import net.minecraft.util.math.BlockPos
@@ -32,6 +33,7 @@ object Lambda {
3233
.registerTypeAdapter(Color::class.java, ColorSerializer)
3334
.registerTypeAdapter(BlockPos::class.java, BlockPosSerializer)
3435
.registerTypeAdapter(Block::class.java, BlockSerializer)
36+
.registerTypeAdapter(GameProfile::class.java, GameProfileSerializer)
3537
.create()
3638

3739
fun initialize() = Loader.initialize()

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,23 +151,23 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
151151
* The type parameter [T] must either be a primitive type or a type with a registered type adapter in [Lambda.gson].
152152
*
153153
* @param name The unique identifier for the setting.
154-
* @param defaultValue The default [List] value of type [T] for the setting.
154+
* @param defaultValue The default [ArrayList] value of type [T] for the setting.
155155
* @param description A brief explanation of the setting's purpose and behavior.
156156
* @param visibility A lambda expression that determines the visibility status of the setting.
157157
*
158158
* ```kotlin
159159
* // the parameter type is inferred from the defaultValue
160-
* private val foo by setting("Foo", listOf("bar", "baz"))
160+
* private val foo by setting("Foo", arrayListOf("bar", "baz"))
161161
* ```
162162
*
163163
* @return The created [ListSetting].
164164
*/
165165
inline fun <reified T : Any> setting(
166166
name: String,
167-
defaultValue: List<T>,
167+
defaultValue: ArrayList<T>,
168168
description: String = "",
169169
noinline visibility: () -> Boolean = { true },
170-
) = ListSetting(name, defaultValue, object : TypeToken<List<T>>() {}.type, description, visibility).also {
170+
) = ListSetting(name, defaultValue, TypeToken.getParameterized(ArrayList::class.java, T::class.java).type, description, visibility).also {
171171
settings.add(it)
172172
}
173173

@@ -188,12 +188,12 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
188188
*
189189
* @return The created [MapSetting].
190190
*/
191-
inline fun <reified K : Any, V : Any> setting(
191+
inline fun <reified K : Any, reified V : Any> setting(
192192
name: String,
193193
defaultValue: Map<K, V>,
194194
description: String = "",
195195
noinline visibility: () -> Boolean = { true },
196-
) = MapSetting(name, defaultValue, object : TypeToken<Map<K, V>>() {}.type, description, visibility).also {
196+
) = MapSetting(name, defaultValue, TypeToken.getParameterized(Map::class.java, K::class.java, V::class.java).type, description, visibility).also {
197197
settings.add(it)
198198
}
199199

@@ -219,7 +219,7 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
219219
defaultValue: Set<T>,
220220
description: String = "",
221221
noinline visibility: () -> Boolean = { true },
222-
) = SetSetting(name, defaultValue, object : TypeToken<Set<T>>() {}.type, description, visibility).also {
222+
) = SetSetting(name, defaultValue, TypeToken.getParameterized(Set::class.java, T::class.java).type, description, visibility).also {
223223
settings.add(it)
224224
}
225225

@@ -448,4 +448,4 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
448448
) = BlockSetting(name, defaultValue, description, visibility).also {
449449
settings.add(it)
450450
}
451-
}
451+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ abstract class Configuration : Jsonable {
9999
this@Configuration.info(message)
100100
}
101101
.onFailure {
102-
val message = "Failed to load ${configName.capitalize()} config from backup, unrecoverable error"
102+
val message =
103+
"Failed to load ${configName.capitalize()} config from backup, unrecoverable error"
103104
LOG.error(message, it)
104105
this@Configuration.logError(message)
105106
}

common/src/main/kotlin/com/lambda/config/configurations/ModuleConfig.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import com.lambda.config.Configuration
44
import com.lambda.config.configurations.ModuleConfig.configName
55
import com.lambda.config.configurations.ModuleConfig.primary
66
import com.lambda.util.FolderRegister
7-
import java.io.File
87

98

109
/**
@@ -18,4 +17,4 @@ import java.io.File
1817
object ModuleConfig : Configuration() {
1918
override val configName = "modules"
2019
override val primary = FolderRegister.config.resolve("$configName.json")
21-
}
20+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.lambda.config.serializer
2+
3+
import com.google.gson.*
4+
import com.mojang.authlib.GameProfile
5+
import java.lang.reflect.Type
6+
import java.util.*
7+
8+
// Yeah yeah I know, there's already a serializer for GameProfile in the Minecraft codebase.
9+
// But who cares, I'm doing it again.
10+
// What you gon' do bout it, huh?
11+
// That's what I thought.
12+
object GameProfileSerializer : JsonSerializer<GameProfile>, JsonDeserializer<GameProfile> {
13+
override fun serialize(
14+
src: GameProfile?,
15+
typeOfSrc: Type?,
16+
context: JsonSerializationContext?,
17+
): JsonElement =
18+
src?.let {
19+
JsonObject().apply {
20+
addProperty("name", it.name)
21+
addProperty("uuid", it.id.toString())
22+
}
23+
} ?: JsonNull.INSTANCE
24+
25+
override fun deserialize(
26+
json: JsonElement?,
27+
typeOfT: Type?,
28+
context: JsonDeserializationContext?,
29+
): GameProfile =
30+
GameProfile(
31+
UUID.fromString(json?.asJsonObject?.get("uuid")?.asString),
32+
json?.asJsonObject?.get("name")?.asString
33+
)
34+
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
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
8+
import java.util.ArrayList
79

810
class ListSetting<T : Any>(
911
override val name: String,
10-
defaultValue: List<T>,
12+
defaultValue: ArrayList<T>,
1113
private val type: Type,
1214
description: String,
1315
visibility: () -> Boolean,
14-
) : AbstractSetting<List<T>>(
16+
) : AbstractSetting<ArrayList<T>>(
1517
defaultValue,
1618
description,
1719
visibility
1820
) {
1921
override fun loadFromJson(serialized: JsonElement) {
2022
value = gson.fromJson(serialized, type)
2123
}
22-
}
24+
}

common/src/main/kotlin/com/lambda/core/Loader.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import com.lambda.Lambda
44
import com.lambda.Lambda.LOG
55
import com.lambda.command.CommandManager
66
import com.lambda.config.configurations.GuiConfig
7+
import com.lambda.friend.FriendManager
78
import com.lambda.graphics.renderer.gui.font.LambdaFont
89
import com.lambda.gui.impl.clickgui.GuiConfigurable
910
import com.lambda.gui.impl.clickgui.LambdaClickGui
1011
import com.lambda.interaction.PlayerPacketManager
1112
import com.lambda.interaction.RotationManager
1213
import com.lambda.module.ModuleRegistry
1314
import com.lambda.util.Communication.ascii
15+
import com.mojang.authlib.GameProfile
16+
import java.util.*
1417
import kotlin.system.measureTimeMillis
1518

1619
object Loader {
@@ -19,7 +22,9 @@ object Loader {
1922
CommandManager,
2023
RotationManager,
2124
PlayerPacketManager,
22-
LambdaFont.Loader
25+
LambdaFont.Loader,
26+
GuiConfigurable,
27+
FriendManager,
2328
)
2429

2530
fun initialize() {
@@ -37,8 +42,8 @@ object Loader {
3742
}
3843
}
3944

40-
LOG.info("${Lambda.MOD_NAME} ${Lambda.VERSION} was successfully initialized (${initTime}ms)")
45+
FriendManager.add(GameProfile(UUID.randomUUID(), "Lambda"))
4146

42-
GuiConfigurable // ToDo: Find more elegant solution
47+
LOG.info("${Lambda.MOD_NAME} ${Lambda.VERSION} was successfully initialized (${initTime}ms)")
4348
}
4449
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.lambda.friend
2+
3+
import com.lambda.config.Configurable
4+
import com.lambda.config.configurations.FriendConfig
5+
import com.lambda.core.Loadable
6+
import com.lambda.event.events.TickEvent
7+
import com.lambda.event.listener.SafeListener.Companion.listener
8+
import com.lambda.util.world.EntityUtils.getEntities
9+
import com.mojang.authlib.GameProfile
10+
import net.minecraft.entity.player.PlayerEntity
11+
import net.minecraft.server.network.ServerPlayerEntity
12+
import net.minecraft.world.event.listener.EntityGameEventHandler
13+
import java.util.Arrays
14+
import java.util.UUID
15+
16+
object FriendManager : Configurable(FriendConfig), Loadable {
17+
override val name = "FriendManager"
18+
19+
private val friends by setting("friends", arrayListOf<GameProfile>())
20+
21+
fun add(vararg profile: GameProfile) =
22+
friends.addAll(profile)
23+
24+
fun remove(vararg profile: GameProfile) =
25+
friends.removeAll(profile.toSet())
26+
27+
fun get(name: String) = friends.firstOrNull { it.name == name }
28+
fun get(uuid: UUID) = friends.firstOrNull { it.id == uuid }
29+
30+
fun contains(profile: GameProfile) = friends.contains(profile)
31+
fun contains(name: String) = friends.any { it.name == name }
32+
fun contains(uuid: UUID) = friends.any { it.id == uuid }
33+
34+
fun clear() = friends.clear()
35+
36+
val ServerPlayerEntity.isFriend: Boolean
37+
get() = contains(gameProfile)
38+
39+
override fun load(): String {
40+
if (friends.isEmpty()) return "No friends loaded, damn bro you don't have to be antisocial online too,"
41+
val word = if (friends.size == 1) "friend" else "friends"
42+
return "Loaded ${friends.size} $word."
43+
}
44+
}

common/src/main/kotlin/com/lambda/gui/impl/clickgui/GuiConfigurable.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package com.lambda.gui.impl.clickgui
22

33
import com.lambda.config.Configurable
44
import com.lambda.config.configurations.GuiConfig
5+
import com.lambda.core.Loadable
56
import com.lambda.gui.impl.clickgui.windows.TagWindow
67

7-
object GuiConfigurable : Configurable(GuiConfig) {
8+
object GuiConfigurable : Configurable(GuiConfig), Loadable {
89
override val name = "gui"
9-
val windows = setting("windows", listOf(TagWindow()))
10-
}
10+
val windows = setting("windows", arrayListOf(TagWindow()))
11+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ object Packetlogger : Module(
3838
private val networkSide by setting("Network Side", NetworkSide.ANY, "Side of the network to log packets from")
3939
private val logTicks by setting("Log Ticks", true, "Show game ticks in the log")
4040
private val scope by setting("Scope", Scope.ANY, "Scope of packets to log")
41-
private val whitelist by setting("Whitelist Packets", listOf<String>(), "Packets to whitelist") { scope == Scope.WHITELIST }
42-
private val blacklist by setting("Blacklist Packets", listOf<String>(), "Packets to blacklist") { scope == Scope.BLACKLIST }
41+
private val whitelist by setting("Whitelist Packets", arrayListOf<String>(), "Packets to whitelist") { scope == Scope.WHITELIST }
42+
private val blacklist by setting("Blacklist Packets", arrayListOf<String>(), "Packets to blacklist") { scope == Scope.BLACKLIST }
4343
private val maxRecursionDepth by setting("Max Recursion Depth", 6, 1..10, 1, "Maximum recursion depth for packet serialization")
4444
private val logConcurrent by setting("Build Data Concurrent", false, "Whether to serialize packets concurrently. Will not save packets in chronological order but wont lag the game.")
4545

@@ -187,4 +187,4 @@ object Packetlogger : Module(
187187
private fun Packet<*>.logSent() {
188188
storageFlow.tryEmit("Sent at ${getTime(entryFormatter)}\n${dynamicString(maxRecursionDepth)}\n")
189189
}
190-
}
190+
}

0 commit comments

Comments
 (0)