Skip to content

Commit 9599b1c

Browse files
committed
Reduce Optional allocs with attribute methods
1 parent 85b42fa commit 9599b1c

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/main/java/ac/grim/grimac/utils/data/packetentity/PacketEntity.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,19 @@ public Optional<ValuedAttribute> getAttribute(Attribute attribute) {
9999
}
100100

101101
public void setAttribute(Attribute attribute, double value) {
102-
ValuedAttribute property = getAttribute(attribute).orElse(null);
103-
if (property == null) throw new IllegalArgumentException("Cannot set attribute " + attribute.getName() + " for entity " + getType().getName().toString() + "!");
102+
ValuedAttribute property = attributeMap.get(attribute);
103+
if (property == null) {
104+
throw new IllegalArgumentException("Cannot set attribute " + attribute.getName() + " for entity " + getType().getName().toString() + "!");
105+
}
104106
property.override(value);
105107
}
106108

107109
public double getAttributeValue(Attribute attribute) {
108-
return getAttribute(attribute).map(ValuedAttribute::get)
109-
.orElseThrow(() -> new IllegalArgumentException("Cannot get attribute " + attribute.getName() + " for entity " + getType().getName().toString() + "!"));
110+
final ValuedAttribute property = attributeMap.get(attribute);
111+
if (property == null) {
112+
throw new IllegalArgumentException("Cannot get attribute " + attribute.getName() + " for entity " + getType().getName().toString() + "!");
113+
}
114+
return property.get();
110115
}
111116

112117
public void resetAttributes() {

0 commit comments

Comments
 (0)