Version 1.3.1
[minetest_doc_identifier.git] / init.lua
blob7630c6d71e8e1493d360f03eb69e7cbf4ff35bc7
1 local S = minetest.get_translator("doc_identifier")
3 local doc_identifier = {}
5 doc_identifier.registered_objects = {}
7 -- API
8 doc.sub.identifier = {}
9 doc.sub.identifier.register_object = function(object_name, category_id, entry_id)
10 doc_identifier.registered_objects[object_name] = { category = category_id, entry = entry_id }
11 end
13 -- END OF API
15 doc_identifier.identify = function(itemstack, user, pointed_thing)
16 local username = user:get_player_name()
17 local show_message = function(username, itype, param)
18 local vsize = 2
19 local message
20 if itype == "error_item" then
21 message = S("No help entry for this item could be found.")
22 elseif itype == "error_node" then
23 message = S("No help entry for this block could be found.")
24 elseif itype == "error_unknown" then
25 vsize = vsize + 2
26 local mod
27 if param ~= nil then
28 local colon = string.find(param, ":")
29 if colon ~= nil and colon > 1 then
30 mod = string.sub(param,1,colon-1)
31 end
32 end
33 message = S("Error: This node, item or object is undefined. This is always an error.").."\n"..
34 S("This can happen for the following reasons:").."\n"..
35 S("• The mod which is required for it is not enabled").."\n"..
36 S("• The author of the game or a mod has made a mistake")
37 message = message .. "\n\n"
39 if mod ~= nil then
40 if minetest.get_modpath(mod) ~= nil then
41 message = message .. S("It appears to originate from the mod “@1”, which is enabled.", mod)
42 message = message .. "\n"
43 else
44 message = message .. S("It appears to originate from the mod “@1”, which is not enabled!", mod)
45 message = message .. "\n"
46 end
47 end
48 if param ~= nil then
49 message = message .. S("Its identifier is “@1”.", param)
50 end
51 elseif itype == "error_ignore" then
52 message = S("This block cannot be identified because the world has not materialized at this point yet. Try again in a few seconds.")
53 elseif itype == "error_object" or itype == "error_unknown_thing" then
54 message = S("No help entry for this object could be found.")
55 elseif itype == "player" then
56 message = S("This is a player.")
57 end
58 minetest.show_formspec(
59 username,
60 "doc_identifier:error_missing_item_info",
61 "size[10,"..vsize..";]" ..
62 "textarea[0.5,0.2;10,"..(vsize-0.2)..";;;"..minetest.formspec_escape(message).."]" ..
63 "button_exit[3.75,"..(-0.5+vsize)..";3,1;okay;"..minetest.formspec_escape(S("OK")).."]"
65 end
66 if pointed_thing.type == "node" then
67 local pos = pointed_thing.under
68 local node = minetest.get_node(pos)
69 if minetest.registered_nodes[node.name] ~= nil then
70 local nodedef = minetest.registered_nodes[node.name]
71 if(node.name == "ignore") then
72 show_message(username, "error_ignore")
73 elseif doc.entry_exists("nodes", node.name) then
74 doc.show_entry(username, "nodes", node.name, true)
75 else
76 show_message(username, "error_node")
77 end
78 else
79 show_message(username, "error_unknown", node.name)
80 end
81 elseif pointed_thing.type == "object" then
82 local object = pointed_thing.ref
83 local le = object:get_luaentity()
84 if object:is_player() then
85 if minetest.get_modpath("doc_basics") ~= nil and doc.entry_exists("basics", "players") then
86 doc.show_entry(username, "basics", "players", true)
87 else
88 -- Fallback message
89 show_message(username, "player")
90 end
91 -- luaentity exists
92 elseif le ~= nil then
93 local ro = doc_identifier.registered_objects[le.name]
94 -- Dropped items
95 if le.name == "__builtin:item" then
96 local itemstring = ItemStack(minetest.deserialize(le:get_staticdata()).itemstring):get_name()
97 if doc.entry_exists("nodes", itemstring) then
98 doc.show_entry(username, "nodes", itemstring, true)
99 elseif doc.entry_exists("tools", itemstring) then
100 doc.show_entry(username, "tools", itemstring, true)
101 elseif doc.entry_exists("craftitems", itemstring) then
102 doc.show_entry(username, "craftitems", itemstring, true)
103 elseif minetest.registered_items[itemstring] == nil or itemstring == "unknown" then
104 show_message(username, "error_unknown", itemstring)
105 else
106 show_message(username, "error_item")
108 -- Falling nodes
109 elseif le.name == "__builtin:falling_node" then
110 local itemstring = minetest.deserialize(le:get_staticdata()).name
111 if doc.entry_exists("nodes", itemstring) then
112 doc.show_entry(username, "nodes", itemstring, true)
114 -- A known registered object
115 elseif ro ~= nil then
116 doc.show_entry(username, ro.category, ro.entry, true)
117 -- Undefined object (error)
118 elseif minetest.registered_entities[le.name] == nil then
119 show_message(username, "error_unknown", le.name)
120 -- Other object (undocumented)
121 else
122 show_message(username, "error_object")
124 else
125 --show_message(username, "error_object")
126 show_message(username, "error_unknown")
128 elseif pointed_thing.type ~= "nothing" then
129 show_message(username, "error_unknown_thing")
131 return itemstack
134 function doc_identifier.solid_mode(itemstack, user, pointed_thing)
135 -- Use pointed node's on_rightclick function first, if present
136 if pointed_thing.type == "node" then
137 local node = minetest.get_node(pointed_thing.under)
138 if user and not user:get_player_control().sneak then
139 if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
140 return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, user, itemstack) or itemstack
145 return ItemStack("doc_identifier:identifier_solid")
148 function doc_identifier.liquid_mode(itemstack, user, pointed_thing)
149 -- Use pointed node's on_rightclick function first, if present
150 if pointed_thing.type == "node" then
151 local node = minetest.get_node(pointed_thing.under)
152 if user and not user:get_player_control().sneak then
153 if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
154 return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, user, itemstack) or itemstack
159 return ItemStack("doc_identifier:identifier_liquid")
162 minetest.register_tool("doc_identifier:identifier_solid", {
163 description = S("Lookup Tool"),
164 _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."),
165 _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 using. In liquid mode, this tool points to liquids as well while in solid mode this is not the case."),
166 _doc_items_hidden = false,
167 tool_capabilities = {},
168 range = 10,
169 groups = { disable_repair = 1 },
170 wield_image = "doc_identifier_identifier.png",
171 inventory_image = "doc_identifier_identifier.png",
172 liquids_pointable = false,
173 on_use = doc_identifier.identify,
174 on_place = doc_identifier.liquid_mode,
175 on_secondary_use = doc_identifier.liquid_mode,
177 minetest.register_tool("doc_identifier:identifier_liquid", {
178 description = S("Lookup Tool"),
179 _doc_items_create_entry = false,
180 tool_capabilities = {},
181 range = 10,
182 groups = { not_in_creative_inventory = 1, not_in_craft_guide = 1, disable_repair = 1 },
183 wield_image = "doc_identifier_identifier_liquid.png",
184 inventory_image = "doc_identifier_identifier_liquid.png",
185 liquids_pointable = true,
186 on_use = doc_identifier.identify,
187 on_place = doc_identifier.solid_mode,
188 on_secondary_use = doc_identifier.solid_mode,
191 minetest.register_craft({
192 output = "doc_identifier:identifier_solid",
193 recipe = { {"group:stick", "group:stick" },
194 {"", "group:stick"},
195 {"group:stick", ""} }
198 if minetest.get_modpath("default") ~= nil then
199 minetest.register_craft({
200 output = "doc_identifier:identifier_solid",
201 recipe = { { "default:glass" },
202 { "group:stick" } }
206 minetest.register_alias("doc_identifier:identifier", "doc_identifier:identifier_solid")
208 doc.add_entry_alias("tools", "doc_identifier:identifier_solid", "tools", "doc_identifier:identifier_liquid")