2024-08-09 03:02:15 -06:00

102 lines
3.2 KiB
Kotlin

package com.github.itzilly.sbt.features
import com.github.itzilly.sbt.setClipboard
import com.github.itzilly.sbt.util.Chatterbox
import com.github.itzilly.sbt.util.SimpleChatMsg
import com.google.gson.Gson
import net.minecraft.client.Minecraft
import net.minecraft.client.settings.KeyBinding
import net.minecraftforge.fml.client.registry.ClientRegistry
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.InputEvent
import org.lwjgl.input.Keyboard
import kotlin.math.round
object RouteBuilder {
var addNodeKeybind: KeyBinding
var deleteLastNodeKeybind: KeyBinding
var finishRouteKeybind: KeyBinding
var nodes: ArrayList<RouteNode> = ArrayList()
init {
addNodeKeybind = KeyBinding("key.addNode", Keyboard.KEY_ADD, "key.categories.sbt")
ClientRegistry.registerKeyBinding(addNodeKeybind)
deleteLastNodeKeybind = KeyBinding("key.removeLastNode", Keyboard.KEY_MINUS, "key.categories.sbt")
ClientRegistry.registerKeyBinding(deleteLastNodeKeybind)
finishRouteKeybind = KeyBinding("key.finishRoute", Keyboard.KEY_F4, "key.categories.sbt")
ClientRegistry.registerKeyBinding(finishRouteKeybind)
}
@SubscribeEvent
fun onKeyInput(event: InputEvent.KeyInputEvent) {
if (addNodeKeybind.isPressed) {
val mc = Minecraft.getMinecraft()
val x = round(mc.thePlayer.posX - 0.5).toInt()
val y = round(mc.thePlayer.posY).toInt()
val z = round(mc.thePlayer.posZ - 0.5).toInt()
if (nodeExists(x, y, z)) {
Chatterbox.say(SimpleChatMsg("Node already exists!").red())
} else {
addNode(x, y, z)
Chatterbox.say("This is a string")
}
}
if (deleteLastNodeKeybind.isPressed) {
val wasRemoved = nodes.removeLastOrNull()
if (wasRemoved == null) {
val msg = SimpleChatMsg("There isn't a node to delete!")
Chatterbox.say(msg.red())
} else {
val msg = SimpleChatMsg("Removed previous node")
Chatterbox.say(msg.green())
}
}
if (finishRouteKeybind.isPressed) {
if (nodes.size == 0) {
val msg = SimpleChatMsg("There aren't any nodes to copy!")
Chatterbox.say(msg.red())
} else {
val jsonString = Gson().toJson(nodes)
val msg = SimpleChatMsg("String copied to clipboard!")
setClipboard(jsonString)
Chatterbox.say(msg.green())
}
}
}
fun addNode(x: Int, y: Int, z: Int) {
nodes.add(RouteNode(x, y, z, 0u, 1u, 0u, NodeOptions("${nodes.size + 1}")))
}
fun addNode(x: Int, y: Int, z: Int, r: UByte, g: UByte, b: UByte) {
nodes.add(RouteNode(x, y, z, r, g, b, NodeOptions("${nodes.size + 1}")))
}
}
data class RouteNode(
val x: Int,
val y: Int,
val z: Int,
val r: UByte,
val g: UByte,
val b: UByte,
val options: NodeOptions
)
data class NodeOptions(
val name: String
)
fun nodeExists(x: Int, y: Int, z: Int): Boolean {
return RouteBuilder.nodes.any { it.x == x && it.y == y && it.z == z }
}