Skip to content

Commit

Permalink
feat(llse): effect method of Entity(#1225) (#1236)
Browse files Browse the repository at this point in the history
add getAllEffects() addEffect() removeEffect() to llse Entity class
  • Loading branch information
harry-xi authored Apr 30, 2023
1 parent 0a6aa7d commit e9f70df
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
59 changes: 59 additions & 0 deletions ScriptEngine/src/api/EntityAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ ClassDefine<EntityClass> EntityClassBuilder =
.instanceFunction("getBiomeId", &EntityClass::getBiomeId)
.instanceFunction("quickEvalMolangScript", &EntityClass::quickEvalMolangScript)

.instanceFunction("getAllEffects", &EntityClass::getAllEffects)
.instanceFunction("addEffect", &EntityClass::addEffect)
.instanceFunction("removeEffect", &EntityClass::removeEffect)

// For Compatibility
.instanceFunction("setTag", &EntityClass::setNbt)
.instanceFunction("setOnFire", &EntityClass::setOnFire)
Expand Down Expand Up @@ -1503,6 +1507,61 @@ Local<Value> EntityClass::getBiomeName() {
CATCH("Fail in getBiomeName!");
}

Local<Value> EntityClass::getAllEffects() {
try {
Actor* actor = get();
if (!actor) {
return Local<Value>();
}
auto effects = actor->getAllEffects();
if (effects.size() == 0) {
return Local<Value>();
}
Local<Array> effectList = Array::newArray();
for (auto effect : effects)
effectList.add(Number::newNumber((int)effect.getId()));
return effectList;
}
CATCH("Fail in getAllEffects!")
}

Local<Value> EntityClass::addEffect(const Arguments& args) {
CHECK_ARGS_COUNT(args, 4);
CHECK_ARG_TYPE(args[0], ValueKind::kNumber);
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);
CHECK_ARG_TYPE(args[2], ValueKind::kNumber);
CHECK_ARG_TYPE(args[3], ValueKind::kBoolean);
try {
Actor* actor = get();
if (!actor) {
return Boolean::newBoolean(false);
}
unsigned int id = args[0].asNumber().toInt32();
int tick = args[1].asNumber().toInt32();
int level = args[2].asNumber().toInt32();
bool showParticles = args[3].asBoolean().value();
MobEffectInstance effect = MobEffectInstance(id, tick, level, false, showParticles, false);
actor->addEffect(effect);
return Boolean::newBoolean(true);
}
CATCH("Fail in addEffect!");
}

Local<Value> EntityClass::removeEffect(const Arguments& args) {
CHECK_ARGS_COUNT(args, 1);
CHECK_ARG_TYPE(args[0], ValueKind::kNumber);
try {
Actor* actor = get();
if (!actor) {
return Boolean::newBoolean(false);
}
int id = args[0].asNumber().toInt32();
actor->removeEffect(id);
return Boolean::newBoolean(true);
}
CATCH("Fail in removeEffect!");
}

Local<Value> McClass::getAllEntities(const Arguments& args) {
try {
auto entityList = Level::getAllEntities();
Expand Down
5 changes: 5 additions & 0 deletions ScriptEngine/src/api/EntityAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ class EntityClass : public ScriptClass {
Local<Value> getBlockFromViewVector(const Arguments& args);

Local<Value> quickEvalMolangScript(const Arguments& args);

Local<Value> getAllEffects();
Local<Value> addEffect(const Arguments& args);
Local<Value> removeEffect(const Arguments& args);

};
extern ClassDefine<EntityClass> EntityClassBuilder;
extern ClassDefine<void> ActorDamageCauseBuilder;

0 comments on commit e9f70df

Please sign in to comment.