Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(llse): effect method of Entity(#1225) #1236

Merged
merged 1 commit into from
Apr 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;