Skip to content

Commit 89d8d28

Browse files
committed
fix: implement AttributeInstance::setCurrentValue in EntityAPI.cpp
1 parent 98bdafb commit 89d8d28

File tree

1 file changed

+35
-46
lines changed

1 file changed

+35
-46
lines changed

src/legacy/api/EntityAPI.cpp

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
#include "mc/legacy/ActorRuntimeID.h"
2323
#include "mc/legacy/ActorUniqueID.h"
2424
#include "mc/nbt/CompoundTag.h"
25+
#include "mc/server/commands/CommandUtils.h"
2526
#include "mc/world/SimpleContainer.h"
27+
#include "mc/world/actor/ActorDamageByActorSource.h"
28+
#include "mc/world/actor/ActorDamageSource.h"
2629
#include "mc/world/actor/ActorDefinitionIdentifier.h"
2730
#include "mc/world/actor/ActorType.h"
2831
#include "mc/world/actor/BuiltInActorComponents.h"
@@ -34,6 +37,7 @@
3437
#include "mc/world/actor/provider/SynchedActorDataAccess.h"
3538
#include "mc/world/attribute/AttributeInstance.h"
3639
#include "mc/world/attribute/AttributeModificationContext.h"
40+
#include "mc/world/attribute/BaseAttributeMap.h"
3741
#include "mc/world/attribute/MutableAttributeWithContext.h"
3842
#include "mc/world/attribute/SharedAttributes.h"
3943
#include "mc/world/effect/EffectDuration.h"
@@ -43,6 +47,7 @@
4347
#include "mc/world/level/biome/Biome.h"
4448
#include "mc/world/level/block/Block.h"
4549
#include "mc/world/level/block/VanillaBlockTypeIds.h"
50+
#include "mc/world/level/dimension/Dimension.h"
4651
#include "mc/world/level/material/Material.h"
4752
#include "mc/world/phys/AABB.h"
4853
#include "mc/world/phys/HitResult.h"
@@ -361,8 +366,6 @@ Local<Value> EntityClass::isMoving() {
361366
CATCH("Fail in isMoving!")
362367
}
363368

