Skip to content

Commit 3b7c810

Browse files
committed
Better friend command handling
1 parent 8683549 commit 3b7c810

File tree

4 files changed

+92
-51
lines changed

4 files changed

+92
-51
lines changed

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

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.lambda.brigadier.CommandResult.Companion.failure
2222
import com.lambda.brigadier.CommandResult.Companion.success
2323
import com.lambda.brigadier.argument.literal
2424
import com.lambda.brigadier.argument.string
25+
import com.lambda.brigadier.argument.uuid
2526
import com.lambda.brigadier.argument.value
2627
import com.lambda.brigadier.execute
2728
import com.lambda.brigadier.executeWithResult
@@ -33,26 +34,36 @@ import com.lambda.util.Communication.info
3334
import com.lambda.util.extension.CommandBuilder
3435
import com.lambda.util.text.ClickEvents
3536
import com.lambda.util.text.buildText
36-
import com.lambda.util.text.color
3737
import com.lambda.util.text.literal
3838
import com.lambda.util.text.styled
3939
import java.awt.Color
4040

4141
object FriendCommand : LambdaCommand(
42-
name = "friend",
43-
usage = "friend <add | remove> <name>",
42+
name = "friends",
43+
usage = "friends <add | remove> <name | uuid>",
4444
description = "Add or remove a friend"
4545
) {
4646
override fun CommandBuilder.create() {
4747
execute {
4848
this@FriendCommand.info(
4949
buildText {
50+
if (FriendManager.friends.isEmpty()) {
51+
literal("You have no friends yet. Go make some! :3\n")
52+
} else {
53+
literal("Your friends (${FriendManager.friends.size}):\n")
54+
55+
FriendManager.friends.forEachIndexed { index, gameProfile ->
56+
literal(" ${index + 1}. ${gameProfile.name}\n")
57+
}
58+
}
59+
60+
literal("\n")
5061
styled(
5162
color = Color.CYAN,
5263
underlined = true,
53-
clickEvent = ClickEvents.openFile(FriendConfig.primary.absolutePath),
64+
clickEvent = ClickEvents.openFile(FriendConfig.primary.path),
5465
) {
55-
literal("Click to open your friend list")
66+
literal("Click to open your friends list as a file")
5667
}
5768
}
5869
)
@@ -72,29 +83,47 @@ object FriendCommand : LambdaCommand(
7283

7384
executeWithResult {
7485
val name = player().value()
75-
if (FriendManager.contains(name))
76-
return@executeWithResult failure("This player is already in your friend list")
77-
7886
val id = mc.networkHandler
7987
?.playerList
8088
?.firstOrNull {
8189
it.profile.name == name &&
8290
it.profile != mc.gameProfile
8391
} ?: return@executeWithResult failure("Could not find the player on the server")
8492

85-
FriendManager.add(id.profile)
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")
98+
}
99+
}
100+
}
86101

87-
this@FriendCommand.info(buildText {
88-
color(Color.GREEN) {
89-
literal("Added ")
90-
color(Color.CYAN) {
91-
literal(name)
92-
color(Color.WHITE) { literal(" to your friend list") }
93-
}
94-
}
95-
})
102+
required(uuid("player uuid")) { player ->
103+
suggests { _, builder ->
104+
mc.networkHandler
105+
?.playerList
106+
?.filter { it.profile != mc.gameProfile }
107+
?.map { it.profile.id }
108+
?.forEach { builder.suggest(it.toString()) }
96109

97-
return@executeWithResult success()
110+
builder.buildFuture()
111+
}
112+
113+
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")
126+
}
98127
}
99128
}
100129
}
@@ -113,19 +142,12 @@ object FriendCommand : LambdaCommand(
113142
val profile = FriendManager.gameProfile(name)
114143
?: return@executeWithResult failure("This player is not in your friend list")
115144

116-
FriendManager.remove(profile)
117-
118-
this@FriendCommand.info(buildText {
119-
color(Color.RED) {
120-
literal("Removed ")
121-
color(Color.CYAN) {
122-
literal(profile.name)
123-
color(Color.WHITE) { literal(" from your friend list") }
124-
}
125-
}
126-
})
127-
128-
return@executeWithResult success()
145+
return@executeWithResult if (FriendManager.unfriend(profile)) {
146+
this@FriendCommand.info(FriendManager.unfriendedText(name))
147+
success()
148+
} else {
149+
failure("This player is not in your friend list")
150+
}
129151
}
130152
}
131153
}

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

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,61 @@ package com.lambda.friend
2020
import com.lambda.config.Configurable
2121
import com.lambda.config.configurations.FriendConfig
2222
import com.lambda.core.Loadable
23+
import com.lambda.util.text.buildText
24+
import com.lambda.util.text.literal
25+
import com.lambda.util.text.text
2326
import com.mojang.authlib.GameProfile
2427
import net.minecraft.client.network.OtherClientPlayerEntity
28+
import net.minecraft.text.Text
29+
import java.awt.Color
2530
import java.util.*
2631

