techpack/tubelib_addons2/programmer.lua

100 lines
2.9 KiB
Lua
Raw Normal View History

2018-03-10 22:14:57 +01:00
--[[
Tubelib Addons 2
================
Copyright (C) 2017-2020 Joachim Stolberg
2018-03-10 22:14:57 +01:00
AGPL v3
2018-03-10 22:14:57 +01:00
See LICENSE.txt for more information
programmer.lua:
]]--
-- Load support for I18n
local S = tubelib_addons2.S
2018-03-10 22:14:57 +01:00
local function join_to_string(tbl)
local t = {}
2018-03-10 22:14:57 +01:00
for key,_ in pairs(tbl) do
t[#t + 1] = key .. " "
end
-- remove the last blank
return string.sub(table.concat(t), 1, -2)
end
local function reset_programmer(itemstack, user, pointed_thing)
2023-05-20 17:41:18 +02:00
local meta = user:get_meta()
meta:set_string("tubelib_prog_numbers", nil)
minetest.chat_send_player(user:get_player_name(), S("[Tubelib Programmer] programmer reset"))
2018-03-10 22:14:57 +01:00
return itemstack
end
local function read_number(itemstack, user, pointed_thing)
local pos = pointed_thing.under
2023-05-20 17:41:18 +02:00
local meta = user:get_meta()
2018-03-10 22:14:57 +01:00
if pos then
local number = tubelib.get_node_number(pos)
if number then
2023-05-20 17:41:18 +02:00
local numbers = minetest.deserialize(meta:get_string("tubelib_prog_numbers")) or {}
2018-03-10 22:14:57 +01:00
numbers[number] = true
2023-05-20 17:41:18 +02:00
meta:set_string("tubelib_prog_numbers", minetest.serialize(numbers))
minetest.chat_send_player(user:get_player_name(), S("[Tubelib Programmer] number").." "..number.." "..S("read"))
else
minetest.chat_send_player(user:get_player_name(), S("[Tubelib Programmer] Unknown node on").." "..minetest.pos_to_string(pos))
2018-03-10 22:14:57 +01:00
end
else
return reset_programmer(itemstack, user, pointed_thing)
end
return itemstack
end
local function program_numbers(itemstack, placer, pointed_thing)
local pos = pointed_thing.under
2023-05-20 17:41:18 +02:00
local pmeta = placer:get_meta()
2018-03-10 22:14:57 +01:00
if pos then
local meta = minetest.get_meta(pos)
local node_number = tubelib.get_node_number(pos)
2023-05-20 17:41:18 +02:00
local numbers = minetest.deserialize(pmeta:get_string("tubelib_prog_numbers")) or {}
pmeta:set_string("tubelib_prog_numbers", nil)
2018-03-10 22:14:57 +01:00
local text = join_to_string(numbers)
local player_name = placer:get_player_name()
if meta and meta:get_string("owner") ~= player_name then
minetest.chat_send_player(player_name, S("[Tubelib Programmer] foreign or unknown node!"))
2018-03-10 22:14:57 +01:00
return itemstack
end
local res = tubelib.send_request(node_number, "set_numbers", text)
if res == true then
minetest.chat_send_player(player_name, S("[Tubelib Programmer] node programmed!"))
2018-03-10 22:14:57 +01:00
else
minetest.chat_send_player(player_name, S("[Tubelib Programmer] Error: programmer not supported!"))
2018-03-10 22:14:57 +01:00
end
return itemstack
else
return reset_programmer(itemstack, placer, pointed_thing)
end
end
minetest.register_craftitem("tubelib_addons2:programmer", {
description = S("Tubelib Programmer"),
2018-03-10 22:14:57 +01:00
inventory_image = "tubelib_addons2_programmer.png",
stack_max = 1,
wield_image = "tubelib_addons2_programmer_wield.png",
groups = {cracky=1, book=1},
-- left mouse button = program
on_use = program_numbers,
on_secondary_use = reset_programmer,
-- right mouse button = read
on_place = read_number,
})
minetest.register_craft({
output = "tubelib_addons2:programmer",
recipe = {
{"", "default:steel_ingot", ""},
{"", "tubelib:wlanchip", ""},
{"", "dye:red", ""},
},
})