@@ -1202,6 +1202,26 @@ inline void NodeDefManager::fixSelectionBoxIntUnion()
1202
1202
}
1203
1203
1204
1204
1205
+ void NodeDefManager::eraseIdFromGroups (content_t id)
1206
+ {
1207
+ // For all groups in m_group_to_items...
1208
+ for (auto iter_groups = m_group_to_items.begin ();
1209
+ iter_groups != m_group_to_items.end ();) {
1210
+ // Get the group items vector.
1211
+ std::vector<content_t > &items = iter_groups->second ;
1212
+
1213
+ // Remove any occurence of the id in the group items vector.
1214
+ items.erase (std::remove (items.begin (), items.end (), id), items.end ());
1215
+
1216
+ // If group is empty, erase its vector from the map.
1217
+ if (items.empty ())
1218
+ iter_groups = m_group_to_items.erase (iter_groups);
1219
+ else
1220
+ ++iter_groups;
1221
+ }
1222
+ }
1223
+
1224
+
1205
1225
// IWritableNodeDefManager
1206
1226
content_t NodeDefManager::set (const std::string &name, const ContentFeatures &def)
1207
1227
{
@@ -1222,19 +1242,24 @@ content_t NodeDefManager::set(const std::string &name, const ContentFeatures &de
1222
1242
assert (id != CONTENT_IGNORE);
1223
1243
addNameIdMapping (id, name);
1224
1244
}
1245
+
1246
+ // If there is already ContentFeatures registered for this id, clear old groups
1247
+ if (id < m_content_features.size ())
1248
+ eraseIdFromGroups (id);
1249
+
1225
1250
m_content_features[id] = def;
1226
1251
verbosestream << " NodeDefManager: registering content id \" " << id
1227
1252
<< " \" : name=\" " << def.name << " \" " <<std::endl;
1228
1253
1229
1254
getNodeBoxUnion (def.selection_box , def, &m_selection_box_union);
1230
1255
fixSelectionBoxIntUnion ();
1256
+
1231
1257
// Add this content to the list of all groups it belongs to
1232
- // FIXME: This should remove a node from groups it no longer
1233
- // belongs to when a node is re-registered
1234
1258
for (const auto &group : def.groups ) {
1235
1259
const std::string &group_name = group.first ;
1236
1260
m_group_to_items[group_name].push_back (id);
1237
1261
}
1262
+
1238
1263
return id;
1239
1264
}
1240
1265
@@ -1260,18 +1285,7 @@ void NodeDefManager::removeNode(const std::string &name)
1260
1285
m_name_id_mapping_with_aliases.erase (name);
1261
1286
}
1262
1287
1263
- // Erase node content from all groups it belongs to
1264
- for (std::unordered_map<std::string, std::vector<content_t >>::iterator iter_groups =
1265
- m_group_to_items.begin (); iter_groups != m_group_to_items.end ();) {
1266
- std::vector<content_t > &items = iter_groups->second ;
1267
- items.erase (std::remove (items.begin (), items.end (), id), items.end ());
1268
-
1269
- // Check if group is empty
1270
- if (items.empty ())
1271
- m_group_to_items.erase (iter_groups++);
1272
- else
1273
- ++iter_groups;
1274
- }
1288
+ eraseIdFromGroups (id);
1275
1289
}
1276
1290
1277
1291
0 commit comments