Skip to content

Commit 4e19791

Browse files
Dumbeldornerzhul
authored andcommitted
[CSM] Add callback on open inventory (#5793)
1 parent b9fb3ce commit 4e19791

File tree

6 files changed

+42
-5
lines changed

6 files changed

+42
-5
lines changed

builtin/client/register.lua

+1
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,4 @@ core.registered_on_placenode, core.register_on_placenode = make_registration()
7373
core.registered_on_item_use, core.register_on_item_use = make_registration()
7474
core.registered_on_modchannel_message, core.register_on_modchannel_message = make_registration()
7575
core.registered_on_modchannel_signal, core.register_on_modchannel_signal = make_registration()
76+
core.registered_on_inventory_open, core.register_on_inventory_open = make_registration()

clientmods/preview/init.lua

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ core.register_on_modchannel_signal(function(channel, signal)
3838
.. channel)
3939
end)
4040

41+
core.register_on_inventory_open(function(inventory)
42+
print("INVENTORY OPEN")
43+
print(dump(inventory))
44+
return false
45+
end)
46+
4147
core.register_on_placenode(function(pointed_thing, node)
4248
print("The local player place a node!")
4349
print("pointed_thing :" .. dump(pointed_thing))

doc/client_lua_api.md

+4
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,10 @@ Call these functions only at load time!
689689
join request.
690690
* If message comes from a server mod, `sender` field is an empty string.
691691

692+
* `minetest.register_on_inventory_open(func(inventory))`
693+
* Called when the local player open inventory
694+
* Newest functions are called first
695+
* If any function returns true, inventory doesn't open
692696
### Sounds
693697
* `minetest.sound_play(spec, parameters)`: returns a handle
694698
* `spec` is a `SimpleSoundSpec`

src/game.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -2671,14 +2671,17 @@ void Game::openInventory()
26712671
infostream << "the_game: " << "Launching inventory" << std::endl;
26722672

26732673
PlayerInventoryFormSource *fs_src = new PlayerInventoryFormSource(client);
2674-
TextDest *txt_dst = new TextDestPlayerInventory(client);
2675-
2676-
create_formspec_menu(&current_formspec, client, &input->joystick, fs_src, txt_dst);
2677-
cur_formname = "";
26782674

26792675
InventoryLocation inventoryloc;
26802676
inventoryloc.setCurrentPlayer();
2681-
current_formspec->setFormSpec(fs_src->getForm(), inventoryloc);
2677+
2678+
if (!client->moddingEnabled()
2679+
|| !client->getScript()->on_inventory_open(fs_src->m_client->getInventory(inventoryloc))) {
2680+
TextDest *txt_dst = new TextDestPlayerInventory(client);
2681+
create_formspec_menu(&current_formspec, client, &input->joystick, fs_src, txt_dst);
2682+
cur_formname = "";
2683+
current_formspec->setFormSpec(fs_src->getForm(), inventoryloc);
2684+
}
26822685
}
26832686

26842687

src/script/cpp_api/s_client.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,27 @@ bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &poi
224224
return lua_toboolean(L, -1);
225225
}
226226

227+
bool ScriptApiClient::on_inventory_open(Inventory *inventory)
228+
{
229+
SCRIPTAPI_PRECHECKHEADER
230+
231+
lua_getglobal(L, "core");
232+
lua_getfield(L, -1, "registered_on_inventory_open");
233+
234+
std::vector<const InventoryList*> lists = inventory->getLists();
235+
std::vector<const InventoryList*>::iterator iter = lists.begin();
236+
lua_createtable(L, 0, lists.size());
237+
for (; iter != lists.end(); iter++) {
238+
const char* name = (*iter)->getName().c_str();
239+
lua_pushstring(L, name);
240+
push_inventory_list(L, inventory, name);
241+
lua_rawset(L, -3);
242+
}
243+
244+
runCallbacks(1, RUN_CALLBACKS_MODE_OR);
245+
return lua_toboolean(L, -1);
246+
}
247+
227248
void ScriptApiClient::setEnv(ClientEnvironment *env)
228249
{
229250
ScriptApiBase::setEnv(env);

src/script/cpp_api/s_client.h

+2
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,7 @@ class ScriptApiClient : virtual public ScriptApiBase
5757
bool on_placenode(const PointedThing &pointed, const ItemDefinition &item);
5858
bool on_item_use(const ItemStack &item, const PointedThing &pointed);
5959

60+
bool on_inventory_open(Inventory *inventory);
61+
6062
void setEnv(ClientEnvironment *env);
6163
};

0 commit comments

Comments
 (0)