MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / checkInventoryFilter

Method checkInventoryFilter

source/game/StarPlayerInventory.cpp:969–1028  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

967}
968
969bool PlayerInventory::checkInventoryFilter(ItemPtr const& items, String const& filterName) {
970 Json filterConfig;
971
972 auto itemFilters = items->instanceValue("inventoryFilters");
973 if (itemFilters.isType(Json::Type::Object)) {
974 filterConfig = itemFilters.opt(filterName).value();
975 if (!filterConfig.isType(Json::Type::Object))
976 filterConfig = itemFilters.opt("default").value();
977 }
978
979 if (!filterConfig.isType(Json::Type::Object)) {
980 auto config = Root::singleton().assets()->json("/player.config:inventoryFilters");
981 filterConfig = config.opt(filterName).value();
982 if (!filterConfig.isType(Json::Type::Object))
983 filterConfig = config.get("default");
984 }
985
986 // filter by item type if an itemTypes filter is set
987 auto itemDatabase = Root::singleton().itemDatabase();
988 auto itemTypeName = ItemTypeNames.getRight(itemDatabase->itemType(items->name()));
989 if (filterConfig.contains("typeWhitelist") && !filterConfig.getArray("typeWhitelist").contains(itemTypeName))
990 return false;
991
992 if (filterConfig.contains("typeBlacklist") && filterConfig.getArray("typeBlacklist").contains(itemTypeName))
993 return false;
994
995 // filter by item tags if an itemTags filter is set
996 // this is an inclusive filter
997 auto itemTags = itemDatabase->itemTags(items->name());
998 if (filterConfig.contains("tagWhitelist")) {
999 auto whitelistedTags = filterConfig.getArray("tagWhitelist").filtered([itemTags](Json const& tag) {
1000 return itemTags.contains(tag.toString());
1001 });
1002 if (whitelistedTags.size() == 0)
1003 return false;
1004 }
1005
1006 if (filterConfig.contains("tagBlacklist")) {
1007 auto blacklistedTags = filterConfig.getArray("tagBlacklist").filtered([itemTags](Json const& tag) {
1008 return itemTags.contains(tag.toString());
1009 });
1010 if (blacklistedTags.size() > 0)
1011 return false;
1012 }
1013
1014 auto itemCategory = items->category();
1015 if (auto categoryWhitelist = filterConfig.optArray("categoryWhitelist")) {
1016 auto categoryWhiteset = jsonToStringSet(*categoryWhitelist);
1017 if (!categoryWhiteset.contains(itemCategory))
1018 return false;
1019 }
1020
1021 if (auto categoryBlacklist = filterConfig.optArray("categoryBlacklist")) {
1022 auto categoryBlackset = jsonToStringSet(*categoryBlacklist);
1023 if (categoryBlackset.contains(itemCategory))
1024 return false;
1025 }
1026

Callers

nothing calls this directly

Calls 15

singletonClass · 0.85
jsonToStringSetFunction · 0.85
instanceValueMethod · 0.80
jsonMethod · 0.80
assetsMethod · 0.80
itemDatabaseMethod · 0.80
getArrayMethod · 0.80
optArrayMethod · 0.80
isTypeMethod · 0.45
valueMethod · 0.45
optMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected