Skip to content

Commit 8a15e80

Browse files
authored
improve: ItemAttribute Safety Improvements (zimbadev#550)
1 parent d187f7f commit 8a15e80

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

src/items/functions/item/attribute.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,9 @@ const int64_t &ItemAttribute::getAttributeValue(ItemAttribute_t type) const {
5353
}
5454

5555
const Attributes* ItemAttribute::getAttribute(ItemAttribute_t type) const {
56-
if (hasAttribute(type)) {
57-
for (const Attributes &attribute : attributeVector) {
58-
if (attribute.getAttributeType() == type) {
59-
return &attribute;
60-
}
56+
for (auto it = attributeVector.cbegin(); it != attributeVector.cend(); ++it) {
57+
if (it->getAttributeType() == type) {
58+
return &*it;
6159
}
6260
}
6361
return nullptr;
@@ -120,10 +118,12 @@ const std::map<std::string, CustomAttribute, std::less<>> &ItemAttribute::getCus
120118
=============================
121119
*/
122120
const CustomAttribute* ItemAttribute::getCustomAttribute(const std::string &attributeName) const {
123-
if (customAttributeMap.contains(asLowerCaseString(attributeName))) {
124-
return &customAttributeMap.at(asLowerCaseString(attributeName));
121+
const auto key = asLowerCaseString(attributeName);
122+
const auto it = customAttributeMap.find(key);
123+
if (it == customAttributeMap.end()) {
124+
return nullptr;
125125
}
126-
return nullptr;
126+
return &it->second;
127127
}
128128

129129
void ItemAttribute::setCustomAttribute(const std::string &key, const int64_t value) {

0 commit comments

Comments
 (0)