@@ -31,6 +31,7 @@ import com.lambda.command.LambdaCommand
3131import com.lambda.config.configurations.FriendConfig
3232import com.lambda.friend.FriendManager
3333import com.lambda.network.mojang.getProfile
34+ import com.lambda.threading.runIO
3435import com.lambda.util.Communication.info
3536import com.lambda.util.extension.CommandBuilder
3637import com.lambda.util.text.ClickEvents
@@ -39,42 +40,48 @@ import com.lambda.util.text.literal
3940import com.lambda.util.text.styled
4041import kotlinx.coroutines.runBlocking
4142import java.awt.Color
43+ import java.util.UUID
4244
4345object FriendCommand : LambdaCommand(
4446 name = " friends" ,
45- usage = " friends <add <name> | add-uuid <uuid> | remove <name>>" ,
47+ usage = " friends <add <name> | add-uuid <uuid> | remove <name> | remove-uuid <uuid> >" ,
4648 description = " Add or remove a friend"
4749) {
4850 override fun CommandBuilder.create () {
4951 execute {
50- info(
51- buildText {
52- if (FriendManager .friends.isEmpty()) {
53- literal(" You have no friends yet. Go make some! :3\n " )
54- } else {
55- literal(" Your friends (${FriendManager .friends.size} ):\n " )
56-
57- FriendManager .friends.forEachIndexed { index, gameProfile ->
58- literal(" ${index + 1 } . ${gameProfile.name} " )
59- styled(
60- color = Color .RED ,
61- clickEvent = ClickEvents .suggestCommand(" ;friends remove ${gameProfile.name} " )
62- ) {
63- literal(" x\n " )
52+ runIO {
53+ info(
54+ buildText {
55+ if (FriendManager .friends.isEmpty()) {
56+ literal(" You have no friends yet. Go make some! :3\n " )
57+ } else {
58+ literal(" Your friends (${FriendManager .friends.size} ):\n " )
59+
60+ FriendManager .friends.forEachIndexed { index, uuid ->
61+ val profile = FriendManager .latestGameProfile(uuid)
62+ val displayName = profile?.name ? : uuid.toString()
63+
64+ literal(" ${index + 1 } . $displayName " )
65+ styled(
66+ color = Color .RED ,
67+ clickEvent = ClickEvents .suggestCommand(" ;friends remove $displayName " )
68+ ) {
69+ literal(" x\n " )
70+ }
6471 }
6572 }
66- }
6773
68- literal(" \n " )
69- styled(
70- color = Color .CYAN ,
71- underlined = true ,
72- clickEvent = ClickEvents .openFile(FriendConfig .primary.path),
73- ) {
74- literal(" Click to open your friends list as a file" )
74+ literal(" \n " )
75+ styled(
76+ color = Color .CYAN ,
77+ underlined = true ,
78+ clickEvent = ClickEvents .openFile(FriendConfig .primary.path),
79+ ) {
80+ literal(" Click to open your friends list as a file" )
81+ }
7582 }
76- }
77- )
83+ )
84+ }
7885 }
7986
8087 required(literal(" add" )) {
@@ -92,19 +99,15 @@ object FriendCommand : LambdaCommand(
9299 executeWithResult {
93100 val name = player().value()
94101
95- if (FriendManager .isFriend(name))
96- return @executeWithResult failure(" This player is already in your friend list" )
102+ runBlocking {
103+ val profile = FriendManager .latestGameProfile(name)
104+ ? : return @runBlocking failure(" Could not find the player" )
97105
98- if (mc.gameProfile.name == name )
99- return @executeWithResult failure(" You can't befriend yourself" )
106+ if (mc.gameProfile.id == profile.id )
107+ return @runBlocking failure(" You can't befriend yourself" )
100108
101- runBlocking {
102- val profile = mc.networkHandler
103- ?.playerList
104- ?.map { it.profile }
105- ?.firstOrNull { it.name == name }
106- ? : getProfile(name)
107- .getOrElse { return @runBlocking failure(" Could not find the player" ) }
109+ if (FriendManager .isFriend(profile.id))
110+ return @runBlocking failure(" This player is already in your friend list" )
108111
109112 FriendManager .befriend(profile)
110113
@@ -130,23 +133,23 @@ object FriendCommand : LambdaCommand(
130133 executeWithResult {
131134 val uuid = player().value()
132135
133- if (FriendManager .isFriend(uuid))
134- return @executeWithResult failure(" This player is already in your friend list" )
135-
136136 if (mc.gameProfile.id == uuid)
137137 return @executeWithResult failure(" You can't befriend yourself" )
138138
139- runBlocking {
140- val profile = mc.networkHandler
141- ?.playerList
142- ?.map { it.profile }
143- ?.firstOrNull { it.id == uuid }
144- ? : getProfile(uuid)
145- .getOrElse { return @runBlocking failure(" Could not find the player" ) }
139+ if (FriendManager .isFriend(uuid))
140+ return @executeWithResult failure(" This player is already in your friend list" )
146141
147- FriendManager .befriend(profile)
142+ runBlocking {
143+ val profile = FriendManager .latestGameProfile(uuid)
144+
145+ if (profile != null ) {
146+ FriendManager .befriend(profile)
147+ info(FriendManager .befriendedText(profile.name))
148+ } else {
149+ FriendManager .befriend(uuid)
150+ info(FriendManager .befriendedText(uuid.toString()))
151+ }
148152
149- info(FriendManager .befriendedText(profile.name))
150153 success()
151154 }
152155 }
@@ -156,20 +159,46 @@ object FriendCommand : LambdaCommand(
156159 required(literal(" remove" )) {
157160 required(string(" player name" )) { player ->
158161 suggests { _, builder ->
159- FriendManager .friends.map { it.name }
162+ FriendManager .friends
163+ .map { FriendManager .friendDisplayName(it) }
160164 .forEach { builder.suggest(it) }
161165
162166 builder.buildFuture()
163167 }
164168
165169 executeWithResult {
166170 val name = player().value()
167- val profile = FriendManager .gameProfile(name)
168- ? : return @executeWithResult failure(" This player is not in your friend list" )
169171
170- FriendManager .unfriend(profile)
172+ runBlocking {
173+ val uuid = FriendManager .gameProfile(name)?.id
174+ ? : getProfile(name).getOrNull()?.id
175+ ? : runCatching { UUID .fromString(name) }.getOrNull()
176+ ? : return @runBlocking failure(" Could not resolve the player name" )
177+
178+ if (! FriendManager .isFriend(uuid))
179+ return @runBlocking failure(" This player is not in your friend list" )
180+
181+ FriendManager .unfriend(uuid)
182+
183+ info(FriendManager .unfriendedText(name))
184+ success()
185+ }
186+ }
187+ }
188+ }
189+
190+ required(literal(" remove-uuid" )) {
191+ required(uuid(" player uuid" )) { player ->
192+ executeWithResult {
193+ val uuid = player().value()
194+
195+ if (! FriendManager .isFriend(uuid))
196+ return @executeWithResult failure(" This player is not in your friend list" )
197+
198+ val displayName = FriendManager .friendDisplayName(uuid)
199+ FriendManager .unfriend(uuid)
171200
172- info(FriendManager .unfriendedText(name ))
201+ info(FriendManager .unfriendedText(displayName ))
173202 success()
174203 }
175204 }
0 commit comments