aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api/l_minimap.cpp
diff options
context:
space:
mode:
authorJude Melton-Houghton <jwmhjwmh@gmail.com>2022-10-04 08:31:36 -0400
committerGitHub <noreply@github.com>2022-10-04 08:31:36 -0400
commit7632af3c73fc4e4ae3ad4c98c90c39b47c3b8379 (patch)
tree08b974d6ecafb6c20de565f96e329748030c1519 /src/script/lua_api/l_minimap.cpp
parentb21fb1837955e6385137184cac906245821b20e4 (diff)
downloadminetest-7632af3c73fc4e4ae3ad4c98c90c39b47c3b8379.tar.xz
Consolidate API object code (#12728)
Co-authored-by: sfan5 <sfan5@live.de>
Diffstat (limited to 'src/script/lua_api/l_minimap.cpp')
-rw-r--r--src/script/lua_api/l_minimap.cpp59
1 files changed, 15 insertions, 44 deletions
diff --git a/src/script/lua_api/l_minimap.cpp b/src/script/lua_api/l_minimap.cpp
index a135e0bd5..d70063c51 100644
--- a/src/script/lua_api/l_minimap.cpp
+++ b/src/script/lua_api/l_minimap.cpp
@@ -50,7 +50,7 @@ void LuaMinimap::create(lua_State *L, Minimap *m)
int LuaMinimap::l_get_pos(lua_State *L)
{
- LuaMinimap *ref = checkobject(L, 1);
+ LuaMinimap *ref = checkObject<LuaMinimap>(L, 1);
Minimap *m = getobject(ref);
push_v3s16(L, m->getPos());
@@ -59,7 +59,7 @@ int LuaMinimap::l_get_pos(lua_State *L)
int LuaMinimap::l_set_pos(lua_State *L)
{
- LuaMinimap *ref = checkobject(L, 1);
+ LuaMinimap *ref = checkObject<LuaMinimap>(L, 1);
Minimap *m = getobject(ref);
m->setPos(read_v3s16(L, 2));
@@ -68,7 +68,7 @@ int LuaMinimap::l_set_pos(lua_State *L)
int LuaMinimap::l_get_angle(lua_State *L)
{
- LuaMinimap *ref = checkobject(L, 1);
+ LuaMinimap *ref = checkObject<LuaMinimap>(L, 1);
Minimap *m = getobject(ref);
lua_pushinteger(L, m->getAngle());
@@ -77,7 +77,7 @@ int LuaMinimap::l_get_angle(lua_State *L)
int LuaMinimap::l_set_angle(lua_State *L)
{
- LuaMinimap *ref = checkobject(L, 1);
+ LuaMinimap *ref = checkObject<LuaMinimap>(L, 1);
Minimap *m = getobject(ref);
m->setAngle(lua_tointeger(L, 2));
@@ -86,7 +86,7 @@ int LuaMinimap::l_set_angle(lua_State *L)
int LuaMinimap::l_get_mode(lua_State *L)
{
- LuaMinimap *ref = checkobject(L, 1);
+ LuaMinimap *ref = checkObject<LuaMinimap>(L, 1);
Minimap *m = getobject(ref);
lua_pushinteger(L, m->getModeIndex());
@@ -95,7 +95,7 @@ int LuaMinimap::l_get_mode(lua_State *L)
int LuaMinimap::l_set_mode(lua_State *L)
{
- LuaMinimap *ref = checkobject(L, 1);
+ LuaMinimap *ref = checkObject<LuaMinimap>(L, 1);
Minimap *m = getobject(ref);
u32 mode = lua_tointeger(L, 2);
@@ -108,7 +108,7 @@ int LuaMinimap::l_set_mode(lua_State *L)
int LuaMinimap::l_set_shape(lua_State *L)
{
- LuaMinimap *ref = checkobject(L, 1);
+ LuaMinimap *ref = checkObject<LuaMinimap>(L, 1);
Minimap *m = getobject(ref);
if (!lua_isnumber(L, 2))
return 0;
@@ -119,7 +119,7 @@ int LuaMinimap::l_set_shape(lua_State *L)
int LuaMinimap::l_get_shape(lua_State *L)
{
- LuaMinimap *ref = checkobject(L, 1);
+ LuaMinimap *ref = checkObject<LuaMinimap>(L, 1);
Minimap *m = getobject(ref);
lua_pushnumber(L, (int)m->getMinimapShape());
@@ -135,7 +135,7 @@ int LuaMinimap::l_show(lua_State *L)
Client *client = getClient(L);
assert(client);
- LuaMinimap *ref = checkobject(L, 1);
+ LuaMinimap *ref = checkObject<LuaMinimap>(L, 1);
Minimap *m = getobject(ref);
// This is not very adapted to new minimap mode management. Btw, tried
@@ -153,7 +153,7 @@ int LuaMinimap::l_hide(lua_State *L)
Client *client = getClient(L);
assert(client);
- LuaMinimap *ref = checkobject(L, 1);
+ LuaMinimap *ref = checkObject<LuaMinimap>(L, 1);
Minimap *m = getobject(ref);
// This is not very adapted to new minimap mode management. Btw, tried
@@ -166,19 +166,6 @@ int LuaMinimap::l_hide(lua_State *L)
return 1;
}
-LuaMinimap *LuaMinimap::checkobject(lua_State *L, int narg)
-{
- NO_MAP_LOCK_REQUIRED;
-
- luaL_checktype(L, narg, LUA_TUSERDATA);
-
- void *ud = luaL_checkudata(L, narg, className);
- if (!ud)
- luaL_typerror(L, narg, className);
-
- return *(LuaMinimap **)ud; // unbox pointer
-}
-
Minimap* LuaMinimap::getobject(LuaMinimap *ref)
{
return ref->m_minimap;
@@ -192,27 +179,11 @@ int LuaMinimap::gc_object(lua_State *L) {
void LuaMinimap::Register(lua_State *L)
{
- lua_newtable(L);
- int methodtable = lua_gettop(L);
- luaL_newmetatable(L, className);
- int metatable = lua_gettop(L);
-
- lua_pushliteral(L, "__metatable");
- lua_pushvalue(L, methodtable);
- lua_settable(L, metatable); // hide metatable from Lua getmetatable()
-
- lua_pushliteral(L, "__index");
- lua_pushvalue(L, methodtable);
- lua_settable(L, metatable);
-
- lua_pushliteral(L, "__gc");
- lua_pushcfunction(L, gc_object);
- lua_settable(L, metatable);
-
- lua_pop(L, 1); // drop metatable
-
- luaL_register(L, nullptr, methods); // fill methodtable
- lua_pop(L, 1); // drop methodtable
+ static const luaL_Reg metamethods[] = {
+ {"__gc", gc_object},
+ {0, 0}
+ };
+ registerClass(L, className, methods, metamethods);
}
const char LuaMinimap::className[] = "Minimap";