init.lua 7.2 KB

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