Newer
Older
-- SPDX-FileCopyrightText: 2024 Tuxilio <mail@tuxil.io>
--
-- SPDX-License-Identifier: GPL-3.0-or-later
local modname = core.get_current_modname()
local modpath = core.get_modpath(modname)
local led_textures = {
"black",
"blue",
"cyan",
"gray",
"green",
"magenta",
"red",
"white",
"yellow",
}
local led_colors = {}
local active_pos
local active_id
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
local formspec = "size[3,3]" ..
"image_button[0,0;1,1;red.png;red;red;false;false;]" ..
"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)
mqtt.publish(client, mqttBaseTopic.."/led/"..id.."/color", "["..rgb.."]", {retain = true,})
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
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",},
on_rightclick = function(pos, node, player, itemstack, pointed_thing)
change_color(player, id, pos)
end,
})
end
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,
})
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
--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")