Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CSM] add on_item_use (#5544)
  • Loading branch information
red-001 authored and nerzhul committed May 6, 2017
1 parent 6658ad3 commit 5ebf8f9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions builtin/client/register.lua
Expand Up @@ -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()
7 changes: 7 additions & 0 deletions clientmods/preview/init.lua
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions doc/client_lua_api.md
Expand Up @@ -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`
Expand Down
3 changes: 2 additions & 1 deletion src/game.cpp
Expand Up @@ -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 =
Expand Down
17 changes: 17 additions & 0 deletions src/script/cpp_api/s_client.cpp
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/script/cpp_api/s_client.h
Expand Up @@ -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>
Expand Down Expand Up @@ -54,6 +56,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);
bool on_item_use(const ItemStack &item, const PointedThing &pointed);

void setEnv(ClientEnvironment *env);
};
Expand Down

0 comments on commit 5ebf8f9

Please sign in to comment.