Move infostick, pencil, & wand into tools.lua
parent
aac506c6d2
commit
4c17be1eab
11
README.md
11
README.md
|
@ -6,7 +6,7 @@ A [Minetest](http://minetest.net/) mod that allows administrators with *server*
|
|||
|
||||
### Usage:
|
||||
|
||||
**Chat commands:**
|
||||
#### Chat commands:
|
||||
|
||||
- */getmeta <x> <y> <z> <key>*
|
||||
- prints the value of `key` in meta data of node at `x,y,z`.
|
||||
|
@ -15,6 +15,8 @@ A [Minetest](http://minetest.net/) mod that allows administrators with *server*
|
|||
- */unsetmeta <x> <y> <z> <key>*
|
||||
- Unsets the value of `key` in meta data of node at `x,y,z`.
|
||||
|
||||
#### Tools:
|
||||
|
||||
**Info stick:**
|
||||
|
||||
Invoke `/giveme alternode:infostick`. Use the infostick on a node to receive node coordinates, name, & some select meta info.
|
||||
|
@ -23,6 +25,13 @@ Invoke `/giveme alternode:infostick`. Use the infostick on a node to receive nod
|
|||
|
||||
The `alternode:pencil` is a tool for players to set/unset the `infotext` meta value of nodes within protected/owned areas.
|
||||
|
||||
- *left-click (use):* Opens formspec to set/unset infotext meta attribute.
|
||||
|
||||
**Wand:**
|
||||
|
||||
- *left-click (use):* Sets/Unsets user as owner.
|
||||
- *right-click (place):* Checks owner status & outputs to chat log.
|
||||
|
||||
### Licensing:
|
||||
|
||||
- Code: [MIT](LICENSE.txt)
|
||||
|
|
|
@ -1,74 +0,0 @@
|
|||
|
||||
local S = core.get_translator(alternode.modname)
|
||||
|
||||
|
||||
local function check_permissions(player)
|
||||
local pname = player:get_player_name()
|
||||
|
||||
local granted, missing = core.check_player_privs(pname, {server=true,})
|
||||
if not granted then
|
||||
core.chat_send_player(pname,
|
||||
S("You do not have privileges to use this item (missing priviliges: @1)", table.concat(missing, ", ")))
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
local function target_is_node(target)
|
||||
if target.type ~= "node" then
|
||||
core.chat_send_player(pname, S("This item only works on nodes"))
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
core.register_craftitem(alternode.modname .. ":infostick", {
|
||||
description = S("Tool for retrieving information about a node"),
|
||||
short_description = S("Info Stick"),
|
||||
inventory_image = "alternode_infostick.png",
|
||||
stack_max = 1,
|
||||
on_use = function(itemstack, user, pointed_thing)
|
||||
if not user:is_player() then return end
|
||||
if not check_permissions(user) then return end
|
||||
if not target_is_node(pointed_thing) then return end
|
||||
|
||||
local pname = user:get_player_name()
|
||||
local pos = core.get_pointed_thing_position(pointed_thing, false)
|
||||
local node = core.get_node_or_nil(pos)
|
||||
if not node then
|
||||
core.chat_send_player(pname, S("That doesn't seem to be a proper node"))
|
||||
return
|
||||
end
|
||||
local meta = core.get_meta(pos)
|
||||
|
||||
local infostring = S("pos: x@=@1, y@=@2, z@=@3; name@=@4",
|
||||
tostring(pos.x), tostring(pos.y), tostring(pos.z), node.name)
|
||||
|
||||
for _, key in ipairs({"id", "infotext", "owner"}) do
|
||||
local value = meta:get_string(key)
|
||||
if value and value ~= "" then
|
||||
infostring = infostring .. "; "
|
||||
.. key .. "=" .. value
|
||||
end
|
||||
end
|
||||
|
||||
core.chat_send_player(pname, infostring)
|
||||
end,
|
||||
on_place = function(itemstack, placer, pointed_thing)
|
||||
if not placer:is_player() then return end
|
||||
if not check_permissions(placer) then return end
|
||||
if not target_is_node(pointed_thing) then return end
|
||||
|
||||
local pos = core.get_pointed_thing_position(pointed_thing, false)
|
||||
local node = core.get_node_or_nil(pos)
|
||||
if not node then
|
||||
core.chat_send_player(pname, S("That doesn't seem to be a proper node"))
|
||||
return
|
||||
end
|
||||
|
||||
alternode.show_formspec(pos, node, placer)
|
||||
end,
|
||||
})
|
4
init.lua
4
init.lua
|
@ -7,9 +7,7 @@ local scripts = {
|
|||
"api",
|
||||
"commands",
|
||||
"formspec",
|
||||
"infostick",
|
||||
"pencil",
|
||||
"wand",
|
||||
"tools",
|
||||
}
|
||||
|
||||
for _, script in ipairs(scripts) do
|
||||
|
|
97
pencil.lua
97
pencil.lua
|
@ -1,97 +0,0 @@
|
|||
|
||||
local S = core.get_translator(alternode.modname)
|
||||
|
||||
|
||||
local use_s_protect = core.global_exists("s_protect")
|
||||
|
||||
local function is_area_owner(pos, pname)
|
||||
if not pname then return false end
|
||||
|
||||
if use_s_protect then
|
||||
local claim = s_protect.get_claim(pos)
|
||||
if claim then return pname == claim.owner end
|
||||
else
|
||||
return core.is_protected(pos, pname)
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
local function check_node_pos(target)
|
||||
if target.type ~= "node" then return end
|
||||
local pos = core.get_pointed_thing_position(target, false)
|
||||
if not core.get_node_or_nil(pos) then return end
|
||||
|
||||
return pos
|
||||
end
|
||||
|
||||
|
||||
--- The pencil is an infotext editor for players.
|
||||
--
|
||||
-- Players can alter infotext on nodes they own or located
|
||||
-- areas owned by them.
|
||||
|
||||
core.register_craftitem(alternode.modname .. ":pencil", {
|
||||
description = S("Tool for editing node infotext"),
|
||||
short_description = S("Pencil"),
|
||||
inventory_image = "alternode_pencil.png",
|
||||
stack_max = 1,
|
||||
on_use = function(itemstack, user, pointed_thing)
|
||||
if not user:is_player() then return end
|
||||
local pos = check_node_pos(pointed_thing)
|
||||
if not pos then return end
|
||||
|
||||
local pname = user:get_player_name()
|
||||
if not is_area_owner(pos, pname) then
|
||||
core.chat_send_player(pname, "You cannot alter nodes in areas you do not own")
|
||||
return
|
||||
end
|
||||
|
||||
local infotext = core.get_meta(pos):get_string("infotext")
|
||||
local formspec = "formspec_version[4]"
|
||||
.. "size[6,4]"
|
||||
.. "textarea[1,1;4,1.5;input;Infotext;" .. infotext .. "]"
|
||||
.. "button_exit[1.5,2.75;1.25,0.75;btn_write;Write]"
|
||||
.. "button_exit[3.3,2.75;1.25,0.75;btn_erase;Erase]"
|
||||
|
||||
-- store pos info for retrieval in callbacks
|
||||
user:get_meta():set_string(alternode.modname .. ":pencil:pos", core.serialize(pos))
|
||||
core.show_formspec(pname, alternode.modname .. ":pencil", formspec)
|
||||
end,
|
||||
})
|
||||
|
||||
|
||||
-- craft recipe
|
||||
-- FIXME: don't need to depend on default if "group:stick" exists
|
||||
if core.global_exists("default") and core.global_exists("technic") then
|
||||
core.register_craft({
|
||||
output = alternode.modname .. ":pencil",
|
||||
recipe = {
|
||||
{"", "", "technic:lead_lump"},
|
||||
{"", "group:stick", ""},
|
||||
{"technic:rubber", "", ""},
|
||||
},
|
||||
})
|
||||
end
|
||||
|
||||
|
||||
core.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname == alternode.modname .. ":pencil" then
|
||||
-- FIXME: how to get node meta without storing in player meta?
|
||||
local pmeta = player:get_meta()
|
||||
local pos = core.deserialize(pmeta:get_string(alternode.modname .. ":pencil:pos"))
|
||||
local nmeta = core.get_meta(pos)
|
||||
|
||||
if fields.btn_write then
|
||||
if fields.input:trim() == "" then
|
||||
nmeta:set_string("infotext", nil)
|
||||
else
|
||||
nmeta:set_string("infotext", fields.input)
|
||||
end
|
||||
elseif fields.btn_erase then
|
||||
nmeta:set_string("infotext", nil)
|
||||
end
|
||||
|
||||
pmeta:set_string(alternode.modname .. ":pencil:pos", nil)
|
||||
end
|
||||
end)
|
|
@ -0,0 +1,233 @@
|
|||
|
||||
local S = core.get_translator(alternode.modname)
|
||||
|
||||
local use_s_protect = core.global_exists("s_protect")
|
||||
|
||||
|
||||
local function check_permissions(player)
|
||||
local pname = player:get_player_name()
|
||||
|
||||
local granted, missing = core.check_player_privs(pname, {server=true,})
|
||||
if not granted then
|
||||
core.chat_send_player(pname,
|
||||
S("You do not have privileges to use this item (missing priviliges: @1)", table.concat(missing, ", ")))
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
local function target_is_node(target)
|
||||
if target.type ~= "node" then
|
||||
core.chat_send_player(pname, S("This item only works on nodes"))
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
local function is_area_owner(pos, pname)
|
||||
if not pname then return false end
|
||||
|
||||
if use_s_protect then
|
||||
local claim = s_protect.get_claim(pos)
|
||||
if claim then return pname == claim.owner end
|
||||
else
|
||||
return core.is_protected(pos, pname)
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
local function check_node_pos(target)
|
||||
if target.type ~= "node" then return end
|
||||
local pos = core.get_pointed_thing_position(target, false)
|
||||
if not core.get_node_or_nil(pos) then return end
|
||||
|
||||
return pos
|
||||
end
|
||||
|
||||
|
||||
--- Admin tool to retrieve node node coordinates, name, & some select meta info.
|
||||
--
|
||||
-- @craftitem alternode:infostick
|
||||
-- @use
|
||||
-- @place
|
||||
core.register_craftitem(alternode.modname .. ":infostick", {
|
||||
description = S("Tool for retrieving information about a node"),
|
||||
short_description = S("Info Stick"),
|
||||
inventory_image = "alternode_infostick.png",
|
||||
stack_max = 1,
|
||||
on_use = function(itemstack, user, pointed_thing)
|
||||
if not user:is_player() then return end
|
||||
if not check_permissions(user) then return end
|
||||
if not target_is_node(pointed_thing) then return end
|
||||
|
||||
local pname = user:get_player_name()
|
||||
local pos = core.get_pointed_thing_position(pointed_thing, false)
|
||||
local node = core.get_node_or_nil(pos)
|
||||
if not node then
|
||||
core.chat_send_player(pname, S("That doesn't seem to be a proper node"))
|
||||
return
|
||||
end
|
||||
local meta = core.get_meta(pos)
|
||||
|
||||
local infostring = S("pos: x@=@1, y@=@2, z@=@3; name@=@4",
|
||||
tostring(pos.x), tostring(pos.y), tostring(pos.z), node.name)
|
||||
|
||||
for _, key in ipairs({"id", "infotext", "owner"}) do
|
||||
local value = meta:get_string(key)
|
||||
if value and value ~= "" then
|
||||
infostring = infostring .. "; "
|
||||
.. key .. "=" .. value
|
||||
end
|
||||
end
|
||||
|
||||
core.chat_send_player(pname, infostring)
|
||||
end,
|
||||
on_place = function(itemstack, placer, pointed_thing)
|
||||
if not placer:is_player() then return end
|
||||
if not check_permissions(placer) then return end
|
||||
if not target_is_node(pointed_thing) then return end
|
||||
|
||||
local pos = core.get_pointed_thing_position(pointed_thing, false)
|
||||
local node = core.get_node_or_nil(pos)
|
||||
if not node then
|
||||
core.chat_send_player(pname, S("That doesn't seem to be a proper node"))
|
||||
return
|
||||
end
|
||||
|
||||
alternode.show_formspec(pos, node, placer)
|
||||
end,
|
||||
})
|
||||
|
||||
|
||||
--- Player tool to alter *infotext* meta value.
|
||||
--
|
||||
-- @craftitem alternode:pencil
|
||||
-- @use
|
||||
-- @place
|
||||
core.register_craftitem(alternode.modname .. ":pencil", {
|
||||
description = S("Tool for editing node infotext"),
|
||||
short_description = S("Pencil"),
|
||||
inventory_image = "alternode_pencil.png",
|
||||
stack_max = 1,
|
||||
on_use = function(itemstack, user, pointed_thing)
|
||||
if not user:is_player() then return end
|
||||
local pos = check_node_pos(pointed_thing)
|
||||
if not pos then return end
|
||||
|
||||
local pname = user:get_player_name()
|
||||
if not is_area_owner(pos, pname) then
|
||||
core.chat_send_player(pname, "You cannot alter nodes in areas you do not own")
|
||||
return
|
||||
end
|
||||
|
||||
local infotext = core.get_meta(pos):get_string("infotext")
|
||||
local formspec = "formspec_version[4]"
|
||||
.. "size[6,4]"
|
||||
.. "textarea[1,1;4,1.5;input;Infotext;" .. infotext .. "]"
|
||||
.. "button_exit[1.5,2.75;1.25,0.75;btn_write;Write]"
|
||||
.. "button_exit[3.3,2.75;1.25,0.75;btn_erase;Erase]"
|
||||
|
||||
-- store pos info for retrieval in callbacks
|
||||
user:get_meta():set_string(alternode.modname .. ":pencil:pos", core.serialize(pos))
|
||||
core.show_formspec(pname, alternode.modname .. ":pencil", formspec)
|
||||
end,
|
||||
})
|
||||
|
||||
|
||||
-- craft recipe
|
||||
-- FIXME: don't need to depend on default if "group:stick" exists
|
||||
if core.global_exists("default") and core.global_exists("technic") then
|
||||
core.register_craft({
|
||||
output = alternode.modname .. ":pencil",
|
||||
recipe = {
|
||||
{"", "", "technic:lead_lump"},
|
||||
{"", "group:stick", ""},
|
||||
{"technic:rubber", "", ""},
|
||||
},
|
||||
})
|
||||
end
|
||||
|
||||
|
||||
core.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname == alternode.modname .. ":pencil" then
|
||||
-- FIXME: how to get node meta without storing in player meta?
|
||||
local pmeta = player:get_meta()
|
||||
local pos = core.deserialize(pmeta:get_string(alternode.modname .. ":pencil:pos"))
|
||||
local nmeta = core.get_meta(pos)
|
||||
|
||||
if fields.btn_write then
|
||||
if fields.input:trim() == "" then
|
||||
nmeta:set_string("infotext", nil)
|
||||
else
|
||||
nmeta:set_string("infotext", fields.input)
|
||||
end
|
||||
elseif fields.btn_erase then
|
||||
nmeta:set_string("infotext", nil)
|
||||
end
|
||||
|
||||
pmeta:set_string(alternode.modname .. ":pencil:pos", nil)
|
||||
end
|
||||
end)
|
||||
|
||||
|
||||
--- Player tool to set/unset *owner* meta value.
|
||||
--
|
||||
-- @craftitem alternode:wand
|
||||
-- @use
|
||||
-- @place
|
||||
core.register_craftitem(alternode.modname .. ":wand", {
|
||||
description = "Tool for setting node owner",
|
||||
short_description = "Ownit Wand",
|
||||
inventory_image = "alternode_wand.png",
|
||||
stack_max = 1,
|
||||
on_use = function(itemstack, user, pointed_thing)
|
||||
if not user:is_player() then return end
|
||||
local pos = check_node_pos(pointed_thing)
|
||||
if not pos then return end
|
||||
|
||||
local pname = user:get_player_name()
|
||||
local nmeta = core.get_meta(pos)
|
||||
local node_owner = nmeta:get_string("owner")
|
||||
|
||||
if node_owner ~= "" and pname ~= node_owner then
|
||||
core.chat_send_player(pname, "You cannot take ownership of a node owned by " .. node_owner)
|
||||
return
|
||||
end
|
||||
|
||||
local unown = false
|
||||
if pname == node_owner then unown = true end
|
||||
|
||||
if unown then
|
||||
nmeta:set_string("owner", nil)
|
||||
core.chat_send_player(pname, "You no longer own this node")
|
||||
else
|
||||
if not is_area_owner(pos, pname) then
|
||||
core.chat_send_player(pname, "You cannot take ownership of a node that is not in an area owned by you")
|
||||
return
|
||||
end
|
||||
|
||||
nmeta:set_string("owner", pname)
|
||||
core.chat_send_player(pname, "You now own this node")
|
||||
end
|
||||
end,
|
||||
on_place = function(itemstack, placer, pointed_thing)
|
||||
if not placer:is_player() then return end
|
||||
local pos = check_node_pos(pointed_thing)
|
||||
if not pos then return end
|
||||
|
||||
local pname = placer:get_player_name()
|
||||
local node_owner = core.get_meta(pos):get_string("owner")
|
||||
|
||||
if node_owner == "" then
|
||||
core.chat_send_player(pname, "This node is unowned")
|
||||
else
|
||||
core.chat_send_player(pname, "This node is owned by " .. node_owner)
|
||||
end
|
||||
|
||||
return true
|
||||
end,
|
||||
})
|
||||
core.register_alias("ownit:wand", alternode.modname .. ":wand")
|
78
wand.lua
78
wand.lua
|
@ -1,78 +0,0 @@
|
|||
|
||||
local use_s_protect = core.global_exists("s_protect")
|
||||
|
||||
local function is_area_owner(pos, pname)
|
||||
if not pname then return false end
|
||||
|
||||
if use_s_protect then
|
||||
local claim = s_protect.get_claim(pos)
|
||||
if claim then return pname == claim.owner end
|
||||
else
|
||||
return core.is_protected(pos, pname)
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
local function check_node_pos(target)
|
||||
if target.type ~= "node" then return end
|
||||
local pos = core.get_pointed_thing_position(target, false)
|
||||
if not core.get_node_or_nil(pos) then return end
|
||||
|
||||
return pos
|
||||
end
|
||||
|
||||
|
||||
core.register_craftitem(alternode.modname .. ":wand", {
|
||||
description = "Tool for setting node owner",
|
||||
short_description = "Ownit Wand",
|
||||
inventory_image = "alternode_wand.png",
|
||||
stack_max = 1,
|
||||
on_use = function(itemstack, user, pointed_thing)
|
||||
if not user:is_player() then return end
|
||||
local pos = check_node_pos(pointed_thing)
|
||||
if not pos then return end
|
||||
|
||||
local pname = user:get_player_name()
|
||||
local nmeta = core.get_meta(pos)
|
||||
local node_owner = nmeta:get_string("owner")
|
||||
|
||||
if node_owner ~= "" and pname ~= node_owner then
|
||||
core.chat_send_player(pname, "You cannot take ownership of a node owned by " .. node_owner)
|
||||
return
|
||||
end
|
||||
|
||||
local unown = false
|
||||
if pname == node_owner then unown = true end
|
||||
|
||||
if unown then
|
||||
nmeta:set_string("owner", nil)
|
||||
core.chat_send_player(pname, "You no longer own this node")
|
||||
else
|
||||
if not is_area_owner(pos, pname) then
|
||||
core.chat_send_player(pname, "You cannot take ownership of a node that is not in an area owned by you")
|
||||
return
|
||||
end
|
||||
|
||||
nmeta:set_string("owner", pname)
|
||||
core.chat_send_player(pname, "You now own this node")
|
||||
end
|
||||
end,
|
||||
on_place = function(itemstack, placer, pointed_thing)
|
||||
if not placer:is_player() then return end
|
||||
local pos = check_node_pos(pointed_thing)
|
||||
if not pos then return end
|
||||
|
||||
local pname = placer:get_player_name()
|
||||
local node_owner = core.get_meta(pos):get_string("owner")
|
||||
|
||||
if node_owner == "" then
|
||||
core.chat_send_player(pname, "This node is unowned")
|
||||
else
|
||||
core.chat_send_player(pname, "This node is owned by " .. node_owner)
|
||||
end
|
||||
|
||||
return true
|
||||
end,
|
||||
})
|
||||
core.register_alias("ownit:wand", alternode.modname .. ":wand")
|
Loading…
Reference in New Issue