Skip to content

Commit 046923f

Browse files
committed
refactor: optimize BlockAPI
1 parent 0fe6d59 commit 046923f

File tree

9 files changed

+88
-80
lines changed

9 files changed

+88
-80
lines changed

src/legacy/api/BlockAPI.cpp

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "mc/world/level/block/block_serialization_utils/BlockSerializationUtils.h"
2020
#include "mc/world/level/block/components/BlockLiquidDetectionComponent.h"
2121
#include "mc/world/level/dimension/Dimension.h"
22+
#include "mc/world/level/block/BedrockBlockNames.h"
2223

2324
#include <exception>
2425

@@ -67,54 +68,64 @@ ClassDefine<BlockClass> BlockClassBuilder =
6768

6869
//////////////////// Classes ////////////////////
6970

70-
BlockClass::BlockClass(Block const* p)
71-
: ScriptClass(ScriptClass::ConstructFromCpp<BlockClass>{}),
72-
block(const_cast<Block*>(p)) {
73-
preloadData({0, 0, 0}, -1);
71+
BlockClass::BlockClass(Block const& block) : ScriptClass(ScriptClass::ConstructFromCpp<BlockClass>{}), block(&block) {
72+
preloadData(BlockPos::ZERO(), -1);
7473
}
7574

76-
BlockClass::BlockClass(Block const* p, BlockPos bp, int dim)
75+
BlockClass::BlockClass(Block const& block, BlockPos const& pos, DimensionType dim)
7776
: ScriptClass(ScriptClass::ConstructFromCpp<BlockClass>{}),
78-
block(const_cast<Block*>(p)) {
79-
preloadData(bp, dim);
77+
block(&block) {
78+
preloadData(pos, dim);
8079
}
8180

8281
// generating function
83-
Local<Object> BlockClass::newBlock(Block const* p, BlockPos const* pos, int dim) {
84-
auto newp = new BlockClass(p, *pos, dim);
82+
Local<Object> BlockClass::newBlock(Block const& block, BlockPos const& pos, DimensionType dim) {
83+
auto newp = new BlockClass(block, pos, dim);
8584
return newp->getScriptObject();
8685
}
8786

88-
Local<Object> BlockClass::newBlock(BlockPos const* pos, int dim) {
89-
auto& bl = ll::service::getLevel()->getDimension(dim)->getBlockSourceFromMainChunkSource().getBlock(*pos);
90-
return BlockClass::newBlock(&bl, pos, dim);
87+
Local<Object> BlockClass::newBlock(const BlockPos& 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);
91+
}
92+
auto block = Block::tryGetFromRegistry(BedrockBlockNames::Air());
93+
if (!block) {
94+
return Object::newObject();
95+
}
96+
return BlockClass::newBlock(block, pos, dim);
9197
}
9298

93-
Local<Object> BlockClass::newBlock(const BlockPos& pos, int dim) { return newBlock(&pos, dim); }
94-
95-
Local<Object> BlockClass::newBlock(Block const* p, BlockPos const* pos, BlockSource const* bs) {
96-
auto newp = new BlockClass(p, *pos, bs->getDimensionId());
99+
Local<Object> BlockClass::newBlock(Block const& block, BlockPos const& pos, BlockSource const& bs) {
100+
auto newp = new BlockClass(block, pos, bs.getDimensionId());
97101
return newp->getScriptObject();
98102
}
99103

100104
Local<Object> BlockClass::newBlock(IntVec4 pos) {
101105
BlockPos bp = {(float)pos.x, (float)pos.y, (float)pos.z};
102-
auto& bl = ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource().getBlock(bp);
103-
return BlockClass::newBlock(&bl, &bp, pos.dim);
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);
109+
}
110+
auto block = Block::tryGetFromRegistry(BedrockBlockNames::Air());
111+
if (!block) {
112+
return Object::newObject();
113+
}
114+
return BlockClass::newBlock(block, bp, pos.dim);
104115
}
105116

106-
Block* BlockClass::extract(Local<Value> v) {
117+
Block const* BlockClass::extract(Local<Value> v) {
107118
if (EngineScope::currentEngine()->isInstanceOf<BlockClass>(v))
108119
return EngineScope::currentEngine()->getNativeInstance<BlockClass>(v)->get();
109120
else return nullptr;
110121
}
111122

112123
// member function
113-
void BlockClass::preloadData(BlockPos bp, int dim) {
114-
name = block->buildDescriptionName();
115-
type = block->getTypeName();
116-
id = block->getBlockItemId();
117-
pos = {bp.x, bp.y, bp.z, dim};
124+
void BlockClass::preloadData(BlockPos pos, DimensionType dim) {
125+
name = block->buildDescriptionName();
126+
type = block->getTypeName();
127+
id = block->getBlockItemId();
128+
blockPos = {pos.x, pos.y, pos.z, dim};
118129
}
119130

120131
Local<Value> BlockClass::getName() {
@@ -144,7 +155,7 @@ Local<Value> BlockClass::getId() {
144155
Local<Value> BlockClass::getPos() {
145156
try {
146157
// preloaded
147-
return IntPos::newPos(pos);
158+
return IntPos::newPos(blockPos);
148159
}
149160
CATCH("Fail in getBlockPos!");
150161
}
@@ -276,9 +287,9 @@ Local<Value> BlockClass::destroyBlock(const Arguments& args) {
276287
try {
277288
// same as `Level::getBlockInstance(pos.getBlockPos(),
278289
// pos.dim).breakNaturally()` when drop
279-
BlockSource& bl = ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource();
290+
BlockSource& bl = ll::service::getLevel()->getDimension(blockPos.dim)->getBlockSourceFromMainChunkSource();
280291
return Boolean::newBoolean(
281-
ll::service::getLevel()->destroyBlock(bl, pos.getBlockPos(), args[0].asBoolean().value())
292+
ll::service::getLevel()->destroyBlock(bl, blockPos.getBlockPos(), args[0].asBoolean().value())
282293
);
283294
}
284295
CATCH("Fail in destroyBlock!");
@@ -303,11 +314,11 @@ Local<Value> BlockClass::setNbt(const Arguments& args) {
303314
const Block* bl = result.second;
304315
if (bl) {
305316
ll::service::getLevel()
306-
->getDimension(pos.dim)
317+
->getDimension(blockPos.dim)
307318
->getBlockSourceFromMainChunkSource()
308-
.setBlock(pos.getBlockPos(), *bl, 3, nullptr, nullptr);
319+
.setBlock(blockPos.getBlockPos(), *bl, 3, nullptr, nullptr);
309320
}
310-
preloadData(pos.getBlockPos(), pos.getDimensionId());
321+
preloadData(blockPos.getBlockPos(), blockPos.getDimensionId());
311322
return Boolean::newBoolean(true);
312323
}
313324
CATCH("Fail in setNbt!")
@@ -330,9 +341,10 @@ Local<Value> BlockClass::getBlockState(const Arguments&) {
330341

331342
Local<Value> BlockClass::hasContainer(const Arguments&) {
332343
try {
333-
auto& bl = ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource().getBlock(
334-
pos.getBlockPos()
335-
);
344+
auto& bl = ll::service::getLevel()
345+
->getDimension(blockPos.dim)
346+
->getBlockSourceFromMainChunkSource()
347+
.getBlock(blockPos.getBlockPos());
336348
return Boolean::newBoolean(bl.isContainerBlock());
337349
}
338350
CATCH("Fail in hasContainer!");
@@ -341,9 +353,9 @@ Local<Value> BlockClass::hasContainer(const Arguments&) {
341353
Local<Value> BlockClass::getContainer(const Arguments&) {
342354
try {
343355
Container* container = ll::service::getLevel()
344-
->getDimension(pos.dim)
356+
->getDimension(blockPos.dim)
345357
->getBlockSourceFromMainChunkSource()
346-
.getBlockEntity(pos.getBlockPos())
358+
.getBlockEntity(blockPos.getBlockPos())
347359
->getContainer();
348360
return container ? ContainerClass::newContainer(container) : Local<Value>();
349361
}
@@ -359,20 +371,21 @@ Local<Value> BlockClass::hasBlockEntity(const Arguments&) {
359371

360372
Local<Value> BlockClass::getBlockEntity(const Arguments&) {
361373
try {
362-
BlockActor* be =
363-
ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource().getBlockEntity(
364-
pos.getBlockPos()
365-
);
366-
return be ? BlockEntityClass::newBlockEntity(be, pos.dim) : Local<Value>();
374+
BlockActor* be = ll::service::getLevel()
375+
->getDimension(blockPos.dim)
376+
->getBlockSourceFromMainChunkSource()
377+
.getBlockEntity(blockPos.getBlockPos());
378+
return be ? BlockEntityClass::newBlockEntity(be, blockPos.dim) : Local<Value>();
367379
}
368380
CATCH("Fail in getBlockEntity!");
369381
}
370382

371383
Local<Value> BlockClass::removeBlockEntity(const Arguments&) {
372384
try {
373-
ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource().removeBlockEntity(
374-
pos.getBlockPos()
375-
); //==========???
385+
ll::service::getLevel()
386+
->getDimension(blockPos.dim)
387+
->getBlockSourceFromMainChunkSource()
388+
.removeBlockEntity(blockPos.getBlockPos());
376389
return Boolean::newBoolean(true);
377390
}
378391
CATCH("Fail in removeBlockEntity!");
@@ -433,13 +446,9 @@ Local<Value> McClass::getBlock(const Arguments& args) {
433446
short minHeight = dimPtr->getMinHeight();
434447
if (pos.y < minHeight || pos.y > dimPtr->getHeight()) return {};
435448
ChunkBlockPos cbpos = ChunkBlockPos(pos.getBlockPos(), minHeight);
436-
auto block = &const_cast<Block&>(lc->getBlock(cbpos));
437-
if (!block) {
438-
// LOG_WRONG_ARG_TYPE(__FUNCTION__);
439-
return {};
440-
}
441-
BlockPos bp{pos.x, pos.y, pos.z};
442-
return BlockClass::newBlock(block, &bp, pos.dim);
449+
auto& block = lc->getBlock(cbpos);
450+
BlockPos bp{pos.x, pos.y, pos.z};
451+
return BlockClass::newBlock(block, bp, pos.dim);
443452
}
444453
CATCH("Fail in GetBlock!")
445454
}
@@ -518,7 +527,7 @@ Local<Value> McClass::setBlock(const Arguments& args) {
518527
return Boolean::newBoolean(bs.setBlock(pos.getBlockPos(), bl, 3, nullptr, nullptr));
519528
} else {
520529
// other block object
521-
Block* bl = BlockClass::extract(block);
530+
Block const* bl = BlockClass::extract(block);
522531
if (!bl) {
523532
LOG_WRONG_ARG_TYPE(__FUNCTION__);
524533
return Local<Value>();

src/legacy/api/BlockAPI.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,25 @@
88
class Block;
99
class BlockClass : public ScriptClass {
1010
private:
11-
Block* block;
11+
Block const* block;
1212

1313
// Pre data
14-
std::string name, type;
15-
int id;
16-
IntVec4 pos;
14+
std::string name, type;
15+
DimensionType id;
16+
IntVec4 blockPos;
1717

1818
public:
19-
explicit BlockClass(Block const* p);
20-
BlockClass(Block const* p, BlockPos bp, int dim);
19+
explicit BlockClass(Block const& block);
20+
BlockClass(Block const& block, BlockPos const& pos, DimensionType dim);
2121

22-
void preloadData(BlockPos bp, int dim);
23-
Block* get() { return block; }
22+
void preloadData(BlockPos bp, DimensionType dim);
23+
Block const* get() { return block; }
2424

25-
static Local<Object> newBlock(Block const* p, BlockPos const* pos, int dim);
26-
static Local<Object> newBlock(BlockPos const* pos, int dim);
27-
static Local<Object> newBlock(const BlockPos& pos, int dim);
28-
static Local<Object> newBlock(Block const* p, BlockPos const* pos, BlockSource const* bs);
25+
static Local<Object> newBlock(Block const& block, BlockPos const& pos, DimensionType dim);
26+
static Local<Object> newBlock(BlockPos const& pos, DimensionType dim);
27+
static Local<Object> newBlock(Block const& block, BlockPos const& pos, BlockSource const& bs);
2928
static Local<Object> newBlock(IntVec4 pos);
30-
static Block* extract(Local<Value> v);
29+
static Block const* extract(Local<Value> v);
3130

3231
Local<Value> getName();
3332
Local<Value> getType();

src/legacy/api/BlockEntityAPI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Local<Value> BlockEntityClass::getBlock(const Arguments&) {
9696
try {
9797
BlockPos bp = blockEntity->getPosition();
9898
auto& bl = ll::service::getLevel()->getDimension(dim)->getBlockSourceFromMainChunkSource().getBlock(bp);
99-
return BlockClass::newBlock(&bl, &bp, dim);
99+
return BlockClass::newBlock(bl, bp, dim);
100100
}
101101
CATCH("Fail in getBlock!")
102102
}

src/legacy/api/CommandAPI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ Local<Value> convertResult(ParamStorageType const& result, CommandOrigin const&
9292
return String::newString(std::get<RuntimeSoftEnum>(result.value()));
9393
} else if (result.hold(ParamKind::Kind::BlockName)) {
9494
return BlockClass::newBlock(
95-
std::get<CommandBlockName>(result.value()).resolveBlock(0).getBlock(),
96-
&BlockPos::MIN(),
95+
*std::get<CommandBlockName>(result.value()).resolveBlock(0).getBlock(),
96+
BlockPos::MIN(),
9797
-1
9898
);
9999
} else if (result.hold(ParamKind::Kind::Item)) {

src/legacy/api/EntityAPI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ Local<Value> EntityClass::getBlockStandingOn(const Arguments&) {
925925
Actor* entity = get();
926926
if (!entity) return Local<Value>();
927927

928-
return BlockClass::newBlock(entity->getBlockPosCurrentlyStandingOn(nullptr), (int)entity->getDimensionId());
928+
return BlockClass::newBlock(entity->getBlockPosCurrentlyStandingOn(nullptr), entity->getDimensionId());
929929
}
930930
CATCH("Fail in getBlockStandingOn!");
931931
}
@@ -1426,7 +1426,7 @@ Local<Value> EntityClass::getBlockFromViewVector(const Arguments& args) {
14261426
if (bl.isEmpty()) {
14271427
return Local<Value>();
14281428
}
1429-
return BlockClass::newBlock(std::move(&bl), &bp, actor->getDimensionId().id);
1429+
return BlockClass::newBlock(bl, bp, actor->getDimensionId());
14301430
}
14311431
CATCH("Fail in getBlockFromViewVector!");
14321432
}

src/legacy/api/EventAPI.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ void EnableEventListener(int eventId) {
360360
EVENT_TYPES::onUseItemOn,
361361
PlayerClass::newPlayer(&ev.self()),
362362
ItemClass::newItem(&ev.item()),
363-
BlockClass::newBlock(&ev.block().get(), &ev.blockPos(), ev.self().getDimensionId()),
363+
BlockClass::newBlock(ev.block(), ev.blockPos(), ev.self().getDimensionId()),
364364
Number::newNumber((schar)ev.face()),
365365
FloatPos::newPos(ev.clickPos(), ev.self().getDimensionId())
366366
)) {
@@ -686,8 +686,8 @@ void EnableEventListener(int eventId) {
686686
IF_LISTENED(EVENT_TYPES::onBlockChanged) {
687687
CallEvent(
688688
EVENT_TYPES::onBlockChanged,
689-
BlockClass::newBlock(&ev.previousBlock(), &ev.pos(), &ev.blockSource()),
690-
BlockClass::newBlock(&ev.newBlock(), &ev.pos(), &ev.blockSource())
689+
BlockClass::newBlock(ev.previousBlock(), ev.pos(), ev.blockSource()),
690+
BlockClass::newBlock(ev.newBlock(), ev.pos(), ev.blockSource())
691691
); // Not cancellable
692692
}
693693
IF_LISTENED_END(EVENT_TYPES::onBlockChanged);

src/legacy/api/PlayerAPI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3169,7 +3169,7 @@ Local<Value> PlayerClass::getBlockFromViewVector(const Arguments& args) {
31693169
if (bl.isEmpty()) {
31703170
return Local<Value>();
31713171
}
3172-
return BlockClass::newBlock(std::move(&bl), std::move(&bp), &player->getDimensionBlockSource());
3172+
return BlockClass::newBlock(bl, bp, player->getDimensionBlockSource());
31733173
}
31743174
CATCH("Fail in getBlockFromViewVector!");
31753175
}

src/legacy/api/RemoteCallAPI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ Local<Value> _extractValue(std::nullptr_t) { return Local<Value>(); };
101101
Local<Value> _extractValue(std::string*) { return Local<Value>(); };
102102
Local<Value> _extractValue(Player* v) { return PlayerClass::newPlayer(v); };
103103
Local<Value> _extractValue(Actor* v) { return EntityClass::newEntity(v); };
104-
Local<Value> _extractValue(Block* v) { return BlockClass::newBlock(v, &BlockPos::ZERO(), -1); };
104+
Local<Value> _extractValue(Block* v) { return BlockClass::newBlock(*v, BlockPos::ZERO(), -1); };
105105
Local<Value> _extractValue(BlockActor* const& v) { return BlockEntityClass::newBlockEntity(v, -1); };
106106
Local<Value> _extractValue(Container* v) { return ContainerClass::newContainer(v); };
107107
Local<Value> _extractValue(RemoteCall::WorldPosType v) { return FloatPos::newPos(v.pos, v.dimId); };
108108
Local<Value> _extractValue(RemoteCall::BlockPosType v) { return IntPos::newPos(v.pos, v.dimId); };
109109
Local<Value> _extractValue(RemoteCall::BlockType v) {
110-
return BlockClass::newBlock(v.get<Block const*>(), &v.blockPos, v.dimension);
110+
return BlockClass::newBlock(*v.get<Block const*>(), v.blockPos, v.dimension);
111111
};
112112
Local<Value> _extractValue(RemoteCall::NumberType v) { return Number::newNumber(v.get<double>()); };
113113
Local<Value> _extractValue(RemoteCall::ItemType&& v) {

src/lse/events/EventHooks.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ LL_TYPE_INSTANCE_HOOK(
9393
if (!CallEvent(
9494
EVENT_TYPES::onStartDestroyBlock,
9595
PlayerClass::newPlayer(&player),
96-
BlockClass::newBlock(&hitBlock, &blockPos, player.getDimensionId())
96+
BlockClass::newBlock(hitBlock, blockPos, player.getDimensionId())
9797
)) {
9898
return;
9999
}
@@ -1006,7 +1006,7 @@ LL_TYPE_INSTANCE_HOOK(
10061006
EVENT_TYPES::onUseBucketPlace,
10071007
PlayerClass::newPlayer(static_cast<Player*>(placer)),
10081008
ItemClass::newItem(&const_cast<ItemStack&>(instance)),
1009-
BlockClass::newBlock(&contents, &pos, &region),
1009+
BlockClass::newBlock(contents, pos, region),
10101010
Number::newNumber(face),
10111011
FloatPos::newPos(pos, region.getDimensionId())
10121012
)) {
@@ -1032,7 +1032,7 @@ LL_TYPE_INSTANCE_HOOK(
10321032
EVENT_TYPES::onUseBucketTake,
10331033
PlayerClass::newPlayer(&static_cast<Player&>(entity)),
10341034
ItemClass::newItem(&item),
1035-
BlockClass::newBlock(&pos, entity.getDimensionId()),
1035+
BlockClass::newBlock(pos, entity.getDimensionId()),
10361036
Number::newNumber(-1),
10371037
FloatPos::newPos(pos, entity.getDimensionId())
10381038
)) {
@@ -1058,7 +1058,7 @@ LL_TYPE_INSTANCE_HOOK(
10581058
EVENT_TYPES::onUseBucketTake,
10591059
PlayerClass::newPlayer(&static_cast<Player&>(entity)),
10601060
ItemClass::newItem(&item),
1061-
BlockClass::newBlock(&pos, entity.getDimensionId()),
1061+
BlockClass::newBlock(pos, entity.getDimensionId()),
10621062
Number::newNumber(-1),
10631063
FloatPos::newPos(pos, entity.getDimensionId())
10641064
)) {
@@ -1167,7 +1167,7 @@ LL_TYPE_INSTANCE_HOOK(
11671167
if (pos != BlockPos::ZERO() && !this->isAir()) {
11681168
if (!CallEvent(
11691169
EVENT_TYPES::onProjectileHitBlock,
1170-
BlockClass::newBlock(this, &pos, &region),
1170+
BlockClass::newBlock(*this, pos, region),
11711171
EntityClass::newEntity(&const_cast<Actor&>(projectile))
11721172
)) {
11731173
return;

0 commit comments

Comments
 (0)