I am working on a command for my mod, that update a node in the location specified through a chatcommand to the block specified. This should be pretty simple, but I'm trying to write it in a way that players don't get all the weird errors and get confused (like me), but instead get simple error messages through the chat.
I'm having two issues. When I run /update default:dirt 125 1.5 -26.2, I get "attempt to index a nil value," at line 39.
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
local item_string_type = minetest.registered_items[node].type -- item string type
This line gets the type (node, craftitem, tool) for an if statement that confirms the type is node.
When I run the command the other way around, /update 125 1.5 -26.2 default:dirt, I get and error in the chat saying:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
2016-06-10 13:14:11: ERROR[Server]: Map::setNode(): Not allowing to place CONTENT_IGNORE while trying to replace "air" at (125,2,-26) (block (7,0,-2))
The /update functions accepts two values, name and param. player is then set from param, and node, p is set from param. Then I set item_string_type, for the if statement.
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
-- variables
local player = minetest.get_player_by_name(name) -- player name
local node, p = string.match(param, "^([^ ]+) *(.*)$")
local p = minetest.string_to_pos(p)
local item_string_type = minetest.registered_items[node].type -- item string type
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
-- Update Block
function server_tools.update_node(name, param)
-- variables
local player = minetest.get_player_by_name(name) -- player name
local node, p = string.match(param, "^([^ ]+) *(.*)$")
local p = minetest.string_to_pos(p)
local item_string_type = minetest.registered_items[node].type -- item string type
-- confirm node param
if minetest.registered_items[node] == nil or minetest.registered_items[node] == "" then
minetest.chat_send_player(name, "Invalid item string. Please try again.") --print to chat
minetest.log("action", "[Server_Tools] "..name.." tried to update a node with invalid item string: "..item_string) --print to log
else
-- make sure item string is not a craftitem of tool
if item_string_type == "tool" or item_string_type == "craftitem" then
minetest.chat_send_player(name, "Item string cannot be a "..item_string_type) --print to chat
minetest.log("action", "[Server_Tools] "..name.." tried to update a node to "..item_string_type) --print to log
else
-- confirm position params
if p.x == "" or p.x == nil or p.y == "" or p.y == nil or p.y == nil or p.z == "" or p.z == nil then
-- print error to chat and log
minetest.chat_send_player(name, "Invalid position coordinates. Please try again.") -- print to chat
minetest.log("action", "[Server_Tools] "..name.." tried to update a node using invalid coordinates.") --print to log
else
-- update node
minetest.set_node(p, {name = param})
end
end
end
end
You can browse the code of the whole mod
here, though functions.lua is missing several small fixes that I have not committed yet because of the above issues.