From 5ebf8f945050e9c74a3bb6784a0844d1fb68bdff Mon Sep 17 00:00:00 2001 From: red-001 Date: Sat, 6 May 2017 20:12:44 +0100 Subject: [PATCH] [CSM] add `on_item_use` (#5544) --- builtin/client/register.lua | 1 + clientmods/preview/init.lua | 7 +++++++ doc/client_lua_api.md | 4 ++++ src/game.cpp | 3 ++- src/script/cpp_api/s_client.cpp | 17 +++++++++++++++++ src/script/cpp_api/s_client.h | 3 +++ 6 files changed, 34 insertions(+), 1 deletion(-) diff --git a/builtin/client/register.lua b/builtin/client/register.lua index b35ecc849..6b12ddec8 100644 --- a/builtin/client/register.lua +++ b/builtin/client/register.lua @@ -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() diff --git a/clientmods/preview/init.lua b/clientmods/preview/init.lua index fb606b3f4..821f7c714 100644 --- a/clientmods/preview/init.lua +++ b/clientmods/preview/init.lua @@ -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) diff --git a/doc/client_lua_api.md b/doc/client_lua_api.md index a4293500f..19947a525 100644 --- a/doc/client_lua_api.md +++ b/doc/client_lua_api.md @@ -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` diff --git a/src/game.cpp b/src/game.cpp index 416320e5d..61282b463 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -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 = diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp index 4bc368d1d..d5ec52407 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -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); diff --git a/src/script/cpp_api/s_client.h b/src/script/cpp_api/s_client.h index f252cf499..9133637a6 100644 --- a/src/script/cpp_api/s_client.h +++ b/src/script/cpp_api/s_client.h @@ -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 @@ -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); }; -- 2.25.1