Skip to content

Commit 0b5b32b

Browse files
rubenwardySmallJoker
authored andcommittedApr 30, 2018
MetaDataRef: Add contains() and get() (#7214)
1 parent 54606e1 commit 0b5b32b

File tree

8 files changed

+72
-6
lines changed

8 files changed

+72
-6
lines changed
 

‎doc/lua_api.txt

+9-6
Original file line numberDiff line numberDiff line change
@@ -3909,12 +3909,15 @@ An interface to use mod channels on client and server
39093909
See `StorageRef`, `NodeMetaRef`, `ItemStackMetaRef`, and `PlayerMetaRef`.
39103910

39113911
#### Methods
3912-
* `set_string(name, value)`
3913-
* `get_string(name)`
3914-
* `set_int(name, value)`
3915-
* `get_int(name)`
3916-
* `set_float(name, value)`
3917-
* `get_float(name)`
3912+
* `contains(key)`: Returns true if key present, otherwise false.
3913+
* Returns `nil` when the MetaData is inexistent.
3914+
* `get(key)`: Returns `nil` if key not present, else the stored string.
3915+
* `set_string(key, value)`: Value of `""` will delete the key.
3916+
* `get_string(key)`: Returns `""` if key not present.
3917+
* `set_int(key, value)`
3918+
* `get_int(key)`: Returns `0` if key not present.
3919+
* `set_float(key, value)`
3920+
* `get_float(key)`: Returns `0` if key not present.
39183921
* `to_table()`: returns `nil` or a table with keys:
39193922
* `fields`: key-value storage
39203923
* `inventory`: `{list1 = {}, ...}}` (NodeMetaRef only)

‎games/minimal/mods/test/init.lua

+11
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,32 @@ end)
4343
local function run_player_meta_tests(player)
4444
local meta = player:get_meta()
4545
meta:set_string("foo", "bar")
46+
assert(meta:contains("foo"))
4647
assert(meta:get_string("foo") == "bar")
48+
assert(meta:get("foo") == "bar")
4749

4850
local meta2 = player:get_meta()
4951
assert(meta2:get_string("foo") == "bar")
52+
assert(meta2:get("foo") == "bar")
5053
assert(meta:equals(meta2))
5154
assert(player:get_attribute("foo") == "bar")
5255

5356
meta:set_string("bob", "dillan")
5457
assert(meta:get_string("foo") == "bar")
5558
assert(meta:get_string("bob") == "dillan")
59+
assert(meta:get("bob") == "dillan")
5660
assert(meta2:get_string("foo") == "bar")
5761
assert(meta2:get_string("bob") == "dillan")
62+
assert(meta2:get("bob") == "dillan")
5863
assert(meta:equals(meta2))
5964
assert(player:get_attribute("foo") == "bar")
6065
assert(player:get_attribute("bob") == "dillan")
66+
67+
meta:set_string("foo", "")
68+
assert(not meta:contains("foo"))
69+
assert(meta:get("foo") == nil)
70+
assert(meta:get_string("foo") == "")
71+
assert(meta:equals(meta2))
6172
end
6273

6374
local function run_player_tests(player)

‎src/script/lua_api/l_itemstackmeta.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ void ItemStackMetaRef::Register(lua_State *L)
123123

124124
const char ItemStackMetaRef::className[] = "ItemStackMetaRef";
125125
const luaL_Reg ItemStackMetaRef::methods[] = {
126+
luamethod(MetaDataRef, contains),
127+
luamethod(MetaDataRef, get),
126128
luamethod(MetaDataRef, get_string),
127129
luamethod(MetaDataRef, set_string),
128130
luamethod(MetaDataRef, get_int),

‎src/script/lua_api/l_metadata.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,42 @@ MetaDataRef* MetaDataRef::checkobject(lua_State *L, int narg)
5151

5252
// Exported functions
5353

54+
// contains(self, name)
55+
int MetaDataRef::l_contains(lua_State *L)
56+
{
57+
MAP_LOCK_REQUIRED;
58+
59+
MetaDataRef *ref = checkobject(L, 1);
60+
std::string name = luaL_checkstring(L, 2);
61+
62+
Metadata *meta = ref->getmeta(false);
63+
if (meta == NULL)
64+
return 0;
65+
66+
lua_pushboolean(L, meta->contains(name));
67+
return 1;
68+
}
69+
70+
// get(self, name)
71+
int MetaDataRef::l_get(lua_State *L)
72+
{
73+
MAP_LOCK_REQUIRED;
74+
75+
MetaDataRef *ref = checkobject(L, 1);
76+
std::string name = luaL_checkstring(L, 2);
77+
78+
Metadata *meta = ref->getmeta(false);
79+
if (meta == NULL)
80+
return 0;
81+
82+
std::string str;
83+
if (meta->getStringToRef(name, str)) {
84+
lua_pushlstring(L, str.c_str(), str.size());
85+
return 1;
86+
}
87+
return 0;
88+
}
89+
5490
// get_string(self, name)
5591
int MetaDataRef::l_get_string(lua_State *L)
5692
{

‎src/script/lua_api/l_metadata.h

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ class MetaDataRef : public ModApiBase
4646

4747
// Exported functions
4848

49+
// contains(self, name)
50+
static int l_contains(lua_State *L);
51+
52+
// get(self, name)
53+
static int l_get(lua_State *L);
54+
4955
// get_string(self, name)
5056
static int l_get_string(lua_State *L);
5157

‎src/script/lua_api/l_nodemeta.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ void NodeMetaRef::Register(lua_State *L)
242242

243243

244244
const luaL_Reg NodeMetaRef::methodsServer[] = {
245+
luamethod(MetaDataRef, contains),
246+
luamethod(MetaDataRef, get),
245247
luamethod(MetaDataRef, get_string),
246248
luamethod(MetaDataRef, set_string),
247249
luamethod(MetaDataRef, get_int),
@@ -266,6 +268,8 @@ void NodeMetaRef::RegisterClient(lua_State *L)
266268

267269

268270
const luaL_Reg NodeMetaRef::methodsClient[] = {
271+
luamethod(MetaDataRef, contains),
272+
luamethod(MetaDataRef, get),
269273
luamethod(MetaDataRef, get_string),
270274
luamethod(MetaDataRef, get_int),
271275
luamethod(MetaDataRef, get_float),

‎src/script/lua_api/l_playermeta.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ void PlayerMetaRef::Register(lua_State *L)
107107
// clang-format off
108108
const char PlayerMetaRef::className[] = "PlayerMetaRef";
109109
const luaL_Reg PlayerMetaRef::methods[] = {
110+
luamethod(MetaDataRef, contains),
111+
luamethod(MetaDataRef, get),
110112
luamethod(MetaDataRef, get_string),
111113
luamethod(MetaDataRef, set_string),
112114
luamethod(MetaDataRef, get_int),

‎src/script/lua_api/l_storage.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ void StorageRef::clearMeta()
134134

135135
const char StorageRef::className[] = "StorageRef";
136136
const luaL_Reg StorageRef::methods[] = {
137+
luamethod(MetaDataRef, contains),
138+
luamethod(MetaDataRef, get),
137139
luamethod(MetaDataRef, get_string),
138140
luamethod(MetaDataRef, set_string),
139141
luamethod(MetaDataRef, get_int),

0 commit comments

Comments
 (0)
Please sign in to comment.