aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/misc/tiarelay/TiaRelayHelper.kt
blob: a622fa02aaf76502f917da8588887736f2cadcc4 (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
package at.hannibal2.skyhanni.features.misc.tiarelay

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.GuiContainerEvent
import at.hannibal2.skyhanni.events.PlaySoundEvent
import at.hannibal2.skyhanni.events.RenderInventoryItemTipEvent
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.LocationUtils
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.sorted
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent

class TiaRelayHelper {

    private var tick = 0
    private var inInventory = false

    private var lastClickSlot = 0
    private var lastClickTime = 0L
    private var sounds = mutableMapOf<Int, Sound>()

    private var resultDisplay = mutableMapOf<Int, Int>()

    @SubscribeEvent
    fun onSoundPlay(event: PlaySoundEvent) {
        if (!LorenzUtils.inSkyBlock) return
        val soundName = event.soundName

        if (SkyHanniMod.feature.misc.tiaRelayMute) {
            if (soundName == "mob.wolf.whine") {
                event.isCanceled = true
            }
        }

        if (!SkyHanniMod.feature.misc.tiaRelayHelper) return
        if (!inInventory) return

        val location = event.location
        val distance = location.distance(LocationUtils.playerLocation())
        if (distance >= 2) return

        if (lastClickSlot == 0) return
        val duration = System.currentTimeMillis() - lastClickTime
        if (duration > 1_000) return
        if (sounds.contains(lastClickSlot)) return

        sounds[lastClickSlot] = Sound(soundName, event.pitch)

        lastClickSlot = 0

        tryResult()
    }

    @SubscribeEvent
    fun onTick(event: TickEvent.ClientTickEvent) {
        if (event.phase != TickEvent.Phase.START) return
        if (!LorenzUtils.inSkyBlock) return
        if (!SkyHanniMod.feature.misc.tiaRelayHelper) return

        tick++

        if (tick % 20 == 0) {
            if (InventoryUtils.openInventoryName().contains("Network Relay")) {
                inInventory = true
            } else {
                inInventory = false
                sounds.clear()
                resultDisplay.clear()
            }
        }
    }

    private fun tryResult() {
        if (sounds.size < 4) return

        val name = sounds.values.first().name
        for (sound in sounds.toMutableMap()) {
            if (sound.value.name != name) {
                LorenzUtils.chat("§c[SkyHanni] Tia Relay Helper error: Too much background noise! Please try again.")
                sounds.clear()
                return
            }
        }

        val pitchMap = mutableMapOf<Int, Float>()
        for (sound in sounds) {
            pitchMap[sound.key] = sound.value.pitch
        }
        sounds.clear()
        resultDisplay.clear()

        var i = 1
        for (entry in pitchMap.sorted()) {
            resultDisplay[entry.key] = i
            i++
        }
    }

    @SubscribeEvent
    fun onRenderItemTip(event: RenderInventoryItemTipEvent) {
        if (!LorenzUtils.inSkyBlock) return
        if (!SkyHanniMod.feature.misc.tiaRelayHelper) return
        if (!inInventory) return

        val slot = event.slot
        val stack = slot.stack

        val slotNumber = slot.slotNumber

        val position = resultDisplay.getOrDefault(slotNumber, null)
        if (position != null) {
            if (stack.getLore().any { it.contains("Done!") }) {
                resultDisplay.clear()
                return
            }
            event.stackTip = "#$position"
            return
        }

        if (!sounds.contains(slotNumber)) {
            if (stack.getLore().any { it.contains("Hear!") }) {
                event.stackTip = "Hear!"
                event.offsetX = 5
                event.offsetY = -5
                return
            }
        }
    }

    @SubscribeEvent
    fun onSlotClick(event: GuiContainerEvent.SlotClickEvent) {
        if (!LorenzUtils.inSkyBlock) return
        if (!SkyHanniMod.feature.misc.tiaRelayHelper) return
        if (!inInventory) return

        // only listen to right clicks
        if (event.clickedButton != 1) return

        lastClickSlot = event.slotId
        lastClickTime = System.currentTimeMillis()
    }

    class Sound(val name: String, val pitch: Float)
}