s_client.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 "s_item.h"
  23. void ScriptApiClient::on_mods_loaded()
  24. {
  25. SCRIPTAPI_PRECHECKHEADER
  26. // Get registered shutdown hooks
  27. lua_getglobal(L, "core");
  28. lua_getfield(L, -1, "registered_on_mods_loaded");
  29. // Call callbacks
  30. try {
  31. runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
  32. } catch (LuaError &e) {
  33. getClient()->setFatalError(e);
  34. }
  35. }
  36. void ScriptApiClient::on_shutdown()
  37. {
  38. SCRIPTAPI_PRECHECKHEADER
  39. // Get registered shutdown hooks
  40. lua_getglobal(L, "core");
  41. lua_getfield(L, -1, "registered_on_shutdown");
  42. // Call callbacks
  43. try {
  44. runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
  45. } catch (LuaError &e) {
  46. getClient()->setFatalError(e);
  47. }
  48. }
  49. bool ScriptApiClient::on_sending_message(const std::string &message)
  50. {
  51. SCRIPTAPI_PRECHECKHEADER
  52. // Get core.registered_on_chat_messages
  53. lua_getglobal(L, "core");
  54. lua_getfield(L, -1, "registered_on_sending_chat_message");
  55. // Call callbacks
  56. lua_pushstring(L, message.c_str());
  57. try {
  58. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  59. } catch (LuaError &e) {
  60. getClient()->setFatalError(e);
  61. return true;
  62. }
  63. return readParam<bool>(L, -1);
  64. }
  65. bool ScriptApiClient::on_receiving_message(const std::string &message)
  66. {
  67. SCRIPTAPI_PRECHECKHEADER
  68. // Get core.registered_on_chat_messages
  69. lua_getglobal(L, "core");
  70. lua_getfield(L, -1, "registered_on_receiving_chat_message");
  71. // Call callbacks
  72. lua_pushstring(L, message.c_str());
  73. try {
  74. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  75. } catch (LuaError &e) {
  76. getClient()->setFatalError(e);
  77. return true;
  78. }
  79. return readParam<bool>(L, -1);
  80. }
  81. void ScriptApiClient::on_damage_taken(int32_t damage_amount)
  82. {
  83. SCRIPTAPI_PRECHECKHEADER
  84. // Get core.registered_on_chat_messages
  85. lua_getglobal(L, "core");
  86. lua_getfield(L, -1, "registered_on_damage_taken");
  87. // Call callbacks
  88. lua_pushinteger(L, damage_amount);
  89. try {
  90. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  91. } catch (LuaError &e) {
  92. getClient()->setFatalError(e);
  93. }
  94. }
  95. void ScriptApiClient::on_hp_modification(int32_t newhp)
  96. {
  97. SCRIPTAPI_PRECHECKHEADER
  98. // Get core.registered_on_chat_messages
  99. lua_getglobal(L, "core");
  100. lua_getfield(L, -1, "registered_on_hp_modification");
  101. // Call callbacks
  102. lua_pushinteger(L, newhp);
  103. try {
  104. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  105. } catch (LuaError &e) {
  106. getClient()->setFatalError(e);
  107. }
  108. }
  109. void ScriptApiClient::on_death()
  110. {
  111. SCRIPTAPI_PRECHECKHEADER
  112. // Get registered shutdown hooks
  113. lua_getglobal(L, "core");
  114. lua_getfield(L, -1, "registered_on_death");
  115. // Call callbacks
  116. try {
  117. runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
  118. } catch (LuaError &e) {
  119. getClient()->setFatalError(e);
  120. }
  121. }
  122. void ScriptApiClient::environment_step(float dtime)
  123. {
  124. SCRIPTAPI_PRECHECKHEADER
  125. // Get core.registered_globalsteps
  126. lua_getglobal(L, "core");
  127. lua_getfield(L, -1, "registered_globalsteps");
  128. // Call callbacks
  129. lua_pushnumber(L, dtime);
  130. try {
  131. runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
  132. } catch (LuaError &e) {
  133. getClient()->setFatalError(e);
  134. }
  135. }
  136. void ScriptApiClient::on_formspec_input(const std::string &formname,
  137. const StringMap &fields)
  138. {
  139. SCRIPTAPI_PRECHECKHEADER
  140. // Get core.registered_on_chat_messages
  141. lua_getglobal(L, "core");
  142. lua_getfield(L, -1, "registered_on_formspec_input");
  143. // Call callbacks
  144. // param 1
  145. lua_pushstring(L, formname.c_str());
  146. // param 2
  147. lua_newtable(L);
  148. StringMap::const_iterator it;
  149. for (it = fields.begin(); it != fields.end(); ++it) {
  150. const std::string &name = it->first;
  151. const std::string &value = it->second;
  152. lua_pushstring(L, name.c_str());
  153. lua_pushlstring(L, value.c_str(), value.size());
  154. lua_settable(L, -3);
  155. }
  156. try {
  157. runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
  158. } catch (LuaError &e) {
  159. getClient()->setFatalError(e);
  160. }
  161. }
  162. bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
  163. {
  164. SCRIPTAPI_PRECHECKHEADER
  165. // Get core.registered_on_dignode
  166. lua_getglobal(L, "core");
  167. lua_getfield(L, -1, "registered_on_dignode");
  168. // Push data
  169. push_v3s16(L, p);
  170. pushnode(L, node);
  171. // Call functions
  172. try {
  173. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  174. } catch (LuaError &e) {
  175. getClient()->setFatalError(e);
  176. return true;
  177. }
  178. return lua_toboolean(L, -1);
  179. }
  180. bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
  181. {
  182. SCRIPTAPI_PRECHECKHEADER
  183. // Get core.registered_on_punchgnode
  184. lua_getglobal(L, "core");
  185. lua_getfield(L, -1, "registered_on_punchnode");
  186. // Push data
  187. push_v3s16(L, p);
  188. pushnode(L, node);
  189. // Call functions
  190. try {
  191. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  192. } catch (LuaError &e) {
  193. getClient()->setFatalError(e);
  194. return true;
  195. }
  196. return readParam<bool>(L, -1);
  197. }
  198. bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefinition &item)
  199. {
  200. SCRIPTAPI_PRECHECKHEADER
  201. // Get core.registered_on_placenode
  202. lua_getglobal(L, "core");
  203. lua_getfield(L, -1, "registered_on_placenode");
  204. // Push data
  205. push_pointed_thing(L, pointed, true);
  206. push_item_definition(L, item);
  207. // Call functions
  208. try {
  209. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  210. } catch (LuaError &e) {
  211. getClient()->setFatalError(e);
  212. return true;
  213. }
  214. return readParam<bool>(L, -1);
  215. }
  216. bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &pointed)
  217. {
  218. SCRIPTAPI_PRECHECKHEADER
  219. // Get core.registered_on_item_use
  220. lua_getglobal(L, "core");
  221. lua_getfield(L, -1, "registered_on_item_use");
  222. // Push data
  223. LuaItemStack::create(L, item);
  224. push_pointed_thing(L, pointed, true);
  225. // Call functions
  226. try {
  227. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  228. } catch (LuaError &e) {
  229. getClient()->setFatalError(e);
  230. return true;
  231. }
  232. return readParam<bool>(L, -1);
  233. }
  234. bool ScriptApiClient::on_inventory_open(Inventory *inventory)
  235. {
  236. SCRIPTAPI_PRECHECKHEADER
  237. lua_getglobal(L, "core");
  238. lua_getfield(L, -1, "registered_on_inventory_open");
  239. push_inventory_lists(L, *inventory);
  240. try {
  241. runCallbacks(1, RUN_CALLBACKS_MODE_OR);
  242. } catch (LuaError &e) {
  243. getClient()->setFatalError(e);
  244. return true;
  245. }
  246. return readParam<bool>(L, -1);
  247. }
  248. void ScriptApiClient::setEnv(ClientEnvironment *env)
  249. {
  250. ScriptApiBase::setEnv(env);
  251. }