Reveal help entry by default
[minetest_doc_identifier.git] / init.lua
blob22f48ce717f5cd78e4b3473e0bb68bbd355a1a12
1 -- Boilerplate to support localized strings if intllib mod is installed.
2 local S
3 if minetest.get_modpath("intllib") then
4 S = intllib.Getter()
5 else
6 S = function(s) return s end
7 end
9 local doc_identifier = {}
11 doc_identifier.registered_objects = {}
13 -- API
14 doc.sub.identifier = {}
15 doc.sub.identifier.register_object = function(object_name, category_id, entry_id)
16 doc_identifier.registered_objects[object_name] = { category = category_id, entry = entry_id }
17 end
19 -- END OF API
21 doc_identifier.identify = function(itemstack, user, pointed_thing)
22 local username = user:get_player_name()
23 local show_message = function(username, itype, param)
24 local vsize = 2
25 local message
26 if itype == "error_item" then
27 message = S("No help entry for this item could be found.")
28 elseif itype == "error_node" then
29 message = S("No help entry for this block could be found.")
30 elseif itype == "error_unknown" then
31 vsize = vsize + 3
32 local mod
33 if param ~= nil then
34 local colon = string.find(param, ":")
35 if colon ~= nil and colon > 1 then
36 mod = string.sub(param,1,colon-1)
37 end
38 end
39 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 subgame or a mod has made a mistake")
40 message = message .. "\n\n"
42 if mod ~= nil then
43 if minetest.get_modpath(mod) ~= nil then
44 message = message .. string.format(S("It appears to originate from the mod “%s”, which is enabled."), mod)
45 message = message .. "\n"
46 else
47 message = message .. string.format(S("It appears to originate from the mod “%s”, which is not enabled!"), mod)
48 message = message .. "\n"
49 end
50 end
51 if param ~= nil then
52 message = message .. string.format(S("Its identifier is “%s”."), param)
53 end
54 elseif itype == "error_ignore" then
55 message = S("This block cannot be identified because the world has not materialized at this point yet. Try again in a few seconds.")
56 elseif itype == "error_object" or itype == "error_unknown_thing" then
57 message = S("No help entry for this object could be found.")
58 elseif itype == "player" then
59 message = S("This is a player.")
60 end
61 minetest.show_formspec(
62 username,
63 "doc_identifier:error_missing_item_info",
64 "size[12,"..vsize..";]" ..
65 "label[0,0.2;"..minetest.formspec_escape(message).."]" ..
66 "button_exit[4.5,"..(-0.5+vsize)..";3,1;okay;"..minetest.formspec_escape(S("OK")).."]"
68 end
69 if pointed_thing.type == "node" then
70 local pos = pointed_thing.under
71 local node = minetest.get_node(pos)
72 if minetest.registered_nodes[node.name] ~= nil then
73 local nodedef = minetest.registered_nodes[node.name]
74 if(node.name == "ignore") then
75 show_message(username, "error_ignore")
76 elseif doc.entry_exists("nodes", node.name) then
77 doc.show_entry(username, "nodes", node.name, true)
78 else
79 show_message(username, "error_node")
80 end
81 else
82 show_message(username, "error_unknown", node.name)
83 end
84 elseif pointed_thing.type == "object" then
85 local object = pointed_thing.ref
86 local le = object:get_luaentity()
87 if object:is_player() then
88 if minetest.get_modpath("doc_basics") ~= nil and doc.entry_exists("basics", "players") then
89 doc.show_entry(username, "basics", "players", true)
90 else
91 -- Fallback message
92 show_message(username, "player")
93 end
94 -- luaentity exists
95 elseif le ~= nil then
96 local ro = doc_identifier.registered_objects[le.name]
97 -- Dropped items
98 if le.name == "__builtin:item" then
99 local itemstring = ItemStack(minetest.deserialize(le:get_staticdata()).itemstring):get_name()
100 if doc.entry_exists("nodes", itemstring) then
101 doc.show_entry(username, "nodes", itemstring, true)
102 elseif doc.entry_exists("tools", itemstring) then
103 doc.show_entry(username, "tools", itemstring, true)
104 elseif doc.entry_exists("craftitems", itemstring) then
105 doc.show_entry(username, "craftitems", itemstring, true)
106 elseif minetest.registered_items[itemstring] == nil or itemstring == "unknown" then
107 show_message(username, "error_unknown", itemstring)
108 else
109 show_message(username, "error_item")
111 -- Falling nodes
112 elseif le.name == "__builtin:falling_node" then
113 local itemstring = minetest.deserialize(le:get_staticdata()).name
114 if doc.entry_exists("nodes", itemstring) then
115 doc.show_entry(username, "nodes", itemstring, true)
117 -- A known registered object
118 elseif ro ~= nil then
119 doc.show_entry(username, ro.category, ro.entry, true)
120 -- Undefined object (error)
121 elseif minetest.registered_entities[le.name] == nil then
122 show_message(username, "error_unknown", le.name)
123 -- Other object (undocumented)
124 else
125 show_message(username, "error_object")
127 else
128 --show_message(username, "error_object")
129 show_message(username, "error_unknown")
131 elseif pointed_thing.type ~= "nothing" then
132 show_message(username, "error_unknown_thing")
134 return itemstack
137 function doc_identifier.solid_mode(itemstack, user, pointed_thing)
138 return ItemStack("doc_identifier:identifier_solid")
141 function doc_identifier.liquid_mode(itemstack, user, pointed_thing)
142 return ItemStack("doc_identifier:identifier_liquid")
145 minetest.register_tool("doc_identifier:identifier_solid", {
146 description = S("Lookup tool"),
147 _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."),
148 _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."),
149 _doc_items_hidden = false,
150 tool_capabilities = {},
151 range = 10,
152 wield_image = "doc_identifier_identifier.png",
153 inventory_image = "doc_identifier_identifier.png",
154 liquids_pointable = false,
155 on_use = doc_identifier.identify,
156 on_place = doc_identifier.liquid_mode,
157 on_secondary_use = doc_identifier.liquid_mode,
159 minetest.register_tool("doc_identifier:identifier_liquid", {
160 description = S("Lookup tool"),
161 _doc_items_create_entry = false,
162 tool_capabilities = {},
163 range = 10,
164 groups = { not_in_creative_inventory = 1, },
165 wield_image = "doc_identifier_identifier_liquid.png",
166 inventory_image = "doc_identifier_identifier_liquid.png",
167 liquids_pointable = true,
168 on_use = doc_identifier.identify,
169 on_place = doc_identifier.solid_mode,
170 on_secondary_use = doc_identifier.solid_mode,
173 minetest.register_craft({
174 output = "doc_identifier:identifier_solid",
175 recipe = { {"group:stick", "group:stick" },
176 {"", "group:stick"},
177 {"group:stick", ""} }
180 if minetest.get_modpath("default") ~= nil then
181 minetest.register_craft({
182 output = "doc_identifier:identifier_solid",
183 recipe = { { "default:glass" },
184 { "group:stick" } }
188 minetest.register_alias("doc_identifier:identifier", "doc_identifier:identifier_solid")
190 doc.add_entry_alias("tools", "doc_identifier:identifier_solid", "doc_identifier:identifier_liquid")