2016-08-10 03:16:01 +02:00

142 lines
5.1 KiB
Lua

doc_identifier = {}
doc_identifier.registered_objects = {}
-- API
doc_identifier.register_object = function(object_name, category_id, entry_id)
doc_identifier.registered_objects[object_name] = { category = category_id, entry = entry_id }
end
-- END OF API
doc_identifier.identify = function(itemstack, user, pointed_thing)
local username = user:get_player_name()
local show_message = function(username, itype)
local message
if itype == "error_item" then
message = "No help entry for this item could be found."
elseif itype == "error_node" then
message = "No help entry for this block could be found."
elseif itype == "error_object" or itype == "error_unknown_thing" then
message = "No help entry for this object could be found."
elseif itype == "player" then
message = "This is a player."
end
minetest.show_formspec(
username,
"doc_identifier:error_missing_item_info",
"size[12,2;]label[0,0.2;"..minetest.formspec_escape(message).."]button_exit[4.5,1.5;3,1;okay;OK]"
)
end
if pointed_thing.type == "node" then
local pos = pointed_thing.under
local node = minetest.get_node(pos)
if minetest.registered_nodes[node.name] ~= nil then
local nodedef = minetest.registered_nodes[node.name]
if(node.name == "ignore") then
show_message(username, "error_node")
end
if doc.entry_exists("nodes", node.name) then
doc.show_entry(username, "nodes", node.name)
else
show_message(username, "error_node")
end
end
elseif pointed_thing.type == "object" then
local object = pointed_thing.ref
local le = object:get_luaentity()
if object:is_player() then
-- TODO: Show entry about players instead
show_message(username, "player")
-- luaentity exists
elseif le ~= nil then
local ro = doc_identifier.registered_objects[le.name]
-- Dropped items
if le.name == "__builtin:item" then
local itemstring = ItemStack(minetest.deserialize(le:get_staticdata()).itemstring):get_name()
if doc.entry_exists("nodes", itemstring) then
doc.show_entry(username, "nodes", itemstring)
elseif doc.entry_exists("tools", itemstring) then
doc.show_entry(username, "tools", itemstring)
elseif doc.entry_exists("craftitems", itemstring) then
doc.show_entry(username, "craftitems", itemstring)
else
show_message(username, "error_item")
end
-- Falling nodes
elseif le.name == "__builtin:falling_node" then
local itemstring = minetest.deserialize(le:get_staticdata()).name
if doc.entry_exists("nodes", itemstring) then
doc.show_entry(username, "nodes", itemstring)
end
-- A known registered object
elseif ro ~= nil then
doc.show_entry(username, ro.category, ro.entry)
-- Other object (unknown)
else
show_message(username, "error_object")
end
else
show_message(username, "error_object")
end
elseif pointed_thing.type ~= "nothing" then
show_message(username, "error_unknown_thing")
end
return itemstack
end
function doc_identifier.solid_mode(itemstack, user, pointed_thing)
return ItemStack("doc_identifier:identifier_solid")
end
function doc_identifier.liquid_mode(itemstack, user, pointed_thing)
return ItemStack("doc_identifier:identifier_liquid")
end
minetest.register_tool("doc_identifier:identifier_solid", {
description = "Lookup tool",
tool_capabilities = {},
range = 10,
wield_image = "doc_identifier_identifier.png",
inventory_image = "doc_identifier_identifier.png",
liquids_pointable = false,
on_use = doc_identifier.identify,
on_place = doc_identifier.liquid_mode,
on_secondary_use = doc_identifier.liquid_mode,
})
minetest.register_tool("doc_identifier:identifier_liquid", {
description = "Lookup tool",
tool_capabilities = {},
range = 10,
groups = { not_in_creative_inventory = 1 },
wield_image = "doc_identifier_identifier_liquid.png",
inventory_image = "doc_identifier_identifier_liquid.png",
liquids_pointable = true,
on_use = doc_identifier.identify,
on_place = doc_identifier.solid_mode,
on_secondary_use = doc_identifier.solid_mode,
})
minetest.register_craft({
output = "doc_identifier:identifier_solid",
recipe = { {"group:stick", "group:stick" },
{"", "group:stick"},
{"group:stick", ""} }
})
if minetest.get_modpath("default") ~= nil then
minetest.register_craft({
output = "doc_identifier:identifier_solid",
recipe = { { "default:glass" },
{ "group:stick" } }
})
end
minetest.register_alias("doc_identifier:identifier", "doc_identifier:identifier_solid")
-- Add documentation
doc.sub.items.set_items_longdesc(
{ ["doc_identifier:identifier_solid"] = "This useful little helper can be used to quickly learn more about about one's closer environment. It identifies and analyzes blocks and dropped items and it shows extensive information about the item on which it is used." })
doc.sub.items.set_items_usagehelp(
{ ["doc_identifier:identifier_solid"] = "Punch any block or item about you wish to learn more about. This will open up the Documentation System entry of this particular item. The tool comes in two modes which are changed by a rightclick. In liquid mode (blue) this tool points to liquids as well while in solid mode (red) this is not the case. Liquid mode is required if you want to identify a liquid." })