]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
[CSM] Add event on_place_node API lua (#5548)
authorVincent Glize <vincentglize@hotmail.fr>
Sat, 29 Apr 2017 10:08:16 +0000 (12:08 +0200)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Sat, 29 Apr 2017 10:08:16 +0000 (12:08 +0200)
* [CSM] Add event on_place_node API lua

12 files changed:
builtin/client/register.lua
clientmods/preview/init.lua
doc/client_lua_api.md
src/game.cpp
src/script/common/c_content.cpp
src/script/common/c_content.h
src/script/cpp_api/s_base.cpp
src/script/cpp_api/s_base.h
src/script/cpp_api/s_client.cpp
src/script/cpp_api/s_client.h
src/script/cpp_api/s_entity.cpp
src/script/cpp_api/s_item.cpp

index e6ce25654111262176f521ca54162e25e729a7da..b35ecc849506870abba7a3af97b0c2efb03340fb 100644 (file)
@@ -69,3 +69,4 @@ core.registered_on_damage_taken, core.register_on_damage_taken = make_registrati
 core.registered_on_formspec_input, core.register_on_formspec_input = make_registration()
 core.registered_on_dignode, core.register_on_dignode = make_registration()
 core.registered_on_punchnode, core.register_on_punchnode = make_registration()
+core.registered_on_placenode, core.register_on_placenode = make_registration()
index 4b31fa3233536f83f6eb66efcb6c71290db8b218..255c0b83f060625a74082425fc56e273af50b92f 100644 (file)
@@ -10,6 +10,13 @@ core.register_on_connect(function()
        print("[PREVIEW] Player connection completed")
 end)
 
+core.register_on_placenode(function(pointed_thing, node)
+       print("The local player place a node!")
+       print("pointed_thing :" .. dump(pointed_thing))
+       print("node placed :" .. dump(node))
+       return false
+end)
+
 -- This is an example function to ensure it's working properly, should be removed before merge
 core.register_on_receiving_chat_messages(function(message)
        print("[PREVIEW] Received message " .. message)
index 86fcd02a7e7a3845a5415156457462055be42a0f..d435c4aae3aad25ebb335be858ca8036f9a5fae0 100644 (file)
@@ -667,6 +667,8 @@ Call these functions only at load time!
     * Called when the local player punches a node
     * Newest functions are called first
     * If any function returns true, the punch is ignored
+* `minetest.register_on_placenode(function(pointed_thing, node))`    
+    * Called when a node has been placed
 ### Sounds
 * `minetest.sound_play(spec, parameters)`: returns a handle
     * `spec` is a `SimpleSoundSpec`
index eb59ee5ae9e30f9e44284ae3dd06426661c7699f..12fe6a6e9480b6ecfb73ec25755379ed7de2cf2f 100644 (file)
@@ -3730,6 +3730,9 @@ void Game::handlePointingAtNode(const PointedThing &pointed, const ItemDefinitio
                                // Read the sound
                                soundmaker->m_player_rightpunch_sound =
                                                playeritem_def.sound_place;
+
+                               if (client->moddingEnabled())
+                                       client->getScript()->on_placenode(pointed, playeritem_def);
                        } else {
                                soundmaker->m_player_rightpunch_sound =
                                                SimpleSoundSpec();
index 573347b4c3bc95a88b5eae17bda5d72d4eda9cdd..5fe5af58d05fb4379914d7cedd4eea235073f716 100644 (file)
@@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "porting.h"
 #include "mg_schematic.h"
 #include "noise.h"
+#include "util/pointedthing.h"
 #include <json/json.h>
 
 struct EnumString es_TileAnimationType[] =
@@ -117,6 +118,16 @@ void read_item_definition(lua_State* L, int index,
                        def.node_placement_prediction);
 }
 
+/******************************************************************************/
+void push_item_definition(lua_State *L, const ItemDefinition &i)
+{
+       lua_newtable(L);
+       lua_pushstring(L, i.name.c_str());
+       lua_setfield(L, -2, "name");
+       lua_pushstring(L, i.description.c_str());
+       lua_setfield(L, -2, "description");
+}
+
 /******************************************************************************/
 void read_object_properties(lua_State *L, int index,
                ObjectProperties *prop, IItemDefManager *idef)
@@ -1427,3 +1438,36 @@ void read_json_value(lua_State *L, Json::Value &root, int index, u8 recursion)
        }
        lua_pop(L, 1); // Pop value
 }
