s_client.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "s_client.h"
  18. #include "s_internal.h"
  19. #include "client/client.h"
  20. #include "common/c_converter.h"
  21. #include "common/c_content.h"
  22. #include "lua_api/l_item.h"
  23. #include "itemdef.h"
  24. #include "s_item.h"
  25. void ScriptApiClient::on_mods_loaded()
  26. {
  27. SCRIPTAPI_PRECHECKHEADER
  28. // Get registered shutdown hooks
  29. lua_getglobal(L, "core");
  30. lua_getfield(L, -1, "registered_on_mods_loaded");
  31. // Call callbacks
  32. try {
  33. runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
  34. } catch (LuaError &e) {
  35. getClient()->setFatalError(e);
  36. }
  37. }
  38. void ScriptApiClient::on_shutdown()
  39. {
  40. SCRIPTAPI_PRECHECKHEADER
  41. // Get registered shutdown hooks
  42. lua_getglobal(L, "core");
  43. lua_getfield(L, -1, "registered_on_shutdown");
  44. // Call callbacks
  45. try {
  46. runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
  47. } catch (LuaError &e) {
  48. getClient()->setFatalError(e);
  49. }
  50. }
  51. bool ScriptApiClient::on_sending_message(const std::string &message)
  52. {
  53. SCRIPTAPI_PRECHECKHEADER
  54. // Get core.registered_on_chat_messages
  55. lua_getglobal(L, "core");
  56. lua_getfield(L, -1, "registered_on_sending_chat_message");
  57. // Call callbacks
  58. lua_pushstring(L, message.c_str());
  59. try {
  60. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  61. } catch (LuaError &e) {
  62. getClient()->setFatalError(e);
  63. return true;
  64. }
  65. return readParam<bool>(L, -1);
  66. }
  67. bool ScriptApiClient::on_receiving_message(const std::string &message)
  68. {
  69. SCRIPTAPI_PRECHECKHEADER
  70. // Get core.registered_on_chat_messages
  71. lua_getglobal(L, "core");
  72. lua_getfield(L, -1, "registered_on_receiving_chat_message");
  73. // Call callbacks
  74. lua_pushstring(L, message.c_str());
  75. try {
  76. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  77. } catch (LuaError &e) {
  78. getClient()->setFatalError(e);
  79. return true;
  80. }
  81. return readParam<bool>(L, -1);
  82. }
  83. void ScriptApiClient::on_damage_taken(int32_t damage_amount)
  84. {
  85. SCRIPTAPI_PRECHECKHEADER
  86. // Get core.registered_on_chat_messages
  87. lua_getglobal(L, "core");
  88. lua_getfield(L, -1, "registered_on_damage_taken");
  89. // Call callbacks
  90. lua_pushinteger(L, damage_amount);
  91. try {
  92. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  93. } catch (LuaError &e) {
  94. getClient()->setFatalError(e);
  95. }
  96. }
  97. void ScriptApiClient::on_hp_modification(int32_t newhp)
  98. {
  99. SCRIPTAPI_PRECHECKHEADER
  100. // Get core.registered_on_chat_messages
  101. lua_getglobal(L, "core");
  102. lua_getfield(L, -1, "registered_on_hp_modification");
  103. // Call callbacks
  104. lua_pushinteger(L, newhp);
  105. try {
  106. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  107. } catch (LuaError &e) {
  108. getClient()->setFatalError(e);
  109. }
  110. }
  111. void ScriptApiClient::on_death()
  112. {
  113. SCRIPTAPI_PRECHECKHEADER
  114. // Get registered shutdown hooks
  115. lua_getglobal(L, "core");
  116. lua_getfield(L, -1, "registered_on_death");
  117. // Call callbacks
  118. try {
  119. runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
  120. } catch (LuaError &e) {
  121. getClient()->setFatalError(e);
  122. }
  123. }
  124. void ScriptApiClient::environment_step(float dtime)
  125. {
  126. SCRIPTAPI_PRECHECKHEADER
  127. // Get core.registered_globalsteps
  128. lua_getglobal(L, "core");
  129. lua_getfield(L, -1, "registered_globalsteps");
  130. // Call callbacks
  131. lua_pushnumber(L, dtime);
  132. try {
  133. runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
  134. } catch (LuaError &e) {
  135. getClient()->setFatalError(e);
  136. }
  137. }
  138. void ScriptApiClient::on_formspec_input(const std::string &formname,
  139. const StringMap &fields)
  140. {
  141. SCRIPTAPI_PRECHECKHEADER
  142. // Get core.registered_on_chat_messages
  143. lua_getglobal(L, "core");
  144. lua_getfield(L, -1, "registered_on_formspec_input");
  145. // Call callbacks
  146. // param 1
  147. lua_pushstring(L, formname.c_str());
  148. // param 2
  149. lua_newtable(L);
  150. StringMap::const_iterator it;
  151. for (it = fields.begin(); it != fields.end(); ++it) {
  152. const std::string &name = it->first;
  153. const std::string &value = it->second;
  154. lua_pushstring(L, name.c_str());
  155. lua_pushlstring(L, value.c_str(), value.size());
  156. lua_settable(L, -3);
  157. }
  158. try {
  159. runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
  160. } catch (LuaError &e) {
  161. getClient()->setFatalError(e);
  162. }
  163. }
  164. bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
  165. {
  166. SCRIPTAPI_PRECHECKHEADER
  167. // Get core.registered_on_dignode
  168. lua_getglobal(L, "core");
  169. lua_getfield(L, -1, "registered_on_dignode");
  170. // Push data
  171. push_v3s16(L, p);
  172. pushnode(L, node);
  173. // Call functions
  174. try {
  175. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  176. } catch (LuaError &e) {
  177. getClient()->setFatalError(e);
  178. return true;
  179. }
  180. return lua_toboolean(L, -1);
  181. }
  182. bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
  183. {
  184. SCRIPTAPI_PRECHECKHEADER
  185. // Get core.registered_on_punchgnode
  186. lua_getglobal(L, "core");
  187. lua_getfield(L, -1, "registered_on_punchnode");
  188. // Push data
  189. push_v3s16(L, p);
  190. pushnode(L, node);
  191. // Call functions
  192. try {
  193. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  194. } catch (LuaError &e) {
  195. getClient()->setFatalError(e);
  196. return true;
  197. }
  198. return readParam<bool>(L, -1);
  199. }
  200. bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefinition &item)
  201. {
  202. SCRIPTAPI_PRECHECKHEADER
  203. // Get core.registered_on_placenode
  204. lua_getglobal(L, "core");
  205. lua_getfield(L, -1, "registered_on_placenode");
  206. // Push data
  207. push_pointed_thing(L, pointed, true);
  208. push_item_definition(L, item);
  209. // Call functions
  210. try {
  211. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  212. } catch (LuaError &e) {
  213. getClient()->setFatalError(e);
  214. return true;
  215. }
  216. return readParam<bool>(L, -1);
  217. }
  218. bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &pointed)
  219. {
  220. SCRIPTAPI_PRECHECKHEADER
  221. // Get core.registered_on_item_use
  222. lua_getglobal(L, "core");
  223. lua_getfield(L, -1, "registered_on_item_use");
  224. // Push data
  225. LuaItemStack::create(L, item);
  226. push_pointed_thing(L, pointed, true);
  227. // Call functions
  228. try {
  229. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  230. } catch (LuaError &e) {
  231. getClient()->setFatalError(e);
  232. return true;
  233. }
  234. return readParam<bool>(L, -1);
  235. }
  236. bool ScriptApiClient::on_inventory_open(Inventory *inventory)
  237. {
  238. SCRIPTAPI_PRECHECKHEADER
  239. lua_getglobal(L, "core");
  240. lua_getfield(L, -1, "registered_on_inventory_open");
  241. push_inventory_lists(L, *inventory);
  242. try {
  243. runCallbacks(1, RUN_CALLBACKS_MODE_OR);
  244. } catch (LuaError &e) {
  245. getClient()->setFatalError(e);
  246. return true;
  247. }
  248. return readParam<bool>(L, -1);
  249. }
  250. void ScriptApiClient::setEnv(ClientEnvironment *env)
  251. {
  252. ScriptApiBase::setEnv(env);
  253. }