Skip to content

Commit 0ecbae7

Browse files
committed
feat: Block
1 parent 2a57cf1 commit 0ecbae7

File tree

4 files changed

+90
-27
lines changed

4 files changed

+90
-27
lines changed

src/API/Block/Block.cc

Lines changed: 80 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "Utils/Convert.h"
1414

1515
#include <mc/common/Brightness.h>
16+
#include <mc/deps/core/string/HashedString.h>
1617

1718
using namespace Komomo;
1819

@@ -22,7 +23,6 @@ ClassDefine<BlockClass> blockClassBuilder =
2223

2324
.instanceProperty("allowsRunes", &BlockClass::getAllowsRunes)
2425
.instanceProperty("blockEntityType", &BlockClass::getBlockEntityType)
25-
// .instanceProperty("blockState", &BlockClass::getBlockState)
2626
.instanceProperty("blockTintType", &BlockClass::getBlockTintType)
2727
.instanceProperty("blockItemId", &BlockClass::getBlockItemId)
2828
.instanceProperty("burnOdds", &BlockClass::getBurnOdds)
@@ -80,7 +80,7 @@ ClassDefine<BlockClass> blockClassBuilder =
8080

8181
.InstanceFunction(addAABBs, BlockClass)
8282
// .InstanceFunction(addCollisionShapes, BlockClass)
83-
// .InstanceFunction(addTags, BlockClass)
83+
.InstanceFunction(addTag, BlockClass)
8484
.InstanceFunction(allowStateMismatchOnPlacement, BlockClass)
8585
// .InstanceFunction(asItemInstance, BlockClass)
8686
.InstanceFunction(attack, BlockClass)
@@ -109,6 +109,7 @@ ClassDefine<BlockClass> blockClassBuilder =
109109
// .InstanceFunction(executeItemEvent, BlockClass)
110110
// .InstanceFunction(executeTrigger, BlockClass)
111111
// .InstanceFunction(forEachState, BlockClass)
112+
.InstanceFunction(getBlockState, BlockClass)
112113
// .InstanceFunction(getClientPredictionOverride, BlockClass)
113114
// .InstanceFunction(getCollisionShape, BlockClass)
114115
// .InstanceFunction(getCollisionShapeForCamera, BlockClass)
@@ -126,12 +127,12 @@ ClassDefine<BlockClass> blockClassBuilder =
126127
.InstanceFunction(getSecondPart, BlockClass)
127128
.InstanceFunction(getVisualShape, BlockClass)
128129
// .InstanceFunction(getVisualShapeInWorld, BlockClass)
129-
// .InstanceFunction(hasProperty, BlockClass)
130-
// .InstanceFunction(hasState, BlockClass)
130+
.InstanceFunction(hasProperty, BlockClass)
131+
.InstanceFunction(hasState, BlockClass)
131132
.InstanceFunction(hasTag, BlockClass)
132133
.InstanceFunction(ignoreEntitiesOnPistonMove, BlockClass)
133134
.InstanceFunction(isAttachedTo, BlockClass)
134-
// .InstanceFunction(isFilteredOut, BlockClass)
135+
.InstanceFunction(isFilteredOut, BlockClass)
135136
.InstanceFunction(isObstructingChests, BlockClass)
136137
.InstanceFunction(isPartialBlock, BlockClass)
137138
.InstanceFunction(isPreservingMediumWhenPlaced, BlockClass)
@@ -145,7 +146,7 @@ ClassDefine<BlockClass> blockClassBuilder =
145146
.InstanceFunction(neighborChanged, BlockClass)
146147
.InstanceFunction(onExploded, BlockClass)
147148
.InstanceFunction(onFallOn, BlockClass)
148-
// .InstanceFunction(onFertilized, BlockClass)
149+
.InstanceFunction(onFertilized, BlockClass)
149150
.InstanceFunction(onHitByActivatingAttack, BlockClass)
150151
.InstanceFunction(onLightningHit, BlockClass)
151152
.InstanceFunction(onPlace, BlockClass)
@@ -337,7 +338,16 @@ Local<Value> BlockClass::addAABBs(const Arguments& args) {
337338
// Local<Value> BlockClass::addCollisionShapes(const Arguments& args) { CallVoidMethod(addCollisionShapes, shapes); }
338339

339340
// MCAPI class Block& addTag(class HashedString const& tag);
340-
// Local<Value> BlockClass::addTag(const Arguments& args) { }
341+
Local<Value> BlockClass::addTag(const Arguments& args) {
342+
CheckArgsCount(args, 1);
343+
CheckArgType(args[0], ValueKind::kString);
344+
try {
345+
if (!mBlock) return Local<Value>();
346+
auto tag = HashedString(args[0].asString().toString());
347+
return BlockClass::newBlock(&mBlock->addTag(tag));
348+
}
349+
Catch;
350+
}
341351

342352
Local<Value> BlockClass::allowStateMismatchOnPlacement(const Arguments& args) {
343353
CheckArgsCount(args, 1);
@@ -611,12 +621,21 @@ Local<Value> BlockClass::entityInside(const Arguments& args) {
611621
// ) const;
612622
// Local<Value> BlockClass::executeItemEvent(const Arguments& args) {}
613623

614-
// BlockState not implemented
615624
// MCAPI void forEachState(std::function<bool(class BlockState const&, int)> callback) const;
616625
// Local<Value> BlockClass::forEachState(const Arguments& args) {}
617626

618627
// MCAPI ::BlockState const* getBlockState(::HashedString const& name) const;
619-
// Local<Value> getBlockState(const Arguments& args) {}
628+
Local<Value> BlockClass::getBlockState(const Arguments& args) {
629+
CheckArgsCount(args, 1);
630+
CheckArgType(args[0], ValueKind::kString);
631+
try {
632+
if (!mBlock) return Local<Value>();
633+
auto name = HashedString(args[0].asString().toString());
634+
auto result = const_cast<BlockState*>(mBlock->getBlockState(name));
635+
return BlockStateClass::newBlockState(result);
636+
}
637+
Catch;
638+
}
620639

621640
// MCAPI bool getClientPredictionOverride(::BlockClientPredictionOverrides) const;
622641
// Local<Value> getClientPredictionOverride(const Arguments& args) {}
@@ -771,23 +790,39 @@ Local<Value> BlockClass::getVisualShape(const Arguments& args) {
771790
// getVisualShapeInWorld(::IConstBlockSource const& region, ::BlockPos const& pos, ::AABB& bufferAABB) const;
772791
// Local<Value> BlockClass::getVisualShapeInWorld(const Arguments& args) {}
773792

774-
// BlockProperty not implemented
775793
// MCAPI bool hasProperty(::BlockProperty type) const;
776-
// Local<Value> BlockClass::hasProperty(const Arguments& args) {}
794+
Local<Value> BlockClass::hasProperty(const Arguments& args) {
795+
CheckArgsCount(args, 1);
796+
CheckArgType(args[0], ValueKind::kNumber);
797+
try {
798+
if (!mBlock) return Local<Value>();
799+
auto type = ConvertFromScriptX<BlockProperty>(args[0]);
800+
return Boolean::newBoolean(mBlock->hasProperty(type));
801+
}
802+
Catch;
803+
}
804+
777805

778-
// BlockState not implemented
779806
// MCAPI bool hasState(::BlockState const& stateType) const;
780-
// Local<Value> BlockClass::hasState(const Arguments& args) {}
807+
Local<Value> BlockClass::hasState(const Arguments& args) {
808+
CheckArgsCount(args, 1);
809+
CheckInstanceType(args[0], BlockStateClass);
810+
try {
811+
if (!mBlock) return Local<Value>();
812+
auto state = EngineScope::currentEngine()->getNativeInstance<BlockStateClass>(args[0])->mBlockState;
813+
return Boolean::newBoolean(mBlock->hasState(*state));
814+
}
815+
Catch;
816+
}
781817

782-
// another overload: HashedString not implemented
783818
// MCAPI bool hasTag(::HashedString const& tagName) const;
784819
Local<Value> BlockClass::hasTag(const Arguments& args) {
785820
CheckArgsCount(args, 1);
786-
CheckArgType(args[0], ValueKind::kNumber);
821+
CheckArgType(args[0], ValueKind::kString);
787822
try {
788823
if (!mBlock) return Local<Value>();
789-
auto hash = static_cast<uint64>(args[0].asNumber().toInt64());
790-
return Boolean::newBoolean(mBlock->hasTag(hash));
824+
auto tagName = HashedString(args[0].asString().toString());
825+
return Boolean::newBoolean(mBlock->hasTag(tagName));
791826
}
792827
Catch;
793828
}
@@ -810,9 +845,18 @@ Local<Value> BlockClass::isAttachedTo(const Arguments& args) {
810845
Catch;
811846
}
812847

813-
// BlockRenderLayer not implemented
848+
814849
// MCAPI bool isFilteredOut(::BlockRenderLayer heldItemRenderLayer) const;
815-
// Local<Value> BlockClass::isFaceSturdy(const Arguments& args) {}
850+
Local<Value> BlockClass::isFilteredOut(const Arguments& args) {
851+
CheckArgsCount(args, 1);
852+
CheckArgType(args[0], ValueKind::kNumber);
853+
try {
854+
if (!mBlock) return Local<Value>();
855+
auto heldItemRenderLayer = ConvertFromScriptX<BlockRenderLayer>(args[0]);
856+
return Boolean::newBoolean(mBlock->isFilteredOut(heldItemRenderLayer));
857+
}
858+
Catch;
859+
}
816860

817861
Local<Value> BlockClass::isObstructingChests(const Arguments& args) {
818862
CheckArgsCount(args, 2);
@@ -1012,10 +1056,25 @@ Local<Value> BlockClass::onFallOn(const Arguments& args) {
10121056
CatchReturn(Boolean::newBoolean(false));
10131057
}
10141058

1015-
// FertilizerType not implemented
10161059
// MCAPI bool
10171060
// onFertilized(::BlockSource& region, ::BlockPos const& pos, ::Actor* entity, ::FertilizerType fType) const;
1018-
// Local<Value> BlockClass::onFertilized(const Arguments& args) {}
1061+
Local<Value> BlockClass::onFertilized(const Arguments& args) {
1062+
CheckArgsCount(args, 4);
1063+
CheckInstanceType(args[0], BlockSourceClass);
1064+
CheckInstanceType(args[1], BlockPosClass);
1065+
CheckInstanceType(args[2], ActorClass);
1066+
CheckArgType(args[3], ValueKind::kNumber);
1067+
try {
1068+
if (!mBlock) return Local<Value>();
1069+
auto engine = EngineScope::currentEngine();
1070+
auto region = engine->getNativeInstance<BlockSourceClass>(args[0])->mBlockSource;
1071+
auto pos = engine->getNativeInstance<BlockPosClass>(args[1])->mBlockPos;
1072+
auto entity = engine->getNativeInstance<ActorClass>(args[2])->mActor;
1073+
auto fertilizerId = ConvertFromScriptX<FertilizerType>(args[3]);
1074+
return Boolean::newBoolean(mBlock->onFertilized(*region, pos, entity, fertilizerId));
1075+
}
1076+
Catch;
1077+
}
10191078

10201079
Local<Value> BlockClass::onHitByActivatingAttack(const Arguments& args) {
10211080
CheckArgsCountReturn(args, 3, Boolean::newBoolean(false));

src/API/Block/Block.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class BlockClass : public ScriptClass {
7676
public: /* Method */
7777
Local<Value> addAABBs(const Arguments& args);
7878
// Local<Value> addCollisionShapes(const Arguments& args);
79-
// Local<Value> addTag(const Arguments& args);
79+
Local<Value> addTag(const Arguments& args);
8080
Local<Value> allowStateMismatchOnPlacement(const Arguments& args);
8181
// Local<Value> asItemInstance(const Arguments& args);
8282
Local<Value> attack(const Arguments& args);
@@ -105,7 +105,7 @@ class BlockClass : public ScriptClass {
105105
// Local<Value> executeItemEvent(const Arguments& args);
106106
// Local<Value> executeTrigger(const Arguments& args);
107107
// Local<Value> forEachState(const Arguments& args);
108-
// Local<Value> getBlockState(const Arguments& args);
108+
Local<Value> getBlockState(const Arguments& args);
109109
// Local<Value> getClientPredictionOverride(const Arguments& args);
110110
// Local<Value> getCollisionShape(const Arguments& args);
111111
// Local<Value> getCollisionShapeForCamera(const Arguments& args);
@@ -123,12 +123,12 @@ class BlockClass : public ScriptClass {
123123
Local<Value> getSecondPart(const Arguments& args);
124124
Local<Value> getVisualShape(const Arguments& args);
125125
// Local<Value> getVisualShapeInWorld(const Arguments& args);
126-
// Local<Value> hasProperty(const Arguments& args);
127-
// Local<Value> hasState(const Arguments& args);
126+
Local<Value> hasProperty(const Arguments& args);
127+
Local<Value> hasState(const Arguments& args);
128128
Local<Value> hasTag(const Arguments& args);
129129
Local<Value> ignoreEntitiesOnPistonMove();
130130
Local<Value> isAttachedTo(const Arguments& args);
131-
// Local<Value> isFilteredOut(const Arguments& args);
131+
Local<Value> isFilteredOut(const Arguments& args);
132132
Local<Value> isObstructingChests(const Arguments& args);
133133
Local<Value> isPartialBlock(const Arguments& args);
134134
Local<Value> isPreservingMediumWhenPlaced(const Arguments& args);
@@ -142,7 +142,7 @@ class BlockClass : public ScriptClass {
142142
Local<Value> neighborChanged(const Arguments& args);
143143
Local<Value> onExploded(const Arguments& args);
144144
Local<Value> onFallOn(const Arguments& args);
145-
// Local<Value> onFertilized(const Arguments& args);
145+
Local<Value> onFertilized(const Arguments& args);
146146
Local<Value> onHitByActivatingAttack(const Arguments& args);
147147
Local<Value> onLightningHit(const Arguments& args);
148148
Local<Value> onPlace(const Arguments& args);

src/API/Enum/EnumBuilder.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ void RegisterEnum(ScriptEngine* engine) {
199199
REGISTER_ENUM_MACRO(BlockEnum, BlockActorType, "BlockActorType");
200200
REGISTER_ENUM_MACRO(BlockEnum, BlockSupportType, "BlockSupportType");
201201
REGISTER_ENUM_MACRO(BlockEnum, BlockTintType, "BlockTintType");
202+
REGISTER_ENUM_MACRO(BlockEnum, BlockProperty, "BlockProperty");
203+
REGISTER_ENUM_MACRO(BlockEnum, BlockRenderLayer, "BlockRenderLayer");
202204
REGISTER_ENUM_MACRO(BlockEnum, MaterialType, "MaterialType");
203205
engine->set("BlockEnum", ConvertToScriptX(BlockEnum));
204206

src/API/Enum/EnumInclude.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@
3838
#include <mc/world/actor/TypeExecutingEvent.h>
3939

4040
#include <mc/world/level/block/actor/BlockActorType.h>
41+
#include <mc/world/level/block/BlockRenderLayer.h>
4142
#include <mc/world/level/block/BlockSupportType.h>
4243
#include <mc/world/level/block/BlockTintType.h>
44+
#include <mc/world/level/block/BlockProperty.h>
4345
#include <mc/world/level/material/MaterialType.h>
4446

4547
#include <mc/world/actor/player/AbilitiesIndex.h>

0 commit comments

Comments
 (0)