32+
// ToDo:
33+
// - Allow adding of offline players by name or uuid.
34+
// - Should store the data until the player was seen.
35+
// Either no UUID but with name or no name but with uuid or both.
36+
// -> Should update the record if the player was seen again.
37+
// - Handle player changing names.
38+
// - Improve save file structure.
2739
object FriendManager : Configurable(FriendConfig), Loadable {
2840
override val name = "friends"
2941
val friends by setting("friends", setOf<GameProfile>())
3042

31-
fun add(profile: GameProfile) = friends.add(profile)
43+
fun befriend(profile: GameProfile) = friends.add(profile)
3244

33-
fun remove(profile: GameProfile): Boolean = friends.remove(profile)
45+
fun unfriend(profile: GameProfile): Boolean = friends.remove(profile)
3446

3547
fun gameProfile(name: String) = friends.firstOrNull { it.name == name }
3648
fun gameProfile(uuid: UUID) = friends.firstOrNull { it.id == uuid }
3749

38-
fun contains(profile: GameProfile) = friends.contains(profile)
39-
fun contains(name: String) = friends.any { it.name == name }
40-
fun contains(uuid: UUID) = friends.any { it.id == uuid }
50+
fun isFriend(profile: GameProfile) = friends.contains(profile)
51+
fun isFriend(name: String) = friends.any { it.name == name }
52+
fun isFriend(uuid: UUID) = friends.any { it.id == uuid }
4153

4254
fun clear() = friends.clear()
4355

4456
val OtherClientPlayerEntity.isFriend: Boolean
45-
get() = contains(gameProfile)
57+
get() = isFriend(gameProfile)
4658

47-
fun OtherClientPlayerEntity.befriend() = add(gameProfile)
48-
fun OtherClientPlayerEntity.unfriend() = remove(gameProfile)
59+
fun OtherClientPlayerEntity.befriend() = befriend(gameProfile)
60+
fun OtherClientPlayerEntity.unfriend() = unfriend(gameProfile)
4961

5062
override fun load(): String {
5163
// TODO: Because the settings are loaded after the property and the loadables, the friend list is empty at that point
5264
return "Loaded ${friends.size} friends"
5365
}
66+
67+
fun befriendedText(name: String): Text = befriendedText(Text.of(name))
68+
fun befriendedText(name: Text) = buildText {
69+
literal(Color.GREEN, "Added ")
70+
text(name)
71+
literal(" to your friend list")
72+
}
73+
74+
fun unfriendedText(name: String): Text = unfriendedText(Text.of(name))
75+
fun unfriendedText(name: Text) = buildText {
76+
literal(Color.RED, "Removed ")
77+
text(name)
78+
literal(" from your friend list")
79+
}
5480
}

common/src/main/kotlin/com/lambda/module/modules/player/ClickFriend.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.lambda.module.modules.player
1919

2020
import com.lambda.event.events.MouseEvent
2121
import com.lambda.event.listener.SafeListener.Companion.listener
22+
import com.lambda.friend.FriendManager
2223
import com.lambda.friend.FriendManager.befriend
2324
import com.lambda.friend.FriendManager.isFriend
2425
import com.lambda.friend.FriendManager.unfriend
@@ -62,18 +63,10 @@ object ClickFriend : Module(
6263
if (modUnfriend.flagsPresent(it.modifiers) || !comboUnfriend) {
6364
when {
6465
target.isFriend && target.unfriend() -> {
65-
this@ClickFriend.info(buildText {
66-
literal(Color.RED, "Removed ")
67-
text(target.name)
68-
literal(Color.WHITE, " from your friend list")
69-
})
66+
this@ClickFriend.info(FriendManager.unfriendedText(target.name))
7067
}
7168
!target.isFriend && target.befriend() -> {
72-
this@ClickFriend.info(buildText {
73-
literal(Color.GREEN, "Added ")
74-
text(target.name)
75-
literal(Color.WHITE, " to your friend list")
76-
})
69+
this@ClickFriend.info(FriendManager.befriendedText(target.name))
7770
}
7871
}
7972
}

common/src/main/kotlin/com/lambda/util/text/TextDsl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fun TextBuilder.literal(value: String) {
120120
* @see StyleBuilder for action
121121
*/
122122
@TextDsl
123-
fun TextBuilder.literal(color: Color, value: String) {
123+
fun TextBuilder.literal(color: Color = Color.WHITE, value: String) {
124124
color(color) {
125125
literal(value)
126126
}

0 commit comments

Comments
 (0)