Skip to content

Commit cf5bb5e

Browse files
committed
fix: fix SimulatedPlayerAPI
1 parent 4517181 commit cf5bb5e

File tree

3 files changed

+149
-57
lines changed

3 files changed

+149
-57
lines changed

src/legacy/api/SimulatedPlayerAPI.cpp

Lines changed: 38 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,64 @@
11
#include "api/APIHelp.h"
22
#include "api/BaseAPI.h"
33
#include "api/BlockAPI.h"
4-
#include "api/DeviceAPI.h"
54
#include "api/EntityAPI.h"
65
#include "api/ItemAPI.h"
76
#include "api/McAPI.h"
87
#include "api/PlayerAPI.h"
98
#include "engine/EngineOwnData.h"
109
#include "engine/GlobalShareData.h"
1110
#include "ll/api/service/Bedrock.h"
12-
#include "ll/api/utils/RandomUtils.h"
11+
#include "lse/api/SimulatedPlayerHelper.h"
1312
#include "mc/nbt/CompoundTag.h"
1413
#include "mc/network/ServerNetworkHandler.h"
1514
#include "mc/scripting/modules/gametest/ScriptNavigationResult.h"
1615
#include "mc/server/SimulatedPlayer.h"
1716
#include "mc/server/sim/LookDuration.h"
1817
#include "mc/world/Container.h"
1918
#include "mc/world/Minecraft.h"
20-
#include "mc/world/SimpleContainer.h"
2119
#include "mc/world/actor/Actor.h"
22-
#include "mc/world/actor/player/Player.h"
2320
#include "mc/world/level/BlockSource.h"
24-
#include "mc/world/level/block/Block.h"
25-
#include "mc/world/scores/Objective.h"
2621

2722
#include <string>
2823
#include <vector>
2924

25+
using lse::api::SimulatedPlayerHelper;
26+
3027
Local<Value> McClass::spawnSimulatedPlayer(const Arguments& args) {
3128
CHECK_ARGS_COUNT(args, 1);
3229
CHECK_ARG_TYPE(args[0], ValueKind::kString);
3330

3431
try {
3532
std::string name = args[0].asString().toString();
3633
if (args.size() == 1) {
37-
if (auto sp = SimulatedPlayer::create(name)) return PlayerClass::newPlayer(sp);
34+
if (auto sp = SimulatedPlayer::create(name, ll::service::getLevel()->getSharedSpawnPos()))
35+
return PlayerClass::newPlayer(sp);
3836
else return Local<Value>();
3937
}
40-
auto dimid = 0;
41-
Vec3 bpos;
38+
auto dimId = 0;
39+
Vec3 spawnPos;
4240
if (IsInstanceOf<IntPos>(args[1])) {
4341
auto pos = IntPos::extractPos(args[1]);
44-
bpos = pos->getBlockPos().bottomCenter();
45-
dimid = pos->getDimensionId();
42+
spawnPos = pos->getBlockPos().bottomCenter();
43+
dimId = pos->getDimensionId();
4644
} else if (IsInstanceOf<FloatPos>(args[1])) {
4745
auto pos = FloatPos::extractPos(args[1]);
48-
bpos = pos->getVec3();
49-
dimid = pos->getDimensionId();
46+
spawnPos = pos->getVec3();
47+
dimId = pos->getDimensionId();
5048
} else {
5149
CHECK_ARGS_COUNT(args, 4);
5250
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);
5351
CHECK_ARG_TYPE(args[2], ValueKind::kNumber);
5452
CHECK_ARG_TYPE(args[3], ValueKind::kNumber);
5553
if (args.size() > 4) {
5654
CHECK_ARG_TYPE(args[4], ValueKind::kNumber);
57-
dimid = args[4].asNumber().toInt32();
55+
dimId = args[4].asNumber().toInt32();
5856
}
59-
bpos = BlockPos(args[1].asNumber().toInt32(), args[2].asNumber().toInt32(), args[3].asNumber().toInt32())
60-
.bottomCenter();
57+
spawnPos =
58+
BlockPos(args[1].asNumber().toInt32(), args[2].asNumber().toInt32(), args[3].asNumber().toInt32())
59+
.bottomCenter();
6160
}
62-
if (auto sp = SimulatedPlayer::create(name, bpos, dimid)) return PlayerClass::newPlayer(sp);
61+
if (auto sp = SimulatedPlayer::create(name, spawnPos, dimId)) return PlayerClass::newPlayer(sp);
6362
else return Local<Value>();
6463
}
6564
CATCH("Fail in " __FUNCTION__ "!")
@@ -82,8 +81,6 @@ Local<Value> PlayerClass::simulateSneak(const Arguments&) {
8281
CATCH("Fail in " __FUNCTION__ "!")
8382
}
8483

