summaryrefslogtreecommitdiff
path: root/mesecons_commandblock/init.lua
blob: af7034d20be5226109c5c1007e3ee08a69833371 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
minetest.register_chatcommand("say", {
	params = "<text>",
	description = "Say <text> as the server",
	privs = {server=true},
	func = function(name, param)
		minetest.chat_send_all(name .. ": " .. param)
	end
})

minetest.register_chatcommand("tell", {
	params = "<name> <text>",
	description = "Say <text> to <name> privately",
	func = function(name, param)
		local found, _, target, message = param:find("^([^%s]+)%s+(.*)$")
		if found == nil then
			minetest.chat_send_player(name, "Invalid usage: " .. param)
			return
		end
		if not minetest.get_player_by_name(target) then
			minetest.chat_send_player(name, "Invalid target: " .. target)
		end
		minetest.chat_send_player(target, name .. " whispers: " .. message, false)
	end
})

minetest.register_chatcommand("tellme", {
	params = "<text>",
	description = "Say <text> to yourself",
	func = function(name, param)
		minetest.chat_send_player(name, param, false)
	end
})

minetest.register_chatcommand("hp", {
	params = "<name> <value>",
	description = "Set health of <name> to <value> hitpoints",
	privs = {ban=true},
	func = function(name, param)
		local found, _, target, value = param:find("^([^%s]+)%s+(%d+)$")
		if found == nil then
			minetest.chat_send_player(name, "Invalid usage: " .. param)
			return
		end
		local player = minetest.get_player_by_name(target)
		if player then
			player:set_hp(value)
		else
			minetest.chat_send_player(name, "Invalid target: " .. target)
		end
	end
})

local initialize_data = function(meta, player, command, param)
	meta:set_string("formspec",
		"invsize[9,6;]" ..
		"field[1,1;7.5,1;player;Player;" .. player .. "]" ..
		"button[1.3,2;2,1;nearest;Nearest]" ..
		"button[3.3,2;2,1;farthest;Farthest]" ..
		"button[5.3,2;2,1;random;Random]" ..
		"field[1,4;2,1;command;Command;" .. command .. "]" ..
		"field[3,4;5.5,1;param;Parameter;" .. param .. "]" ..
		"button_exit[3.3,5;2,1;submit;Submit]")
	local owner = meta:get_string("owner")
	if owner == "" then
		owner = "not owned"
	else
		owner = "owned by " .. owner
	end
	meta:set_string("infotext", "Command Block\n" ..
		"(" .. owner .. ")\n" ..
		"Command: /" .. command .. " " .. param)
end

local construct = function(pos)
	local meta = minetest.get_meta(pos)

	meta:set_string("player", "@nearest")
	meta:set_string("command", "time")
	meta:set_string("param", "7000")

	meta:set_string("owner", "")

	initialize_data(meta, "@nearest", "time", "7000")
end

local after_place = function(pos, placer)
	if placer then
		local meta = minetest.get_meta(pos)
		meta:set_string("owner", placer:get_player_name())
		initialize_data(meta, "@nearest", "time", "7000")
	end
end

local receive_fields = function(pos, formname, fields, sender)
	if fields.quit then
		return
	end
	local meta = minetest.get_meta(pos)
	if fields.nearest then
		initialize_data(meta, "@nearest", fields.command, fields.param)
	elseif fields.farthest then
		initialize_data(meta, "@farthest", fields.command, fields.param)
	elseif fields.random then
		initialize_data(meta, "@random", fields.command, fields.param)
	else --fields.submit or pressed enter
		meta:set_string("player", fields.player)
		meta:set_string("command", fields.command)
		meta:set_string("param", fields.param)

		initialize_data(meta, fields.player, fields.command, fields.param)
	end
end

local resolve_player = function(name, pos)
	local get_distance = function(pos1, pos2)
		return math.sqrt((pos1.x - pos2.x) ^ 2 + (pos1.y - pos2.y) ^ 2 + (pos1.z - pos2.z) ^ 2)
	end

	if name == "@nearest" then
		local min_distance = math.huge
		for index, player in ipairs(minetest.get_connected_players()) do
			local distance = get_distance(pos, player:getpos())
			if distance < min_distance then
				min_distance = distance
				name = player:get_player_name()
			end
		end
	elseif name == "@farthest" then
		local max_distance = -1
		for index, player in ipairs(minetest.get_connected_players()) do
			local distance = get_distance(pos, player:getpos())
			if distance > max_distance then
				max_distance = distance
				name = player:get_player_name()
			end
		end
	elseif name == "@random" then
		local players = minetest.get_connected_players()
		local player = players[math.random(#players)]
		name = player:get_player_name()
	end
	return name
end

local commandblock_action_on = function(pos, node)
	if node.name ~= "mesecons_commandblock:commandblock_off" then
		return
	end

	minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_on"})

	local meta = minetest.get_meta(pos)
	local command = minetest.chatcommands[meta:get_string("command")]
	if command == nil then
		return
	end
	local owner = meta:get_string("owner")
	if owner == "" then
		return
	end
	local has_privs, missing_privs = minetest.check_player_privs(owner, command.privs)
	if not has_privs then
		minetest.chat_send_player(owner, "You don't have permission to run this command (missing privileges: "..table.concat(missing_privs, ", ")..")")
		return
	end
	local player = resolve_player(meta:get_string("player"), pos)
	command.func(player, meta:get_string("param"))
end

local commandblock_action_off = function(pos, node)
	if node.name == "mesecons_commandblock:commandblock_on" then
		minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_off"})
	end
end

minetest.register_node("mesecons_commandblock:commandblock_off", {
	description = "Command Block",
	tiles = {"jeija_commandblock_off.png"},
	inventory_image = minetest.inventorycube("jeija_commandblock_off.png"),
	groups = {cracky=2, mesecon_effector_off=1},
	on_construct = construct,
	after_place_node = after_place,
	on_receive_fields = receive_fields,
	can_dig = function(pos,player)
		local owner = minetest.get_meta(pos):get_string("owner")
		return owner == "" or owner == player:get_player_name()
	end,
	sounds = default.node_sound_stone_defaults(),
	mesecons = {effector = {
		action_on = commandblock_action_on
	}}
})

minetest.register_node("mesecons_commandblock:commandblock_on", {
	tiles = {"jeija_commandblock_on.png"},
	groups = {cracky=2, mesecon_effector_on=1, not_in_creative_inventory=1},
	light_source = 10,
	drop = "mesecons_commandblock:commandblock_off",
	on_construct = construct,
	after_place_node = after_place,
	on_receive_fields = receive_fields,
	can_dig = function(pos,player)
		local owner = minetest.get_meta(pos):get_string("owner")
		return owner == "" or owner == player:get_player_name()
	end,
	sounds = default.node_sound_stone_defaults(),
	mesecons = {effector = {
		action_off = commandblock_action_off
	}}
})