@@ -25,9 +25,11 @@ import com.mojang.brigadier.builder.RequiredArgumentBuilder
2525import com.mojang.brigadier.context.CommandContext
2626import kotlinx.coroutines.launch
2727import net.chariskar.breakthemod.client.api.Fetch
28+ import net.chariskar.breakthemod.client.api.Fetch.Items
2829import net.chariskar.breakthemod.client.objects.Resident
2930import net.chariskar.breakthemod.client.objects.StaffList
3031import net.chariskar.breakthemod.client.utils.ServerUtils.getEnabled
32+ import net.chariskar.breakthemod.client.utils.serialization.SerializableUUID
3133import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
3234import net.minecraft.text.MutableText
3335import net.minecraft.text.Style
@@ -52,107 +54,69 @@ class onlineStaff : command() {
5254 if (! getEnabled()) {return @Command 0 }
5355 val arg: String = context.getArgument(" api" , String ::class .java)
5456
55- if (arg == " api" ) return @Command execAPI(context)
56- return @Command execNormal(context)
57+ return @Command exec(context, arg == " api" )
5758 }))
5859 .executes(Command { context: CommandContext <FabricClientCommandSource > ->
5960 if (! getEnabled()) {return @Command 0 }
6061
61- return @Command execNormal (context)
62+ return @Command exec (context, null )
6263 })
6364 )
6465 }
6566
66- fun execNormal (ctx : CommandContext <FabricClientCommandSource >): Int {
67- scope.launch {
68- val staff: StaffList = Fetch .getRequest<StaffList >(Fetch .Items .STAFF .url)!!
69- val uuids: List <UUID > = staff.allStaff().map { uid-> uid.toUUID() }
7067
71- val onlinePlayers: List <UUID > = client.networkHandler!! .playerList.stream().map { pl -> pl.profile.id }.toList()
72- val onlineStaff: MutableList <UUID > = mutableListOf ()
73- onlinePlayers.forEach { pl ->
74- if (pl in uuids) onlineStaff.add(pl)
75- }
76- val staffNames: List <String > = onlineStaff.mapNotNull { uuid ->
77- client.networkHandler!! .playerList.firstOrNull {
78- it.profile.id == uuid
79- }?.profile?.name
80- }
81- var onlineStaffText: MutableText = Text .empty()
68+ suspend fun onlineStaff (api : Boolean ): Text {
69+ val staff: List <SerializableUUID >? = fetch.getRequest<StaffList >(Items .STAFF .url)?.allStaff()
8270
83- for (i in 0 .. < staffNames.size) {
84- onlineStaffText = onlineStaffText.append(
85- Text .literal(staffNames.get(i)).setStyle(Style .EMPTY .withColor(Formatting .AQUA ))
86- )
71+ if (staff.isNullOrEmpty()) return Text .literal(" Received invalid staff list." ).setStyle(Style .EMPTY .withColor(Formatting .RED ))
8772
88- if (i < staffNames.size - 1 ) {
89- onlineStaffText = onlineStaffText.append(
90- Text .literal(" , " ).setStyle(Style .EMPTY .withColor(Formatting .WHITE ))
91- )
92- }
93- }
73+ var onlineStaffText: MutableText = Text .empty()
74+ var message: MutableText
9475
95- var message: Text
96- if (staffNames.isNotEmpty()) {
97- message = Text .literal(" " )
98- .append(onlineStaffText)
99- .append(Text .literal(" [" ).setStyle(Style .EMPTY .withColor(Formatting .GRAY )))
100- .append(
101- Text .literal(java.lang.String .valueOf(staffNames.size))
102- .setStyle(Style .EMPTY .withColor(Formatting .WHITE ))
103- )
104- .append(Text .literal(" ]" ).setStyle(Style .EMPTY .withColor(Formatting .GRAY )))
105- } else {
106- message = Text .empty()
107- .append(" No online staff" ).setStyle(Style .EMPTY .withColor(Formatting .AQUA ))
108- }
109- sendMessage(message)
110- }
111-
112- return 0
113- }
114-
115- fun execAPI (ctx : CommandContext <FabricClientCommandSource >): Int {
116- scope.launch {
117- val staff: StaffList = Fetch .getRequest<StaffList >(Fetch .Items .STAFF .url)!!
118-
119- val staffNames: List <String > = fetch.getObjects<Resident >(Fetch .Items .PLAYER , staff.allStaff().map { v-> v.toUUID() }.toString() )!!
76+ val staffNames: List <String > = if (api) {
77+ fetch.getObjects<Resident >(Items .PLAYER , staff.map { v-> v.toUUID() }.toString() )!!
12078 .filter { r -> r.status!! .isOnline == true }
12179 .map { r -> r.name }
80+ } else {
81+ staff.mapNotNull { uuid ->
82+ client.networkHandler!! .playerList.firstOrNull {
83+ it.profile.id == uuid.toUUID()
84+ }?.profile?.name
85+ }
86+ }
12287
12388
124- var onlineStaffText: MutableText = Text .empty()
89+ for (i in staffNames.indices) {
90+ onlineStaffText = onlineStaffText.append(
91+ Text .literal(staffNames[i]).setStyle(Style .EMPTY .withColor(Formatting .AQUA ))
92+ )
12593
126- for (i in 0 .. < staffNames.size) {
94+ if (i < staffNames.size - 1 ) {
12795 onlineStaffText = onlineStaffText.append(
128- Text .literal(staffNames[i] ).setStyle(Style .EMPTY .withColor(Formatting .AQUA ))
96+ Text .literal(" , " ).setStyle(Style .EMPTY .withColor(Formatting .WHITE ))
12997 )
130-
131- if (i < staffNames.size - 1 ) {
132- onlineStaffText = onlineStaffText.append(
133- Text .literal(" , " ).setStyle(Style .EMPTY .withColor(Formatting .WHITE ))
134- )
135- }
13698 }
137- var message: Text
138-
139- if (staffNames.isNotEmpty()) {
140- message = Text .literal(" " )
141- .append(onlineStaffText)
142- .append(Text .literal(" [" ).setStyle(Style .EMPTY .withColor(Formatting .GRAY )))
143- .append(
144- Text .literal(java.lang.String .valueOf(staffNames.size))
145- .setStyle(Style .EMPTY .withColor(Formatting .WHITE ))
146- )
147- .append(Text .literal(" ]" ).setStyle(Style .EMPTY .withColor(Formatting .GRAY )))
148- } else {
149- message = Text .empty()
150- .append(" No online staff" ).setStyle(Style .EMPTY .withColor(Formatting .AQUA ))
151- }
152- sendMessage( message)
15399 }
154100
101+ if (staffNames.isNotEmpty()) {
102+ message = Text .literal(" " )
103+ .append(onlineStaffText)
104+ .append(Text .literal(" [" ).setStyle(Style .EMPTY .withColor(Formatting .GRAY )))
105+ .append(
106+ Text .literal(java.lang.String .valueOf(staffNames.size))
107+ .setStyle(Style .EMPTY .withColor(Formatting .WHITE ))
108+ )
109+ .append(Text .literal(" ]" ).setStyle(Style .EMPTY .withColor(Formatting .GRAY )))
110+ } else {
111+ message = Text .empty()
112+ .append(" No online staff" ).setStyle(Style .EMPTY .withColor(Formatting .AQUA ))
113+ }
114+
115+ return message
116+ }
155117
118+ fun exec (ctx : CommandContext <FabricClientCommandSource >, api : Boolean? ): Int {
119+ scope.launch { sendMessage(onlineStaff(api ? : false )) }
156120 return 0
157121 }
158122}
0 commit comments