Skip to content

Commit 54c5cd4

Browse files
authored
Merge branch 'master' into refactor/buildscript
2 parents 783a9ee + b2f11c8 commit 54c5cd4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1214
-82
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ val yarnMappings = property("yarn_mappings").toString()
1010
val libs = file("libs")
1111

1212
plugins {
13-
kotlin("jvm") version ("1.9.22")
13+
kotlin("jvm") version "1.9.22"
1414
id("org.jetbrains.dokka") version "1.9.20"
1515
id("architectury-plugin") version "3.4-SNAPSHOT"
1616
id("dev.architectury.loom") version "1.5-SNAPSHOT" apply false

common/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies {
2424

2525
// Add dependencies on the required Kotlin modules.
2626
modImplementation("net.fabricmc:fabric-language-kotlin:$fabricKotlinVersion")
27+
implementation("org.reflections:reflections:0.10.2")
2728
implementation(annotationProcessor("io.github.llamalad7:mixinextras-common:$mixinExtrasVersion")!!)
2829
}
2930

common/src/main/java/com/lambda/mixin/MinecraftClientMixin.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.lambda.mixin;
22

33
import com.lambda.event.EventFlow;
4+
import com.lambda.event.events.ClientEvent;
45
import com.lambda.event.events.TickEvent;
56
import net.minecraft.client.MinecraftClient;
67
import org.spongepowered.asm.mixin.Mixin;
@@ -19,4 +20,17 @@ void onTickPre(CallbackInfo ci) {
1920
void onTickPost(CallbackInfo ci) {
2021
EventFlow.post(new TickEvent.Post());
2122
}
23+
24+
@Inject(at = @At(value = "INVOKE", target = "Lorg/slf4j/Logger;info(Ljava/lang/String;)V", shift = At.Shift.AFTER, remap = false), method = "stop")
25+
private void onShutdown(CallbackInfo ci) {
26+
EventFlow.post(ClientEvent.Shutdown.INSTANCE);
27+
}
28+
29+
/**
30+
* Inject after the thread field is set so `ThreadExecutor#getThread` is available
31+
*/
32+
@Inject(at = @At(value = "FIELD", target = "Lnet/minecraft/client/MinecraftClient;thread:Ljava/lang/Thread;", shift = At.Shift.AFTER, ordinal = 0), method = "run")
33+
private void onStartup(CallbackInfo ci) {
34+
EventFlow.post(ClientEvent.Startup.INSTANCE);
35+
}
2236
}

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

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.lambda
22

3-
import com.lambda.event.events.KeyPressEvent
4-
import com.lambda.event.listener.SafeListener.Companion.listener
5-
import com.lambda.task.tasks.HelloWorldTask
6-
import com.lambda.threading.taskContext
3+
import com.google.gson.Gson
4+
import com.google.gson.GsonBuilder
5+
import com.lambda.config.serializer.BlockPosSerializer
6+
import com.lambda.config.serializer.BlockSerializer
7+
import com.lambda.module.ModuleRegistry
8+
import com.lambda.module.modules.BoringModule
9+
import net.minecraft.block.Block
710
import net.minecraft.client.MinecraftClient
11+
import net.minecraft.util.math.BlockPos
812
import org.apache.logging.log4j.LogManager
913
import org.apache.logging.log4j.Logger
10-
import org.lwjgl.glfw.GLFW
1114

1215
object Lambda {
1316
private const val MOD_NAME = "Lambda"
@@ -17,33 +20,42 @@ object Lambda {
1720

1821
val LOG: Logger = LogManager.getLogger(SYMBOL)
1922
val mc: MinecraftClient by lazy { MinecraftClient.getInstance() }
23+
val gson: Gson = GsonBuilder()
24+
.setPrettyPrinting()
25+
.registerTypeAdapter(BlockPos::class.java, BlockPosSerializer)
26+
.registerTypeAdapter(Block::class.java, BlockSerializer)
27+
.create()
2028

2129
init {
22-
listener<KeyPressEvent> {
23-
if (it.key != GLFW.GLFW_KEY_Z) {
24-
return@listener
25-
}
30+
// ToDo: Why do i have to call these?
31+
BoringModule
32+
ModuleRegistry
2633

27-
taskContext {
28-
HelloWorldTask()
29-
.withDelay(500L)
30-
.withTimeout(200L)
31-
.withRepeats(10)
32-
.withMaxAttempts(5)
33-
.onSuccess {
34-
LOG.info("Hello, World! Task completed")
35-
}.onRepeat { repeats ->
36-
LOG.info("Hello, World! Task $name repeated $repeats times")
37-
}.onTimeout {
38-
LOG.warn("Hello, World! Task $name timed out")
39-
HelloWorldTask().execute()
40-
}.onRetry {
41-
LOG.warn("Hello, World! Task $name retrying")
42-
}.onFailure { error ->
43-
LOG.error("Hello, World! Task failed", error)
44-
}.execute()
45-
}
46-
}
34+
// listener<KeyPressEvent> {
35+
// if (it.key != GLFW.GLFW_KEY_Z) {
36+
// return@listener
37+
// }
38+
//
39+
// taskContext {
40+
// HelloWorldTask()
41+
// .withDelay(500L)
42+
// .withTimeout(200L)
43+
// .withRepeats(10)
44+
// .withMaxAttempts(5)
45+
// .onSuccess {
46+
// LOG.info("Hello, World! Task completed")
47+
// }.onRepeat { repeats ->
48+
// LOG.info("Hello, World! Task $name repeated $repeats times")
49+
// }.onTimeout {
50+
// LOG.warn("Hello, World! Task $name timed out")
51+
// HelloWorldTask().execute()
52+
// }.onRetry {
53+
// LOG.warn("Hello, World! Task $name retrying")
54+
// }.onFailure { error ->
55+
// LOG.error("Hello, World! Task failed", error)
56+
// }.execute()
57+
// }
58+
// }
4759
}
4860

4961
fun initialize() {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.lambda.config
2+
3+
import com.google.gson.JsonElement
4+
import com.lambda.Lambda.gson
5+
import com.lambda.context.SafeContext
6+
import com.lambda.threading.runSafe
7+
import com.lambda.util.Nameable
8+
import kotlin.properties.Delegates
9+
import kotlin.reflect.KProperty
10+
11+
abstract class AbstractSetting<T : Any>(
12+
private val defaultValue: T,
13+
val visibility: () -> Boolean,
14+
val description: String,
15+
) : Jsonable, Nameable {
16+
private val listeners = mutableListOf<(from: T, to: T) -> Unit>()
17+
18+
var value: T by Delegates.observable(defaultValue) { _, from, to ->
19+
if (from == to) return@observable
20+
listeners.forEach { it(from, to) }
21+
}
22+
23+
val isVisible get() = visibility()
24+
val isModified get() = value != defaultValue
25+
26+
operator fun getValue(thisRef: Any?, property: KProperty<*>) = value
27+
open operator fun setValue(thisRef: Any?, property: KProperty<*>, valueIn: T) {
28+
value = valueIn
29+
}
30+
31+
override fun toJson(): JsonElement =
32+
gson.toJsonTree(value)
33+
34+
override fun loadFromJson(serialized: JsonElement) {
35+
value = gson.fromJson(serialized, value::class.java)
36+
}
37+
38+
fun listener(block: SafeContext.(from: T, to: T) -> Unit) {
39+
listeners.add { from, to ->
40+
runSafe {
41+
block(from, to)
42+
}
43+
}
44+
}
45+
46+
fun unsafeListener(block: (from: T, to: T) -> Unit) {
47+
listeners.add(block)
48+
}
49+
50+
fun reset() {
51+
value = defaultValue
52+
}
53+
}
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
package com.lambda.config
2+
3+
import com.google.gson.JsonElement
4+
import com.google.gson.JsonObject
5+
import com.lambda.Lambda.LOG
6+
import com.lambda.config.settings.StringSetting
7+
import com.lambda.config.settings.collections.ListSetting
8+
import com.lambda.config.settings.collections.MapSetting
9+
import com.lambda.config.settings.collections.SetSetting
10+
import com.lambda.config.settings.comparable.BooleanSetting
11+
import com.lambda.config.settings.comparable.EnumSetting
12+
import com.lambda.config.settings.complex.BlockPosSetting
13+
import com.lambda.config.settings.complex.BlockSetting
14+
import com.lambda.config.settings.complex.KeyBindSetting
15+
import com.lambda.config.settings.numeric.*
16+
import com.lambda.util.KeyCode
17+
import com.lambda.util.Nameable
18+
import net.minecraft.block.Block
19+
import net.minecraft.util.math.BlockPos
20+
21+
/**
22+
* Holds a set of [AbstractSetting]s that are associated with the [name] of the [Configurable].
23+
*/
24+
abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
25+
val settings = mutableSetOf<AbstractSetting<*>>()
26+
27+
init {
28+
configuration.configurables.add(this) // ToDo: Find non-leaking solution
29+
}
30+
31+
override fun toJson() =
32+
JsonObject().apply {
33+
settings.forEach { setting ->
34+
add(setting.name, setting.toJson())
35+
}
36+
}
37+
38+
override fun loadFromJson(serialized: JsonElement) {
39+
serialized.asJsonObject.entrySet().forEach { (name, value) ->
40+
settings.find {
41+
it.name == name
42+
}?.loadFromJson(value)
43+
?: LOG.warn("No saved setting found for $name with $value in ${this::class.simpleName}")
44+
}
45+
}
46+
47+
fun setting(
48+
name: String,
49+
defaultValue: Boolean,
50+
visibility: () -> Boolean = { true },
51+
description: String = "",
52+
) = BooleanSetting(name, defaultValue, visibility, description).also {
53+
settings.add(it)
54+
}
55+
56+
inline fun <reified T : Enum<T>> setting(
57+
name: String,
58+
defaultValue: T,
59+
noinline visibility: () -> Boolean = { true },
60+
description: String = "",
61+
) = EnumSetting(name, defaultValue, visibility, description).also {
62+
settings.add(it)
63+
}
64+
65+
fun setting(
66+
name: String,
67+
defaultValue: String,
68+
visibility: () -> Boolean = { true },
69+
description: String = "",
70+
) = StringSetting(name, defaultValue, visibility, description).also {
71+
settings.add(it)
72+
}
73+
74+
inline fun <reified T : Any> setting(
75+
name: String,
76+
defaultValue: List<T>,
77+
noinline visibility: () -> Boolean = { true },
78+
description: String = "",
79+
) = ListSetting(name, defaultValue, visibility, description).also {
80+
settings.add(it)
81+
}
82+
83+
inline fun <reified K : Any, V : Any> setting(
84+
name: String,
85+
defaultValue: Map<K, V>,
86+
noinline visibility: () -> Boolean = { true },
87+
description: String = "",
88+
) = MapSetting(name, defaultValue, visibility, description).also {
89+
settings.add(it)
90+
}
91+
92+
inline fun <reified T : Any> setting(
93+
name: String,
94+
defaultValue: Set<T>,
95+
noinline visibility: () -> Boolean = { true },
96+
description: String = "",
97+
) = SetSetting(name, defaultValue, visibility, description).also {
98+
settings.add(it)
99+
}
100+
101+
fun setting(
102+
name: String,
103+
defaultValue: Byte,
104+
range: ClosedRange<Byte>,
105+
step: Byte = 1,
106+
visibility: () -> Boolean = { true },
107+
description: String = "",
108+
) = ByteSetting(name, defaultValue, range, step, visibility, description).also {
109+
settings.add(it)
110+
}
111+
112+
fun setting(
113+
name: String,
114+
defaultValue: Double,
115+
range: ClosedRange<Double>,
116+
step: Double = 1.0,
117+
visibility: () -> Boolean = { true },
118+
description: String = "",
119+
) = DoubleSetting(name, defaultValue, range, step, visibility, description).also {
120+
settings.add(it)
121+
}
122+
123+
fun setting(
124+
name: String,
125+
defaultValue: Float,
126+
range: ClosedRange<Float>,
127+
step: Float = 1f,
128+
visibility: () -> Boolean = { true },
129+
description: String = "",
130+
) = FloatSetting(name, defaultValue, range, step, visibility, description).also {
131+
settings.add(it)
132+
}
133+
134+
fun setting(
135+
name: String,
136+
defaultValue: Int,
137+
range: ClosedRange<Int>,
138+
step: Int = 1,
139+
visibility: () -> Boolean = { true },
140+
description: String = "",
141+
) = IntegerSetting(name, defaultValue, range, step, visibility, description).also {
142+
settings.add(it)
143+
}
144+
145+
fun setting(
146+
name: String,
147+
defaultValue: Long,
148+
range: ClosedRange<Long>,
149+
step: Long = 1,
150+
visibility: () -> Boolean = { true },
151+
description: String = "",
152+
) = LongSetting(name, defaultValue, range, step, visibility, description).also {
153+
settings.add(it)
154+
}
155+
156+
fun setting(
157+
name: String,
158+
defaultValue: Short,
159+
range: ClosedRange<Short>,
160+
step: Short = 1,
161+
visibility: () -> Boolean = { true },
162+
description: String = "",
163+
) = ShortSetting(name, defaultValue, range, step, visibility, description).also {
164+
settings.add(it)
165+
}
166+
167+
fun setting(
168+
name: String,
169+
defaultValue: KeyCode,
170+
visibility: () -> Boolean = { true },
171+
description: String = "",
172+
) = KeyBindSetting(name, defaultValue, visibility, description).also {
173+
settings.add(it)
174+
}
175+
176+
fun setting(
177+
name: String,
178+
defaultValue: BlockPos,
179+
visibility: () -> Boolean = { true },
180+
description: String = "",
181+
) = BlockPosSetting(name, defaultValue, visibility, description).also {
182+
settings.add(it)
183+
}
184+
185+
fun setting(
186+
name: String,
187+
defaultValue: Block,
188+
visibility: () -> Boolean = { true },
189+
description: String = "",
190+
) = BlockSetting(name, defaultValue, visibility, description).also {
191+
settings.add(it)
192+
}
193+
}

0 commit comments

Comments
 (0)