Skip to content
Snippets Groups Projects
init.lua 4.14 KiB
Newer Older
Tuxilio's avatar
Tuxilio committed
-- SPDX-FileCopyrightText: 2024 Tuxilio <mail@tuxil.io>
--
-- SPDX-License-Identifier: GPL-3.0-or-later

Tuxilio's avatar
Tuxilio committed
local modname = core.get_current_modname()
local modpath = core.get_modpath(modname)
Tuxilio's avatar
Tuxilio committed

Tuxilio's avatar
Tuxilio committed
local data = {
Tuxilio's avatar
Tuxilio committed
    url = "mqtt.felix-zauberer.de",
Tuxilio's avatar
Tuxilio committed
    clientId = "ledtree_luanti",
    username = "kalle1",
Tuxilio's avatar
Tuxilio committed
    password = "eZcKn1zBE6KtbeFnZ33lV1EKnr9Bcn8B",
Tuxilio's avatar
Tuxilio committed
}

Tuxilio's avatar
Tuxilio committed
local ledCount = 350

local led_textures = {
    "black",
    "blue",
    "cyan",
    "gray",
    "green",
    "magenta",
    "red",
    "white",
    "yellow",
}

local led_colors = {}

local active_pos
local active_id

Tuxilio's avatar
Tuxilio committed
local mqttBaseTopic = core.settings:get("ledtree_base_topic") or "ledtree"

local client = mqtt.connect(data.url, data.clientId, data.username, data.password)

local change_color = function(player, led_id, pos)
    local pos_string = core.serialize(pos)
    active_pos = pos
    active_id = led_id
Tuxilio's avatar
Tuxilio committed
    local formspec = "size[3,3]" ..
                     "image_button[0,0;1,1;red.png;red;red;false;false;]" ..
Tuxilio's avatar
Tuxilio committed
                     "image_button[1,0;1,1;green.png;green;green;false;false;]" ..
Tuxilio's avatar
Tuxilio committed
                     "image_button[2,0;1,1;blue.png;blue;blue;false;false;]" ..
                     "image_button[0,1;1,1;yellow.png;yellow;yellow;false;false;]" ..
                     "image_button[1,1;1,1;cyan.png;cyan;cyan;false;false;]" ..
                     "image_button[2,1;1,1;magenta.png;magenta;magenta;false;false;]" ..
                     "image_button[0,2;1,1;white.png;white;white;false;false;]" ..
                     "image_button[1,2;1,1;black.png;black;black;false;false;]" ..
                     "image_button[2,2;1,1;gray.png;gray;gray;false;false;]"

    core.show_formspec(player:get_player_name(), "ledtree:colorpicker", formspec)
end

local function send_mqtt(id, rgb)
Tuxilio's avatar
Tuxilio committed
    mqtt.publish(client, mqttBaseTopic.."/led/"..id.."/color", "["..rgb.."]", {retain = true,})
Tuxilio's avatar
Tuxilio committed
end

local function replace_block(pos, id, new_texture)
    --local node = core.get_node(pos)
    local node_name = "ledtree:led" .. id .. "_" .. new_texture
    core.set_node(pos, {name = node_name})
end

Tuxilio's avatar
Tuxilio committed
for id = 0, ledCount-1 do
    led_colors[id] = ""
    for index, texture in ipairs(led_textures) do
        core.register_node("ledtree:led" .. id .. "_" .. texture, {
            description = "LED" .. id .." " .. texture,
            tiles = {texture .. ".png",},
            light_source = 10,
            on_rightclick = function(pos, node, player, itemstack, pointed_thing)
                change_color(player, id, pos)
            end,
        })
    end
Tuxilio's avatar
Tuxilio committed
end
Tuxilio's avatar
Tuxilio committed

Tuxilio's avatar
Tuxilio committed
core.register_node("ledtree:party", {
    description = "Party",
    tiles = {"party.png"},
    light_source = 10,
    on_rightclick = function(pos, node, player, itemstack, pointed_thing)
        mqtt.publish(client, mqttBaseTopic.."/effects", "[party]")
    end,
})

Tuxilio's avatar
Tuxilio committed
core.register_on_player_receive_fields(function(player, formname, fields)
    if formname == "ledtree:colorpicker" then
        for color, _ in pairs(fields) do
            if color then
                local rgb = ""
                if color == "red" then
                    rgb = "255,0,0"
                elseif color == "green" then
                    rgb = "0,255,0"
                elseif color == "blue" then
                    rgb = "0,0,255"
                elseif color == "yellow" then
                    rgb = "255,255,0"
                elseif color == "cyan" then
                    rgb = "0,255,255"
                elseif color == "magenta" then
                    rgb = "255,0,255"
                elseif color == "white" then
                    rgb = "255,255,255"
                elseif color == "black" then
                    rgb = "0,0,0"
                elseif color == "gray" then
                    rgb = "128,128,128"
                end

                if rgb ~= "" then
Tuxilio's avatar
Tuxilio committed
                    --core.chat_send_player(player:get_player_name(), "LED: ".. active_id .. " - RGB: " .. rgb .. " ("..color..")")
                    send_mqtt(active_id, rgb)
                    replace_block(active_pos, active_id, color)

                    core.close_formspec(player:get_player_name(), "ledtree:colorpicker")
Tuxilio's avatar
Tuxilio committed
                end
            end
        end
    end
end)
Tuxilio's avatar
Tuxilio committed

dofile(modpath .. "/tree.lua")