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

import at.hannibal2.skyhanni.events.BlockClickEvent
import at.hannibal2.skyhanni.events.EntityClickEvent
import at.hannibal2.skyhanni.events.ItemClickEvent
import at.hannibal2.skyhanni.events.PacketEvent
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.toLorenzVec
import net.minecraft.client.Minecraft
import net.minecraft.network.play.client.C07PacketPlayerDigging
import net.minecraft.network.play.client.C08PacketPlayerBlockPlacement
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.InputEvent
import org.lwjgl.input.Mouse

class ItemClickData {

    @SubscribeEvent
    fun onItemClickSend(event: PacketEvent.SendEvent) {
        val packet = event.packet
        if (packet is C08PacketPlayerBlockPlacement) {
            if (packet.placedBlockDirection != 255) {
                val position = packet.position.toLorenzVec()
                BlockClickEvent(ClickType.RIGHT_CLICK, position, packet.stack).postAndCatch()
            } else {
                ItemClickEvent(InventoryUtils.getItemInHand(), ClickType.RIGHT_CLICK).postAndCatch()
            }
        }
        if (packet is C07PacketPlayerDigging && packet.status == C07PacketPlayerDigging.Action.START_DESTROY_BLOCK) {
            val position = packet.position.toLorenzVec()
            BlockClickEvent(ClickType.LEFT_CLICK, position, InventoryUtils.getItemInHand()).postAndCatch()
        }
    }

    @SubscribeEvent
    fun onEntityClick(event: InputEvent.MouseInputEvent) {
        if (!LorenzUtils.inSkyBlock) return

        val minecraft = Minecraft.getMinecraft()
        val clickedEntity = minecraft.pointedEntity
        if (minecraft.thePlayer == null) return
        if (clickedEntity == null) return

        val clickType = when (Mouse.getEventButton()) {
            0 -> ClickType.LEFT_CLICK
            1 -> ClickType.RIGHT_CLICK
            else -> return
        }

        EntityClickEvent(clickType, clickedEntity).postAndCatch()
    }
}