[CSM] add `on_item_use` (#5544)

master
red-001 2017-05-06 20:12:44 +01:00 committed by Loïc Blot
parent 6658ad3d94
commit 5ebf8f9450
6 changed files with 34 additions and 1 deletions

View 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()

View 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)

View 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`

View 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 =

View 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);

View 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);
};