211 lines
8.1 KiB
Lua
Raw Normal View History

2016-10-22 18:41:43 +02:00
-- Boilerplate to support localized strings if intllib mod is installed.
local S
if minetest.get_modpath("intllib") then
S = intllib.Getter()
2016-10-22 18:41:43 +02:00
else
S = function(s) return s end
end
2016-08-13 13:46:18 +02:00
local doc_identifier = {}
2016-07-24 16:45:43 +02:00
doc_identifier.registered_objects = {}
-- API
2016-08-13 13:46:18 +02:00
doc.sub.identifier = {}
doc.sub.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
2016-07-24 18:27:15 +02:00
doc_identifier.identify = function(itemstack, user, pointed_thing)
2016-07-24 16:45:43 +02:00
local username = user:get_player_name()
2016-08-16 16:12:32 +02:00
local show_message = function(username, itype, param)
local vsize = 2
2016-08-04 22:53:06 +02:00
local message
2016-08-09 23:49:33 +02:00
if itype == "error_item" then
2016-10-22 18:41:43 +02:00
message = S("No help entry for this item could be found.")
2016-08-09 23:49:33 +02:00
elseif itype == "error_node" then
2016-10-22 18:41:43 +02:00
message = S("No help entry for this block could be found.")
elseif itype == "error_unknown" then
vsize = vsize + 3
local mod
2016-08-16 17:13:46 +02:00
if param ~= nil then
local colon = string.find(param, ":")
if colon ~= nil and colon > 1 then
mod = string.sub(param,1,colon-1)
end
end
2018-03-25 22:13:30 +02:00
message = S("Error: This node, item or object is undefined. This is always an error.\nThis can happen for the following reasons:\n• The mod which is required for it is not enabled\n• The author of the game or a mod has made a mistake")
2016-10-22 18:41:43 +02:00
message = message .. "\n\n"
if mod ~= nil then
if minetest.get_modpath(mod) ~= nil then
2016-10-22 18:41:43 +02:00
message = message .. string.format(S("It appears to originate from the mod “%s”, which is enabled."), mod)
message = message .. "\n"
else
2016-10-22 18:41:43 +02:00
message = message .. string.format(S("It appears to originate from the mod “%s”, which is not enabled!"), mod)
message = message .. "\n"
end
end
2016-08-16 17:13:46 +02:00
if param ~= nil then
2016-10-22 18:41:43 +02:00
message = message .. string.format(S("Its identifier is “%s”."), param)
2016-08-16 17:13:46 +02:00
end
2016-08-12 15:57:30 +02:00
elseif itype == "error_ignore" then
2016-10-22 18:41:43 +02:00
message = S("This block cannot be identified because the world has not materialized at this point yet. Try again in a few seconds.")
elseif itype == "error_object" or itype == "error_unknown_thing" then
2016-10-22 18:41:43 +02:00
message = S("No help entry for this object could be found.")
2016-08-09 23:49:33 +02:00
elseif itype == "player" then
2016-10-22 18:41:43 +02:00
message = S("This is a player.")
2016-08-04 22:53:06 +02:00
end
minetest.show_formspec(
username,
"doc_identifier:error_missing_item_info",
2016-10-22 18:41:43 +02:00
"size[12,"..vsize..";]" ..
"label[0,0.2;"..minetest.formspec_escape(message).."]" ..
"button_exit[4.5,"..(-0.5+vsize)..";3,1;okay;"..minetest.formspec_escape(S("OK")).."]"
2016-08-04 22:53:06 +02:00
)
end
2016-07-24 16:45:43 +02:00
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]
2016-08-01 23:53:48 +02:00
if(node.name == "ignore") then
2016-08-12 15:57:30 +02:00
show_message(username, "error_ignore")
elseif doc.entry_exists("nodes", node.name) then
2016-08-30 23:25:49 +02:00
doc.show_entry(username, "nodes", node.name, true)
2016-08-01 23:53:48 +02:00
else
2016-08-09 23:49:33 +02:00
show_message(username, "error_node")
2016-08-04 22:53:06 +02:00
end
2016-08-16 16:12:32 +02:00
else
show_message(username, "error_unknown", node.name)
2016-08-04 22:53:06 +02:00
end
elseif pointed_thing.type == "object" then
local object = pointed_thing.ref
local le = object:get_luaentity()
if object:is_player() then
2016-08-15 16:49:23 +02:00
if minetest.get_modpath("doc_basics") ~= nil and doc.entry_exists("basics", "players") then
2016-08-30 23:25:49 +02:00
doc.show_entry(username, "basics", "players", true)
2016-08-15 16:49:23 +02:00
else
-- Fallback message
show_message(username, "player")
end
2016-08-10 03:16:01 +02:00
-- luaentity exists
2016-08-04 22:53:06 +02:00
elseif le ~= nil then
local ro = doc_identifier.registered_objects[le.name]
2016-08-04 22:53:06 +02:00
-- 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
2016-08-30 23:25:49 +02:00
doc.show_entry(username, "nodes", itemstring, true)
2016-08-04 22:53:06 +02:00
elseif doc.entry_exists("tools", itemstring) then
2016-08-30 23:25:49 +02:00
doc.show_entry(username, "tools", itemstring, true)
2016-08-04 22:53:06 +02:00
elseif doc.entry_exists("craftitems", itemstring) then
2016-08-30 23:25:49 +02:00
doc.show_entry(username, "craftitems", itemstring, true)
2016-08-16 16:12:32 +02:00
elseif minetest.registered_items[itemstring] == nil or itemstring == "unknown" then
show_message(username, "error_unknown", itemstring)
2016-08-04 22:53:06 +02:00
else
2016-08-09 23:49:33 +02:00
show_message(username, "error_item")
2016-08-04 22:53:06 +02:00
end
2016-08-04 23:12:44 +02:00
-- Falling nodes
elseif le.name == "__builtin:falling_node" then
local itemstring = minetest.deserialize(le:get_staticdata()).name
if doc.entry_exists("nodes", itemstring) then
2016-08-30 23:25:49 +02:00
doc.show_entry(username, "nodes", itemstring, true)
2016-08-04 23:12:44 +02:00
end
-- A known registered object
elseif ro ~= nil then
2016-08-30 23:25:49 +02:00
doc.show_entry(username, ro.category, ro.entry, true)
2016-08-16 17:13:46 +02:00
-- Undefined object (error)
elseif minetest.registered_entities[le.name] == nil then
show_message(username, "error_unknown", le.name)
-- Other object (undocumented)
else
show_message(username, "error_object")
2016-08-01 23:53:48 +02:00
end
else
2016-08-16 17:13:46 +02:00
--show_message(username, "error_object")
show_message(username, "error_unknown")
2016-07-24 16:45:43 +02:00
end
elseif pointed_thing.type ~= "nothing" then
show_message(username, "error_unknown_thing")
2016-07-24 16:45:43 +02:00
end
return itemstack
end
2016-07-24 18:27:15 +02:00
function doc_identifier.solid_mode(itemstack, user, pointed_thing)
-- Use pointed node's on_rightclick function first, if present
if pointed_thing.type == "node" then
local node = minetest.get_node(pointed_thing.under)
if user and not user:get_player_control().sneak then
if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, user, itemstack) or itemstack
end
end
end
2016-07-24 18:27:15 +02:00
return ItemStack("doc_identifier:identifier_solid")
end
function doc_identifier.liquid_mode(itemstack, user, pointed_thing)
-- Use pointed node's on_rightclick function first, if present
if pointed_thing.type == "node" then
local node = minetest.get_node(pointed_thing.under)
if user and not user:get_player_control().sneak then
if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, user, itemstack) or itemstack
end
end
end
2016-07-24 18:27:15 +02:00
return ItemStack("doc_identifier:identifier_liquid")
end
2016-07-24 16:45:43 +02:00
minetest.register_tool("doc_identifier:identifier_solid", {
2016-10-22 18:41:43 +02:00
description = S("Lookup tool"),
2016-11-16 06:36:16 +01:00
_doc_items_longdesc = S("This useful little helper can be used to quickly learn more about about one's closer environment. It identifies and analyzes blocks, items and other things and it shows extensive information about the thing on which it is used."),
2016-12-12 12:45:12 +01:00
_doc_items_usagehelp = S("Punch any block, item or other thing about you wish to learn more about. This will open up the appropriate help entry. 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."),
2016-12-22 18:54:40 +01:00
_doc_items_hidden = false,
2016-07-24 16:45:43 +02:00
tool_capabilities = {},
2016-07-24 19:05:11 +02:00
range = 10,
2016-07-24 16:45:43 +02:00
wield_image = "doc_identifier_identifier.png",
inventory_image = "doc_identifier_identifier.png",
liquids_pointable = false,
2016-07-24 18:27:15 +02:00
on_use = doc_identifier.identify,
on_place = doc_identifier.liquid_mode,
on_secondary_use = doc_identifier.liquid_mode,
2016-07-24 16:45:43 +02:00
})
minetest.register_tool("doc_identifier:identifier_liquid", {
2016-10-22 18:41:43 +02:00
description = S("Lookup tool"),
2016-11-16 06:36:16 +01:00
_doc_items_create_entry = false,
2016-07-24 16:45:43 +02:00
tool_capabilities = {},
2016-07-24 19:05:11 +02:00
range = 10,
groups = { not_in_creative_inventory = 1, not_in_craft_guide = 1 },
2016-07-24 16:45:43 +02:00
wield_image = "doc_identifier_identifier_liquid.png",
inventory_image = "doc_identifier_identifier_liquid.png",
liquids_pointable = true,
2016-07-24 18:27:15 +02:00
on_use = doc_identifier.identify,
on_place = doc_identifier.solid_mode,
on_secondary_use = doc_identifier.solid_mode,
2016-07-24 16:45:43 +02:00
})
2016-08-01 23:53:48 +02:00
minetest.register_craft({
output = "doc_identifier:identifier_solid",
recipe = { {"group:stick", "group:stick" },
{"", "group:stick"},
{"group:stick", ""} }
})
2016-08-04 18:24:46 +02:00
if minetest.get_modpath("default") ~= nil then
minetest.register_craft({
output = "doc_identifier:identifier_solid",
recipe = { { "default:glass" },
{ "group:stick" } }
})
end
2016-08-01 23:53:48 +02:00
2016-07-24 16:45:43 +02:00
minetest.register_alias("doc_identifier:identifier", "doc_identifier:identifier_solid")
2016-07-24 17:57:44 +02:00
2016-12-28 17:35:31 +01:00
doc.add_entry_alias("tools", "doc_identifier:identifier_solid", "tools", "doc_identifier:identifier_liquid")