aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/github/moulberry/notenoughupdates/miscgui/customtodos/CustomTodoEditor.kt
blob: 2b69af489da213fabf6d1c125a15fbba0c83cf0e (plain)
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright (C) 2023 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.miscgui.customtodos

import io.github.moulberry.moulconfig.common.IItemStack
import io.github.moulberry.moulconfig.forge.ForgeItemStack
import io.github.moulberry.moulconfig.internal.ClipboardUtils
import io.github.moulberry.moulconfig.observer.ObservableList
import io.github.moulberry.moulconfig.xml.Bind
import io.github.moulberry.moulconfig.xml.XMLUniverse
import io.github.moulberry.notenoughupdates.util.SBInfo
import io.github.moulberry.notenoughupdates.util.Utils
import io.github.moulberry.notenoughupdates.util.loadResourceLocation
import net.minecraft.client.Minecraft
import net.minecraft.util.ResourceLocation

class CustomTodoEditor(
    val from: CustomTodo,
    val todos: ObservableList<CustomTodoEditor>,
    val xmlUniverse: XMLUniverse
) {
    @field:Bind
    var label: String = from.label

    @field:Bind
    var enabled: Boolean = from.isEnabledOnCurrentProfile

    @field:Bind
    var timer: String = from.timer.toString()

    @field:Bind
    var showWhen: String = from.showWhen.toString()

    @field:Bind
    var trigger: String = from.trigger

    @field:Bind
    var icon: String = from.icon

    @field:Bind
    var isResetOffset: Boolean = from.isResetOffset

    @field:Bind
    var showOnlyWhenReady: Boolean = from.showOnlyWhenReady

    var target = from.triggerTarget
    var matchMode = from.triggerMatcher

    fun into(): CustomTodo {
        return CustomTodo(
            label,
            timer.toIntOrNull() ?: 0,
            trigger,
            icon,
            isResetOffset,
            showWhen.toIntOrNull() ?: 0,
            showOnlyWhenReady,
            target, matchMode,
            from.readyAt,
            from.enabled.toMutableMap().also { it[SBInfo.getInstance().currentProfile ?: return@also] = enabled }
        )
    }

    @Bind
    fun setChat() {
        target = CustomTodo.TriggerTarget.CHAT
    }

    @Bind
    fun setActionbar() {
        target = CustomTodo.TriggerTarget.ACTIONBAR
    }

    @Bind
    fun setSidebar() {
        target = CustomTodo.TriggerTarget.SIDEBAR
    }

    @Bind
    fun setTablist() {
        target = CustomTodo.TriggerTarget.TAB_LIST
    }

    private fun colorFromBool(b: Boolean): String {
        return if (b) "§a" else "§c"
    }

    @Bind
    fun getChat(): String {
        return colorFromBool(target == CustomTodo.TriggerTarget.CHAT) + "Chat"
    }

    @Bind
    fun getActionbar(): String {
        return colorFromBool(target == CustomTodo.TriggerTarget.ACTIONBAR) + "Actionbar"
    }

    @Bind
    fun getSidebar(): String {
        return colorFromBool(target == CustomTodo.TriggerTarget.SIDEBAR) + "Sidebar"
    }

    @Bind
    fun getTablist(): String {
        return colorFromBool(target == CustomTodo.TriggerTarget.TAB_LIST) + "Tablist"
    }

    @Bind
    fun setRegex() {
        matchMode = CustomTodo.TriggerMatcher.REGEX
    }

    @Bind
    fun setStartsWith() {
        matchMode = CustomTodo.TriggerMatcher.STARTS_WITH
    }

    @Bind
    fun setContains() {
        matchMode = CustomTodo.TriggerMatcher.CONTAINS
    }

    @Bind
    fun setEquals() {
        matchMode = CustomTodo.TriggerMatcher.EQUALS
    }

    @Bind
    fun getRegex(): String {
        return colorFromBool(matchMode == CustomTodo.TriggerMatcher.REGEX) + "Regex"
    }

    @Bind
    fun getStartsWith(): String {
        return colorFromBool(matchMode == CustomTodo.TriggerMatcher.STARTS_WITH) + "Starts With"
    }

    @Bind
    fun getContains(): String {
        return colorFromBool(matchMode == CustomTodo.TriggerMatcher.CONTAINS) + "Contains"
    }

    @Bind
    fun getEquals(): String {
        return colorFromBool(matchMode == CustomTodo.TriggerMatcher.EQUALS) + "Equals"
    }

    @Bind
    fun getItemStack(): IItemStack {
        val item = CustomTodoHud.parseItem(icon)
        return ForgeItemStack.of(item)
    }

    @Bind
    fun copyTemplate() {
        ClipboardUtils.copyToClipboard(into().toTemplate())
    }

    @Bind
    fun markAsReady() {
        from.readyAtOnCurrentProfile = System.currentTimeMillis()
    }

    @Bind
    fun markAsCompleted() {
        from.setDoneNow()
    }

    @Bind
    fun getFancyTimeTimer(): String {
        val tint = timer.toIntOrNull() ?: return "§3Invalid Time"
        val timeFormat = Utils.prettyTime(tint * 1000L)
        if (isResetOffset) {
            return "Reset $timeFormat after 00:00 GMT"
        }
        return "Reset $timeFormat after completion"
    }

    @Bind
    fun getFancyTimeShowWhen(): String {
        if (showOnlyWhenReady) {
            return "Shown only when task is ready"
        }
        val tint = showWhen.toIntOrNull() ?: return "§3Invalid Time"
        val timeFormat = Utils.prettyTime(tint * 1000L)
        if (tint == 0) {
            return "Always shown"
        }
        return "Show if less than $timeFormat until ready"
    }

    fun changeTimer(value: Int) {
        timer = ((timer.toIntOrNull() ?: 0) + value).coerceAtLeast(0).toString()
    }

    fun changeShowWhen(value: Int) {
        showWhen = ((showWhen.toIntOrNull() ?: 0) + value).coerceAtLeast(0).toString()
    }

    @Bind
    fun plusDayTimer() {
        changeTimer(60 * 60 * 24)
    }

    @Bind
    fun minusDayTimer() {
        changeTimer(-60 * 60 * 24)
    }

    @Bind
    fun minusHourTimer() {
        changeTimer(-60 * 60)
    }

    @Bind
    fun plusHourTimer() {
        changeTimer(60 * 60)
    }

    @Bind
    fun plusMinuteTimer() {
        changeTimer(60)
    }

    @Bind
    fun minusMinuteTimer() {
        changeTimer(-60)
    }

    @Bind
    fun plusDayShowWhen() {
        changeShowWhen(60 * 60 * 24)
    }

    @Bind
    fun minusDayShowWhen() {
        changeShowWhen(-60 * 60 * 24)
    }

    @Bind
    fun minusHourShowWhen() {
        changeShowWhen(-60 * 60)
    }

    @Bind
    fun plusHourShowWhen() {
        changeShowWhen(60 * 60)
    }

    @Bind
    fun plusMinuteShowWhen() {
        changeShowWhen(60)
    }

    @Bind
    fun minusMinuteShowWhen() {
        changeShowWhen(-60)
    }

    @Bind
    fun delete() {
        todos.remove(this)
        CustomTodoList(todos, xmlUniverse).save()
    }

    @Bind
    fun getTitle(): String {
        return "Editing $label"
    }

    @Bind
    fun afterClose() {
        CustomTodoList(todos, xmlUniverse).save()
    }

    @Bind
    fun close() {
        Minecraft.getMinecraft().displayGuiScreen(
            CustomTodoList(
                todos, xmlUniverse
            ).open()
        )
    }

    @Bind
    fun edit() {
        Minecraft.getMinecraft().displayGuiScreen(
            xmlUniverse.loadResourceLocation(this, ResourceLocation("notenoughupdates:gui/customtodos/edit.xml"))
        )
    }
}