[CSM] add `on_item_use` (#5544)
authorred-001 <red-001@outlook.ie>
Sat, 6 May 2017 19:12:44 +0000 (20:12 +0100)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Sat, 6 May 2017 19:12:44 +0000 (21:12 +0200)
builtin/client/register.lua
clientmods/preview/init.lua
doc/client_lua_api.md
src/game.cpp
src/script/cpp_api/s_client.cpp
src/script/cpp_api/s_client.h

index b35ecc849506870abba7a3af97b0c2efb03340fb..6b12ddec88c0b0e1ca2f5f82950a917532314b84 100644 (file)
@@ -70,3 +70,4 @@ core.registered_on_formspec_input, core.register_on_formspec_input = make_regist
 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()
+core.registered_on_item_use, core.register_on_item_use = make_registration()
index fb606b3f46b28e8b6ab2d6dd3657b8abf649744c..821f7c7143857954d074b949ced3bea870437142 100644 (file)
@@ -22,6 +22,13 @@ core.register_on_placenode(function(pointed_thing, node)
        return false
 end)
 
+core.register_on_item_use(function(itemstack, pointed_thing)
+       print("The local player used an item!")
+       print("pointed_thing :" .. dump(pointed_thing))
+       print("item = " .. itemstack:get_name())
+       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 a4293500f79664dba276fbd4b6680d567bdf9a4b..19947a525682390c48849492426676b59daba3da 100644 (file)
@@ -669,6 +669,10 @@ Call these functions only at load time!
     * If any function returns true, the punch is ignored
 * `minetest.register_on_placenode(function(pointed_thing, node))`    
     * Called when a node has been placed
+* `minetest.register_on_item_use(func(item, pointed_thing))`
+    * Called when the local player uses an item.
+    * Newest functions are called first.
+    * If any function returns true, the item use is not sent to server.
 ### Sounds
 * `minetest.sound_play(spec, parameters)`: returns a handle
     * `spec` is a `SimpleSoundSpec`
index 416320e5d29aec5450bf55446e00cec221382fa1..61282b463dcce7b7099bd29997649e90ee30e1f9 100644 (file)
@@ -3553,7 +3553,8 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
                runData.repeat_rightclick_timer = 0;
 
        if (playeritem_def.usable && isLeftPressed()) {
-               if (getLeftClicked())
+               if (getLeftClicked() && (!client->moddingEnabled()
+                               || !client->getScript()->on_item_use(playeritem, pointed)))
                        client->interact(4, pointed);
        } else if (pointed.type == POINTEDTHING_NODE) {
                ToolCapabilities playeritem_toolcap =
index 4bc368d1d58c27deeab110e900d94b662dc1f7dc..d5ec52407d93d73aaa7b1b26f6abcd2bcb12b90e 100644 (file)
@@ -207,6 +207,23 @@ bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefini
        return lua_toboolean(L, -1);
 }
 
+bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &pointed)
+{
+       SCRIPTAPI_PRECHECKHEADER
+
+       // Get core.registered_on_item_use
+       lua_getglobal(L, "core");
+       lua_getfield(L, -1, "registered_on_item_use");
+
+       // Push data
+       LuaItemStack::create(L, item);
+       push_pointed_thing(L, pointed);
+
+       // Call functions
+       runCallbacks(2, RUN_CALLBACKS_MODE_OR);
+       return lua_toboolean(L, -1);
+}
+
 void ScriptApiClient::setEnv(ClientEnvironment *env)
 {
        ScriptApiBase::setEnv(env);
index f252cf4993b53f75999dcee3bab2c55907cac66f..9133637a640fdf8d9514f0a45a9e6e99045293ee 100644 (file)
@@ -26,6 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "mapnode.h"
 #include "itemdef.h"
 #include "util/string.h"
+#include "util/pointedthing.h"
+#include "lua_api/l_item.h"
 
 #ifdef _CRT_MSVCP_CURRENT
 #include <cstdint>
@@ -54,6 +56,7 @@ public:
        bool on_dignode(v3s16 p, MapNode node);
        bool on_punchnode(v3s16 p, MapNode node);
        bool on_placenode(const PointedThing &pointed, const ItemDefinition &item);
+       bool on_item_use(const ItemStack &item, const PointedThing &pointed);
 
        void setEnv(ClientEnvironment *env);
 };