364-
#include "mc/server/commands/CommandUtils.h"
365-
366369
Local<Value> EntityClass::getName() {
367370
try {
368371
Actor* entity = get();
@@ -947,9 +950,6 @@ Local<Value> EntityClass::refreshItems(const Arguments&) {
947950
CATCH("Fail in refreshItems!");
948951
}
949952

950-
#include "mc/world/level/BlockSource.h"
951-
#include "mc/world/level/dimension/Dimension.h"
952-
953953
Local<Value> EntityClass::hasContainer(const Arguments&) {
954954
try {
955955
Actor* entity = get();
@@ -973,9 +973,6 @@ Local<Value> EntityClass::getContainer(const Arguments&) {
973973
CATCH("Fail in getContainer!");
974974
}
975975

976-
#include "mc/world/actor/ActorDamageByActorSource.h"
977-
#include "mc/world/actor/ActorDamageSource.h"
978-
979976
Local<Value> EntityClass::hurt(const Arguments& args) {
980977
CHECK_ARGS_COUNT(args, 1);
981978
CHECK_ARG_TYPE(args[0], ValueKind::kNumber);
@@ -1019,6 +1016,12 @@ Local<Value> EntityClass::heal(const Arguments& args) {
10191016
CATCH("Fail in heal!");
10201017
}
10211018

1019+
void SetAttributeCurrentValue(MutableAttributeWithContext& attribute, float value) {
1020+
auto& instance = attribute.mInstance;
1021+
instance->mCurrentValue = value;
1022+
attribute.mContext->mAttributeMap->_onAttributeModified(*instance);
1023+
}
1024+
10221025
Local<Value> EntityClass::setHealth(const Arguments& args) {
10231026
CHECK_ARGS_COUNT(args, 1);
10241027
CHECK_ARG_TYPE(args[0], ValueKind::kNumber);
@@ -1027,10 +1030,8 @@ Local<Value> EntityClass::setHealth(const Arguments& args) {
10271030
Actor* entity = get();
10281031
if (!entity) return Local<Value>();
10291032

1030-
MutableAttributeWithContext healthAttribute = entity->getMutableAttribute(SharedAttributes::HEALTH());
1031-
1032-
healthAttribute->setCurrentValue(args[0].asNumber().toFloat());
1033-
1033+
MutableAttributeWithContext attribute = entity->getMutableAttribute(SharedAttributes::HEALTH());
1034+
SetAttributeCurrentValue(attribute, args[0].asNumber().toFloat());
10341035
return Boolean::newBoolean(true);
10351036
}
10361037
CATCH("Fail in setHealth!");
@@ -1044,9 +1045,8 @@ Local<Value> EntityClass::setAbsorption(const Arguments& args) {
10441045
Actor* entity = get();
10451046
if (!entity) return Local<Value>();
10461047

1047-
AttributeInstance* absorptionAttribute = entity->getMutableAttribute(SharedAttributes::ABSORPTION());
1048-
1049-
absorptionAttribute->setCurrentValue(args[0].asNumber().toFloat());
1048+
MutableAttributeWithContext attribute = entity->getMutableAttribute(SharedAttributes::ABSORPTION());
1049+
SetAttributeCurrentValue(attribute, args[0].asNumber().toFloat());
10501050

10511051
return Boolean::newBoolean(true);
10521052
}
@@ -1061,9 +1061,8 @@ Local<Value> EntityClass::setAttackDamage(const Arguments& args) {
10611061
Actor* entity = get();
10621062
if (!entity) return Local<Value>();
10631063

1064-
AttributeInstance* attactDamageAttribute = entity->getMutableAttribute(SharedAttributes::ATTACK_DAMAGE());
1065-
1066-
attactDamageAttribute->setCurrentValue(args[0].asNumber().toFloat());
1064+
MutableAttributeWithContext attribute = entity->getMutableAttribute(SharedAttributes::ATTACK_DAMAGE());
1065+
SetAttributeCurrentValue(attribute, args[0].asNumber().toFloat());
10671066

10681067
return Boolean::newBoolean(true);
10691068
}
@@ -1078,9 +1077,8 @@ Local<Value> EntityClass::setMaxAttackDamage(const Arguments& args) {
10781077
Actor* entity = get();
10791078
if (!entity) return Local<Value>();
10801079

1081-
AttributeInstance* attactDamageAttribute = entity->getMutableAttribute(SharedAttributes::ATTACK_DAMAGE());
1082-
1083-
attactDamageAttribute->setMaxValue(args[0].asNumber().toFloat());
1080+
MutableAttributeWithContext attribute = entity->getMutableAttribute(SharedAttributes::ATTACK_DAMAGE());
1081+
SetAttributeCurrentValue(attribute, args[0].asNumber().toFloat());
10841082

10851083
return Boolean::newBoolean(true);
10861084
}
@@ -1095,9 +1093,8 @@ Local<Value> EntityClass::setFollowRange(const Arguments& args) {
10951093
Actor* entity = get();
10961094
if (!entity) return Local<Value>();
10971095

1098-
AttributeInstance* followRangeAttribute = entity->getMutableAttribute(SharedAttributes::FOLLOW_RANGE());
1099-
1100-
followRangeAttribute->setCurrentValue(args[0].asNumber().toFloat());
1096+
MutableAttributeWithContext attribute = entity->getMutableAttribute(SharedAttributes::FOLLOW_RANGE());
1097+
SetAttributeCurrentValue(attribute, args[0].asNumber().toFloat());
11011098

11021099
return Boolean::newBoolean(true);
11031100
}
@@ -1112,10 +1109,8 @@ Local<Value> EntityClass::setKnockbackResistance(const Arguments& args) {
11121109
Actor* entity = get();
11131110
if (!entity) return Local<Value>();
11141111

1115-
AttributeInstance* knockbackResistanceAttribute =
1116-
entity->getMutableAttribute(SharedAttributes::KNOCKBACK_RESISTANCE());
1117-
1118-
knockbackResistanceAttribute->setCurrentValue(args[0].asNumber().toFloat());
1112+
MutableAttributeWithContext attribute = entity->getMutableAttribute(SharedAttributes::KNOCKBACK_RESISTANCE());
1113+
SetAttributeCurrentValue(attribute, args[0].asNumber().toFloat());
11191114

11201115
return Boolean::newBoolean(true);
11211116
}
@@ -1130,9 +1125,8 @@ Local<Value> EntityClass::setLuck(const Arguments& args) {
11301125
Actor* entity = get();
11311126
if (!entity) return Local<Value>();
11321127

1133-
AttributeInstance* luckAttribute = entity->getMutableAttribute(SharedAttributes::LUCK());
1134-
1135-
luckAttribute->setCurrentValue(args[0].asNumber().toFloat());
1128+
MutableAttributeWithContext attribute = entity->getMutableAttribute(SharedAttributes::LUCK());
1129+
SetAttributeCurrentValue(attribute, args[0].asNumber().toFloat());
11361130

11371131
return Boolean::newBoolean(true);
11381132
}
@@ -1147,11 +1141,8 @@ Local<Value> EntityClass::setMovementSpeed(const Arguments& args) {
11471141
Actor* entity = get();
11481142
if (!entity) return Local<Value>();
11491143

1150-
AttributeInstance* movementSpeedAttribute = entity->getMutableAttribute(SharedAttributes::MOVEMENT_SPEED());
1151-
if (movementSpeedAttribute) {
1152-
movementSpeedAttribute->setCurrentValue(args[0].asNumber().toFloat());
1153-
return Boolean::newBoolean(true);
1154-
}
1144+
MutableAttributeWithContext attribute = entity->getMutableAttribute(SharedAttributes::MOVEMENT_SPEED());
1145+
SetAttributeCurrentValue(attribute, args[0].asNumber().toFloat());
11551146

11561147
return Boolean::newBoolean(false);
11571148
}
@@ -1166,10 +1157,11 @@ Local<Value> EntityClass::setUnderwaterMovementSpeed(const Arguments& args) {
11661157
Actor* entity = get();
11671158
if (!entity) return Local<Value>();
11681159

1169-
AttributeInstance* underwaterMovementSpeedAttribute =
1160+
MutableAttributeWithContext attribute =
11701161
entity->getMutableAttribute(SharedAttributes::UNDERWATER_MOVEMENT_SPEED());
1171-
1172-
underwaterMovementSpeedAttribute->setCurrentValue(args[0].asNumber().toFloat());
1162+
auto& instance = attribute.mInstance;
1163+
instance->mCurrentValue = args[0].asNumber().toFloat();
1164+
attribute.mContext->mAttributeMap->_onAttributeModified(*instance);
11731165

11741166
return Boolean::newBoolean(true);
11751167
}
@@ -1184,10 +1176,8 @@ Local<Value> EntityClass::setLavaMovementSpeed(const Arguments& args) {
11841176
Actor* entity = get();
11851177
if (!entity) return Local<Value>();
11861178

1187-
AttributeInstance* lavaMovementSpeedAttribute =
1188-
entity->getMutableAttribute(SharedAttributes::LAVA_MOVEMENT_SPEED());
1189-
1190-
lavaMovementSpeedAttribute->setCurrentValue(args[0].asNumber().toFloat());
1179+
MutableAttributeWithContext attribute = entity->getMutableAttribute(SharedAttributes::LAVA_MOVEMENT_SPEED());
1180+
SetAttributeCurrentValue(attribute, args[0].asNumber().toFloat());
11911181

11921182
return Boolean::newBoolean(true);
11931183
}
@@ -1202,9 +1192,8 @@ Local<Value> EntityClass::setMaxHealth(const Arguments& args) {
12021192
Actor* entity = get();
12031193
if (!entity) return Local<Value>();
12041194

1205-
AttributeInstance* healthAttribute = entity->getMutableAttribute(SharedAttributes::HEALTH());
1206-
1207-
healthAttribute->setMaxValue(args[0].asNumber().toFloat());
1195+
MutableAttributeWithContext attribute = entity->getMutableAttribute(SharedAttributes::HEALTH());
1196+
SetAttributeCurrentValue(attribute, args[0].asNumber().toFloat());
12081197

12091198
return Boolean::newBoolean(true);
12101199
}

0 commit comments

Comments
 (0)