summaryrefslogtreecommitdiff
path: root/init.lua
diff options
context:
space:
mode:
authorcheapie <no-email-for-you@example.com>2026-01-16 09:54:35 -0600
committercheapie <no-email-for-you@example.com>2026-01-16 09:54:35 -0600
commitdcae4fd87dc72c9960008af4123f1064fcb835c7 (patch)
tree6097092aed0e02958b125100fec804b1a562ccfa /init.lua
downloadluablock-dcae4fd87dc72c9960008af4123f1064fcb835c7.tar
luablock-dcae4fd87dc72c9960008af4123f1064fcb835c7.tar.gz
luablock-dcae4fd87dc72c9960008af4123f1064fcb835c7.tar.bz2
luablock-dcae4fd87dc72c9960008af4123f1064fcb835c7.tar.xz
luablock-dcae4fd87dc72c9960008af4123f1064fcb835c7.zip
Add initial contentHEADmain
Diffstat (limited to 'init.lua')
-rw-r--r--init.lua116
1 files changed, 116 insertions, 0 deletions
diff --git a/init.lua b/init.lua
new file mode 100644
index 0000000..bd498f4
--- /dev/null
+++ b/init.lua
@@ -0,0 +1,116 @@
+local S = core.get_translator("luablock")
+
+local function on_receive_fields(pos,_,fields,player)
+ if not fields.save then return end
+ if not player then return end
+ local name = player:get_player_name()
+ if not core.check_player_privs(name,{server=true}) then
+ core.chat_send_player(name,S("You need the 'server' privilege to use this!"))
+ return
+ end
+ local meta = core.get_meta(pos)
+ meta:set_string("code",fields.code)
+ meta:set_string("infotext",S("Lua Block (operating normally)"))
+end
+
+local function run(pos,event)
+ local env = {}
+ for k,v in pairs(_G) do env[k] = v end
+ env.event = event
+ local meta = core.get_meta(pos)
+ local code = meta:get_string("code")
+ local func,loaderr = loadstring(code)
+ if not func then
+ meta:set_string("infotext",S("Lua Block (error)").."\n"..loaderr)
+ return
+ end
+ setfenv(func,env)
+ local ok,err = pcall(func)
+ --The code may have removed the Lua block as it ran
+ local nname = core.get_node(pos).name
+ if nname ~= "luablock:luablock" and nname ~= "luablock:luablock_on" then return end
+ if ok then
+ meta:set_string("infotext",S("Lua Block (operating normally)"))
+ else
+ meta:set_string("infotext",S("Lua Block (error)").."\n"..err)
+ end
+end
+
+local rules = {
+ vector.new(-1,0,0),
+ vector.new(1,0,0),
+ vector.new(0,0,-1),
+ vector.new(0,0,1),
+}
+local mesecons = {
+ effector = {
+ rules = rules,
+ action_on = function(pos,node)
+ node.name = "luablock:luablock_on"
+ core.swap_node(pos,node)
+ run(pos,{pos=pos,type="on"})
+ end,
+ action_off = function(pos,node)
+ node.name = "luablock:luablock"
+ core.swap_node(pos,node)
+ end,
+ }
+}
+
+local digilines = {
+ receptor = {},
+ effector = {
+ action = function(pos,_,channel,msg)
+ run(pos,{channel=channel,msg=msg,pos=pos})
+ end,
+ },
+}
+
+core.register_node("luablock:luablock",{
+ description = S("Lua Block"),
+ tiles = {
+ "luablock_luablock.png",
+ },
+ groups = {
+ cracky = 2,
+ not_in_creative_inventory = 1,
+ },
+ after_place_node = function(pos,player)
+ if not player then return end
+ local name = player:get_player_name()
+ if not core.check_player_privs(name,{server=true}) then
+ core.remove_node(pos)
+ core.chat_send_player(name,S("You need the 'server' privilege to use this!"))
+ return true
+ end
+ local meta = core.get_meta(pos)
+ local fs = "formspec_version[10]"
+ fs = fs.."size[20,15]"
+ fs = fs.."textarea[0.25,0.25;19.5,13;code;;${code}]"
+ fs = fs.."button[9,13.5;2,1;save;"..S("Save").."]"
+ meta:set_string("formspec",fs)
+ meta:set_string("infotext",S("Lua Block (unprogrammed)"))
+ end,
+ on_receive_fields = on_receive_fields,
+ mesecons = mesecons,
+ digilines = digilines,
+})
+
+core.register_node("luablock:luablock_on",{
+ drop = "luablock:luablock",
+ description = S("Lua Block (on - you hacker you!)"),
+ tiles = {
+ "luablock_luablock.png^[brighten",
+ },
+ groups = {
+ cracky = 2,
+ not_in_creative_inventory = 1,
+ },
+ after_place_node = function(pos)
+ core.remove_node(pos)
+ return true
+ end,
+ on_receive_fields = on_receive_fields,
+ mesecons = mesecons,
+ digilines = digilines,
+})