1111#include " ll/api/service/Bedrock.h"
1212#include " mc/deps/core/string/HashedString.h"
1313#include " mc/deps/core/utility/optional_ref.h"
14- #include " mc/nbt/CompoundTag.h"
1514#include " mc/world/level/BlockSource.h"
16- #include " mc/world/level/Level.h"
1715#include " mc/world/level/block/Block.h"
1816#include " mc/world/level/block/actor/BlockActor.h"
1917#include " mc/world/level/block/block_serialization_utils/BlockSerializationUtils.h"
2018#include " mc/world/level/block/components/BlockLiquidDetectionComponent.h"
2119#include " mc/world/level/dimension/Dimension.h"
2220#include " mc/world/level/block/BedrockBlockNames.h"
21+ #include " mc/world/level/ChunkBlockPos.h"
22+ #include " mc/world/level/chunk/LevelChunk.h"
2323
2424#include < exception>
2525
@@ -66,6 +66,24 @@ ClassDefine<BlockClass> BlockClassBuilder =
6666 .instanceFunction(" getTag" , &BlockClass::getNbt)
6767 .build();
6868
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+
6987// ////////////////// Classes ////////////////////
7088
7189BlockClass::BlockClass (Block const & block) : ScriptClass(ScriptClass::ConstructFromCpp<BlockClass>{}), block(&block) {
@@ -85,9 +103,11 @@ Local<Object> BlockClass::newBlock(Block const& block, BlockPos const& pos, Dime
85103}
86104
87105Local<Object> BlockClass::newBlock (BlockPos const & pos, DimensionType dim) {
88- if (pos.y < 320 && pos.y >= -64 ) {
89- auto & bl = ll::service::getLevel ()->getDimension (dim)->getBlockSourceFromMainChunkSource ().getBlock (pos);
90- return BlockClass::newBlock (bl, pos, dim);
106+ if (auto dimension = ll::service::getLevel ()->getDimension (dim)) {
107+ if (isValidHeight (dimension, pos.y )) {
108+ auto & bl = dimension->getBlockSourceFromMainChunkSource ().getBlock (pos);
109+ return BlockClass::newBlock (bl, pos, dim);
110+ }
91111 }
92112 auto block = Block::tryGetFromRegistry (BedrockBlockNames::Air ());
93113 if (!block) {
@@ -103,9 +123,11 @@ Local<Object> BlockClass::newBlock(Block const& block, BlockPos const& pos, Bloc
103123
104124Local<Object> BlockClass::newBlock (IntVec4 pos) {
105125 BlockPos bp = {(float )pos.x , (float )pos.y , (float )pos.z };
106- if (bp.y < 320 && bp.y >= -64 ) {
107- auto & bl = ll::service::getLevel ()->getDimension (pos.dim )->getBlockSourceFromMainChunkSource ().getBlock (bp);
108- return BlockClass::newBlock (bl, bp, pos.dim );
126+ if (auto dimension = ll::service::getLevel ()->getDimension (pos.dim )) {
127+ if (isValidHeight (dimension, pos.y )) {
128+ auto & bl = dimension->getBlockSourceFromMainChunkSource ().getBlock (bp);
129+ return BlockClass::newBlock (bl, bp, pos.dim );
130+ }
109131 }
110132 auto block = Block::tryGetFromRegistry (BedrockBlockNames::Air ());
111133 if (!block) {
@@ -390,8 +412,6 @@ Local<Value> BlockClass::removeBlockEntity(const Arguments&) {
390412 }
391413 CATCH (" Fail in removeBlockEntity!" );
392414}
393- #include " mc/world/level/ChunkBlockPos.h"
394- #include " mc/world/level/chunk/LevelChunk.h"
395415
396416// public API
397417Local<Value> McClass::getBlock (const Arguments& args) {
@@ -440,11 +460,15 @@ Local<Value> McClass::getBlock(const Arguments& args) {
440460 if (!dimPtr) {
441461 return {};
442462 }
443- BlockSource& bs = dimPtr->getBlockSourceFromMainChunkSource ();
444- auto lc = bs.getChunkAt (pos.getBlockPos ());
445- if (!lc) return {};
446- short minHeight = dimPtr->getMinHeight ();
447- if (pos.y < minHeight || pos.y > dimPtr->getHeight ()) return {};
463+ BlockSource& bs = dimPtr->getBlockSourceFromMainChunkSource ();
464+ short minHeight = dimPtr->getMinHeight ();
465+ if (pos.y < minHeight || pos.y > dimPtr->getHeight ()) {
466+ return {};
467+ }
468+ auto lc = bs.getChunkAt (pos.getBlockPos ());
469+ if (!lc) {
470+ return {};
471+ }
448472 ChunkBlockPos cbpos = ChunkBlockPos (pos.getBlockPos (), minHeight);
449473 auto & block = lc->getBlock (cbpos);
450474 BlockPos bp{pos.x , pos.y , pos.z };
0 commit comments