Skip to content

Commit e4789ae

Browse files
committed
Better logging
1 parent 511a237 commit e4789ae

File tree

9 files changed

+141
-88
lines changed

9 files changed

+141
-88
lines changed

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import com.google.gson.GsonBuilder
55
import com.lambda.config.serializer.BlockPosSerializer
66
import com.lambda.config.serializer.BlockSerializer
77
import com.lambda.config.serializer.ColorSerializer
8-
import com.lambda.util.Eager
98
import net.minecraft.block.Block
109
import net.minecraft.client.MinecraftClient
1110
import net.minecraft.util.math.BlockPos
1211
import org.apache.logging.log4j.LogManager
1312
import org.apache.logging.log4j.Logger
14-
import org.reflections.Reflections
1513
import java.awt.Color
1614

1715
object Lambda {
@@ -33,24 +31,4 @@ object Lambda {
3331
fun initialize() {
3432
Loader.initialize()
3533
}
36-
37-
/**
38-
* Initializes all objects annotated with @[Eager].
39-
*/
40-
private fun initializeEagerObjects() {
41-
val reflections = Reflections("com.lambda")
42-
43-
val eagerTypes = reflections.getTypesAnnotatedWith(Eager::class.java)
44-
val eagerSubTypes = eagerTypes.flatMap { reflections.getSubTypesOf(it) }
45-
(eagerTypes + eagerSubTypes).forEach { eagerType ->
46-
try {
47-
val instanceField = eagerType.getDeclaredField("INSTANCE")
48-
instanceField.isAccessible = true
49-
val instance = instanceField.get(null)
50-
println("Initialized ${instance::class.simpleName}")
51-
} catch (e: Exception) {
52-
println("Failed to initialize ${eagerType.simpleName}")
53-
}
54-
}
55-
}
5634
}

common/src/main/kotlin/com/lambda/command/CommandManager.kt

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import com.lambda.brigadier.register
77
import com.lambda.config.Configurable
88
import com.lambda.context.SafeContext
99
import com.lambda.threading.runSafe
10+
import com.lambda.util.Communication
11+
import com.lambda.util.Communication.logError
1012
import com.lambda.util.text.*
1113
import com.lambda.util.text.ClickEvents.suggestCommand
1214
import com.mojang.brigadier.CommandDispatcher
@@ -30,7 +32,6 @@ object CommandManager : Configurable(LambdaConfig), Loadable {
3032
private val commands = mutableSetOf<LambdaCommand>()
3133
private val dispatcher by lazy { CommandDispatcher<CommandSource>() }
3234
private const val ERROR_PADDING = 10
33-
private val errorColor = Color.RED
3435

3536
fun register(
3637
command: String,
@@ -55,11 +56,7 @@ object CommandManager : Configurable(LambdaConfig), Loadable {
5556
} catch (syntax: CommandSyntaxException) {
5657
createFeedback(syntax, reader)
5758
} catch (e: CommandException) {
58-
player.sendMessage(buildText {
59-
color(errorColor) {
60-
text(e.info)
61-
}
62-
})
59+
this@CommandManager.logError(e.info)
6360
}
6461
}
6562
}
@@ -90,11 +87,7 @@ object CommandManager : Configurable(LambdaConfig), Loadable {
9087
) {
9188
val debugMessage = syntax.message ?: return
9289

93-
player.sendMessage(buildText {
94-
color(errorColor) {
95-
literal(debugMessage)
96-
}
97-
})
90+
this@CommandManager.logError(debugMessage)
9891
if (syntax.input == null || syntax.cursor < 0) {
9992
return
10093
}
@@ -108,11 +101,11 @@ object CommandManager : Configurable(LambdaConfig), Loadable {
108101
literal(syntax.input.substring(max(0, (position - ERROR_PADDING)), position))
109102
}
110103
if (position < syntax.input.length) {
111-
styled(color = errorColor, underlined = true) {
104+
styled(color = Communication.LogLevel.ERROR.logoColor, underlined = true) {
112105
literal(syntax.input.substring(position))
113106
}
114107
}
115-
styled(color = errorColor, italic = true) {
108+
styled(color = Communication.LogLevel.ERROR.logoColor, italic = true) {
116109
translatable("command.context.here")
117110
}
118111
}
Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
11
package com.lambda.command
22

3-
import com.lambda.Lambda
4-
import com.lambda.context.SafeContext
53
import com.lambda.util.Nameable
6-
import com.lambda.util.text.*
7-
import net.minecraft.text.Text
8-
import java.util.*
94

10-
abstract class LambdaCommand : Nameable {
11-
fun SafeContext.sendSuccess(text: Text) {
12-
player.sendMessage(buildText {
13-
literal(" ")
14-
styled(Color.GREEN) {
15-
literal(Lambda.SYMBOL)
16-
}
17-
styled(Color.GREY) {
18-
literal(" ${name.capitalize()} Command ")
19-
}
20-
text(text)
21-
})
22-
}
23-
}
5+
abstract class LambdaCommand : Nameable

common/src/main/kotlin/com/lambda/command/commands/ModuleCommand.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import com.lambda.command.CommandManager.register
1414
import com.lambda.command.LambdaCommand
1515
import com.lambda.module.ModuleRegistry
1616
import com.lambda.threading.runSafe
17+
import com.lambda.util.Communication
18+
import com.lambda.util.Communication.info
19+
import com.lambda.util.Communication.warn
1720
import com.lambda.util.StringUtils
1821
import com.lambda.util.text.*
1922
import com.lambda.util.text.ClickEvents.suggestCommand
@@ -70,14 +73,23 @@ object ModuleCommand : LambdaCommand() {
7073
if (enable == null) {
7174
module.toggle()
7275
} else {
76+
if (enable().value() == module.isEnabled) {
77+
this@ModuleCommand.warn(buildText {
78+
styled(Color.GREY) {
79+
literal("$name already ")
80+
literal(if (module.isEnabled) "enabled" else "disabled")
81+
}
82+
})
83+
return@runSafe success()
84+
}
85+
7386
if (enable().value()) {
7487
module.enable()
7588
} else {
7689
module.disable()
7790
}
7891
}
79-
sendSuccess(buildText {
80-
literal("Module ")
92+
this@ModuleCommand.info(buildText {
8193
styled(Color.GREY) {
8294
literal("$name ")
8395
}
@@ -86,11 +98,9 @@ object ModuleCommand : LambdaCommand() {
8698
}
8799
})
88100
success()
89-
} ?: failure(buildText {
90-
styled(Color.RED) {
91-
literal("Failed to ${if (module.isEnabled) "enable" else "disable"} module $name")
92-
}
93-
})
101+
} ?: failure(Communication.LogLevel.ERROR.text(
102+
"Failed to ${if (module.isEnabled) "enable" else "disable"} module $name")
103+
)
94104
}
95105
}
96106
}

common/src/main/kotlin/com/lambda/event/events/PacketEvent.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import com.lambda.event.EventFlow
55
import com.lambda.event.cancellable.Cancellable
66
import com.lambda.event.cancellable.ICancellable
77
import com.lambda.event.events.PacketEvent.Receive
8-
import com.lambda.event.events.PacketEvent.Receive.Post
9-
import com.lambda.event.events.PacketEvent.Receive.Pre
108
import com.lambda.event.events.PacketEvent.Send
11-
import com.lambda.event.events.PacketEvent.Send.Post
12-
import com.lambda.event.events.PacketEvent.Send.Pre
139
import net.minecraft.network.packet.Packet
1410

1511
/**
@@ -19,7 +15,7 @@ import net.minecraft.network.packet.Packet
1915
* It has two subclasses: [Send] and [Receive],
2016
* which are triggered when a packet is sent and received, respectively.
2117
*
22-
* Each subclass has two further subclasses: [Pre] and [Post],
18+
* Each subclass has two further subclasses: `Pre` and `Post`,
2319
* which are triggered before and after the packet is sent or received.
2420
*
2521
* The [PacketEvent] class is designed to be extended by any class that needs to react to packet events.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ import com.lambda.util.Nameable
7676
* @property name The name of the module, displayed in-game.
7777
* @property description The description of the module,
7878
* shown on hover over the module button in the GUI and in commands.
79-
* @property tags The set of [ModuleTag]s associated with the module.
79+
* @property defaultTags The set of [ModuleTag]s associated with the module.
8080
* @property alwaysListening If true, the module's listeners will be triggered even if the module is not enabled.
8181
* @property isEnabledSetting The setting that determines if the module is enabled.
8282
* @property keybindSetting The setting that determines the keybind for the module.
@@ -87,13 +87,14 @@ import com.lambda.util.Nameable
8787
abstract class Module(
8888
override val name: String,
8989
val description: String = "",
90-
val tags: Set<ModuleTag> = setOf(),
90+
val defaultTags: Set<ModuleTag> = setOf(),
9191
private val alwaysListening: Boolean = false,
9292
enabledByDefault: Boolean = false,
9393
defaultKeybind: KeyCode = KeyCode.Unbound,
9494
) : Nameable, Muteable, Configurable(ModuleConfig) {
9595
private val isEnabledSetting = setting("Enabled", enabledByDefault, { false })
9696
private val keybindSetting = setting("Keybind", defaultKeybind)
97+
private val customTags = setting("Tags", defaultTags)
9798

9899
var isEnabled by isEnabledSetting
99100
override val isMuted: Boolean

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import com.lambda.event.events.TickEvent
55
import com.lambda.event.listener.SafeListener.Companion.listener
66
import com.lambda.module.Module
77
import com.lambda.module.tag.ModuleTag
8+
import com.lambda.util.Communication.info
9+
import com.lambda.util.Communication.toast
810
import com.lambda.util.KeyCode
911
import net.minecraft.util.math.BlockPos
1012
import java.awt.Color
1113

1214
object BoringModule : Module(
1315
name = "BoringModule",
1416
description = "This is a boring module",
15-
tags = setOf(ModuleTag.MISC, ModuleTag.COMBAT),
17+
defaultTags = setOf(ModuleTag.MISC, ModuleTag.COMBAT),
1618
defaultKeybind = KeyCode.Z
1719
) {
1820
private val superBoring by setting("Super Boring", false)
@@ -37,6 +39,7 @@ object BoringModule : Module(
3739
init {
3840
onEnable {
3941
LOG.info("I'm was enabled!")
42+
toast("I'm enabled!")
4043
}
4144

4245
onDisable {
@@ -48,7 +51,7 @@ object BoringModule : Module(
4851
}
4952

5053
listener<TickEvent.Pre> {
51-
LOG.info("I'm ${if (superBoring) "super boring ($boringValue)" else "boring"}! $isEnabled")
54+
this@BoringModule.info("I'm ${if (superBoring) "super boring ($boringValue)" else "boring"}!")
5255
}
5356
}
5457
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.lambda.util
2+
3+
import com.lambda.Lambda
4+
import com.lambda.Lambda.mc
5+
import com.lambda.command.LambdaCommand
6+
import com.lambda.module.Module
7+
import com.lambda.threading.runSafe
8+
import com.lambda.util.text.*
9+
import net.minecraft.client.toast.SystemToast
10+
import net.minecraft.text.Text
11+
12+
object Communication {
13+
fun Any.debug(message: String) = log(LogLevel.DEBUG.text(message), LogLevel.DEBUG)
14+
fun Any.debug(message: Text) = log(message, LogLevel.DEBUG)
15+
fun Any.info(message: String) = log(LogLevel.INFO.text(message), LogLevel.INFO)
16+
fun Any.info(message: Text) = log(message, LogLevel.INFO)
17+
fun Any.warn(message: String) = log(LogLevel.WARN.text(message), LogLevel.WARN)
18+
fun Any.warn(message: Text) = log(message, LogLevel.WARN)
19+
fun Any.logError(message: String) = logError(LogLevel.ERROR.text(message))
20+
fun Any.logError(message: Text) = log(message, LogLevel.ERROR)
21+
22+
fun Any.toast(message: String, logLevel: LogLevel = LogLevel.INFO) {
23+
toast(logLevel.text(message), logLevel)
24+
}
25+
26+
fun Any.toast(message: Text, logLevel: LogLevel = LogLevel.INFO) {
27+
runSafe {
28+
buildText {
29+
text(this@toast.source(logLevel, Color.YELLOW))
30+
}.let { title ->
31+
mc.toastManager.add(logLevel.toast(title, message))
32+
}
33+
}
34+
}
35+
36+
fun Any.logText(message: Text, logLevel: LogLevel = LogLevel.INFO) {
37+
runSafe {
38+
buildText {
39+
text(this@logText.source(logLevel))
40+
text(message)
41+
}
42+
}
43+
}
44+
45+
fun Any.log(message: Text, logLevel: LogLevel = LogLevel.INFO) {
46+
runSafe {
47+
buildText {
48+
text(this@log.source(logLevel))
49+
text(message)
50+
}.let { log ->
51+
player.sendMessage(log)
52+
}
53+
}
54+
}
55+
56+
private fun Any.source(logLevel: LogLevel, color: Color = Color.GREY) = buildText {
57+
text(logLevel.prefix())
58+
59+
if (this@source is LambdaCommand) {
60+
styled(color, italic = true) {
61+
literal("Command ")
62+
}
63+
}
64+
65+
if (this@source is Module) {
66+
styled(color, italic = true) {
67+
literal("Module ")
68+
}
69+
}
70+
71+
// ToDo: HUD elements
72+
73+
if (this@source is Nameable) {
74+
styled(color, italic = true, underlined = true) {
75+
literal(name.capitalize())
76+
}
77+
}
78+
79+
styled(color, italic = true) {
80+
literal(" \$ ")
81+
}
82+
}
83+
84+
private fun LogLevel.prefix() =
85+
buildText {
86+
literal(" ")
87+
styled(logoColor) {
88+
literal(Lambda.SYMBOL)
89+
}
90+
literal(" ")
91+
}
92+
93+
enum class LogLevel(val logoColor: Color, val messageColor: Color, val type: SystemToast.Type) {
94+
DEBUG(Color.WHITE, Color.WHITE, SystemToast.Type.WORLD_BACKUP),
95+
INFO(Color.GREEN, Color.WHITE, SystemToast.Type.NARRATOR_TOGGLE),
96+
WARN(Color.YELLOW, Color.YELLOW, SystemToast.Type.WORLD_ACCESS_FAILURE),
97+
ERROR(Color.RED, Color.YELLOW, SystemToast.Type.WORLD_ACCESS_FAILURE);
98+
99+
fun toast(title: Text, message: Text): SystemToast =
100+
SystemToast.create(mc, type, title, message)
101+
102+
fun text(message: String) = buildText {
103+
styled(messageColor) {
104+
literal(message)
105+
}
106+
}
107+
}
108+
}

common/src/main/kotlin/com/lambda/util/Eager.kt

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)