Skip to content

Commit e16d907

Browse files
committed
Cleanup
1 parent 03bad18 commit e16d907

File tree

3 files changed

+36
-42
lines changed

3 files changed

+36
-42
lines changed

src/main/kotlin/com/lambda/client/command/commands/PlaytimeCommand.kt

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import com.lambda.client.commons.utils.ConnectionUtils
66
import com.lambda.client.commons.utils.grammar
77
import com.lambda.client.manager.managers.UUIDManager
88
import com.lambda.client.util.text.MessageSendHelper
9+
import kotlin.time.DurationUnit
10+
import kotlin.time.toDuration
911

1012
object PlaytimeCommand: ClientCommand(
1113
name = "playtime",
@@ -17,38 +19,26 @@ object PlaytimeCommand: ClientCommand(
1719
init {
1820
string("playerName") { playerName ->
1921
executeAsync("Check a player's playtime on 2b2t") {
20-
UUIDManager.getByName(playerName.value)?.let { profile ->
22+
UUIDManager.getByName(playerName.value)?.let outer@ { profile ->
2123
ConnectionUtils.requestRawJsonFrom("https://api.2b2t.vc/playtime?uuid=${profile.uuid}") {
2224
MessageSendHelper.sendChatMessage("Failed querying playtime data for player: ${it.message}")
2325
}?.let {
2426
if (it.isEmpty()) {
2527
MessageSendHelper.sendChatMessage("No data found for player: ${profile.name}")
26-
return@let
28+
return@outer
2729
}
2830
val jsonElement = parser.parse(it)
29-
val playtimeSeconds = jsonElement.asJsonObject["playtimeSeconds"].asInt
30-
MessageSendHelper.sendChatMessage("${profile.name} has played for ${formatDuration(playtimeSeconds.toLong())}")
31+
val playtimeSeconds = jsonElement.asJsonObject["playtimeSeconds"].asDouble
32+
MessageSendHelper.sendChatMessage("${profile.name} has played for ${
33+
playtimeSeconds.toDuration(DurationUnit.SECONDS)
34+
}")
3135
}
32-
} ?: run{
33-
MessageSendHelper.sendChatMessage("Failed to find player with name ${playerName.value}")
34-
}
35-
}
36-
}
37-
}
3836

39-
private fun formatDuration(durationInSeconds: Long): String {
40-
val secondsInMinute = 60L
41-
val secondsInHour = secondsInMinute * 60L
42-
val secondsInDay = secondsInHour * 24L
43-
val secondsInMonth = secondsInDay * 30L // assuming 30 days per month
37+
return@executeAsync
38+
}
4439

45-
val months = durationInSeconds / secondsInMonth
46-
val days = (durationInSeconds % secondsInMonth) / secondsInDay
47-
val hours = (durationInSeconds % secondsInDay) / secondsInHour
48-
return buildString {
49-
append(if(months > 0) "${grammar(months.toInt(), "month", "months")}, " else "")
50-
append(if(days > 0) "${grammar(days.toInt(), "day", "days")}, " else "")
51-
append(grammar(hours.toInt(), "hour", "hours"))
40+
MessageSendHelper.sendChatMessage("Failed to find player with name ${playerName.value}")
41+
}
5242
}
5343
}
5444
}

src/main/kotlin/com/lambda/client/command/commands/SeenCommand.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,25 @@ object SeenCommand : ClientCommand(
2020
init {
2121
string("playerName") { playerName ->
2222
executeAsync("Check when a player was last seen") {
23-
UUIDManager.getByName(playerName.value)?.let { profile ->
23+
UUIDManager.getByName(playerName.value)?.let outer@ { profile ->
2424
ConnectionUtils.requestRawJsonFrom("https://api.2b2t.vc/seen?uuid=${profile.uuid}") {
2525
MessageSendHelper.sendChatMessage("Failed querying seen data for player: ${it.message}")
2626
}?.let {
2727
if (it.isEmpty()) {
2828
MessageSendHelper.sendChatMessage("No data found for player: ${profile.name}")
29-
return@let
29+
return@outer
3030
}
3131
val jsonElement = parser.parse(it)
3232
val dateRaw = jsonElement.asJsonObject["time"].asString
3333
val parsedDate = ZonedDateTime.parse(dateRaw).withZoneSameInstant(ZoneId.systemDefault())
3434
val dateFormatter = DateTimeFormatter.ofLocalizedDateTime(java.time.format.FormatStyle.LONG)
3535
MessageSendHelper.sendChatMessage("${profile.name} was last seen on ${parsedDate.format(dateFormatter)}")
3636
}
37-
} ?: run {
38-
MessageSendHelper.sendChatMessage("Failed to find player with name ${playerName.value}")
37+
38+
return@executeAsync
3939
}
40+
41+
MessageSendHelper.sendChatMessage("Failed to find player with name ${playerName.value}")
4042
}
4143
}
4244
}

src/main/kotlin/com/lambda/client/gui/hudgui/elements/misc/Queue2B2T.kt

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,28 @@ internal object Queue2B2T : LabelHud(
6868
if (NetworkManager.isOffline) {
6969
displayText.addLine("Cannot connect to api.2b2t.vc", primaryColor)
7070
displayText.add("Make sure your internet is working!", primaryColor)
71-
} else {
72-
if (showPriority) {
73-
displayText.add("Priority: ", primaryColor)
74-
displayText.add("${queueData.prio}", secondaryColor)
75-
}
71+
return
72+
}
7673

77-
if (showRegular) {
78-
displayText.add("Regular: ", primaryColor)
79-
displayText.add("${queueData.regular}", secondaryColor)
80-
}
81-
if (showUpdatedTime) {
82-
displayText.addLine("", primaryColor)
83-
displayText.add("Last updated $lastUpdate ago", primaryColor)
84-
}
74+
if (showPriority) {
75+
displayText.add("Priority: ", primaryColor)
76+
displayText.add("${queueData.prio}", secondaryColor)
77+
}
78+
79+
if (showRegular) {
80+
displayText.add("Regular: ", primaryColor)
81+
displayText.add("${queueData.regular}", secondaryColor)
82+
}
83+
if (showUpdatedTime) {
84+
displayText.addLine("", primaryColor)
85+
displayText.add("Last updated $lastUpdate ago", primaryColor)
8586
}
8687
}
8788

8889
private fun sendWarning() {
8990
MessageSendHelper.sendWarningMessage(
9091
"This module uses an external API, api.2b2t.vc, which is operated by rfresh#2222." +
91-
"If you do not trust this external API / have not verified the safety yourself, disable this HUD component."
92+
" If you do not trust this external API / have not verified the safety yourself, disable this HUD component."
9293
)
9394
hasShownWarning.value = true
9495
}
@@ -101,9 +102,10 @@ internal object Queue2B2T : LabelHud(
101102
}?.let {
102103
gson.fromJson(it, QueueData::class.java)?.let { data ->
103104
queueData = data
104-
} ?: run {
105-
LambdaMod.LOG.error("No queue data received. Is 2b2t down?")
105+
return@runCatching
106106
}
107+
108+
LambdaMod.LOG.error("No queue data received. Is 2b2t down?")
107109
}
108110
}
109111
}

0 commit comments

Comments
 (0)