mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
[CSM] Add local formspecs. (#5094)
This commit is contained in:
parent
2c19d51409
commit
c42c53fccf
12 changed files with 126 additions and 67 deletions
|
@ -112,3 +112,27 @@ void ScriptApiClient::environment_step(float dtime)
|
|||
+ script_get_backtrace(L));
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptApiClient::on_formspec_input(const std::string &formname,
|
||||
const StringMap &fields)
|
||||
{
|
||||
SCRIPTAPI_PRECHECKHEADER
|
||||
|
||||
// Get core.registered_on_chat_messages
|
||||
lua_getglobal(L, "core");
|
||||
lua_getfield(L, -1, "registered_on_formspec_input");
|
||||
// Call callbacks
|
||||
// param 1
|
||||
lua_pushstring(L, formname.c_str());
|
||||
// param 2
|
||||
lua_newtable(L);
|
||||
StringMap::const_iterator it;
|
||||
for (it = fields.begin(); it != fields.end(); ++it) {
|
||||
const std::string &name = it->first;
|
||||
const std::string &value = it->second;
|
||||
lua_pushstring(L, name.c_str());
|
||||
lua_pushlstring(L, value.c_str(), value.size());
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
|
||||
}
|
||||
|
|
|
@ -22,6 +22,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#define S_CLIENT_H_
|
||||
|
||||
#include "cpp_api/s_base.h"
|
||||
#include "util/string.h"
|
||||
|
||||
#ifdef _CRT_MSVCP_CURRENT
|
||||
#include <cstdint>
|
||||
#endif
|
||||
|
||||
class ScriptApiClient: virtual public ScriptApiBase
|
||||
{
|
||||
|
@ -36,7 +41,7 @@ public:
|
|||
void on_damage_taken(int32_t damage_amount);
|
||||
void on_hp_modification(int32_t newhp);
|
||||
void on_death();
|
||||
|
||||
void environment_step(float dtime);
|
||||
void on_formspec_input(const std::string &formname, const StringMap &fields);
|
||||
};
|
||||
#endif
|
||||
|
|
|
@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "l_internal.h"
|
||||
#include "util/string.h"
|
||||
#include "cpp_api/s_base.h"
|
||||
#include "gettext.h"
|
||||
|
||||
int ModApiClient::l_get_current_modname(lua_State *L)
|
||||
{
|
||||
|
@ -44,18 +45,55 @@ int ModApiClient::l_get_last_run_mod(lua_State *L)
|
|||
// set_last_run_mod(modname)
|
||||
int ModApiClient::l_set_last_run_mod(lua_State *L)
|
||||
{
|
||||
if (!lua_isstring(L, 1))
|
||||
return 0;
|
||||
|
||||
const char *mod = lua_tostring(L, 1);
|
||||
getScriptApiBase(L)->setOriginDirect(mod);
|
||||
return 0;
|
||||
lua_pushboolean(L, true);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// display_chat_message(message)
|
||||
int ModApiClient::l_display_chat_message(lua_State *L)
|
||||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
if (!lua_isstring(L, 1))
|
||||
return 0;
|
||||
|
||||
std::string message = luaL_checkstring(L, 1);
|
||||
getClient(L)->pushToChatQueue(utf8_to_wide(message));
|
||||
lua_pushboolean(L, true);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// show_formspec(formspec)
|
||||
int ModApiClient::l_show_formspec(lua_State *L)
|
||||
{
|
||||
if ( !lua_isstring(L, 1) || !lua_isstring(L, 2) )
|
||||
return 0;
|
||||
|
||||
ClientEvent event;
|
||||
event.type = CE_SHOW_LOCAL_FORMSPEC;
|
||||
event.show_formspec.formname = new std::string(luaL_checkstring(L, 1));
|
||||
event.show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
|
||||
getClient(L)->pushToEventQueue(event);
|
||||
lua_pushboolean(L, true);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// send_respawn()
|
||||
int ModApiClient::l_send_respawn(lua_State *L)
|
||||
{
|
||||
getClient(L)->sendRespawn();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// gettext(text)
|
||||
int ModApiClient::l_gettext(lua_State *L)
|
||||
{
|
||||
std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
|
||||
lua_pushstring(L, text.c_str());
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -65,4 +103,7 @@ void ModApiClient::Initialize(lua_State *L, int top)
|
|||
API_FCT(display_chat_message);
|
||||
API_FCT(set_last_run_mod);
|
||||
API_FCT(get_last_run_mod);
|
||||
API_FCT(show_formspec);
|
||||
API_FCT(send_respawn);
|
||||
API_FCT(gettext);
|
||||
}
|
||||
|
|
|
@ -33,6 +33,15 @@ private:
|
|||
// display_chat_message(message)
|
||||
static int l_display_chat_message(lua_State *L);
|
||||
|
||||
// show_formspec(name, fornspec)
|
||||
static int l_show_formspec(lua_State *L);
|
||||
|
||||
// send_respawn()
|
||||
static int l_send_respawn(lua_State *L);
|
||||
|
||||
// gettext(text)
|
||||
static int l_gettext(lua_State *L);
|
||||
|
||||
// get_last_run_mod(n)
|
||||
static int l_get_last_run_mod(lua_State *L);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue