aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/test/WorldEdit.kt
blob: 26ac4d9a7df8d54fea4bd894723da58f52db3a09 (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
package at.hannibal2.skyhanni.test

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.ClickType
import at.hannibal2.skyhanni.events.BlockClickEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.events.withAlpha
import at.hannibal2.skyhanni.utils.ClipboardUtils
import at.hannibal2.skyhanni.utils.LocationUtils
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RenderUtils
import at.hannibal2.skyhanni.utils.RenderUtils.expandBlock
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getItemId
import net.minecraft.util.AxisAlignedBB
import net.minecraft.util.BlockPos
import net.minecraftforge.client.event.RenderWorldLastEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.awt.Color

object WorldEdit {
    private var leftPos = null as BlockPos?
    private var rightPos = null as BlockPos?

    private fun funAABB(left: BlockPos, right: BlockPos) = AxisAlignedBB(
        minOf(left.x, left.x + 1, right.x, right.x + 1).toDouble(),
        minOf(left.y, left.y + 1, right.y, right.y + 1).toDouble(),
        minOf(left.z, left.z + 1, right.z, right.z + 1).toDouble(),
        maxOf(left.x, left.x + 1, right.x, right.x + 1).toDouble(),
        maxOf(left.y, left.y + 1, right.y, right.y + 1).toDouble(),
        maxOf(left.z, left.z + 1, right.z, right.z + 1).toDouble(),
    )

    private val aabb
        get() = leftPos?.let { l ->
            rightPos?.let { r ->
                funAABB(l, r)
            }
        }

    fun copyToClipboard() {
        ClipboardUtils.copyToClipboard(generateCodeSnippet())
    }

    private fun generateCodeSnippet(): String {
        var text = ""
        leftPos?.run { text += "val redLeft = net.minecraft.util.BlockPos($x, $y, $z)\n" }
        rightPos?.run { text += "val blueRight = net.minecraft.util.BlockPos($x, $y, $z)\n" }
        aabb?.run { text += "val aabb = net.minecraft.util.AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ)\n" }
        return text
    }

    @SubscribeEvent
    fun onBlockClick(event: BlockClickEvent) {
        if (!isEnabled()) return
        if (event.itemInHand?.getItemId() != "WOOD_AXE") return

        if (event.clickType == ClickType.LEFT_CLICK) {
            leftPos = event.position.toBlockPos()
        } else if (event.clickType == ClickType.RIGHT_CLICK) {
            rightPos = event.position.toBlockPos()
        }
    }

    @SubscribeEvent
    fun onWorldChange(event: LorenzWorldChangeEvent) {
        leftPos = null
        rightPos = null
    }

    @SubscribeEvent
    fun onRenderWorldLast(event: RenderWorldLastEvent) {
        if (!isEnabled()) return

        leftPos?.let { l ->
            RenderUtils.drawWireframeBoundingBox_nea(
                funAABB(l, l).expandBlock(),
                Color.RED,
                event.partialTicks
            )
        }
        rightPos?.let { r ->
            RenderUtils.drawWireframeBoundingBox_nea(
                funAABB(r, r).expandBlock(),
                Color.BLUE,
                event.partialTicks
            )
        }
        aabb?.let {
            RenderUtils.drawFilledBoundingBox_nea(
                it.expandBlock(),
                Color(Color.CYAN.withAlpha(60), true),
                partialTicks = event.partialTicks,
                renderRelativeToCamera = false
            )
        }
    }

    fun command(it: Array<String>) {
        if (!isEnabled()) {
            LorenzUtils.userError("World Edit is disabled in the config. Enable it if you want to use it.")
            return
        }
        when (it.firstOrNull()) {
            null, "help" -> {
                LorenzUtils.chat("Use a wood axe and left/right click to select a region in the world. Then use /shworldedit copy or /shworldedit reset.")
            }

            "copy" -> {
                copyToClipboard()
                LorenzUtils.chat("Copied text to clipboard.")
            }

            "reset" -> {
                leftPos = null
                rightPos = null
                LorenzUtils.chat("Reset selected region")
            }

            "left", "pos1" -> {
                leftPos = LocationUtils.playerLocation().toBlockPos()
                LorenzUtils.chat("Set left pos.")
            }

            "right", "pos2" -> {
                leftPos = LocationUtils.playerLocation().toBlockPos()
                LorenzUtils.chat("Set right pos.")
            }

            else -> {
                LorenzUtils.chat("Unknown subcommand")
            }
        }
    }

    fun isEnabled() = SkyHanniMod.feature.dev.worldEdit
}