summaryrefslogtreecommitdiff
path: root/init.lua
blob: bd498f4e1cf47d55daf7a32940cd3c421401ff08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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,
})