Skip to content

Commit fcb2429

Browse files
committed
fix: fix BlockAPI
fix: fix #241
1 parent 6608b7c commit fcb2429

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/legacy/api/BlockAPI.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
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"
1312
#include "mc/deps/core/utility/optional_ref.h"
1413
#include "mc/world/level/BlockSource.h"
1514
#include "mc/world/level/ChunkBlockPos.h"
@@ -20,7 +19,6 @@
2019
#include "mc/world/level/block/actor/BlockActor.h"
2120
#include "mc/world/level/block/block_serialization_utils/BlockSerializationUtils.h"
2221
#include "mc/world/level/block/components/BlockComponentDirectData.h"
23-
#include "mc/world/level/block/components/BlockLiquidDetectionComponent.h"
2422
#include "mc/world/level/chunk/LevelChunk.h"
2523
#include "mc/world/level/dimension/Dimension.h"
2624
#include "mc/world/level/dimension/DimensionHeightRange.h"
@@ -357,7 +355,6 @@ Local<Value> BlockClass::setNbt(const Arguments& args) {
357355
}
358356

359357
Local<Value> BlockClass::getBlockState(const Arguments&) {
360-
return Local<Value>();
361358
try {
362359
auto list = block->mSerializationId;
363360
try {
@@ -417,12 +414,16 @@ Local<Value> BlockClass::getBlockEntity(const Arguments&) {
417414

418415
Local<Value> BlockClass::removeBlockEntity(const Arguments&) {
419416
try {
420-
ll::service::getLevel()
421-
->getDimension(blockPos.dim)
422-
.lock()
423-
->getBlockSourceFromMainChunkSource()
424-
.removeBlockEntity(blockPos.getBlockPos());
425-
return Boolean::newBoolean(true);
417+
auto chunk = ll::service::getLevel()
418+
->getDimension(blockPos.dim)
419+
.lock()
420+
->getBlockSourceFromMainChunkSource()
421+
.getChunkAt(blockPos.getBlockPos());
422+
if (chunk) {
423+
return Boolean::newBoolean(chunk->removeBlockEntity(blockPos.getBlockPos()) != nullptr);
424+
} else {
425+
return Boolean::newBoolean(false);
426+
}
426427
}
427428
CATCH("Fail in removeBlockEntity!");
428429
}
@@ -470,23 +471,21 @@ Local<Value> McClass::getBlock(const Arguments& args) {
470471
return Local<Value>();
471472
}
472473

473-
auto dimPtr = ll::service::getLevel()->getDimension(pos.dim).get();
474+
auto dimPtr = ll::service::getLevel()->getDimension(pos.dim).lock();
474475
if (!dimPtr) {
475476
return {};
476477
}
477478
BlockSource& bs = dimPtr->getBlockSourceFromMainChunkSource();
478-
short minHeight = dimPtr->getMinHeight();
479-
if (pos.y < minHeight || pos.y > dimPtr->getHeight()) {
479+
short minHeight = dimPtr->mHeightRange->mMin;
480+
if (pos.y < minHeight || pos.y > dimPtr->mHeightRange->mMax) {
480481
return {};
481482
}
482483
auto lc = bs.getChunkAt(pos.getBlockPos());
483484
if (!lc) {
484485
return {};
485486
}
486-
ChunkBlockPos cbpos = ChunkBlockPos(pos.getBlockPos(), minHeight);
487-
auto& block = lc->getBlock(cbpos);
488-
BlockPos bp{pos.x, pos.y, pos.z};
489-
return BlockClass::newBlock(block, bp, pos.dim);
487+
auto& block = lc->getBlock(ChunkBlockPos{pos.x, pos.z, minHeight});
488+
return BlockClass::newBlock(block, pos.getBlockPos(), pos.dim);
490489
}
491490
CATCH("Fail in GetBlock!")
492491
}
@@ -552,7 +551,8 @@ Local<Value> McClass::setBlock(const Arguments& args) {
552551
if (!bl.has_value()) {
553552
return Boolean::newBoolean(false);
554553
}
555-
BlockSource& bs = ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource();
554+
BlockSource& bs =
555+
ll::service::getLevel()->getDimension(pos.dim).lock()->getBlockSourceFromMainChunkSource();
556556
return Boolean::newBoolean(bs.setBlock(pos.getBlockPos(), bl, 3, nullptr, nullptr));
557557
} else if (IsInstanceOf<NbtCompoundClass>(block)) {
558558
// Nbt
@@ -561,7 +561,8 @@ Local<Value> McClass::setBlock(const Arguments& args) {
561561
if (!bl.has_value()) {
562562
return Boolean::newBoolean(false);
563563
}
564-
BlockSource& bs = ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource();
564+
BlockSource& bs =
565+
ll::service::getLevel()->getDimension(pos.dim).lock()->getBlockSourceFromMainChunkSource();
565566
return Boolean::newBoolean(bs.setBlock(pos.getBlockPos(), bl, 3, nullptr, nullptr));
566567
} else {
567568
// other block object
@@ -570,7 +571,8 @@ Local<Value> McClass::setBlock(const Arguments& args) {
570571
LOG_WRONG_ARG_TYPE(__FUNCTION__);
571572
return Local<Value>();
572573
}
573-
BlockSource& bs = ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource();
574+
BlockSource& bs =
575+
ll::service::getLevel()->getDimension(pos.dim).lock()->getBlockSourceFromMainChunkSource();
574576
return Boolean::newBoolean(bs.setBlock(pos.getBlockPos(), *bl, 3, nullptr, nullptr));
575577
}
576578
}
@@ -634,7 +636,7 @@ Local<Value> McClass::spawnParticle(const Arguments& args) {
634636
ll::service::getLevel()->spawnParticleEffect(
635637
type.asString().toString(),
636638
pos.getVec3(),
637-
ll::service::getLevel()->getDimension(pos.dim).get()
639+
ll::service::getLevel()->getDimension(pos.dim).lock().get()
638640
);
639641
return Boolean::newBoolean(true);
640642
}

0 commit comments

Comments
 (0)