Skip to content

Commit

Permalink
Add rotation para to player.teleport. (LiteLDev#982)
Browse files Browse the repository at this point in the history
  • Loading branch information
Redbeanw44602 committed Jan 14, 2023
1 parent 7f48435 commit 8440580
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
3 changes: 1 addition & 2 deletions LiteLoader/include/llapi/mc/RotationCommandUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class RotationData {

};

;
enum class FacingResult;
enum class FacingResult;

#undef AFTER_EXTRA
/**
Expand Down
7 changes: 7 additions & 0 deletions ScriptEngine/src/api/BaseAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ DirectionAngle* DirectionAngle::create(const Arguments& args) {
}
}

DirectionAngle* DirectionAngle::extract(Local<Value> v) {
if (EngineScope::currentEngine()->isInstanceOf<DirectionAngle>(v))
return EngineScope::currentEngine()->getNativeInstance<DirectionAngle>(v);
else
return nullptr;
}

Local<Value> DirectionAngle::toString() {
try {
return String::newString(fmt::format("({}, {})", pitch, yaw));
Expand Down
1 change: 1 addition & 0 deletions ScriptEngine/src/api/BaseAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class DirectionAngle : public ScriptClass {
: ScriptClass(scriptObj) {
}
static DirectionAngle* create(const Arguments& args);
static DirectionAngle* extract(Local<Value> value);

static Local<Object> newAngle(float pitch, float yaw);
Local<Value> getPitch() {
Expand Down
38 changes: 28 additions & 10 deletions ScriptEngine/src/api/PlayerAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1129,9 +1129,15 @@ Local<Value> PlayerClass::teleport(const Arguments& args) {
CHECK_ARGS_COUNT(args, 1)

try {
Player* player = get();
if (!player)
return Boolean::newBoolean(false);
float pitch;
float yaw;
FloatVec4 pos;
bool rotationIsValid = false;

if (args.size() == 1) {
if (args.size() <= 2) {
if (IsInstanceOf<IntPos>(args[0])) {
// IntPos
IntPos* posObj = IntPos::extractPos(args[0]);
Expand All @@ -1153,9 +1159,15 @@ Local<Value> PlayerClass::teleport(const Arguments& args) {
}
} else {
LOG_WRONG_ARG_TYPE();
return Local<Value>();
return Boolean::newBoolean(false);
}
} else if (args.size() == 4) {
if (args.size() == 2 && IsInstanceOf<DirectionAngle>(args[1])) {
auto angle = DirectionAngle::extract(args[1]);
pitch = angle->pitch;
yaw = angle->yaw;
rotationIsValid = true;
}
} else if (args.size() <= 5) { // teleport(x,y,z,dimid[,rot])
// number pos
CHECK_ARG_TYPE(args[0], ValueKind::kNumber);
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);
Expand All @@ -1166,16 +1178,22 @@ Local<Value> PlayerClass::teleport(const Arguments& args) {
pos.y = args[1].asNumber().toFloat();
pos.z = args[2].asNumber().toFloat();
pos.dim = args[3].toInt();
if (args.size() == 5 && IsInstanceOf<DirectionAngle>(args[4])) {
auto angle = DirectionAngle::extract(args[4]);
pitch = angle->pitch;
yaw = angle->yaw;
rotationIsValid = true;
}
} else {
LOG_WRONG_ARG_TYPE();
return Local<Value>();
return Boolean::newBoolean(false);
}

Player* player = get();
if (!player)
return Local<Value>();
player->teleport(pos.getVec3(), pos.dim);
return Boolean::newBoolean(true); //=========???
if (!rotationIsValid) {
auto ang = player->getRotation();
pitch = ang.x;
yaw = ang.y;
}
return Boolean::newBoolean(player->teleport(pos.getVec3(), pos.dim, pitch, yaw));
}
CATCH("Fail in TeleportPlayer!")
}
Expand Down

0 comments on commit 8440580

Please sign in to comment.