s_client.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.h"
  20. #include "common/c_converter.h"
  21. #include "common/c_content.h"
  22. #include "s_item.h"
  23. void ScriptApiClient::on_shutdown()
  24. {
  25. SCRIPTAPI_PRECHECKHEADER
  26. // Get registered shutdown hooks
  27. lua_getglobal(L, "core");
  28. lua_getfield(L, -1, "registered_on_shutdown");
  29. // Call callbacks
  30. runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
  31. }
  32. bool ScriptApiClient::on_sending_message(const std::string &message)
  33. {
  34. SCRIPTAPI_PRECHECKHEADER
  35. // Get core.registered_on_chat_messages
  36. lua_getglobal(L, "core");
  37. lua_getfield(L, -1, "registered_on_sending_chat_message");
  38. // Call callbacks
  39. lua_pushstring(L, message.c_str());
  40. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  41. bool ate = lua_toboolean(L, -1);
  42. return ate;
  43. }
  44. bool ScriptApiClient::on_receiving_message(const std::string &message)
  45. {
  46. SCRIPTAPI_PRECHECKHEADER
  47. // Get core.registered_on_chat_messages
  48. lua_getglobal(L, "core");
  49. lua_getfield(L, -1, "registered_on_receiving_chat_message");
  50. // Call callbacks
  51. lua_pushstring(L, message.c_str());
  52. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  53. bool ate = lua_toboolean(L, -1);
  54. return ate;
  55. }
  56. void ScriptApiClient::on_damage_taken(int32_t damage_amount)
  57. {
  58. SCRIPTAPI_PRECHECKHEADER
  59. // Get core.registered_on_chat_messages
  60. lua_getglobal(L, "core");
  61. lua_getfield(L, -1, "registered_on_damage_taken");
  62. // Call callbacks
  63. lua_pushinteger(L, damage_amount);
  64. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  65. }
  66. void ScriptApiClient::on_hp_modification(int32_t newhp)
  67. {
  68. SCRIPTAPI_PRECHECKHEADER
  69. // Get core.registered_on_chat_messages
  70. lua_getglobal(L, "core");
  71. lua_getfield(L, -1, "registered_on_hp_modification");
  72. // Call callbacks
  73. lua_pushinteger(L, newhp);
  74. runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
  75. }
  76. void ScriptApiClient::on_death()
  77. {
  78. SCRIPTAPI_PRECHECKHEADER
  79. // Get registered shutdown hooks
  80. lua_getglobal(L, "core");
  81. lua_getfield(L, -1, "registered_on_death");
  82. // Call callbacks
  83. runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
  84. }
  85. void ScriptApiClient::environment_step(float dtime)
  86. {
  87. SCRIPTAPI_PRECHECKHEADER
  88. // Get core.registered_globalsteps
  89. lua_getglobal(L, "core");
  90. lua_getfield(L, -1, "registered_globalsteps");
  91. // Call callbacks
  92. lua_pushnumber(L, dtime);
  93. try {
  94. runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
  95. } catch (LuaError &e) {
  96. getClient()->setFatalError(std::string("Client environment_step: ") + e.what() + "\n"
  97. + script_get_backtrace(L));
  98. }
  99. }
  100. void ScriptApiClient::on_formspec_input(const std::string &formname,
  101. const StringMap &fields)
  102. {
  103. SCRIPTAPI_PRECHECKHEADER
  104. // Get core.registered_on_chat_messages
  105. lua_getglobal(L, "core");
  106. lua_getfield(L, -1, "registered_on_formspec_input");
  107. // Call callbacks
  108. // param 1
  109. lua_pushstring(L, formname.c_str());
  110. // param 2
  111. lua_newtable(L);
  112. StringMap::const_iterator it;
  113. for (it = fields.begin(); it != fields.end(); ++it) {
  114. const std::string &name = it->first;
  115. const std::string &value = it->second;
  116. lua_pushstring(L, name.c_str());
  117. lua_pushlstring(L, value.c_str(), value.size());
  118. lua_settable(L, -3);
  119. }
  120. runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
  121. }
  122. bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
  123. {
  124. SCRIPTAPI_PRECHECKHEADER
  125. const NodeDefManager *ndef = getClient()->ndef();
  126. // Get core.registered_on_dignode
  127. lua_getglobal(L, "core");
  128. lua_getfield(L, -1, "registered_on_dignode");
  129. // Push data
  130. push_v3s16(L, p);
  131. pushnode(L, node, ndef);
  132. // Call functions
  133. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  134. return lua_toboolean(L, -1);
  135. }
  136. bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
  137. {
  138. SCRIPTAPI_PRECHECKHEADER
  139. const NodeDefManager *ndef = getClient()->ndef();
  140. // Get core.registered_on_punchgnode
  141. lua_getglobal(L, "core");
  142. lua_getfield(L, -1, "registered_on_punchnode");
  143. // Push data
  144. push_v3s16(L, p);
  145. pushnode(L, node, ndef);
  146. // Call functions
  147. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  148. bool blocked = lua_toboolean(L, -1);
  149. return blocked;
  150. }
  151. bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefinition &item)
  152. {
  153. SCRIPTAPI_PRECHECKHEADER
  154. // Get core.registered_on_placenode
  155. lua_getglobal(L, "core");
  156. lua_getfield(L, -1, "registered_on_placenode");
  157. // Push data
  158. push_pointed_thing(L, pointed, true);
  159. push_item_definition(L, item);
  160. // Call functions
  161. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  162. return lua_toboolean(L, -1);
  163. }
  164. bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &pointed)
  165. {
  166. SCRIPTAPI_PRECHECKHEADER
  167. // Get core.registered_on_item_use
  168. lua_getglobal(L, "core");
  169. lua_getfield(L, -1, "registered_on_item_use");
  170. // Push data
  171. LuaItemStack::create(L, item);
  172. push_pointed_thing(L, pointed, true);
  173. // Call functions
  174. runCallbacks(2, RUN_CALLBACKS_MODE_OR);
  175. return lua_toboolean(L, -1);
  176. }
  177. bool ScriptApiClient::on_inventory_open(Inventory *inventory)
  178. {
  179. SCRIPTAPI_PRECHECKHEADER
  180. lua_getglobal(L, "core");
  181. lua_getfield(L, -1, "registered_on_inventory_open");
  182. std::vector<const InventoryList*> lists = inventory->getLists();
  183. std::vector<const InventoryList*>::iterator iter = lists.begin();
  184. lua_createtable(L, 0, lists.size());
  185. for (; iter != lists.end(); iter++) {
  186. const char* name = (*iter)->getName().c_str();
  187. lua_pushstring(L, name);
  188. push_inventory_list(L, inventory, name);
  189. lua_rawset(L, -3);
  190. }
  191. runCallbacks(1, RUN_CALLBACKS_MODE_OR);
  192. return lua_toboolean(L, -1);
  193. }
  194. void ScriptApiClient::setEnv(ClientEnvironment *env)
  195. {
  196. ScriptApiBase::setEnv(env);
  197. }