Minetest-Tubelib/button.lua

134 lines
3.3 KiB
Lua
Raw Normal View History

2017-09-07 13:27:30 -07:00
--[[
Tube Library
============
Copyright (C) 2017 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
2017-09-09 09:17:31 -07:00
button.lua:
Example of a "client" node, only sending messages to others.
This is a real client which does not claim a position number nore
providing any server API.
2017-09-07 13:27:30 -07:00
]]--
CYCLE_TIME = 4
2017-09-09 09:17:31 -07:00
local function switch_on(pos, node)
node.name = "tubelib:button_active"
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):start(CYCLE_TIME)
minetest.sound_play("button", {
pos = pos,
gain = 0.5,
max_hear_distance = 5,
})
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
local placer_name = meta:get_string("placer_name")
local clicker_name = nil
if meta:get_string("public") == "false" then
clicker_name = meta:get_string("clicker_name")
2017-09-07 13:27:30 -07:00
end
2017-09-09 09:17:31 -07:00
tubelib.send_message(number, placer_name, clicker_name, "start", nil) -- <<=== tubelib
2017-09-07 13:27:30 -07:00
end
local function switch_off(pos)
local node = minetest.get_node(pos)
2017-09-09 09:17:31 -07:00
node.name = "tubelib:button"
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):stop()
minetest.sound_play("button", {
pos = pos,
gain = 0.5,
max_hear_distance = 5,
})
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
local placer_name = meta:get_string("placer_name")
tubelib.send_message(number, placer_name, nil, "stop", nil) -- <<=== tubelib
2017-09-07 13:27:30 -07:00
end
minetest.register_node("tubelib:button", {
description = "Tubelib Button",
tiles = {
-- up, down, right, left, back, front
'tubelib_front.png',
2017-09-09 12:57:22 -07:00
'tubelib_button.png',
'tubelib_button.png',
'tubelib_button.png',
'tubelib_button.png',
2017-09-07 13:27:30 -07:00
"tubelib_button_off.png",
},
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", "size[4,3]"..
"field[0.5,0.5;3,1;number;Insert destination block number;]" ..
2017-09-09 09:17:31 -07:00
"checkbox[1,1;public;public;false]"..
2017-09-07 13:27:30 -07:00
"button_exit[1,2;2,1;exit;Save]")
2017-09-09 09:17:31 -07:00
meta:set_string("placer_name", placer:get_player_name())
meta:set_string("public", "false")
2017-09-07 13:27:30 -07:00
end,
on_receive_fields = function(pos, formname, fields, player)
2017-09-09 09:17:31 -07:00
local meta = minetest.get_meta(pos)
if tubelib.check_numbers(fields.number) then -- <<=== tubelib
2017-09-07 13:27:30 -07:00
meta:set_string("number", fields.number)
2017-09-08 11:09:33 -07:00
meta:set_string("infotext", "Tubelib Button, connected with block "..fields.number)
2017-09-07 13:27:30 -07:00
end
2017-09-09 09:17:31 -07:00
if fields.public then
meta:set_string("public", fields.public)
end
if fields.exit then
meta:set_string("formspec", nil)
end
2017-09-07 13:27:30 -07:00
end,
on_rightclick = function(pos, node, clicker)
2017-09-09 09:17:31 -07:00
local meta = minetest.get_meta(pos)
meta:set_string("clicker_name", clicker:get_player_name())
switch_on(pos, node)
2017-09-07 13:27:30 -07:00
end,
paramtype2 = "facedir",
groups = {cracky=1},
is_ground_content = false,
})
2017-09-09 12:57:22 -07:00
minetest.register_node("tubelib:button_active", {
2017-09-07 13:27:30 -07:00
description = "Tubelib Button",
tiles = {
-- up, down, right, left, back, front
'tubelib_front.png',
2017-09-09 12:57:22 -07:00
'tubelib_button.png',
'tubelib_button.png',
'tubelib_button.png',
'tubelib_button.png',
2017-09-07 13:27:30 -07:00
"tubelib_button_on.png",
},
on_timer = switch_off,
paramtype2 = "facedir",
groups = {crumbly=0, not_in_creative_inventory=1},
is_ground_content = false,
})
2017-09-10 11:13:07 -07:00
minetest.register_craft({
output = "tubelib:button 4",
recipe = {
{"group:wood", "group:wood", "group:wood"},
{"dye:red", "default:mese_crystal", "tubelib:tube1"},
{"group:wood", "default:glass", "group:wood"},
},
})