Skip to content

Commit 5ebf8f9

Browse files
red-001nerzhul
authored andcommittedMay 6, 2017
[CSM] add on_item_use (#5544)
1 parent 6658ad3 commit 5ebf8f9

File tree

6 files changed

+34
-1
lines changed

6 files changed

+34
-1
lines changed
 

Diff for: ‎builtin/client/register.lua

+1
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ core.registered_on_formspec_input, core.register_on_formspec_input = make_regist
7070
core.registered_on_dignode, core.register_on_dignode = make_registration()
7171
core.registered_on_punchnode, core.register_on_punchnode = make_registration()
7272
core.registered_on_placenode, core.register_on_placenode = make_registration()
73+
core.registered_on_item_use, core.register_on_item_use = make_registration()

Diff for: ‎clientmods/preview/init.lua

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ core.register_on_placenode(function(pointed_thing, node)
2222
return false
2323
end)
2424

25+
core.register_on_item_use(function(itemstack, pointed_thing)
26+
print("The local player used an item!")
27+
print("pointed_thing :" .. dump(pointed_thing))
28+
print("item = " .. itemstack:get_name())
29+
return false
30+
end)
31+
2532
-- This is an example function to ensure it's working properly, should be removed before merge
2633
core.register_on_receiving_chat_messages(function(message)
2734
print("[PREVIEW] Received message " .. message)

Diff for: ‎doc/client_lua_api.md

+4
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,10 @@ Call these functions only at load time!
669669
* If any function returns true, the punch is ignored
670670
* `minetest.register_on_placenode(function(pointed_thing, node))`
671671
* Called when a node has been placed
672+
* `minetest.register_on_item_use(func(item, pointed_thing))`
673+
* Called when the local player uses an item.
674+
* Newest functions are called first.
675+
* If any function returns true, the item use is not sent to server.
672676
### Sounds
673677
* `minetest.sound_play(spec, parameters)`: returns a handle
674678
* `spec` is a `SimpleSoundSpec`

Diff for: ‎src/game.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -3553,7 +3553,8 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
35533553
runData.repeat_rightclick_timer = 0;
35543554

35553555
if (playeritem_def.usable && isLeftPressed()) {
3556-
if (getLeftClicked())
3556+
if (getLeftClicked() && (!client->moddingEnabled()
3557+
|| !client->getScript()->on_item_use(playeritem, pointed)))
35573558
client->interact(4, pointed);
35583559
} else if (pointed.type == POINTEDTHING_NODE) {
35593560
ToolCapabilities playeritem_toolcap =

Diff for: ‎src/script/cpp_api/s_client.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,23 @@ bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefini
207207
return lua_toboolean(L, -1);
208208
}
209209

210+
bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &pointed)
211+
{
212+
SCRIPTAPI_PRECHECKHEADER
213+
214+
// Get core.registered_on_item_use
215+
lua_getglobal(L, "core");
216+
lua_getfield(L, -1, "registered_on_item_use");
217+
218+
// Push data
219+
LuaItemStack::create(L, item);
220+
push_pointed_thing(L, pointed);
221+
222+
// Call functions
223+
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
224+
return lua_toboolean(L, -1);
225+
}
226+
210227
void ScriptApiClient::setEnv(ClientEnvironment *env)
211228
{
212229
ScriptApiBase::setEnv(env);

Diff for: ‎src/script/cpp_api/s_client.h

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2626
#include "mapnode.h"
2727
#include "itemdef.h"
2828
#include "util/string.h"
29+
#include "util/pointedthing.h"
30+
#include "lua_api/l_item.h"
2931

3032
#ifdef _CRT_MSVCP_CURRENT
3133
#include <cstdint>
@@ -54,6 +56,7 @@ class ScriptApiClient : virtual public ScriptApiBase
5456
bool on_dignode(v3s16 p, MapNode node);
5557
bool on_punchnode(v3s16 p, MapNode node);
5658
bool on_placenode(const PointedThing &pointed, const ItemDefinition &item);
59+
bool on_item_use(const ItemStack &item, const PointedThing &pointed);
5760

5861
void setEnv(ClientEnvironment *env);
5962
};

0 commit comments

Comments
 (0)
Please sign in to comment.