aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/data/TrackerManager.kt
blob: c1a7a60c3f495a82bc2e25dc6bedca0b7da321ce (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
package at.hannibal2.skyhanni.data

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.ConfigLoadEvent
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.ItemAddEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.ConditionalUtils
import at.hannibal2.skyhanni.utils.NEUInternalName
import at.hannibal2.skyhanni.utils.NumberUtil.formatIntOrUserError
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

@SkyHanniModule
object TrackerManager {

    private var hasChanged = false
    var dirty = false
    var commandEditTrackerSuccess = false

    @SubscribeEvent
    fun onConfigLoad(event: ConfigLoadEvent) {
        val config = SkyHanniMod.feature.misc.tracker.hideCheapItems
        ConditionalUtils.onToggle(config.alwaysShowBest, config.minPrice, config.enabled) {
            hasChanged = true
        }
    }

    @SubscribeEvent(priority = EventPriority.HIGHEST)
    fun onRenderOverlayFirst(event: GuiRenderEvent) {
        if (hasChanged) {
            dirty = true
        }
    }

    @SubscribeEvent(priority = EventPriority.LOWEST)
    fun onRenderOverlayLast(event: GuiRenderEvent) {
        if (hasChanged) {
            dirty = false
            hasChanged = false
        }
    }

    fun commandEditTracker(args: Array<String>) {
        if (args.size < 2) {
            ChatUtils.userError("Usage: /shedittracker <item name> <amount>")
            return
        }

        val amount = args.last().formatIntOrUserError() ?: return
        if (amount == 0) {
            ChatUtils.userError("Amount can not be zero!")
            return
        }

        val rawName = args.dropLast(1).joinToString(" ")
        val internalName = NEUInternalName.fromItemNameOrInternalName(rawName)
        if (!internalName.isKnownItem()) {
            ChatUtils.chat("No item found for '$rawName'!")
            return
        }

        commandEditTrackerSuccess = false
        ItemAddEvent(internalName, amount, ItemAddManager.Source.COMMAND).postAndCatch()
        if (!commandEditTrackerSuccess) {
            ChatUtils.userError("Could not edit the Item Tracker! Does this item belong to this tracker? Is the tracker active right now?")
        }
    }
}