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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
package at.hannibal2.skyhanni.features.misc.reminders
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.SecondPassedEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.StringUtils
import at.hannibal2.skyhanni.utils.TimeUtils
import at.hannibal2.skyhanni.utils.TimeUtils.format
import at.hannibal2.skyhanni.utils.TimeUtils.minutes
import at.hannibal2.skyhanni.utils.chat.Text
import at.hannibal2.skyhanni.utils.chat.Text.asComponent
import at.hannibal2.skyhanni.utils.chat.Text.center
import at.hannibal2.skyhanni.utils.chat.Text.command
import at.hannibal2.skyhanni.utils.chat.Text.fitToChat
import at.hannibal2.skyhanni.utils.chat.Text.hover
import at.hannibal2.skyhanni.utils.chat.Text.send
import at.hannibal2.skyhanni.utils.chat.Text.style
import at.hannibal2.skyhanni.utils.chat.Text.suggest
import at.hannibal2.skyhanni.utils.chat.Text.wrap
import net.minecraft.util.EnumChatFormatting
import net.minecraft.util.IChatComponent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
@SkyHanniModule
object ReminderManager {
private const val REMINDERS_PER_PAGE = 10
// Random numbers chosen, this will be used to delete the old list and action messages
private const val REMINDERS_LIST_ID = -546745
private const val REMINDERS_ACTION_ID = -546746
private const val REMINDERS_MESSAGE_ID = -546747
private val storage get() = SkyHanniMod.feature.storage.reminders
private val config get() = SkyHanniMod.feature.misc.reminders
private var listPage = 1
private fun getSortedReminders() = storage.entries.sortedBy { it.value.remindAt }
private fun sendMessage(message: String) = Text.join("§e[Reminder]", " ", message).send(REMINDERS_ACTION_ID)
private fun createDivider() = Text.HYPHEN.fitToChat().style {
strikethrough = true
color = EnumChatFormatting.BLUE
}
private fun parseDuration(text: String): Duration? = try {
val duration = TimeUtils.getDuration(text)
if (duration <= 1.seconds) null else duration
} catch (e: Exception) {
null
}
private fun listReminders(page: Int) {
val reminders = getSortedReminders()
val maxPage = (reminders.size + REMINDERS_PER_PAGE - 1) / REMINDERS_PER_PAGE
listPage = page.coerceIn(0, maxPage)
val text: MutableList<IChatComponent> = mutableListOf()
text.add(createDivider())
text.add(
Text.join(
if (listPage > 1) "§6§l<<".asComponent {
hover = "§eClick to view page ${listPage - 1}".asComponent()
command = "/shremind list ${listPage - 1}"
} else null,
" ",
"§6Reminders (Page $listPage of $maxPage)",
" ",
if (listPage < maxPage) "§6§l>>".asComponent {
hover = "§eClick to view page ${listPage + 1}".asComponent()
command = "/shremind list ${listPage + 1}"
} else null,
).center(),
)
if (reminders.isNotEmpty()) {
for (i in (listPage - 1) * REMINDERS_PER_PAGE until listPage * REMINDERS_PER_PAGE) {
if (i >= reminders.size) break
val (id, reminder) = reminders[i]
text.add(
Text.join(
"§c✕".asComponent {
hover = "§7Click to remove".asComponent()
command = "/shremind remove -l $id"
}.wrap("§8[", "§8]"),
" ",
"§e✎".asComponent {
hover = "§7Click to start editing".asComponent()
suggest = "/shremind edit -l $id ${reminder.reason} "
}.wrap("§8[", "§8]"),
" ",
"§6${reminder.formatShort()}".asComponent {
hover = "§7${reminder.formatFull()}".asComponent()
}.wrap("§8[", "§8]"),
" ",
"§7${reminder.reason}",
),
)
}
} else {
text.add(Text.EMPTY)
text.add("§cNo reminders found.".asComponent().center())
text.add(Text.EMPTY)
}
text.add(createDivider())
Text.join(*text.toTypedArray(), separator = Text.NEWLINE).send(REMINDERS_LIST_ID)
}
private fun createReminder(args: Array<String>) {
if (args.size < 2) return help()
val time = parseDuration(args.first()) ?: return ChatUtils.userError("Invalid time format")
val reminder = args.drop(1).joinToString(" ")
val remindAt = SimpleTimeMark.now().plus(time)
storage[StringUtils.generateRandomId()] = Reminder(reminder, remindAt)
sendMessage("§6Reminder set for ${time.format()}")
}
private fun actionReminder(
args: List<String>,
command: String,
vararg arguments: String,
action: (List<String>, Reminder) -> String,
) {
val argumentText = arguments.joinToString(" ")
if (args.size < arguments.size) return ChatUtils.userError("/shremind $command $argumentText")
if (args.first() == "-l") {
if (args.size < arguments.size + 1) return ChatUtils.userError("/shremind $command -l $argumentText")
if (storage[args.drop(1).first()] == null) return ChatUtils.userError("Reminder not found!")
action(args.drop(2), storage[args.drop(1).first()]!!).apply {
listReminders(listPage)
sendMessage(this)
}
} else if (storage[args.first()] == null) {
return ChatUtils.userError("Reminder not found!")
} else {
sendMessage(action(args.drop(1), storage[args.first()]!!))
}
}
private fun removeReminder(args: List<String>) = actionReminder(
args,
"remove",
"[id]",
) { _, reminder ->
storage.values.remove(reminder)
"§cReminder deleted."
}
private fun editReminder(args: List<String>) = actionReminder(
args,
"edit",
"[id]",
"[reminder]",
) { arguments, reminder ->
reminder.reason = arguments.joinToString(" ")
"§6Reminder edited."
}
private fun moveReminder(args: List<String>) = actionReminder(
args,
"move",
"[id]",
"[time]",
) { arguments, reminder ->
val time = parseDuration(arguments.first()) ?: return@actionReminder "§cInvalid time format!"
reminder.remindAt = SimpleTimeMark.now().plus(time)
"§6Reminder moved to ${time.format()}"
}
private fun help() {
createDivider().send()
"§6SkyHanni Reminder Commands:".asComponent().send()
"§e/shremind <time> <reminder> - §bCreates a new reminder".asComponent().send()
"§e/shremind list <page> - §bLists all reminders".asComponent().send()
"§e/shremind remove <id> - §bRemoves a reminder".asComponent().send()
"§e/shremind edit <id> <reminder> - §bEdits a reminder".asComponent().send()
"§e/shremind move <id> <time> - §bMoves a reminder".asComponent().send()
"§e/shremind help - §bShows this help message".asComponent().send()
createDivider().send()
}
@SubscribeEvent
fun onSecondPassed(event: SecondPassedEvent) {
val remindersToSend = mutableListOf<IChatComponent>()
for ((id, reminder) in getSortedReminders()) {
if (!reminder.shouldRemind(config.interval.minutes)) continue
reminder.lastReminder = SimpleTimeMark.now()
var actionsComponent: IChatComponent? = null
if (!config.autoDeleteReminders) {
actionsComponent = Text.join(
" ",
"§a✔".asComponent {
hover = "§7Click to dismiss".asComponent()
command = "/shremind remove $id"
}.wrap("§8[", "§8]"),
" ",
"§e§l⟳".asComponent {
hover = "§7Click to move".asComponent()
suggest = "/shremind move $id 1m"
|