Skip to content

Commit 292ab7f

Browse files
committed
Merge branch 'master' of https://github.com/Avanatiker/NeoLambda
2 parents c912d92 + ccc9a8c commit 292ab7f

File tree

12 files changed

+65
-36
lines changed

12 files changed

+65
-36
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import net.minecraft.util.math.BlockPos
1919
import org.apache.logging.log4j.LogManager
2020
import org.apache.logging.log4j.Logger
2121
import java.awt.Color
22+
import java.util.*
2223

2324

2425
object Lambda {
@@ -41,6 +42,7 @@ object Lambda {
4142
.registerTypeAdapter(BlockPos::class.java, BlockPosSerializer)
4243
.registerTypeAdapter(Block::class.java, BlockSerializer)
4344
.registerTypeAdapter(GameProfile::class.java, GameProfileSerializer)
45+
.registerTypeAdapter(Optional::class.java, OptionalSerializer)
4446
.create()
4547

4648
fun initialize() {

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ abstract class Configurable(
159159
* The type parameter [T] must either be a primitive type or a type with a registered type adapter in [Lambda.gson].
160160
*
161161
* @param name The unique identifier for the setting.
162-
* @param defaultValue The default [ArrayList] value of type [T] for the setting.
162+
* @param defaultValue The default [List] value of type [T] for the setting.
163163
* @param description A brief explanation of the setting's purpose and behavior.
164+
* @param hackDelegates A flag that determines whether the setting should be serialized with the default value.
164165
* @param visibility A lambda expression that determines the visibility status of the setting.
165166
*
166167
* ```kotlin
@@ -175,12 +176,14 @@ abstract class Configurable(
175176
defaultValue: List<T>,
176177
description: String = "",
177178
noinline visibility: () -> Boolean = { true },
179+
hackDelegates: Boolean = false,
178180
) = ListSetting(
179181
name,
180182
defaultValue.toMutableList(),
181183
TypeToken.getParameterized(MutableList::class.java, T::class.java).type,
182184
description,
183-
visibility
185+
hackDelegates,
186+
visibility,
184187
).also {
185188
settings.add(it)
186189
}
@@ -193,6 +196,7 @@ abstract class Configurable(
193196
* @param name The unique identifier for the setting.
194197
* @param defaultValue The default [Map] value of type [K] and [V] for the setting.
195198
* @param description A brief explanation of the setting's purpose and behavior.
199+
* @param hackDelegates A flag that determines whether the setting should be serialized with the default value.
196200
* @param visibility A lambda expression that determines the visibility status of the setting.
197201
*
198202
* ```kotlin
@@ -206,12 +210,14 @@ abstract class Configurable(
206210
name: String,
207211
defaultValue: Map<K, V>,
208212
description: String = "",
213+
hackDelegates: Boolean,
209214
noinline visibility: () -> Boolean = { true },
210215
) = MapSetting(
211216
name,
212217
defaultValue.toMutableMap(),
213-
TypeToken.getParameterized(Map::class.java, K::class.java, V::class.java).type,
218+
TypeToken.getParameterized(MutableMap::class.java, K::class.java, V::class.java).type,
214219
description,
220+
hackDelegates,
215221
visibility
216222
).also {
217223
settings.add(it)
@@ -225,6 +231,7 @@ abstract class Configurable(
225231
* @param name The unique identifier for the setting.
226232
* @param defaultValue The default [Set] value of type [T] for the setting.
227233
* @param description A brief explanation of the setting's purpose and behavior.
234+
* @param hackDelegates A flag that determines whether the setting should be serialized with the default value.
228235
* @param visibility A lambda expression that determines the visibility status of the setting.
229236
*
230237
* ```kotlin
@@ -238,13 +245,15 @@ abstract class Configurable(
238245
name: String,
239246
defaultValue: Set<T>,
240247
description: String = "",
248+
hackDelegates: Boolean = false,
241249
noinline visibility: () -> Boolean = { true },
242250
) = SetSetting(
243251
name,
244252
defaultValue.toMutableSet(),
245-
TypeToken.getParameterized(Set::class.java, T::class.java).type,
253+
TypeToken.getParameterized(MutableSet::class.java, T::class.java).type,
246254
description,
247-
visibility
255+
hackDelegates,
256+
visibility,
248257
).also {
249258
settings.add(it)
250259
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,15 @@ abstract class Configuration : Jsonable {
7979

8080
private fun save() {
8181
with(primary) {
82-
if (exists()) {
83-
copyTo(backup, true)
84-
}
82+
if (exists()) copyTo(backup, true)
83+
8584
parentFile.mkdirs()
8685
writeText(gson.toJson(toJson()))
8786
}
8887
}
8988

9089
private fun load(file: File) {
91-
check(file.exists()) {
92-
"No configuration file found for ${configName.capitalize()}"
93-
}
90+
check(file.exists()) { "No configuration file found for ${configName.capitalize()}" }
9491

9592
loadFromJson(JsonParser.parseReader(file.reader()).asJsonObject)
9693
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.lambda.config.serializer
2+
3+
import com.google.gson.*
4+
import java.lang.reflect.Type
5+
import java.util.*
6+
7+
object OptionalSerializer : JsonSerializer<Optional<Any>>, JsonDeserializer<Optional<Any>> {
8+
override fun serialize(src: Optional<Any>?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement =
9+
src?.map { context?.serialize(it) }?.orElse(JsonNull.INSTANCE) ?: JsonNull.INSTANCE
10+
11+
override fun deserialize(
12+
json: JsonElement?,
13+
typeOfT: Type?,
14+
context: JsonDeserializationContext?
15+
): Optional<Any> =
16+
Optional.ofNullable(json?.let { context?.deserialize(it, typeOfT) ?: Optional.empty<Any>() })
17+
}
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
package com.lambda.config.settings.collections
22

33
import com.google.gson.JsonElement
4-
import com.lambda.Lambda.gson
54
import com.lambda.config.AbstractSetting
65
import java.lang.reflect.Type
76

87
class ListSetting<T : Any>(
98
override val name: String,
10-
defaultValue: MutableList<T>,
11-
private val type: Type,
9+
private val defaultValue: MutableList<T>,
10+
type: Type,
1211
description: String,
12+
private val hackDelegates: Boolean,
1313
visibility: () -> Boolean,
1414
) : AbstractSetting<MutableList<T>>(
1515
defaultValue,
1616
type,
1717
description,
1818
visibility
1919
) {
20-
override fun loadFromJson(serialized: JsonElement) {
21-
value = gson.fromJson(serialized, type)
22-
}
23-
2420
override fun toJson(): JsonElement {
25-
return gson.toJsonTree(value)
21+
if (hackDelegates) value = defaultValue
22+
return super.toJson()
2623
}
2724
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
package com.lambda.config.settings.collections
22

3+
import com.google.gson.JsonElement
34
import com.lambda.config.AbstractSetting
45
import java.lang.reflect.Type
56

67
class MapSetting<K, V>(
78
override val name: String,
8-
defaultValue: MutableMap<K, V>,
9+
private val defaultValue: Map<K, V>,
910
type: Type,
1011
description: String,
12+
private val hackDelegates: Boolean,
1113
visibility: () -> Boolean,
12-
) : AbstractSetting<MutableMap<K, V>>(
14+
) : AbstractSetting<Map<K, V>>(
1315
defaultValue,
1416
type,
1517
description,
1618
visibility
17-
)
19+
) {
20+
override fun toJson(): JsonElement {
21+
if (hackDelegates) value = defaultValue
22+
return super.toJson()
23+
}
24+
}
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
package com.lambda.config.settings.collections
22

3+
import com.google.gson.JsonElement
34
import com.lambda.config.AbstractSetting
45
import java.lang.reflect.Type
56

67
class SetSetting<T : Any>(
78
override val name: String,
8-
defaultValue: MutableSet<T>,
9+
private val defaultValue: MutableSet<T>,
910
type: Type,
1011
description: String,
12+
private val hackDelegates: Boolean,
1113
visibility: () -> Boolean,
1214
) : AbstractSetting<MutableSet<T>>(
1315
defaultValue,
1416
type,
1517
description,
1618
visibility
17-
)
19+
) {
20+
override fun toJson(): JsonElement {
21+
if (hackDelegates) value = defaultValue
22+
return super.toJson()
23+
}
24+
}

common/src/main/kotlin/com/lambda/friend/FriendRegistry.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.mojang.authlib.GameProfile
88
object FriendRegistry : Configurable(FriendConfig), Loadable {
99
override val name = "friends"
1010

11-
val friends by setting("friends", listOf<GameProfile>()) // Todo: Fix the fucking delegates
11+
val friends by setting("friends", listOf<GameProfile>(), hackDelegates = true)
1212

1313
override fun load(): String {
1414
return "Loaded ${friends.size} friends"

common/src/main/kotlin/com/lambda/module/Module.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ abstract class Module(
100100
private val isEnabledSetting = setting("Enabled", enabledByDefault, visibility = { false })
101101
private val keybindSetting = setting("Keybind", defaultKeybind)
102102
private val isVisible = setting("Visible", true)
103-
val customTags = setting("Tags", emptySet<ModuleTag>(), visibility = { false })
103+
val customTags = setting("Tags", setOf<ModuleTag>(), visibility = { false })
104104

105105
var isEnabled by isEnabledSetting
106106
val isDisabled get() = !isEnabled

common/src/main/kotlin/com/lambda/module/modules/network/PacketLogger.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.lambda.module.modules.network
22

33
import com.lambda.Lambda
44
import com.lambda.Lambda.mc
5-
import com.lambda.event.EventFlow.lambdaScope
65
import com.lambda.event.events.PacketEvent
76
import com.lambda.event.events.TickEvent
87
import com.lambda.event.listener.UnsafeListener.Companion.unsafeConcurrentListener
@@ -16,10 +15,8 @@ import com.lambda.util.DynamicReflectionSerializer.dynamicString
1615
import com.lambda.util.FolderRegister
1716
import com.lambda.util.Formatting.getTime
1817
import com.lambda.util.text.*
19-
import kotlinx.coroutines.Dispatchers
2018
import kotlinx.coroutines.channels.BufferOverflow
2119
import kotlinx.coroutines.flow.MutableSharedFlow
22-
import kotlinx.coroutines.launch
2320
import net.minecraft.network.packet.Packet
2421
import java.awt.Color
2522
import java.io.File
@@ -38,8 +35,8 @@ object PacketLogger : Module(
3835
private val networkSide by setting("Network Side", NetworkSide.ANY, "Side of the network to log packets from")
3936
private val logTicks by setting("Log Ticks", true, "Show game ticks in the log")
4037
private val scope by setting("Scope", Scope.ANY, "Scope of packets to log")
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 }
38+
private val whitelist by setting("Whitelist Packets", emptyList<String>(), "Packets to whitelist", visibility = { scope == Scope.WHITELIST })
39+
private val blacklist by setting("Blacklist Packets", emptyList<String>(), "Packets to blacklist", visibility = { scope == Scope.BLACKLIST })
4340
private val maxRecursionDepth by setting("Max Recursion Depth", 6, 1..10, 1, "Maximum recursion depth for packet serialization")
4441
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.")
4542

0 commit comments

Comments
 (0)