Ejemplo n.º 1
0
// from_table(self, table)
bool NodeMetaRef::handleFromTable(lua_State *L, int table, Metadata *_meta)
{
	// fields
	if (!MetaDataRef::handleFromTable(L, table, _meta))
		return false;

	NodeMetadata *meta = (NodeMetadata*) _meta;

	// inventory
	Inventory *inv = meta->getInventory();
	lua_getfield(L, table, "inventory");
	if (lua_istable(L, -1)) {
		int inventorytable = lua_gettop(L);
		lua_pushnil(L);
		while (lua_next(L, inventorytable) != 0) {
			// key at index -2 and value at index -1
			std::string name = lua_tostring(L, -2);
			read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
			lua_pop(L, 1); // Remove value, keep key for next iteration
		}
		lua_pop(L, 1);
	}

	return true;
}
Ejemplo n.º 2
0
// set_list(self, listname, list)
int InvRef::l_set_list(lua_State *L)
{
	NO_MAP_LOCK_REQUIRED;
	InvRef *ref = checkobject(L, 1);
	const char *listname = luaL_checkstring(L, 2);
	Inventory *inv = getinv(L, ref);
	if(inv == NULL){
		return 0;
	}
	InventoryList *list = inv->getList(listname);
	if(list)
		read_inventory_list(L, 3, inv, listname,
				getServer(L), list->getSize());
	else
		read_inventory_list(L, 3, inv, listname, getServer(L));
	reportInventoryChange(L, ref);
	return 0;
}
Ejemplo n.º 3
0
// from_table(self, table)
int NodeMetaRef::l_from_table(lua_State *L)
{
	MAP_LOCK_REQUIRED;

	NodeMetaRef *ref = checkobject(L, 1);
	int base = 2;

	// clear old metadata first
	ref->m_env->getMap().removeNodeMetadata(ref->m_p);

	if(lua_isnil(L, base)){
		// No metadata
		lua_pushboolean(L, true);
		return 1;
	}

	// Create new metadata
	NodeMetadata *meta = getmeta(ref, true);
	if(meta == NULL){
		lua_pushboolean(L, false);
		return 1;
	}
	// Set fields
	lua_getfield(L, base, "fields");
	int fieldstable = lua_gettop(L);
	lua_pushnil(L);
	while(lua_next(L, fieldstable) != 0){
		// key at index -2 and value at index -1
		std::string name = lua_tostring(L, -2);
		size_t cl;
		const char *cs = lua_tolstring(L, -1, &cl);
		std::string value(cs, cl);
		meta->setString(name, value);
		lua_pop(L, 1); // removes value, keeps key for next iteration
	}
	// Set inventory
	Inventory *inv = meta->getInventory();
	lua_getfield(L, base, "inventory");
	int inventorytable = lua_gettop(L);
	lua_pushnil(L);
	while(lua_next(L, inventorytable) != 0){
		// key at index -2 and value at index -1
		std::string name = lua_tostring(L, -2);
		read_inventory_list(L, -1, inv, name.c_str(), getServer(L));
		lua_pop(L, 1); // removes value, keeps key for next iteration
	}
	reportMetadataChange(ref);
	lua_pushboolean(L, true);
	return 1;
}
Ejemplo n.º 4
0
// set_lists(self, lists)
int InvRef::l_set_lists(lua_State *L)
{
    NO_MAP_LOCK_REQUIRED;
    InvRef *ref = checkobject(L, 1);
    Inventory *inv = getinv(L, ref);
    if (!inv) {
        return 0;
    }
    lua_pushnil(L);
    while (lua_next(L, 2)) {
        const char* listname = lua_tostring(L, -2);
        InventoryList *list = inv->getList(listname);
        if (list) {
            read_inventory_list(L, -1, inv, listname,
                                getServer(L), list->getSize());
        } else {
            read_inventory_list(L, -1, inv, listname,
                                getServer(L));
        }
        lua_pop(L, 1);
    }
    return 0;
}
Ejemplo n.º 5
0
// set_lists(self, lists)
int InvRef::l_set_lists(lua_State *L)
{
	NO_MAP_LOCK_REQUIRED;
	InvRef *ref = checkobject(L, 1);
	Inventory *inv = getinv(L, ref);
	if (!inv) {
		return 0;
	}

	// Make a temporary inventory in case reading fails
	Inventory *tempInv(inv);
	tempInv->clear();

	Server *server = getServer(L);

	lua_pushnil(L);
	while (lua_next(L, 2)) {
		const char *listname = lua_tostring(L, -2);
		read_inventory_list(L, -1, tempInv, listname, server);
		lua_pop(L, 1);
	}
	inv = tempInv;
	return 0;
}