]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Port formspec API from waspsaliva
authorElias Fleckenstein <eliasfleckenstein@web.de>
Thu, 18 Feb 2021 19:20:22 +0000 (20:20 +0100)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Thu, 18 Feb 2021 19:20:22 +0000 (20:20 +0100)
This API is inofficial and undocumented; invalid usage causes the game to crash. Use at own risk!

src/script/lua_api/l_client.cpp
src/script/lua_api/l_client.h

index abd27f7dc185af9322750dfb710544840ece1735..484af2ec35e64fc055a9ff10ebed83e0677e2e26 100644 (file)
@@ -621,6 +621,48 @@ int ModApiClient::l_interact(lua_State *L)
        return 1;
 }
 
+StringMap *table_to_stringmap(lua_State *L, int index)
+{
+       StringMap *m = new StringMap;
+
+       lua_pushvalue(L, index);
+       lua_pushnil(L);
+
+       while (lua_next(L, -2)) {
+               lua_pushvalue(L, -2);
+               std::basic_string<char> key = lua_tostring(L, -1);
+               std::basic_string<char> value = lua_tostring(L, -2);
+               (*m)[key] = value;
+               lua_pop(L, 2);
+       }
+
+       lua_pop(L, 1);
+
+       return m;
+}
+
+// send_inventory_fields(formname, fields)
+// Only works if the inventory form was opened beforehand.
+int ModApiClient::l_send_inventory_fields(lua_State *L)
+{
+       std::string formname = luaL_checkstring(L, 1);
+       StringMap *fields = table_to_stringmap(L, 2);
+
+       getClient(L)->sendInventoryFields(formname, *fields);
+       return 0;
+}
+
+// send_nodemeta_fields(position, formname, fields)
+int ModApiClient::l_send_nodemeta_fields(lua_State *L)
+{
+       v3s16 pos = check_v3s16(L, 1);
+       std::string formname = luaL_checkstring(L, 2);
+       StringMap *m = table_to_stringmap(L, 3);
+
+       getClient(L)->sendNodemetaFields(pos, formname, *m);
+       return 0;
+}
+
 void ModApiClient::Initialize(lua_State *L, int top)
 {
        API_FCT(get_current_modname);
@@ -657,4 +699,6 @@ void ModApiClient::Initialize(lua_State *L, int top)
        API_FCT(get_objects_inside_radius);
        API_FCT(make_screenshot);
        API_FCT(interact);
+       API_FCT(send_inventory_fields);
+       API_FCT(send_nodemeta_fields);
 }
index 03a42e0225ed2064fadb5639a44da3374b4b8c5c..caf21f78e7ee671be382936646aab828d5c143e2 100644 (file)
@@ -132,6 +132,12 @@ class ModApiClient : public ModApiBase
        // interact(action, pointed_thing)
        static int l_interact(lua_State *L);
 
+       // send_inventory_fields(formname, fields)
+       static int l_send_inventory_fields(lua_State *L);
+
+       // send_nodemeta_fields(position, formname, fields)
+       static int l_send_nodemeta_fields(lua_State *L);
+
 public:
        static void Initialize(lua_State *L, int top);
 };