m_luastack = luaL_newstate();
assert(m_luastack);
+ // Add and save an error handler
+ lua_pushcfunction(m_luastack, script_error_handler);
+ m_errorhandler = lua_gettop(m_luastack);
+
// Make the ScriptApiBase* accessible to ModApiBase
lua_pushlightuserdata(m_luastack, this);
lua_setfield(m_luastack, LUA_REGISTRYINDEX, "scriptapi");
lua_State *L = getStack();
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
- int ret = luaL_loadfile(L, scriptpath.c_str()) || lua_pcall(L, 0, 0, errorhandler);
- if(ret){
- errorstream<<"========== ERROR FROM LUA ==========="<<std::endl;
- errorstream<<"Failed to load and run script from "<<std::endl;
- errorstream<<scriptpath<<":"<<std::endl;
- errorstream<<std::endl;
- errorstream<<lua_tostring(L, -1)<<std::endl;
- errorstream<<std::endl;
- errorstream<<"======= END OF ERROR FROM LUA ========"<<std::endl;
+ int ret = luaL_loadfile(L, scriptpath.c_str()) || lua_pcall(L, 0, 0, m_errorhandler);
+ if (ret) {
+ errorstream << "========== ERROR FROM LUA ===========" << std::endl;
+ errorstream << "Failed to load and run script from " << std::endl;
+ errorstream << scriptpath << ":" << std::endl;
+ errorstream << std::endl;
+ errorstream << lua_tostring(L, -1) << std::endl;
+ errorstream << std::endl;
+ errorstream << "======= END OF ERROR FROM LUA ========" << std::endl;
lua_pop(L, 1); // Pop error message from stack
- lua_pop(L, 1); // Pop the error handler from stack
return false;
}
- lua_pop(L, 1); // Pop the error handler from stack
return true;
}
void objectrefGet(u16 id);
JMutex m_luastackmutex;
+ // Stack index of Lua error handler
+ int m_errorhandler;
#ifdef SCRIPTAPI_LOCK_DEBUG
bool m_locked;
#endif
+
private:
lua_State* m_luastack;
lua_gettable(L, -2);
// Should be a table, which we will use as a prototype
//luaL_checktype(L, -1, LUA_TTABLE);
- if(lua_type(L, -1) != LUA_TTABLE){
+ if (lua_type(L, -1) != LUA_TTABLE){
errorstream<<"LuaEntity name \""<<name<<"\" not defined"<<std::endl;
return false;
}
// This should be userdata with metatable ObjectRef
objectrefGet(id);
luaL_checktype(L, -1, LUA_TUSERDATA);
- if(!luaL_checkudata(L, -1, "ObjectRef"))
+ if (!luaL_checkudata(L, -1, "ObjectRef"))
luaL_typerror(L, -1, "ObjectRef");
lua_setfield(L, -2, "object");
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
- verbosestream<<"scriptapi_luaentity_activate: id="<<id<<std::endl;
+ verbosestream << "scriptapi_luaentity_activate: id=" << id << std::endl;
// Get minetest.luaentities[id]
luaentity_get(L, id);
// Get on_activate function
lua_getfield(L, -1, "on_activate");
- if(!lua_isnil(L, -1)) {
+ if (!lua_isnil(L, -1)) {
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self
lua_pushlstring(L, staticdata.c_str(), staticdata.size());
lua_pushinteger(L, dtime_s);
// Call with 3 arguments, 0 results
- if(lua_pcall(L, 3, 0, errorhandler))
+ if (lua_pcall(L, 3, 0, m_errorhandler))
scriptError();
} else {
lua_pop(L, 1);
}
- lua_pop(L, 2); // Pop object and error handler
+ lua_pop(L, 1); // Pop object
}
void ScriptApiEntity::luaentity_Remove(u16 id)
{
SCRIPTAPI_PRECHECKHEADER
- verbosestream<<"scriptapi_luaentity_rm: id="<<id<<std::endl;
+ verbosestream << "scriptapi_luaentity_rm: id=" << id << std::endl;
// Get minetest.luaentities table
lua_getglobal(L, "minetest");
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
//infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
// Get minetest.luaentities[id]
// Get get_staticdata function
lua_getfield(L, -1, "get_staticdata");
- if(lua_isnil(L, -1))
+ if (lua_isnil(L, -1)) {
+ lua_pop(L, 2); // Pop entity and get_staticdata
return "";
+ }
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self
// Call with 1 arguments, 1 results
- if(lua_pcall(L, 1, 1, errorhandler))
+ if (lua_pcall(L, 1, 1, m_errorhandler))
scriptError();
lua_remove(L, object); // Remove object
- lua_remove(L, errorhandler); // Remove error handler
size_t len = 0;
const char *s = lua_tolstring(L, -1, &len);
+ lua_pop(L, 1); // Pop static data
return std::string(s, len);
}
//infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
// Get minetest.luaentities[id]
- luaentity_get(L,id);
- //int object = lua_gettop(L);
+ luaentity_get(L, id);
// Set default values that differ from ObjectProperties defaults
prop->hp_max = 10;
getfloatfield(L, -1, "weight", prop->weight);
lua_getfield(L, -1, "collisionbox");
- if(lua_istable(L, -1))
+ if (lua_istable(L, -1))
prop->collisionbox = read_aabb3f(L, -1, 1.0);
lua_pop(L, 1);
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
//infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
// Get minetest.luaentities[id]
// State: object is at top of stack
// Get step function
lua_getfield(L, -1, "on_step");
- if(lua_isnil(L, -1))
+ if (lua_isnil(L, -1)) {
+ lua_pop(L, 2); // Pop on_step and entity
return;
+ }
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self
lua_pushnumber(L, dtime); // dtime
// Call with 2 arguments, 0 results
- if(lua_pcall(L, 2, 0, errorhandler))
+ if (lua_pcall(L, 2, 0, m_errorhandler))
scriptError();
- lua_remove(L, object); // Remove object
- lua_remove(L, errorhandler); // Remove error handler
+ lua_pop(L, 1); // Pop object
}
// Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
//infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
// Get minetest.luaentities[id]
// State: object is at top of stack
// Get function
lua_getfield(L, -1, "on_punch");
- if(lua_isnil(L, -1))
+ if (lua_isnil(L, -1)) {
+ lua_pop(L, 2); // Pop on_punch and entitu
return;
+ }
luaL_checktype(L, -1, LUA_TFUNCTION);
- lua_pushvalue(L, object); // self
- objectrefGetOrCreate(puncher); // Clicker reference
+ lua_pushvalue(L, object); // self
+ objectrefGetOrCreate(puncher); // Clicker reference
lua_pushnumber(L, time_from_last_punch);
push_tool_capabilities(L, *toolcap);
push_v3f(L, dir);
// Call with 5 arguments, 0 results
- if(lua_pcall(L, 5, 0, errorhandler))
+ if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
- lua_remove(L, object); // Remove object
- lua_remove(L, errorhandler); // Remove error handler
+ lua_pop(L, 1); // Pop object
}
// Calls entity:on_rightclick(ObjectRef clicker)
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
//infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
// Get minetest.luaentities[id]
- luaentity_get(L,id);
+ luaentity_get(L, id);
int object = lua_gettop(L);
// State: object is at top of stack
// Get function
lua_getfield(L, -1, "on_rightclick");
- if(lua_isnil(L, -1))
+ if (lua_isnil(L, -1)) {
+ lua_pop(L, 2); // Pop on_rightclick and entity
return;
+ }
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self
objectrefGetOrCreate(clicker); // Clicker reference
// Call with 2 arguments, 0 results
- if(lua_pcall(L, 2, 0, errorhandler))
+ if (lua_pcall(L, 2, 0, m_errorhandler))
scriptError();
- lua_remove(L, object); // Remove object
- lua_remove(L, errorhandler); // Remove error handler
+ lua_pop(L, 1); // Pop object
}
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
// Push callback function on stack
- if(!getDetachedInventoryCallback(name, "allow_move"))
+ if (!getDetachedInventoryCallback(name, "allow_move"))
return count;
// function(inv, from_list, from_index, to_list, to_index, count, player)
lua_pushinteger(L, to_index + 1); // to_index
lua_pushinteger(L, count); // count
objectrefGetOrCreate(player); // player
- if(lua_pcall(L, 7, 1, errorhandler))
+ if (lua_pcall(L, 7, 1, m_errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
throw LuaError("allow_move should return a number. name=" + name);
int ret = luaL_checkinteger(L, -1);
- lua_pop(L, 2); // Pop integer and error handler
+ lua_pop(L, 1); // Pop integer
return ret;
}
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
// Push callback function on stack
- if(!getDetachedInventoryCallback(name, "allow_put"))
+ if (!getDetachedInventoryCallback(name, "allow_put"))
return stack.count; // All will be accepted
// Call function(inv, listname, index, stack, player)
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
- if(lua_pcall(L, 5, 1, errorhandler))
+ if (lua_pcall(L, 5, 1, m_errorhandler))
scriptError();
- if(!lua_isnumber(L, -1))
+ if (!lua_isnumber(L, -1))
throw LuaError("allow_put should return a number. name=" + name);
int ret = luaL_checkinteger(L, -1);
- lua_pop(L, 2); // Pop integer and error handler
+ lua_pop(L, 1); // Pop integer
return ret;
}
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
// Push callback function on stack
- if(!getDetachedInventoryCallback(name, "allow_take"))
+ if (!getDetachedInventoryCallback(name, "allow_take"))
return stack.count; // All will be accepted
// Call function(inv, listname, index, stack, player)
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
- if(lua_pcall(L, 5, 1, errorhandler))
+ if (lua_pcall(L, 5, 1, m_errorhandler))
scriptError();
- if(!lua_isnumber(L, -1))
+ if (!lua_isnumber(L, -1))
throw LuaError("allow_take should return a number. name=" + name);
int ret = luaL_checkinteger(L, -1);
- lua_pop(L, 2); // Pop integer and error handler
+ lua_pop(L, 1); // Pop integer
return ret;
}
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
// Push callback function on stack
- if(!getDetachedInventoryCallback(name, "on_move"))
+ if (!getDetachedInventoryCallback(name, "on_move"))
return;
// function(inv, from_list, from_index, to_list, to_index, count, player)
lua_pushinteger(L, to_index + 1); // to_index
lua_pushinteger(L, count); // count
objectrefGetOrCreate(player); // player
- if(lua_pcall(L, 7, 0, errorhandler))
+ if (lua_pcall(L, 7, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
}
// Report put items
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
// Push callback function on stack
- if(!getDetachedInventoryCallback(name, "on_put"))
+ if (!getDetachedInventoryCallback(name, "on_put"))
return;
// Call function(inv, listname, index, stack, player)
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
- if(lua_pcall(L, 5, 0, errorhandler))
+ if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
}
// Report taken items
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
// Push callback function on stack
- if(!getDetachedInventoryCallback(name, "on_take"))
+ if (!getDetachedInventoryCallback(name, "on_take"))
return;
// Call function(inv, listname, index, stack, player)
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
- if(lua_pcall(L, 5, 0, errorhandler))
+ if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
}
// Retrieves minetest.detached_inventories[name][callbackname]
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
// Push callback function on stack
- if(!getItemCallback(item.name.c_str(), "on_drop"))
+ if (!getItemCallback(item.name.c_str(), "on_drop"))
return false;
// Call function
LuaItemStack::create(L, item);
objectrefGetOrCreate(dropper);
pushFloatPos(L, pos);
- if(lua_pcall(L, 3, 1, errorhandler))
+ if (lua_pcall(L, 3, 1, m_errorhandler))
scriptError();
- if(!lua_isnil(L, -1)) {
+ if (!lua_isnil(L, -1)) {
try {
item = read_item(L,-1, getServer());
} catch (LuaError &e) {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
}
- lua_pop(L, 2); // Pop item and error handler
+ lua_pop(L, 1); // Pop item
return true;
}
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
// Push callback function on stack
- if(!getItemCallback(item.name.c_str(), "on_place"))
+ if (!getItemCallback(item.name.c_str(), "on_place"))
return false;
// Call function
LuaItemStack::create(L, item);
objectrefGetOrCreate(placer);
pushPointedThing(pointed);
- if(lua_pcall(L, 3, 1, errorhandler))
+ if (lua_pcall(L, 3, 1, m_errorhandler))
scriptError();
- if(!lua_isnil(L, -1)) {
+ if (!lua_isnil(L, -1)) {
try {
item = read_item(L,-1, getServer());
} catch (LuaError &e) {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
}
- lua_pop(L, 2); // Pop item and error handler
+ lua_pop(L, 1); // Pop item
return true;
}
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
// Push callback function on stack
- if(!getItemCallback(item.name.c_str(), "on_use"))
+ if (!getItemCallback(item.name.c_str(), "on_use"))
return false;
// Call function
LuaItemStack::create(L, item);
objectrefGetOrCreate(user);
pushPointedThing(pointed);
- if(lua_pcall(L, 3, 1, errorhandler))
+ if (lua_pcall(L, 3, 1, m_errorhandler))
scriptError();
if(!lua_isnil(L, -1)) {
try {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
}
- lua_pop(L, 2); // Pop item and error handler
+ lua_pop(L, 1); // Pop item
return true;
}
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
lua_getglobal(L, "minetest");
lua_getfield(L, -1, "on_craft");
LuaItemStack::create(L, item);
objectrefGetOrCreate(user);
- //Push inventory list
+ // Push inventory list
std::vector<ItemStack> items;
- for(u32 i=0; i<old_craft_grid->getSize(); i++)
+ for (u32 i = 0; i < old_craft_grid->getSize(); i++) {
items.push_back(old_craft_grid->getItem(i));
+ }
push_items(L, items);
InvRef::create(L, craft_inv);
- if(lua_pcall(L, 4, 1, errorhandler))
+ if (lua_pcall(L, 4, 1, m_errorhandler))
scriptError();
- if(!lua_isnil(L, -1)) {
+ if (!lua_isnil(L, -1)) {
try {
item = read_item(L,-1, getServer());
} catch (LuaError &e) {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
}
- lua_pop(L, 2); // Pop item and error handler
+ lua_pop(L, 1); // Pop item
return true;
}
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
lua_getglobal(L, "minetest");
lua_getfield(L, -1, "craft_predict");
LuaItemStack::create(L, item);
//Push inventory list
std::vector<ItemStack> items;
- for(u32 i=0; i<old_craft_grid->getSize(); i++)
+ for (u32 i = 0; i < old_craft_grid->getSize(); i++) {
items.push_back(old_craft_grid->getItem(i));
+ }
push_items(L, items);
InvRef::create(L, craft_inv);
- if(lua_pcall(L, 4, 1, errorhandler))
+ if (lua_pcall(L, 4, 1, m_errorhandler))
scriptError();
- if(!lua_isnil(L, -1)) {
+ if (!lua_isnil(L, -1)) {
try {
item = read_item(L,-1, getServer());
} catch (LuaError &e) {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
}
- lua_pop(L, 2); // Pop item and error handler
+ lua_pop(L, 1); // Pop item
return true;
}
}
}
-
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
// Get handler function
lua_getglobal(L, "engine");
lua_getfield(L, -1, "event_handler");
lua_remove(L, -2); // Remove engine
- if(lua_isnil(L, -1)) {
+ if (lua_isnil(L, -1)) {
lua_pop(L, 1); // Pop event_handler
return;
}
// Call it
lua_pushstring(L, text.c_str());
- if(lua_pcall(L, 1, 0, errorhandler))
+ if (lua_pcall(L, 1, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
}
void ScriptApiMainMenu::handleMainMenuButtons(std::map<std::string, std::string> fields)
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
// Get handler function
lua_getglobal(L, "engine");
lua_getfield(L, -1, "button_handler");
lua_remove(L, -2); // Remove engine
- if(lua_isnil(L, -1)) {
+ if (lua_isnil(L, -1)) {
lua_pop(L, 1); // Pop button handler
return;
}
luaL_checktype(L, -1, LUA_TFUNCTION);
- // Convert fields to lua table
+ // Convert fields to a Lua table
lua_newtable(L);
- for(std::map<std::string, std::string>::const_iterator
- i = fields.begin(); i != fields.end(); i++){
- const std::string &name = i->first;
- const std::string &value = i->second;
+ std::map<std::string, std::string>::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);
}
// Call it
- if(lua_pcall(L, 1, 0, errorhandler))
+ if (lua_pcall(L, 1, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
}
+
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
- if(!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
+ if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
return false;
// Call function
pushnode(L, node, ndef);
objectrefGetOrCreate(puncher);
pushPointedThing(pointed);
- if(lua_pcall(L, 4, 0, errorhandler))
+ if (lua_pcall(L, 4, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
return true;
}
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
- if(!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
+ if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
return false;
// Call function
push_v3s16(L, p);
pushnode(L, node, ndef);
objectrefGetOrCreate(digger);
- if(lua_pcall(L, 3, 0, errorhandler))
+ if (lua_pcall(L, 3, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
return true;
}
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
- if(!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
+ if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
return;
// Call function
push_v3s16(L, p);
- if(lua_pcall(L, 1, 0, errorhandler))
+ if (lua_pcall(L, 1, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
}
void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
- if(!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
+ if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
return;
// Call function
push_v3s16(L, p);
- if(lua_pcall(L, 1, 0, errorhandler))
+ if (lua_pcall(L, 1, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
}
void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
- if(!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
+ if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
return;
// Call function
push_v3s16(L, p);
pushnode(L, node, ndef);
- if(lua_pcall(L, 2, 0, errorhandler))
+ if (lua_pcall(L, 2, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
}
bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
- if(!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
+ if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
return false;
// Call function
push_v3s16(L, p);
lua_pushnumber(L,dtime);
- if(lua_pcall(L, 2, 1, errorhandler))
+ if (lua_pcall(L, 2, 1, m_errorhandler))
scriptError();
- lua_remove(L, errorhandler); // Remove error handler
return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
}
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
INodeDefManager *ndef = getServer()->ndef();
// If node doesn't exist, we don't know what callback to call
MapNode node = getEnv()->getMap().getNodeNoEx(p);
- if(node.getContent() == CONTENT_IGNORE)
+ if (node.getContent() == CONTENT_IGNORE)
return;
// Push callback function on stack
- if(!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
+ if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
return;
// Call function
push_v3s16(L, p); // pos
lua_pushstring(L, formname.c_str()); // formname
lua_newtable(L); // fields
- for(std::map<std::string, std::string>::const_iterator
- i = fields.begin(); i != fields.end(); i++){
- const std::string &name = i->first;
- const std::string &value = i->second;
+ std::map<std::string, std::string>::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);
}
objectrefGetOrCreate(sender); // player
- if(lua_pcall(L, 4, 0, errorhandler))
+ if (lua_pcall(L, 4, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
}
void ScriptApiNode::node_falling_update(v3s16 p)
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
lua_getglobal(L, "nodeupdate");
push_v3s16(L, p);
- if(lua_pcall(L, 1, 0, errorhandler))
+ if (lua_pcall(L, 1, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
}
void ScriptApiNode::node_falling_update_single(v3s16 p)
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
lua_getglobal(L, "nodeupdate_single");
push_v3s16(L, p);
- if(lua_pcall(L, 1, 0, errorhandler))
+ if (lua_pcall(L, 1, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
}
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
INodeDefManager *ndef = getServer()->ndef();
// If node doesn't exist, we don't know what callback to call
MapNode node = getEnv()->getMap().getNodeNoEx(p);
- if(node.getContent() == CONTENT_IGNORE)
+ if (node.getContent() == CONTENT_IGNORE)
return 0;
// Push callback function on stack
std::string nodename = ndef->get(node).name;
- if(!getItemCallback(nodename.c_str(), "allow_metadata_inventory_move"))
+ if (!getItemCallback(nodename.c_str(), "allow_metadata_inventory_move"))
return count;
// function(pos, from_list, from_index, to_list, to_index, count, player)
lua_pushinteger(L, to_index + 1); // to_index
lua_pushinteger(L, count); // count
objectrefGetOrCreate(player); // player
- if(lua_pcall(L, 7, 1, errorhandler))
+ if (lua_pcall(L, 7, 1, m_errorhandler))
scriptError();
- lua_remove(L, errorhandler); // Remove error handler
- if(!lua_isnumber(L, -1))
+ if (!lua_isnumber(L, -1))
throw LuaError("allow_metadata_inventory_move should"
" return a number, guilty node: " + nodename);
int num = luaL_checkinteger(L, -1);
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
INodeDefManager *ndef = getServer()->ndef();
// If node doesn't exist, we don't know what callback to call
MapNode node = getEnv()->getMap().getNodeNoEx(p);
- if(node.getContent() == CONTENT_IGNORE)
+ if (node.getContent() == CONTENT_IGNORE)
return 0;
// Push callback function on stack
std::string nodename = ndef->get(node).name;
- if(!getItemCallback(nodename.c_str(), "allow_metadata_inventory_put"))
+ if (!getItemCallback(nodename.c_str(), "allow_metadata_inventory_put"))
return stack.count;
// Call function(pos, listname, index, stack, player)
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
- if(lua_pcall(L, 5, 1, errorhandler))
+ if (lua_pcall(L, 5, 1, m_errorhandler))
scriptError();
- lua_remove(L, errorhandler); // Remove error handler
if(!lua_isnumber(L, -1))
throw LuaError("allow_metadata_inventory_put should"
" return a number, guilty node: " + nodename);
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
INodeDefManager *ndef = getServer()->ndef();
// If node doesn't exist, we don't know what callback to call
MapNode node = getEnv()->getMap().getNodeNoEx(p);
- if(node.getContent() == CONTENT_IGNORE)
+ if (node.getContent() == CONTENT_IGNORE)
return 0;
// Push callback function on stack
std::string nodename = ndef->get(node).name;
- if(!getItemCallback(nodename.c_str(), "allow_metadata_inventory_take"))
+ if (!getItemCallback(nodename.c_str(), "allow_metadata_inventory_take"))
return stack.count;
// Call function(pos, listname, index, count, player)
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
- if(lua_pcall(L, 5, 1, errorhandler))
+ if (lua_pcall(L, 5, 1, m_errorhandler))
scriptError();
- lua_remove(L, errorhandler); // Remove error handler
- if(!lua_isnumber(L, -1))
+ if (!lua_isnumber(L, -1))
throw LuaError("allow_metadata_inventory_take should"
" return a number, guilty node: " + nodename);
int num = luaL_checkinteger(L, -1);
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
INodeDefManager *ndef = getServer()->ndef();
// If node doesn't exist, we don't know what callback to call
MapNode node = getEnv()->getMap().getNodeNoEx(p);
- if(node.getContent() == CONTENT_IGNORE)
+ if (node.getContent() == CONTENT_IGNORE)
return;
// Push callback function on stack
std::string nodename = ndef->get(node).name;
- if(!getItemCallback(nodename.c_str(), "on_metadata_inventory_move"))
+ if (!getItemCallback(nodename.c_str(), "on_metadata_inventory_move"))
return;
// function(pos, from_list, from_index, to_list, to_index, count, player)
lua_pushinteger(L, to_index + 1); // to_index
lua_pushinteger(L, count); // count
objectrefGetOrCreate(player); // player
- if(lua_pcall(L, 7, 0, errorhandler))
+ if (lua_pcall(L, 7, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
}
// Report put items
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
INodeDefManager *ndef = getServer()->ndef();
// If node doesn't exist, we don't know what callback to call
MapNode node = getEnv()->getMap().getNodeNoEx(p);
- if(node.getContent() == CONTENT_IGNORE)
+ if (node.getContent() == CONTENT_IGNORE)
return;
// Push callback function on stack
std::string nodename = ndef->get(node).name;
- if(!getItemCallback(nodename.c_str(), "on_metadata_inventory_put"))
+ if (!getItemCallback(nodename.c_str(), "on_metadata_inventory_put"))
return;
// Call function(pos, listname, index, stack, player)
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
- if(lua_pcall(L, 5, 0, errorhandler))
+ if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
}
// Report taken items
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
INodeDefManager *ndef = getServer()->ndef();
// If node doesn't exist, we don't know what callback to call
MapNode node = getEnv()->getMap().getNodeNoEx(p);
- if(node.getContent() == CONTENT_IGNORE)
+ if (node.getContent() == CONTENT_IGNORE)
return;
// Push callback function on stack
std::string nodename = ndef->get(node).name;
- if(!getItemCallback(nodename.c_str(), "on_metadata_inventory_take"))
+ if (!getItemCallback(nodename.c_str(), "on_metadata_inventory_take"))
return;
// Call function(pos, listname, index, stack, player)
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
- if(lua_pcall(L, 5, 0, errorhandler))
+ if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
}
ScriptApiNodemeta::ScriptApiNodemeta() {
ScriptApiNodemeta::~ScriptApiNodemeta() {
}
-
-
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
getAuthHandler();
lua_getfield(L, -1, "get_auth");
- if(lua_type(L, -1) != LUA_TFUNCTION)
+ if (lua_type(L, -1) != LUA_TFUNCTION)
throw LuaError("Authentication handler missing get_auth");
lua_pushstring(L, playername.c_str());
- if(lua_pcall(L, 1, 1, errorhandler))
+ if (lua_pcall(L, 1, 1, m_errorhandler))
scriptError();
lua_remove(L, -2); // Remove auth handler
- lua_remove(L, errorhandler); // Remove error handler
// nil = login not allowed
- if(lua_isnil(L, -1))
+ if (lua_isnil(L, -1))
return false;
luaL_checktype(L, -1, LUA_TTABLE);
std::string password;
bool found = getstringfield(L, -1, "password", password);
- if(!found)
+ if (!found)
throw LuaError("Authentication handler didn't return password");
- if(dst_password)
+ if (dst_password)
*dst_password = password;
lua_getfield(L, -1, "privileges");
- if(!lua_istable(L, -1))
+ if (!lua_istable(L, -1))
throw LuaError("Authentication handler didn't return privilege table");
- if(dst_privs)
+ if (dst_privs)
readPrivileges(-1, *dst_privs);
lua_pop(L, 1);
lua_getglobal(L, "minetest");
lua_getfield(L, -1, "registered_auth_handler");
- if(lua_isnil(L, -1)){
+ if (lua_isnil(L, -1)){
lua_pop(L, 1);
lua_getfield(L, -1, "builtin_auth_handler");
}
lua_remove(L, -2); // Remove minetest
- if(lua_type(L, -1) != LUA_TTABLE)
+ if (lua_type(L, -1) != LUA_TTABLE)
throw LuaError("Authentication handler table not valid");
}
result.clear();
lua_pushnil(L);
- if(index < 0)
+ if (index < 0)
index -= 1;
- while(lua_next(L, index) != 0){
+ while (lua_next(L, index) != 0) {
// key at index -2 and value at index -1
std::string key = luaL_checkstring(L, -2);
bool value = lua_toboolean(L, -1);
- if(value)
+ if (value)
result.insert(key);
// removes value, keeps key for next iteration
lua_pop(L, 1);
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
getAuthHandler();
lua_getfield(L, -1, "create_auth");
lua_remove(L, -2); // Remove auth handler
- if(lua_type(L, -1) != LUA_TFUNCTION)
+ if (lua_type(L, -1) != LUA_TFUNCTION)
throw LuaError("Authentication handler missing create_auth");
lua_pushstring(L, playername.c_str());
lua_pushstring(L, password.c_str());
- if(lua_pcall(L, 2, 0, errorhandler))
+ if (lua_pcall(L, 2, 0, m_errorhandler))
scriptError();
- lua_pop(L, 1); // Pop error handler
}
bool ScriptApiServer::setPassword(const std::string &playername,
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
getAuthHandler();
lua_getfield(L, -1, "set_password");
lua_remove(L, -2); // Remove auth handler
- if(lua_type(L, -1) != LUA_TFUNCTION)
+ if (lua_type(L, -1) != LUA_TFUNCTION)
throw LuaError("Authentication handler missing set_password");
lua_pushstring(L, playername.c_str());
lua_pushstring(L, password.c_str());
- if(lua_pcall(L, 2, 1, errorhandler))
+ if (lua_pcall(L, 2, 1, m_errorhandler))
scriptError();
- lua_remove(L, -2); // Remove error handler
return lua_toboolean(L, -1);
}