Skip to content

Commit 3ae5f0d

Browse files
ShrBoxkillcerrn15421
authored
feat: adapt to MCBE 1.21.60 (#248)
* feat: adapt some files * fix: implement AttributeInstance::setCurrentValue in EntityAPI.cpp * fix: fix EntityClass::getAllTag * fix: fix EntityAPI completely * fix: fix some PlayerAPI * fix: fix some PlayerAPI * fix: fix Player::addEffect * fix: fix BaseAPI * fix: fix BlockAPI(WIP) * fix: fix BlockAPI fix: fix #241 * fix: fix BlockEntityAPI * fix: fix CommandAPI * fix: fix EventAPI * fix: fix something * fix: fix ItemAPI and BlockAPI * refactor: move BlockHelper to standalone file * fix: fix PacketAPI * fix: fix Player's scoreboard API * fix: fix ScoreboardAPI * fix: fix ServerAPI * fix: fix StructureAPI * fix: fix SimulatedPlayerAPI * fix: fix BuiltinCommands * fix: fix SimulatedPlayerHelper.cpp * fix: fix events except for inlined functions * chore: update cpp-httplib to 0.18.7 * chore: require openssl3 manually to avoid github actions system package * chore: try fix mariadb * chore: update levilamina * chore: update levilamina * fix: fix BinaryStreamClass::writeItem * chore: update levilamina * chore: update tooth.json and xmake.lua * fix: fix some events (#247) * fix:fix some events Co-authored-by: killcerr <kill_cerr@outlook.com> * feat: fix AttackBlock event --------- Co-authored-by: n15421 <119112174+n15421@users.noreply.github.com> * fix: fix onProjectileHitBlock and onStartDestroyBlock * fix: fix onOpenContainerScreen * chore: disable onNpcCmd temporarily --------- Co-authored-by: killcerr <kill_cerr@outlook.com> Co-authored-by: n15421 <119112174+n15421@users.noreply.github.com>
1 parent ca96469 commit 3ae5f0d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1120
-726
lines changed

src/legacy/api/BaseAPI.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
#include <mc/world/level/BlockSource.h>
1313

1414
///////////////////// Enum //////////////////////
15-
ClassDefine<void> DamageCauseEnumBuilder = EnumDefineBuilder<ActorDamageCause>::build("DamageCause");
15+
ClassDefine<void> DamageCauseEnumBuilder =
16+
EnumDefineBuilder<SharedTypes::Legacy::ActorDamageCause>::build("DamageCause");
1617
// For compatibility
17-
ClassDefine<void> ActorDamageCauseEnumBuilder = EnumDefineBuilder<ActorDamageCause>::build("ActorDamageCause");
18+
ClassDefine<void> ActorDamageCauseEnumBuilder =
19+
EnumDefineBuilder<SharedTypes::Legacy::ActorDamageCause>::build("ActorDamageCause");
1820

1921
//////////////////// Class Definition ////////////////////
2022

@@ -181,7 +183,13 @@ Local<Value> DirectionAngle::toString() {
181183

182184
Local<Value> DirectionAngle::toFacing() {
183185
int facing = -1;
184-
switch (Facing::convertYRotationToFacingDirection(yaw)) {
186+
// Facing::convertYRotationToFacingDirection
187+
float value = yaw * 0.011111111f + 0.5f;
188+
int result = static_cast<int>(value) - 1;
189+
if (static_cast<float>(static_cast<int>(value)) <= value) {
190+
result = static_cast<int>(value);
191+
}
192+
switch (result) {
185193
case 2:
186194
facing = 0;
187195
break;

src/legacy/api/BlockAPI.cpp

+59-59
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,25 @@
99
#include "api/McAPI.h"
1010
#include "api/NbtAPI.h"
1111
#include "ll/api/service/Bedrock.h"
12-
#include "mc/deps/core/string/HashedString.h"
12+
#include "lse/api/helper/BlockHelper.h"
1313
#include "mc/deps/core/utility/optional_ref.h"
1414
#include "mc/world/level/BlockSource.h"
1515
#include "mc/world/level/ChunkBlockPos.h"
1616
#include "mc/world/level/block/BedrockBlockNames.h"
1717
#include "mc/world/level/block/Block.h"
18+
#include "mc/world/level/block/DetectionRule.h"
19+
#include "mc/world/level/block/LiquidReaction.h"
1820
#include "mc/world/level/block/actor/BlockActor.h"
1921
#include "mc/world/level/block/block_serialization_utils/BlockSerializationUtils.h"
20-
#include "mc/world/level/block/components/BlockLiquidDetectionComponent.h"
22+
#include "mc/world/level/block/components/BlockComponentDirectData.h"
2123
#include "mc/world/level/chunk/LevelChunk.h"
2224
#include "mc/world/level/dimension/Dimension.h"
25+
#include "mc/world/level/dimension/DimensionHeightRange.h"
2326

2427
#include <exception>
2528

29+
using lse::api::BlockHelper;
30+
2631
//////////////////// Class Definition ////////////////////
2732

2833
ClassDefine<BlockClass> BlockClassBuilder =
@@ -66,24 +71,6 @@ ClassDefine<BlockClass> BlockClassBuilder =
6671
.instanceFunction("getTag", &BlockClass::getNbt)
6772
.build();
6873

69-
namespace lse::BlockAPI {
70-
inline bool isValidHeight(WeakRef<Dimension> dim, std::variant<int, float> height) {
71-
if (dim) {
72-
if (std::holds_alternative<int>(height)) {
73-
int y = std::get<int>(height);
74-
return dim->getMinHeight() <= y && dim->getHeight() >= y;
75-
} else {
76-
float y = std::get<float>(height);
77-
return dim->getMinHeight() <= y && dim->getHeight() >= y;
78-
}
79-
}
80-
81-
return false;
82-
}
83-
} // namespace lse::BlockAPI
84-
85-
using lse::BlockAPI::isValidHeight;
86-
8774
//////////////////// Classes ////////////////////
8875

8976
BlockClass::BlockClass(Block const& block) : ScriptClass(ScriptClass::ConstructFromCpp<BlockClass>{}), block(&block) {
@@ -103,8 +90,8 @@ Local<Object> BlockClass::newBlock(Block const& block, BlockPos const& pos, Dime
10390
}
10491

10592
Local<Object> BlockClass::newBlock(BlockPos const& pos, DimensionType dim) {
106-
if (auto dimension = ll::service::getLevel()->getDimension(dim)) {
107-
if (isValidHeight(dimension, pos.y)) {
93+
if (auto dimension = ll::service::getLevel()->getDimension(dim).lock()) {
94+
if (BlockHelper::isValidHeight(dimension, pos.y)) {
10895
auto& bl = dimension->getBlockSourceFromMainChunkSource().getBlock(pos);
10996
return BlockClass::newBlock(bl, pos, dim);
11097
}
@@ -123,8 +110,8 @@ Local<Object> BlockClass::newBlock(Block const& block, BlockPos const& pos, Bloc
123110

124111
Local<Object> BlockClass::newBlock(IntVec4 pos) {
125112
BlockPos bp = {(float)pos.x, (float)pos.y, (float)pos.z};
126-
if (auto dimension = ll::service::getLevel()->getDimension(pos.dim)) {
127-
if (isValidHeight(dimension, pos.y)) {
113+
if (auto dimension = ll::service::getLevel()->getDimension(pos.dim).lock()) {
114+
if (BlockHelper::isValidHeight(dimension, pos.y)) {
128115
auto& bl = dimension->getBlockSourceFromMainChunkSource().getBlock(bp);
129116
return BlockClass::newBlock(bl, bp, pos.dim);
130117
}
@@ -185,28 +172,28 @@ Local<Value> BlockClass::getPos() {
185172
Local<Value> BlockClass::getTileData() {
186173
try {
187174
// preloaded
188-
return Number::newNumber(block->getVariant());
175+
return Number::newNumber(block->getLegacyBlock().getVariant(*block));
189176
}
190177
CATCH("Fail in getTileData!");
191178
}
192179

193180
Local<Value> BlockClass::getVariant() {
194181
try {
195-
return Number::newNumber(block->getVariant());
182+
return Number::newNumber(block->getLegacyBlock().getVariant(*block));
196183
}
197184
CATCH("Fail in getVariant!");
198185
}
199186

200187
Local<Value> BlockClass::getTranslucency() {
201188
try {
202-
return Number::newNumber(block->getTranslucency());
189+
return Number::newNumber(block->getLegacyBlock().mTranslucency);
203190
}
204191
CATCH("Fail in getTranslucency!");
205192
}
206193

207194
Local<Value> BlockClass::getThickness() {
208195
try {
209-
return Number::newNumber(block->getThickness());
196+
return Number::newNumber(block->getLegacyBlock().mThickness);
210197
}
211198
CATCH("Fail in getThickness!");
212199
}
@@ -220,7 +207,7 @@ Local<Value> BlockClass::isAir() {
220207

221208
Local<Value> BlockClass::isBounceBlock() {
222209
try {
223-
return Boolean::newBoolean(block->isBounceBlock());
210+
return Boolean::newBoolean(block->getLegacyBlock().isBounceBlock());
224211
}
225212
CATCH("Fail in isBounceBlock!");
226213
}
@@ -248,35 +235,35 @@ Local<Value> BlockClass::isDoorBlock() {
248235

249236
Local<Value> BlockClass::isFenceBlock() {
250237
try {
251-
return Boolean::newBoolean(block->isFenceBlock());
238+
return Boolean::newBoolean(block->getLegacyBlock().isFenceBlock());
252239
}
253240
CATCH("Fail in isFenceBlock!");
254241
}
255242

256243
Local<Value> BlockClass::isFenceGateBlock() {
257244
try {
258-
return Boolean::newBoolean(block->isFenceGateBlock());
245+
return Boolean::newBoolean(block->getLegacyBlock().isFenceGateBlock());
259246
}
260247
CATCH("Fail in isFenceGateBlock!");
261248
}
262249

263250
Local<Value> BlockClass::isThinFenceBlock() {
264251
try {
265-
return Boolean::newBoolean(block->isThinFenceBlock());
252+
return Boolean::newBoolean(block->getLegacyBlock().isThinFenceBlock());
266253
}
267254
CATCH("Fail in isThinFenceBlock!");
268255
}
269256

270257
Local<Value> BlockClass::isHeavyBlock() {
271258
try {
272-
return Boolean::newBoolean(block->isFallingBlock());
259+
return Boolean::newBoolean(block->getLegacyBlock().mFalling);
273260
}
274261
CATCH("Fail in isHeavyBlock!");
275262
}
276263

277264
Local<Value> BlockClass::isStemBlock() {
278265
try {
279-
return Boolean::newBoolean(block->isStemBlock());
266+
return Boolean::newBoolean(block->getLegacyBlock().isStemBlock());
280267
}
281268
CATCH("Fail in isStemBlock!");
282269
}
@@ -290,14 +277,17 @@ Local<Value> BlockClass::isSlabBlock() {
290277

291278
Local<Value> BlockClass::isUnbreakable() {
292279
try {
293-
return Boolean::newBoolean(block->isUnbreakable());
280+
return Boolean::newBoolean(block->mDirectData->mUnkc08fbd.as<float>() < 0.0f);
294281
}
295282
CATCH("Fail in isUnbreakable!");
296283
}
297284

298285
Local<Value> BlockClass::isWaterBlockingBlock() {
299286
try {
300-
return Boolean::newBoolean(BlockLiquidDetectionComponent::isLiquidBlocking(*block));
287+
return Boolean::newBoolean(
288+
block->mDirectData->mUnkd3e7c9.as<DetectionRule>().mUnk21e36d.as<LiquidReaction>()
289+
== LiquidReaction::Blocking
290+
);
301291
}
302292
CATCH("Fail in isWaterBlockingBlock!");
303293
}
@@ -309,7 +299,8 @@ Local<Value> BlockClass::destroyBlock(const Arguments& args) {
309299
try {
310300
// same as `Level::getBlockInstance(pos.getBlockPos(),
311301
// pos.dim).breakNaturally()` when drop
312-
BlockSource& bl = ll::service::getLevel()->getDimension(blockPos.dim)->getBlockSourceFromMainChunkSource();
302+
BlockSource& bl =
303+
ll::service::getLevel()->getDimension(blockPos.dim).lock()->getBlockSourceFromMainChunkSource();
313304
return Boolean::newBoolean(
314305
ll::service::getLevel()->destroyBlock(bl, blockPos.getBlockPos(), args[0].asBoolean().value())
315306
);
@@ -319,7 +310,7 @@ Local<Value> BlockClass::destroyBlock(const Arguments& args) {
319310

320311
Local<Value> BlockClass::getNbt(const Arguments&) {
321312
try {
322-
return NbtCompoundClass::pack(block->getSerializationId().clone());
313+
return NbtCompoundClass::pack(block->mSerializationId->clone());
323314
}
324315
CATCH("Fail in getNbt!");
325316
}
@@ -337,6 +328,7 @@ Local<Value> BlockClass::setNbt(const Arguments& args) {
337328
if (bl) {
338329
ll::service::getLevel()
339330
->getDimension(blockPos.dim)
331+
.lock()
340332
->getBlockSourceFromMainChunkSource()
341333
.setBlock(blockPos.getBlockPos(), *bl, 3, nullptr, nullptr);
342334
}
@@ -347,11 +339,10 @@ Local<Value> BlockClass::setNbt(const Arguments& args) {
347339
}
348340

349341
Local<Value> BlockClass::getBlockState(const Arguments&) {
350-
return Local<Value>();
351342
try {
352-
auto list = block->getSerializationId();
343+
auto list = block->mSerializationId;
353344
try {
354-
return Tag2Value(&list.at("states").get(), true);
345+
return Tag2Value(&list->at("states").get(), true);
355346
} catch (...) {
356347
return Array::newArray();
357348
}
@@ -365,9 +356,10 @@ Local<Value> BlockClass::hasContainer(const Arguments&) {
365356
try {
366357
auto& bl = ll::service::getLevel()
367358
->getDimension(blockPos.dim)
359+
.lock()
368360
->getBlockSourceFromMainChunkSource()
369361
.getBlock(blockPos.getBlockPos());
370-
return Boolean::newBoolean(bl.isContainerBlock());
362+
return Boolean::newBoolean(bl.getLegacyBlock().isContainerBlock());
371363
}
372364
CATCH("Fail in hasContainer!");
373365
}
@@ -376,6 +368,7 @@ Local<Value> BlockClass::getContainer(const Arguments&) {
376368
try {
377369
Container* container = ll::service::getLevel()
378370
->getDimension(blockPos.dim)
371+
.lock()
379372
->getBlockSourceFromMainChunkSource()
380373
.getBlockEntity(blockPos.getBlockPos())
381374
->getContainer();
@@ -386,7 +379,7 @@ Local<Value> BlockClass::getContainer(const Arguments&) {
386379

387380
Local<Value> BlockClass::hasBlockEntity(const Arguments&) {
388381
try {
389-
return Boolean::newBoolean(block->hasBlockEntity());
382+
return Boolean::newBoolean(block->getLegacyBlock().mBlockEntityType != BlockActorType::Undefined);
390383
}
391384
CATCH("Fail in hasBlockEntity!");
392385
}
@@ -395,6 +388,7 @@ Local<Value> BlockClass::getBlockEntity(const Arguments&) {
395388
try {
396389
BlockActor* be = ll::service::getLevel()
397390
->getDimension(blockPos.dim)
391+
.lock()
398392
->getBlockSourceFromMainChunkSource()
399393
.getBlockEntity(blockPos.getBlockPos());
400394
return be ? BlockEntityClass::newBlockEntity(be, blockPos.dim) : Local<Value>();
@@ -404,11 +398,16 @@ Local<Value> BlockClass::getBlockEntity(const Arguments&) {
404398

405399
Local<Value> BlockClass::removeBlockEntity(const Arguments&) {
406400
try {
407-
ll::service::getLevel()
408-
->getDimension(blockPos.dim)
409-
->getBlockSourceFromMainChunkSource()
410-
.removeBlockEntity(blockPos.getBlockPos());
411-
return Boolean::newBoolean(true);
401+
auto chunk = ll::service::getLevel()
402+
->getDimension(blockPos.dim)
403+
.lock()
404+
->getBlockSourceFromMainChunkSource()
405+
.getChunkAt(blockPos.getBlockPos());
406+
if (chunk) {
407+
return Boolean::newBoolean(chunk->removeBlockEntity(blockPos.getBlockPos()) != nullptr);
408+
} else {
409+
return Boolean::newBoolean(false);
410+
}
412411
}
413412
CATCH("Fail in removeBlockEntity!");
414413
}
@@ -456,23 +455,21 @@ Local<Value> McClass::getBlock(const Arguments& args) {
456455
return Local<Value>();
457456
}
458457

459-
auto dimPtr = ll::service::getLevel()->getDimension(pos.dim).get();
458+
auto dimPtr = ll::service::getLevel()->getDimension(pos.dim).lock();
460459
if (!dimPtr) {
461460
return {};
462461
}
463462
BlockSource& bs = dimPtr->getBlockSourceFromMainChunkSource();
464-
short minHeight = dimPtr->getMinHeight();
465-
if (pos.y < minHeight || pos.y > dimPtr->getHeight()) {
463+
short minHeight = dimPtr->mHeightRange->mMin;
464+
if (pos.y < minHeight || pos.y > dimPtr->mHeightRange->mMax) {
466465
return {};
467466
}
468467
auto lc = bs.getChunkAt(pos.getBlockPos());
469468
if (!lc) {
470469
return {};
471470
}
472-
ChunkBlockPos cbpos = ChunkBlockPos(pos.getBlockPos(), minHeight);
473-
auto& block = lc->getBlock(cbpos);
474-
BlockPos bp{pos.x, pos.y, pos.z};
475-
return BlockClass::newBlock(block, bp, pos.dim);
471+
auto& block = lc->getBlock(ChunkBlockPos{(uchar)pos.x, (uchar)pos.z, minHeight});
472+
return BlockClass::newBlock(block, pos.getBlockPos(), pos.dim);
476473
}
477474
CATCH("Fail in GetBlock!")
478475
}
@@ -538,7 +535,8 @@ Local<Value> McClass::setBlock(const Arguments& args) {
538535
if (!bl.has_value()) {
539536
return Boolean::newBoolean(false);
540537
}
541-
BlockSource& bs = ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource();
538+
BlockSource& bs =
539+
ll::service::getLevel()->getDimension(pos.dim).lock()->getBlockSourceFromMainChunkSource();
542540
return Boolean::newBoolean(bs.setBlock(pos.getBlockPos(), bl, 3, nullptr, nullptr));
543541
} else if (IsInstanceOf<NbtCompoundClass>(block)) {
544542
// Nbt
@@ -547,7 +545,8 @@ Local<Value> McClass::setBlock(const Arguments& args) {
547545
if (!bl.has_value()) {
548546
return Boolean::newBoolean(false);
549547
}
550-
BlockSource& bs = ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource();
548+
BlockSource& bs =
549+
ll::service::getLevel()->getDimension(pos.dim).lock()->getBlockSourceFromMainChunkSource();
551550
return Boolean::newBoolean(bs.setBlock(pos.getBlockPos(), bl, 3, nullptr, nullptr));
552551
} else {
553552
// other block object
@@ -556,7 +555,8 @@ Local<Value> McClass::setBlock(const Arguments& args) {
556555
LOG_WRONG_ARG_TYPE(__FUNCTION__);
557556
return Local<Value>();
558557
}
559-
BlockSource& bs = ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource();
558+
BlockSource& bs =
559+
ll::service::getLevel()->getDimension(pos.dim).lock()->getBlockSourceFromMainChunkSource();
560560
return Boolean::newBoolean(bs.setBlock(pos.getBlockPos(), *bl, 3, nullptr, nullptr));
561561
}
562562
}
@@ -620,7 +620,7 @@ Local<Value> McClass::spawnParticle(const Arguments& args) {
620620
ll::service::getLevel()->spawnParticleEffect(
621621
type.asString().toString(),
622622
pos.getVec3(),
623-
ll::service::getLevel()->getDimension(pos.dim).get()
623+
ll::service::getLevel()->getDimension(pos.dim).lock().get()
624624
);
625625
return Boolean::newBoolean(true);
626626
}

0 commit comments

Comments
 (0)