-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathMessageUtils.kt
More file actions
147 lines (133 loc) · 6.06 KB
/
MessageUtils.kt
File metadata and controls
147 lines (133 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.github.quiltservertools.ledger.utility
import com.github.quiltservertools.ledger.Ledger
import com.github.quiltservertools.ledger.actionutils.SearchResults
import com.github.quiltservertools.ledger.config.SearchSpec
import com.github.quiltservertools.ledger.database.DatabaseManager
import com.github.quiltservertools.ledger.network.Networking.hasNetworking
import com.github.quiltservertools.ledger.network.packet.action.ActionPacket
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.text.ClickEvent
import net.minecraft.text.HoverEvent
import net.minecraft.text.MutableText
import net.minecraft.text.Style
import net.minecraft.text.Text
import java.time.Duration
import java.time.Instant
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import kotlin.time.ExperimentalTime
import kotlin.time.toKotlinDuration
object MessageUtils {
@OptIn(ExperimentalTime::class)
suspend fun sendSearchResults(source: ServerCommandSource, results: SearchResults, header: Text) {
// If the player has a Ledger compatible client, we send results as action packets rather than as chat messages
if (source.hasPlayer() && source.playerOrThrow.hasNetworking()) {
for (n in results.page..results.pages) {
val networkResults = DatabaseManager.searchActions(results.searchParams, n)
networkResults.actions.forEach {
if (results.searchParams.chatMessage) {
actionType -> source.sendFeedback({ actionType.getMessage() }, false)
}
val packet = ActionPacket()
packet.populate(it)
ServerPlayNetworking.send(source.player, packet.channel, packet.buf)
}
}
return
}
source.sendFeedback({ header }, false)
results.actions.forEach { actionType ->
source.sendFeedback({ actionType.getMessage() }, false)
}
source.sendFeedback(
{
Text.translatable(
"text.ledger.footer.search",
Text.translatable("text.ledger.footer.page_backward").setStyle(TextColorPallet.primaryVariant)
.styled {
if (results.page > 1) {
it.withHoverEvent(
HoverEvent(
HoverEvent.Action.SHOW_TEXT,
Text.translatable("text.ledger.footer.page_backward.hover")
)
).withClickEvent(
ClickEvent(ClickEvent.Action.RUN_COMMAND, "/lg pg ${results.page - 1}")
)
} else {
Style.EMPTY
}
},
results.page.toString().literal().setStyle(TextColorPallet.primaryVariant),
results.pages.toString().literal().setStyle(TextColorPallet.primaryVariant),
Text.translatable("text.ledger.footer.page_forward").setStyle(TextColorPallet.primaryVariant)
.styled {
if (results.page < results.pages) {
it.withHoverEvent(
HoverEvent(
HoverEvent.Action.SHOW_TEXT,
Text.translatable("text.ledger.footer.page_forward.hover")
)
).withClickEvent(
ClickEvent(ClickEvent.Action.RUN_COMMAND, "/lg pg ${results.page + 1}")
)
} else {
Style.EMPTY
}
}
).setStyle(TextColorPallet.primary)
},
false
)
}
fun sendPlayerMessage(source: ServerCommandSource, results: List<PlayerResult>) {
if (results.isEmpty()) {
source.sendFeedback(
{ "error.ledger.command.no_results".translate().setStyle(TextColorPallet.primary) },
false
)
return
}
source.sendFeedback({ "text.ledger.header.search".translate().setStyle(TextColorPallet.secondary) }, false)
results.forEach {
source.sendFeedback({ it.toText() }, false)
}
}
fun warnBusy(source: ServerCommandSource) {
// if (DatabaseManager.dbMutex.isLocked) { //TODO
// source.sendFeedback(
// {
// Text.translatable(
// "text.ledger.database.busy"
// ).setStyle(TextColorPallet.primary)
// },
// false
// )
// }
}
fun instantToText(time: Instant): MutableText {
val duration = Duration.between(time, Instant.now()).toKotlinDuration()
val text: MutableText = "".literal()
duration.toComponents { days, hours, minutes, seconds, _ ->
when {
days > 0 -> text.append(days.toString()).append("d")
hours > 0 -> text.append(hours.toString()).append("h")
minutes > 0 -> text.append(minutes.toString()).append("m")
else -> text.append(seconds.toString()).append("s")
}
}
val message = Text.translatable("text.ledger.action_message.time_diff", text)
val formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
val timeMessage = formatter.format(time.atZone(Ledger.config[SearchSpec.timeZone])).literal()
message.styled {
it.withHoverEvent(
HoverEvent(
HoverEvent.Action.SHOW_TEXT,
timeMessage
)
)
}
return message
}
}