Skip to content

Commit 06c6a1b

Browse files
committed
Merge branch 'master' into feature/packetmine-rewrite
# Conflicts: # common/src/main/kotlin/com/lambda/module/modules/player/PacketMine.kt
2 parents e0a95b2 + 010f110 commit 06c6a1b

File tree

24 files changed

+373
-221
lines changed

24 files changed

+373
-221
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.lambda.brigadier.argument.value
2323
import com.lambda.brigadier.execute
2424
import com.lambda.brigadier.required
2525
import com.lambda.command.LambdaCommand
26+
import com.lambda.network.CapeManager
2627
import com.lambda.network.CapeManager.updateCape
2728
import com.lambda.network.NetworkManager
2829
import com.lambda.util.Communication.info
@@ -38,7 +39,7 @@ object CapeCommand : LambdaCommand(
3839
required(literal("set")) {
3940
required(string("id")) { id ->
4041
suggests { _, builder ->
41-
NetworkManager.capes
42+
CapeManager.capeList
4243
.forEach { builder.suggest(it) }
4344

4445
builder.buildFuture()

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

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ import com.lambda.brigadier.required
3030
import com.lambda.command.LambdaCommand
3131
import com.lambda.config.configurations.FriendConfig
3232
import com.lambda.friend.FriendManager
33+
import com.lambda.network.mojang.getProfile
3334
import com.lambda.util.Communication.info
3435
import com.lambda.util.extension.CommandBuilder
3536
import com.lambda.util.text.ClickEvents
3637
import com.lambda.util.text.buildText
3738
import com.lambda.util.text.literal
3839
import com.lambda.util.text.styled
40+
import kotlinx.coroutines.runBlocking
3941
import java.awt.Color
4042

4143
object FriendCommand : LambdaCommand(
@@ -82,19 +84,24 @@ object FriendCommand : LambdaCommand(
8284
}
8385

8486
executeWithResult {
85-
val name = player().value()
86-
val id = mc.networkHandler
87-
?.playerList
88-
?.firstOrNull {
89-
it.profile.name == name &&
90-
it.profile != mc.gameProfile
91-
} ?: return@executeWithResult failure("Could not find the player on the server")
92-
93-
return@executeWithResult if (FriendManager.befriend(id.profile)) {
94-
this@FriendCommand.info(FriendManager.befriendedText(id.profile.name))
95-
success()
96-
} else {
97-
failure("This player is already in your friend list")
87+
runBlocking {
88+
val name = player().value()
89+
90+
if (mc.gameProfile.name == name) return@runBlocking failure("You can't befriend yourself")
91+
92+
val profile = mc.networkHandler
93+
?.playerList
94+
?.map { it.profile }
95+
?.firstOrNull { it.name == name }
96+
?: getProfile(name)
97+
.getOrElse { return@runBlocking failure("Could not find the player") }
98+
99+
return@runBlocking if (FriendManager.befriend(profile)) {
100+
info(FriendManager.befriendedText(profile.name))
101+
success()
102+
} else {
103+
failure("This player is already in your friend list")
104+
}
98105
}
99106
}
100107
}
@@ -111,18 +118,24 @@ object FriendCommand : LambdaCommand(
111118
}
112119

113120
executeWithResult {
114-
val uuid = player().value()
115-
val id = mc.networkHandler
116-
?.playerList
117-
?.firstOrNull {
118-
it.profile.id == uuid && it.profile != mc.gameProfile
119-
} ?: return@executeWithResult failure("Could not find the player on the server")
120-
121-
return@executeWithResult if (FriendManager.befriend(id.profile)) {
122-
this@FriendCommand.info(FriendManager.befriendedText(id.profile.name))
123-
success()
124-
} else {
125-
failure("This player is already in your friend list")
121+
runBlocking {
122+
val uuid = player().value()
123+
124+
if (mc.gameProfile.id == uuid) return@runBlocking failure("You can't befriend yourself")
125+
126+
val profile = mc.networkHandler
127+
?.playerList
128+
?.map { it.profile }
129+
?.firstOrNull { it.id == uuid }
130+
?: getProfile(uuid)
131+
.getOrElse { return@runBlocking failure("Could not find the player") }
132+
133+
return@runBlocking if (FriendManager.befriend(profile)) {
134+
this@FriendCommand.info(FriendManager.befriendedText(profile.name))
135+
success()
136+
} else {
137+
failure("This player is already in your friend list")
138+
}
126139
}
127140
}
128141
}

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

Lines changed: 58 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
package com.lambda.config
1919

2020
import com.google.gson.JsonElement
21+
import com.google.gson.JsonIOException
2122
import com.google.gson.JsonObject
2223
import com.google.gson.JsonParser
24+
import com.google.gson.JsonSyntaxException
2325
import com.lambda.Lambda.LOG
2426
import com.lambda.Lambda.gson
2527
import com.lambda.config.configurations.ModuleConfig
@@ -28,10 +30,13 @@ import com.lambda.event.listener.UnsafeListener.Companion.listenUnsafe
2830
import com.lambda.threading.runIO
2931
import com.lambda.util.Communication.info
3032
import com.lambda.util.Communication.logError
33+
import com.lambda.util.FileUtils.createIfNotExists
34+
import com.lambda.util.FileUtils.ifExists
35+
import com.lambda.util.FileUtils.ifNotExists
3136
import com.lambda.util.StringUtils.capitalize
3237
import java.io.File
33-
import java.time.Duration
3438
import kotlin.concurrent.fixedRateTimer
39+
import kotlin.time.Duration.Companion.minutes
3540

3641

3742
/**
@@ -57,8 +62,7 @@ abstract class Configuration : Jsonable {
5762
get() = File("${primary.parent}/${primary.nameWithoutExtension}-backup.${primary.extension}")
5863

5964
init {
60-
listenUnsafe<ClientEvent.Startup> { tryLoad() }
61-
65+
tryLoad() // ToDo: This will do at the moment but i'd like to run this at the first first loadable stage
6266
listenUnsafe<ClientEvent.Shutdown>(Int.MIN_VALUE) { trySave() }
6367

6468
register()
@@ -69,11 +73,9 @@ abstract class Configuration : Jsonable {
6973
fixedRateTimer(
7074
daemon = true,
7175
name = "Scheduler-config-${configName}",
72-
initialDelay = Duration.ofMinutes(5).toMillis(),
73-
period = Duration.ofMinutes(5).toMillis()
74-
) {
75-
trySave()
76-
}
76+
initialDelay = 5.minutes.inWholeMilliseconds,
77+
period = 5.minutes.inWholeMilliseconds,
78+
) { trySave() }
7779

7880
configurations.add(this)
7981
}
@@ -87,71 +89,64 @@ abstract class Configuration : Jsonable {
8789

8890
override fun loadFromJson(serialized: JsonElement) {
8991
serialized.asJsonObject.entrySet().forEach { (name, value) ->
90-
configurables.find {
91-
it.name == name
92-
}?.loadFromJson(value)
92+
configurableByName(name)
93+
?.loadFromJson(value)
9394
?: LOG.warn("No matching setting found for saved setting $name with $value in ${configName.capitalize()} config")
9495
}
9596
}
9697

97-
private fun save() {
98-
with(primary) {
99-
if (exists()) copyTo(backup, true)
100-
101-
parentFile.mkdirs()
102-
writeText(gson.toJson(toJson()))
103-
}
98+
fun save() = runCatching {
99+
primary.createIfNotExists()
100+
.let {
101+
it.writeText(gson.toJson(toJson()))
102+
it.copyTo(backup, true)
103+
}
104104
}
105105

106-
private fun load(file: File) {
107-
if (!file.exists()) {
108-
LOG.warn("No configuration file found for ${configName.capitalize()}. Creating new file when saving.")
109-
return
110-
}
111-
112-
loadFromJson(JsonParser.parseReader(file.reader()).asJsonObject)
106+
/**
107+
* Loads the config from the [file]
108+
* Encapsulates [JsonIOException] and [JsonSyntaxException] in a runCatching block
109+
*/
110+
fun load(file: File) = runCatching {
111+
file.ifNotExists { LOG.warn("No configuration file found for ${configName.capitalize()}. Creating new file when saving.") }
112+
.ifExists { loadFromJson(JsonParser.parseReader(it.reader()).asJsonObject) }
113113
}
114114

115-
fun tryLoad() {
116-
runIO {
117-
runCatching { load(primary) }
118-
.onSuccess {
119-
val message = "${configName.capitalize()} config loaded."
120-
LOG.info(message)
121-
info(message)
122-
}
123-
.onFailure {
124-
var message: String
125-
runCatching { load(backup) }
126-
.onSuccess {
127-
message = "${configName.capitalize()} config loaded from backup"
128-
LOG.info(message)
129-
info(message)
130-
}
131-
.onFailure { error ->
132-
message =
133-
"Failed to load ${configName.capitalize()} config from backup, unrecoverable error"
134-
LOG.error(message, error)
135-
logError(message)
136-
}
137-
}
138-
}
115+
fun tryLoad() = runIO {
116+
load(primary)
117+
.onSuccess {
118+
val message = "${configName.capitalize()} config loaded."
119+
LOG.info(message)
120+
info(message)
121+
}
122+
.onFailure {
123+
var message: String
124+
runCatching { load(backup) }
125+
.onSuccess {
126+
message = "${configName.capitalize()} config loaded from backup"
127+
LOG.info(message)
128+
info(message)
129+
}
130+
.onFailure { error ->
131+
message = "Failed to load ${configName.capitalize()} config from backup, unrecoverable error"
132+
LOG.error(message, error)
133+
logError(message)
134+
}
135+
}
139136
}
140137

141-
fun trySave(logToChat: Boolean = false) {
142-
runIO {
143-
runCatching { save() }
144-
.onSuccess {
145-
val message = "Saved ${configName.capitalize()} config."
146-
LOG.info(message)
147-
if (logToChat) info(message)
148-
}
149-
.onFailure {
150-
val message = "Failed to save ${configName.capitalize()} config"
151-
LOG.error(message, it)
152-
logError(message)
153-
}
154-
}
138+
fun trySave(logToChat: Boolean = false) = runIO {
139+
save()
140+
.onSuccess {
141+
val message = "Saved ${configName.capitalize()} config."
142+
LOG.info(message)
143+
if (logToChat) info(message)
144+
}
145+
.onFailure {
146+
val message = "Failed to save ${configName.capitalize()} config"
147+
LOG.error(message, it)
148+
logError(message)
149+
}
155150
}
156151

157152
companion object {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ sealed class KeyboardEvent {
5656

5757
val hasShift = modifiers and GLFW_MOD_SHIFT != 0
5858
val hasControl = modifiers and GLFW_MOD_CONTROL != 0
59-
val hasClt = modifiers and GLFW_MOD_ALT != 0
59+
val hasAlt = modifiers and GLFW_MOD_ALT != 0
6060
val hasSuper = modifiers and GLFW_MOD_SUPER != 0
6161
val hasCapsLock = modifiers and GLFW_MOD_CAPS_LOCK != 0
6262
val hasNumLock = modifiers and GLFW_MOD_NUM_LOCK != 0

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ import com.lambda.event.callback.Cancellable
2121
import com.lambda.event.callback.ICancellable
2222
import com.lambda.util.Mouse
2323
import com.lambda.util.math.Vec2d
24+
import org.lwjgl.glfw.GLFW.GLFW_MOD_ALT
25+
import org.lwjgl.glfw.GLFW.GLFW_MOD_CAPS_LOCK
26+
import org.lwjgl.glfw.GLFW.GLFW_MOD_CONTROL
27+
import org.lwjgl.glfw.GLFW.GLFW_MOD_NUM_LOCK
28+
import org.lwjgl.glfw.GLFW.GLFW_MOD_SHIFT
29+
import org.lwjgl.glfw.GLFW.GLFW_MOD_SUPER
2430

2531
sealed class MouseEvent {
2632
/**
@@ -32,17 +38,32 @@ sealed class MouseEvent {
3238
* @property position The x and y position of the mouse on the screen
3339
*/
3440
data class Click(
35-
val button: Mouse.Button,
36-
val action: Mouse.Action,
41+
val button: Int,
42+
val action: Int,
3743
val modifiers: Int,
3844
val position: Vec2d,
3945
) : ICancellable by Cancellable() {
40-
constructor(button: Int, action: Int, modifiers: Int, position: Vec2d) : this(
41-
Mouse.Button.fromMouseCode(button),
42-
Mouse.Action.fromActionCode(action),
46+
constructor(button: Mouse.Button, action: Mouse.Action, modifiers: Int, position: Vec2d) : this(
47+
button.ordinal,
48+
action.ordinal,
4349
modifiers,
4450
position
4551
)
52+
53+
val isMainButton = button <= 2
54+
val isSideButton = button > 2
55+
val isLeftButton = button == 0
56+
val isRightButton = button == 1
57+
val isMiddleButton = button == 2
58+
59+
val hasShift = hasModifier(GLFW_MOD_SHIFT)
60+
val hasControl = hasModifier(GLFW_MOD_CONTROL)
61+
val hasAlt = hasModifier(GLFW_MOD_ALT)
62+
val hasSuper = hasModifier(GLFW_MOD_SUPER)
63+
val hasCapsLock = hasModifier(GLFW_MOD_CAPS_LOCK)
64+
val hasNumLock = hasModifier(GLFW_MOD_NUM_LOCK)
65+
66+
fun hasModifier(mod: Int) = modifiers and mod == mod
4667
}
4768

4869
/**

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ object FriendManager : Configurable(FriendConfig), Loadable {
5656
fun OtherClientPlayerEntity.befriend() = befriend(gameProfile)
5757
fun OtherClientPlayerEntity.unfriend() = unfriend(gameProfile)
5858

59-
override fun load(): String {
60-
// TODO: Because the settings are loaded after the property and the loadables, the friend list is empty at that point
61-
return "Loaded ${friends.size} friends"
62-
}
59+
override fun load() = "Loaded ${friends.size} friends"
6360

6461
fun befriendedText(name: String): Text = befriendedText(Text.of(name))
6562
fun befriendedText(name: Text) = buildText {

common/src/main/kotlin/com/lambda/module/modules/client/Discord.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.module.modules.client
1919

2020
import com.lambda.Lambda
21+
import com.lambda.Lambda.LOG
2122
import com.lambda.context.SafeContext
2223
import com.lambda.event.EventFlow
2324
import com.lambda.event.events.WorldEvent
@@ -80,8 +81,8 @@ object Discord : Module(
8081
val auth = rpc.applicationManager.authenticate()
8182

8283
linkDiscord(discordToken = auth.accessToken)
83-
.fold(onSuccess = { updateToken(it); discordAuth = auth },
84-
onFailure = { warn("Failed to link the discord account to the minecraft auth") })
84+
.onSuccess { updateToken(it); discordAuth = auth }
85+
.onFailure { LOG.error(it); warn("Failed to link your discord account") }
8586
}
8687

8788
private fun stop() {

0 commit comments

Comments
 (0)