Skip to content

Commit 1176f2e

Browse files
committed
fix: add default value process into Entity::hurt #91
1 parent 0114bcd commit 1176f2e

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/legacy/api/EntityAPI.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,20 +1000,20 @@ Local<Value> EntityClass::hurt(const Arguments& args) {
10001000
}
10011001
float damage = args[0].asNumber().toFloat();
10021002
int type = 0;
1003-
if (args.size() == 2) {
1003+
if (args.size() >= 2) {
10041004
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);
1005-
type = args[1].asNumber().toInt32();
1006-
ActorDamageSource damageSource = ActorDamageSource((ActorDamageCause)type);
1007-
return Boolean::newBoolean(entity->hurt(damageSource, damage, true, false));
1005+
type = args[1].asNumber().toInt32();
10081006
}
10091007
if (args.size() == 3) {
1010-
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);
1011-
auto source = EntityClass::extract(args[2]);
1012-
type = args[1].asNumber().toInt32();
1013-
ActorDamageByActorSource damageSource = ActorDamageByActorSource(*source, (ActorDamageCause)type);
1014-
return Boolean::newBoolean(entity->hurt(damageSource, damage, true, false));
1008+
auto source = EntityClass::extract(args[2]);
1009+
if (!source) {
1010+
return Boolean::newBoolean(false);
1011+
}
1012+
ActorDamageByActorSource damageBySource = ActorDamageByActorSource(*source, (ActorDamageCause)type);
1013+
return Boolean::newBoolean(entity->hurt(damageBySource, damage, true, false));
10151014
}
1016-
return Boolean::newBoolean(false);
1015+
ActorDamageSource damageSource = ActorDamageSource((ActorDamageCause)type);
1016+
return Boolean::newBoolean(entity->hurt(damageSource, damage, true, false));
10171017
}
10181018
CATCH("Fail in hurt!");
10191019
}

src/legacy/api/PlayerAPI.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,18 +2461,18 @@ Local<Value> PlayerClass::hurt(const Arguments& args) {
24612461
}
24622462
float damage = args[0].asNumber().toFloat();
24632463
int type = 0;
2464-
if (args.size() == 2) {
2464+
if (args.size() >= 2) {
24652465
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);
24662466
type = args[1].asNumber().toInt32();
2467-
return Boolean::newBoolean(player->hurtByCause(damage, (ActorDamageCause)type));
24682467
}
24692468
if (args.size() == 3) {
2470-
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);
24712469
auto source = EntityClass::extract(args[2]);
2472-
type = args[1].asNumber().toInt32();
2470+
if (!source) {
2471+
return Boolean::newBoolean(false);
2472+
}
24732473
return Boolean::newBoolean(player->hurtByCause(damage, (ActorDamageCause)type, source));
24742474
}
2475-
return Boolean::newBoolean(false);
2475+
return Boolean::newBoolean(player->hurtByCause(damage, (ActorDamageCause)type));
24762476
}
24772477
CATCH("Fail in hurt!");
24782478
}

0 commit comments

Comments
 (0)