85-
// bool simulateAttack(class Actor*);
86-
// bool simulateAttack();
8784
Local<Value> PlayerClass::simulateAttack(const Arguments& args) {
8885
try {
8986
auto sp = asSimulatedPlayer();
@@ -92,8 +89,8 @@ Local<Value> PlayerClass::simulateAttack(const Arguments& args) {
9289
if (args.size() == 0) return Boolean::newBoolean(sp->simulateAttack());
9390

9491
if (auto actor = EntityClass::tryExtractActor(args[0])) {
95-
if (!actor) return Local<Value>();
96-
return Boolean::newBoolean(sp->simulateAttack(actor));
92+
sp->_trySwing();
93+
return Boolean::newBoolean(sp->attack(*actor, SharedTypes::Legacy::ActorDamageCause::EntityAttack));
9794
}
9895

9996
LOG_WRONG_ARG_TYPE(__FUNCTION__);
@@ -102,8 +99,6 @@ Local<Value> PlayerClass::simulateAttack(const Arguments& args) {
10299
CATCH("Fail in " __FUNCTION__ "!")
103100
};
104101

105-
// bool simulateDestroy(); // LIAPI
106-
// bool simulateDestroyBlock(class BlockPos const&, enum ScriptFacing);
107102
Local<Value> PlayerClass::simulateDestroy(const Arguments& args) {
108103
try {
109104
auto sp = asSimulatedPlayer();
@@ -156,29 +151,26 @@ Local<Value> PlayerClass::simulateDestroy(const Arguments& args) {
156151
CATCH("Fail in " __FUNCTION__ "!")
157152
};
158153

159-
// void simulateDisconnect();
160154
Local<Value> PlayerClass::simulateDisconnect(const Arguments&) {
161155
try {
162156
auto sp = asSimulatedPlayer();
163157
if (!sp) return Local<Value>();
164-
sp->simulateDisconnect();
158+
sp->disconnect();
159+
sp->remove();
160+
sp->setGameTestHelper(nullptr);
165161
return Boolean::newBoolean(true);
166162
}
167163
CATCH("Fail in " __FUNCTION__ "!")
168164
};
169165

170-
// bool simulateInteract(class Actor&);
171-
// bool simulateInteract(class BlockPos const&, enum ScriptFacing);
172-
// bool simulateInteract();
173166
Local<Value> PlayerClass::simulateInteract(const Arguments& args) {
174167
try {
175168
auto sp = asSimulatedPlayer();
176169
if (!sp) return Local<Value>();
177170
if (args.size() == 0) return Boolean::newBoolean(sp->simulateInteract());
178171

179172
if (auto actor = EntityClass::tryExtractActor(args[0])) {
180-
if (!actor) return Local<Value>();
181-
return Boolean::newBoolean(sp->simulateInteract(*actor));
173+
return Boolean::newBoolean(sp->isAlive() && sp->interact(*actor, Vec3::ZERO()));
182174
}
183175

184176
int dimid = sp->getDimensionId();
@@ -216,13 +208,12 @@ Local<Value> PlayerClass::simulateInteract(const Arguments& args) {
216208
LOG_WRONG_ARG_TYPE(__FUNCTION__);
217209
return Local<Value>();
218210
}
219-
// TODO
211+
220212
return Boolean::newBoolean(sp->simulateInteract(bpos, face));
221213
}
222214
CATCH("Fail in " __FUNCTION__ "!")
223215
};
224216

225-
// bool simulateJump();
226217
Local<Value> PlayerClass::simulateJump(const Arguments&) {
227218
try {
228219
auto sp = asSimulatedPlayer();
@@ -232,13 +223,13 @@ Local<Value> PlayerClass::simulateJump(const Arguments&) {
232223
CATCH("Fail in " __FUNCTION__ "!")
233224
};
234225

235-
// bool simulateRespawn();
236226
Local<Value> PlayerClass::simulateRespawn(const Arguments&) {
237227
try {
238228
auto sp = asSimulatedPlayer();
239229
if (!sp) return Local<Value>();
240-
if (sp->simulateRespawn()) {
241-
get()->teleport(sp->getSpawnPosition().bottomCenter(), sp->getSpawnDimension());
230+
if (SimulatedPlayerHelper::simulateRespawn(*sp)) {
231+
auto& spawnPoint = sp->mPlayerRespawnPoint;
232+
get()->teleport(spawnPoint->mPlayerPosition->bottomCenter(), spawnPoint->mDimension);
242233
return Boolean::newBoolean(true);
243234
} else {
244235
return Boolean::newBoolean(false);
@@ -247,7 +238,6 @@ Local<Value> PlayerClass::simulateRespawn(const Arguments&) {
247238
CATCH("Fail in " __FUNCTION__ "!")
248239
};
249240

250-
// void simulateLocalMove(class Vec3 const&, float);
251241
Local<Value> PlayerClass::simulateLocalMove(const Arguments& args) {
252242
CHECK_ARGS_COUNT(args, 1);
253243
try {
@@ -291,7 +281,6 @@ Local<Value> PlayerClass::simulateLocalMove(const Arguments& args) {
291281
CATCH("Fail in " __FUNCTION__ "!")
292282
}
293283

294-
// void simulateWorldMove(class Vec3 const&, float);
295284
Local<Value> PlayerClass::simulateWorldMove(const Arguments& args) {
296285
CHECK_ARGS_COUNT(args, 1);
297286
try {
@@ -335,7 +324,6 @@ Local<Value> PlayerClass::simulateWorldMove(const Arguments& args) {
335324
CATCH("Fail in " __FUNCTION__ "!")
336325
};
337326

338-
// void simulateMoveToLocation(class Vec3 const&, float);
339327
Local<Value> PlayerClass::simulateMoveTo(const Arguments& args) {
340328
CHECK_ARGS_COUNT(args, 1);
341329
try {
@@ -379,15 +367,11 @@ Local<Value> PlayerClass::simulateMoveTo(const Arguments& args) {
379367
CATCH("Fail in " __FUNCTION__ "!")
380368
};
381369

382-
// void simulateLookAt(class Actor&);
383-
// void simulateLookAt(class BlockPos const&);
384-
// void simulateLookAt(class Vec3 const&);
385370
Local<Value> PlayerClass::simulateLookAt(const Arguments& args) {
386371
CHECK_ARGS_COUNT(args, 1);
387372
try {
388373
auto sp = asSimulatedPlayer();
389374
if (!sp) return Local<Value>();
390-
Vec3 target;
391375
int dimid = sp->getDimensionId();
392376
int lookDuration = 2; // 0 = Instant, 1 = Continuous, 2 = UntilMove
393377
if (args.size() > 1) {
@@ -400,7 +384,7 @@ Local<Value> PlayerClass::simulateLookAt(const Arguments& args) {
400384
auto pos = IntPos::extractPos(args[0]);
401385
auto did = pos->getDimensionId();
402386
if (dimid == did || did < 0 || did > 2) {
403-
sp->simulateLookAt(pos->getBlockPos(), (sim::LookDuration)lookDuration);
387+
SimulatedPlayerHelper::simulateLookAt(*sp, pos->getBlockPos(), (sim::LookDuration)lookDuration);
404388
return Boolean::newBoolean(true);
405389
}
406390
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().debug(
@@ -411,7 +395,7 @@ Local<Value> PlayerClass::simulateLookAt(const Arguments& args) {
411395
auto pos = FloatPos::extractPos(args[0]);
412396
auto did = pos->getDimensionId();
413397
if (dimid == did || did < 0 || did > 2) {
414-
sp->simulateLookAt(pos->getVec3(), (sim::LookDuration)lookDuration);
398+
SimulatedPlayerHelper::simulateLookAt(*sp, pos->getVec3(), (sim::LookDuration)lookDuration);
415399
return Boolean::newBoolean(true);
416400
}
417401
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().debug(
@@ -423,16 +407,15 @@ Local<Value> PlayerClass::simulateLookAt(const Arguments& args) {
423407
auto pos = IntPos::extractPos(block->getPos());
424408
auto did = pos->getDimensionId();
425409
if (dimid == did || did < 0 || did > 2) {
426-
sp->simulateLookAt(pos->getBlockPos(), (sim::LookDuration)lookDuration);
410+
SimulatedPlayerHelper::simulateLookAt(*sp, pos->getBlockPos(), (sim::LookDuration)lookDuration);
427411
return Boolean::newBoolean(true);
428412
}
429413
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().debug(
430414
"Can't simulate look at other dimension!"
431415
);
432416
return Boolean::newBoolean(false);
433417
} else if (auto actor = EntityClass::tryExtractActor(args[0])) {
434-
if (!actor) return Local<Value>();
435-
sp->simulateLookAt(*actor, (sim::LookDuration)lookDuration);
418+
SimulatedPlayerHelper::simulateLookAt(*sp, *actor, (sim::LookDuration)lookDuration);
436419
return Boolean::newBoolean(true);
437420
}
438421
LOG_WRONG_ARG_TYPE(__FUNCTION__);
@@ -510,7 +493,6 @@ Local<Value> PlayerClass::simulateNavigateTo(const Arguments& args) {
510493
sp->simulateNavigateToLocations(std::move(path), speed);
511494
return Boolean::newBoolean(true);
512495
} else if (auto actor = EntityClass::tryExtractActor(args[0])) {
513-
if (!actor) return Local<Value>();
514496
auto res = sp->simulateNavigateToEntity(*actor, speed);
515497
return NavigateResultToObject(res);
516498
} else if (IsInstanceOf<IntPos>(args[0]) || IsInstanceOf<FloatPos>(args[0])) {
@@ -566,7 +548,7 @@ Local<Value> PlayerClass::simulateUseItem(const Arguments& args) {
566548
return Local<Value>();
567549
}
568550
if (args.size() == 1) {
569-
if (item) return Boolean::newBoolean(sp->simulateUseItem(*item));
551+
if (item) return Boolean::newBoolean(SimulatedPlayerHelper::simulateUseItem(*sp, *item));
570552
else return Boolean::newBoolean(sp->simulateUseItemInSlot(slot));
571553
}
572554

@@ -592,15 +574,14 @@ Local<Value> PlayerClass::simulateUseItem(const Arguments& args) {
592574
}
593575
}
594576
if (item) return Boolean::newBoolean(sp->simulateUseItemOnBlock(*item, bpos, face, relativePos));
595-
else return Boolean::newBoolean(sp->simulateUseItemInSlotOnBlock(slot, bpos, face, relativePos));
577+
else
578+
return Boolean::newBoolean(
579+
SimulatedPlayerHelper::simulateUseItemInSlotOnBlock(*sp, slot, bpos, face, relativePos)
580+
);
596581
}
597582
CATCH("Fail in " __FUNCTION__ "!")
598583
};
599584

600-
// void simulateStopDestroyingBlock();
601-
// void simulateStopInteracting();
602-
// void simulateStopMoving();
603-
// void simulateStopUsingItem();
604585
Local<Value> PlayerClass::simulateStopDestroyingBlock(const Arguments&) {
605586
try {
606587
auto sp = asSimulatedPlayer();
@@ -615,7 +596,7 @@ Local<Value> PlayerClass::simulateStopInteracting(const Arguments&) {
615596
try {
616597
auto sp = asSimulatedPlayer();
617598
if (!sp) return Local<Value>();
618-
sp->simulateStopInteracting();
599+
sp->deleteContainerManager();
619600
return Boolean::newBoolean(true);
620601
}
621602
CATCH("Fail in " __FUNCTION__ "!")
@@ -625,7 +606,7 @@ Local<Value> PlayerClass::simulateStopMoving(const Arguments&) {
625606
try {
626607
auto sp = asSimulatedPlayer();
627608
if (!sp) return Local<Value>();
628-
sp->simulateStopMoving();
609+
SimulatedPlayerHelper::simulateStopMoving(*sp);
629610
return Boolean::newBoolean(true);
630611
}
631612
CATCH("Fail in " __FUNCTION__ "!")
@@ -635,7 +616,7 @@ Local<Value> PlayerClass::simulateStopUsingItem(const Arguments&) {
635616
try {
636617
auto sp = asSimulatedPlayer();
637618
if (!sp) return Local<Value>();
638-
sp->simulateStopUsingItem();
619+
SimulatedPlayerHelper::simulateStopUsingItem(*sp);
639620
return Boolean::newBoolean(true);
640621
}
641622
CATCH("Fail in " __FUNCTION__ "!")

0 commit comments

Comments
 (0)