+
+void push_pointed_thing(lua_State *L, const PointedThing &pointed)
+{
+       lua_newtable(L);
+       if (pointed.type == POINTEDTHING_NODE) {
+               lua_pushstring(L, "node");
+               lua_setfield(L, -2, "type");
+               push_v3s16(L, pointed.node_undersurface);
+               lua_setfield(L, -2, "under");
+               push_v3s16(L, pointed.node_abovesurface);
+               lua_setfield(L, -2, "above");
+       } else if (pointed.type == POINTEDTHING_OBJECT) {
+               lua_pushstring(L, "object");
+               lua_setfield(L, -2, "type");
+               push_objectRef(L, pointed.object_id);
+               lua_setfield(L, -2, "ref");
+       } else {
+               lua_pushstring(L, "nothing");
+               lua_setfield(L, -2, "type");
+       }
+}
+
+void push_objectRef(lua_State *L, const u16 id)
+{
+       // Get core.object_refs[i]
+       lua_getglobal(L, "core");
+       lua_getfield(L, -1, "object_refs");
+       luaL_checktype(L, -1, LUA_TTABLE);
+       lua_pushnumber(L, id);
+       lua_gettable(L, -2);
+       lua_remove(L, -2); // object_refs
+       lua_remove(L, -2); // core
+}
index c701c0384248a1cdca9ea24026d41f41b0ee9aa6..219c5eb7cbe105569cf36ab893d75b0130d73894 100644 (file)
@@ -88,6 +88,8 @@ void               push_tool_capabilities    (lua_State *L,
 
 void read_item_definition (lua_State *L, int index, const ItemDefinition &default_def,
                ItemDefinition &def);
+void               push_item_definition      (lua_State *L,
+                                              const ItemDefinition &i);
 void               read_object_properties    (lua_State *L, int index,
                                               ObjectProperties *prop,
                                               IItemDefManager *idef);
@@ -162,6 +164,10 @@ bool               push_json_value           (lua_State *L,
 void               read_json_value           (lua_State *L, Json::Value &root,
                                               int index, u8 recursion = 0);
 
+void               push_pointed_thing        (lua_State *L, const PointedThing &pointed);
+
+void               push_objectRef            (lua_State *L, const u16 id);
+
 extern struct EnumString es_TileAnimationType[];
 
 #endif /* C_CONTENT_H_ */
index 6a843810f9b4b9f3a1d7ff76d6dd1ac0989f675b..e72af22c6acdc8af17cd96bf2e872de5df00f848 100644 (file)
@@ -42,6 +42,7 @@ extern "C" {
 
 #include <stdio.h>
 #include <cstdarg>
+#include "script/common/c_content.h"
 #include <sstream>
 
 
@@ -320,22 +321,10 @@ void ScriptApiBase::objectrefGetOrCreate(lua_State *L,
        if (cobj == NULL || cobj->getId() == 0) {
                ObjectRef::create(L, cobj);
        } else {
-               objectrefGet(L, cobj->getId());
+               push_objectRef(L, cobj->getId());
        }
 }
 
-void ScriptApiBase::objectrefGet(lua_State *L, u16 id)
-{
-       // Get core.object_refs[i]
-       lua_getglobal(L, "core");
-       lua_getfield(L, -1, "object_refs");
-       luaL_checktype(L, -1, LUA_TTABLE);
-       lua_pushnumber(L, id);
-       lua_gettable(L, -2);
-       lua_remove(L, -2); // object_refs
-       lua_remove(L, -2); // core
-}
-
 Server* ScriptApiBase::getServer()
 {
        return dynamic_cast<Server *>(m_gamedef);
index 19d71df6517f27660f0ef2ec225ab3fd94709271..5b047a08113fc9cadeb9430efef83935a209f699 100644 (file)
@@ -115,7 +115,6 @@ class ScriptApiBase {
        void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
 
        void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
-       void objectrefGet(lua_State *L, u16 id);
 
        RecursiveMutex  m_luastackmutex;
        std::string     m_last_run_mod;
index a8a7d5e262b99ae8101f74812e9d0eecc34b9a86..4bc368d1d58c27deeab110e900d94b662dc1f7dc 100644 (file)
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "client.h"
 #include "common/c_converter.h"
 #include "common/c_content.h"
+#include "s_item.h"
 
 void ScriptApiClient::on_shutdown()
 {
@@ -189,6 +190,23 @@ bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
        return blocked;
 }
 
+bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefinition &item)
+{
+       SCRIPTAPI_PRECHECKHEADER
+
+       // Get core.registered_on_placenode
+       lua_getglobal(L, "core");
+       lua_getfield(L, -1, "registered_on_placenode");
+
+       // Push data
+       push_pointed_thing(L, pointed);
+       push_item_definition(L, item);
+
+       // Call functions
+       runCallbacks(2, RUN_CALLBACKS_MODE_OR);
+       return lua_toboolean(L, -1);
+}
+
 void ScriptApiClient::setEnv(ClientEnvironment *env)
 {
        ScriptApiBase::setEnv(env);
index 94a597b2ccdf633946cfdd089afc7d675bfb0dd3..f252cf4993b53f75999dcee3bab2c55907cac66f 100644 (file)
@@ -21,8 +21,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #ifndef S_CLIENT_H_
 #define S_CLIENT_H_
 
+#include "util/pointedthing.h"
 #include "cpp_api/s_base.h"
 #include "mapnode.h"
+#include "itemdef.h"
 #include "util/string.h"
 
 #ifdef _CRT_MSVCP_CURRENT
@@ -51,6 +53,7 @@ class ScriptApiClient : virtual public ScriptApiBase
 
        bool on_dignode(v3s16 p, MapNode node);
        bool on_punchnode(v3s16 p, MapNode node);
+       bool on_placenode(const PointedThing &pointed, const ItemDefinition &item);
 
        void setEnv(ClientEnvironment *env);
 };
index 2e1d277e48d4261b99f2cebe0f86c1f55b979e9e..4c1e296d4925e63fa2aba18af9a0ac99fc24737f 100644 (file)
@@ -57,7 +57,7 @@ bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
 
        // Add object reference
        // This should be userdata with metatable ObjectRef
-       objectrefGet(L, id);
+       push_objectRef(L, id);
        luaL_checktype(L, -1, LUA_TUSERDATA);
        if (!luaL_checkudata(L, -1, "ObjectRef"))
                luaL_typerror(L, -1, "ObjectRef");
index cbb83380735095033d89f11660aa374585422f20..032018f2f7438c59bef671b7a5b3b85d3c290fc1 100644 (file)
@@ -249,27 +249,6 @@ void ScriptApiItem::pushPointedThing(const PointedThing& pointed)
 {
        lua_State* L = getStack();
 
-       lua_newtable(L);
-       if(pointed.type == POINTEDTHING_NODE)
-       {
-               lua_pushstring(L, "node");
-               lua_setfield(L, -2, "type");
-               push_v3s16(L, pointed.node_undersurface);
-               lua_setfield(L, -2, "under");
-               push_v3s16(L, pointed.node_abovesurface);
-               lua_setfield(L, -2, "above");
-       }
-       else if(pointed.type == POINTEDTHING_OBJECT)
-       {
-               lua_pushstring(L, "object");
-               lua_setfield(L, -2, "type");
-               objectrefGet(L, pointed.object_id);
-               lua_setfield(L, -2, "ref");
-       }
-       else
-       {
-               lua_pushstring(L, "nothing");
-               lua_setfield(L, -2, "type");
-       }
+       push_pointed_thing(L, pointed);
 }