Skip to content

Commit e2aa7b4

Browse files
committed
fix: fix ItemAPI and BlockAPI
1 parent b5df49d commit e2aa7b4

File tree

5 files changed

+71
-24
lines changed

5 files changed

+71
-24
lines changed

src/legacy/api/BlockAPI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ Local<Value> McClass::getBlock(const Arguments& args) {
484484
if (!lc) {
485485
return {};
486486
}
487-
auto& block = lc->getBlock(ChunkBlockPos{pos.x, pos.z, minHeight});
487+
auto& block = lc->getBlock(ChunkBlockPos{(uchar)pos.x, (uchar)pos.z, minHeight});
488488
return BlockClass::newBlock(block, pos.getBlockPos(), pos.dim);
489489
}
490490
CATCH("Fail in GetBlock!")

src/legacy/api/ItemAPI.cpp

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
#include "api/McAPI.h"
77
#include "api/NbtAPI.h"
88
#include "ll/api/service/Bedrock.h"
9-
#include "mc/nbt/CompoundTag.h"
9+
#include "lse/api/ItemHelper.h"
1010
#include "mc/safety/RedactableString.h"
11-
#include "mc/world/actor/Actor.h"
1211
#include "mc/world/actor/item/ItemActor.h"
1312
#include "mc/world/item/Item.h"
1413
#include "mc/world/item/ItemStack.h"
@@ -20,6 +19,8 @@
2019
#include <variant>
2120
#include <vector>
2221

22+
using lse::api::ItemHelper;
23+
2324
//////////////////// Class Definition ////////////////////
2425

2526
ClassDefine<ItemClass> ItemClassBuilder = defineClass<ItemClass>("LLSE_Item")
@@ -154,7 +155,12 @@ Local<Value> ItemClass::getDamage() {
154155

155156
Local<Value> ItemClass::getAttackDamage() {
156157
try {
157-
return Number::newNumber(get()->getAttackDamage());
158+
auto mItem = get()->mItem;
159+
if (mItem) {
160+
return Number::newNumber(mItem->getAttackDamage());
161+
} else {
162+
return Number::newNumber(0);
163+
}
158164
}
159165
CATCH("Fail in GetAttackDamage!");
160166
}
@@ -232,7 +238,12 @@ Local<Value> ItemClass::isEnchantingBook() {
232238

233239
Local<Value> ItemClass::isFireResistant() {
234240
try {
235-
return Boolean::newBoolean(get()->isFireResistant());
241+
auto mItem = get()->mItem;
242+
if (mItem) {
243+
return Boolean::newBoolean(mItem->mFireResistant);
244+
} else {
245+
return Boolean::newBoolean(false);
246+
}
236247
}
237248
CATCH("Fail in isFireResistant!");
238249
}
@@ -260,7 +271,12 @@ Local<Value> ItemClass::isHorseArmorItem() {
260271

261272
Local<Value> ItemClass::isLiquidClipItem() {
262273
try {
263-
return Boolean::newBoolean(get()->isLiquidClipItem());
274+
auto mItem = get()->mItem;
275+
if (mItem) {
276+
return Boolean::newBoolean(mItem->isLiquidClipItem());
277+
} else {
278+
return Boolean::newBoolean(false);
279+
}
264280
}
265281
CATCH("Fail in isLiquidClipItem!");
266282
}
@@ -274,7 +290,12 @@ Local<Value> ItemClass::isMusicDiscItem() {
274290

275291
Local<Value> ItemClass::isOffhandItem() {
276292
try {
277-
return Boolean::newBoolean(get()->isOffhandItem());
293+
auto mItem = get()->mItem;
294+
if (mItem) {
295+
return Boolean::newBoolean(mItem->mAllowOffhand);
296+
} else {
297+
return Boolean::newBoolean(false);
298+
}
278299
}
279300
CATCH("Fail in isOffhandItem!");
280301
}
@@ -295,7 +316,11 @@ Local<Value> ItemClass::isStackable() {
295316

296317
Local<Value> ItemClass::isWearableItem() {
297318
try {
298-
return Boolean::newBoolean(get()->isHumanoidWearableItem());
319+
if (get()->mItem) {
320+
return Boolean::newBoolean(get()->isHumanoidWearableBlockItem());
321+
} else {
322+
return Boolean::newBoolean(false);
323+
}
299324
}
300325
CATCH("Fail in isWearableItem!");
301326
}
@@ -309,9 +334,9 @@ Local<Value> ItemClass::set(const Arguments& args) {
309334

310335
auto tag = itemNew->save(*SaveContextFactory::createCloneSaveContext());
311336
if (std::holds_alternative<std::unique_ptr<ItemStack>>(item)) {
312-
std::get<std::unique_ptr<ItemStack>>(item)->load(*tag);
337+
ItemHelper::load(*std::get<std::unique_ptr<ItemStack>>(item), *tag);
313338
} else {
314-
std::get<ItemStack*>(item)->load(*tag);
339+
ItemHelper::load(*std::get<ItemStack*>(item), *tag);
315340
}
316341
return Boolean::newBoolean(true);
317342
}
@@ -348,7 +373,7 @@ Local<Value> ItemClass::setAux(const Arguments& args) {
348373
CHECK_ARG_TYPE(args[0], ValueKind::kNumber);
349374

350375
try {
351-
get()->setAuxValue(args[0].asNumber().toInt32());
376+
get()->mAuxValue = args[0].asNumber().toInt32();
352377
return Boolean::newBoolean(true);
353378
}
354379
CATCH("Fail in setAux!");
@@ -378,7 +403,9 @@ Local<Value> ItemClass::setDisplayName(const Arguments& args) {
378403
CHECK_ARG_TYPE(args[0], ValueKind::kString);
379404

380405
try {
381-
get()->setCustomName(Bedrock::Safety::RedactableString(args[0].asString().toString()));
406+
Bedrock::Safety::RedactableString redactableString;
407+
redactableString.mUnredactedString = args[0].asString().toString();
408+
get()->setCustomName(redactableString);
382409
return Boolean::newBoolean(true);
383410
}
384411
CATCH("Fail in setDisplayName!");
@@ -412,8 +439,7 @@ Local<Value> ItemClass::setNbt(const Arguments& args) {
412439
try {
413440
auto nbt = NbtCompoundClass::extract(args[0]);
414441
if (!nbt) return Local<Value>(); // Null
415-
auto itemStack = get();
416-
itemStack->load(*nbt);
442+
ItemHelper::load(*get(), *nbt);
417443
// update Pre Data
418444
preloadData();
419445
return Boolean::newBoolean(true);
@@ -442,13 +468,9 @@ Local<Value> McClass::newItem(const Arguments& args) {
442468
auto nbt = NbtCompoundClass::extract(args[0]);
443469
if (nbt) {
444470
auto newItem = new ItemStack{ItemStack::EMPTY_ITEM()};
445-
newItem->load(*nbt);
446-
if (!newItem) return Local<Value>(); // Null
447-
else
448-
return ItemClass::newItem(
449-
newItem,
450-
false
451-
); // Not managed by BDS, pointer will be saved as unique_ptr
471+
ItemHelper::load(*newItem, *nbt);
472+
return ItemClass::newItem(newItem,
473+
false); // Not managed by BDS, pointer will be saved as unique_ptr
452474
} else {
453475
LOG_WRONG_ARG_TYPE(__FUNCTION__);
454476
return Local<Value>();
@@ -507,7 +529,7 @@ Local<Value> McClass::spawnItem(const Arguments& args) {
507529
// By Item
508530
;
509531
ItemActor* entity = ll::service::getLevel()->getSpawner().spawnItem(
510-
ll::service::getLevel()->getDimension(pos.dim)->getBlockSourceFromMainChunkSource(),
532+
ll::service::getLevel()->getDimension(pos.dim).lock()->getBlockSourceFromMainChunkSource(),
511533
*it,
512534
0,
513535
pos.getVec3(),

src/lse/api/ItemHelper.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "lse/api/ItemHelper.h"
2+
3+
#include "mc/nbt/CompoundTag.h"
4+
#include "mc/world/item/Item.h"
5+
6+
namespace lse::api {
7+
void ItemHelper::load(ItemStack& itemStack, CompoundTag& tag) {
8+
itemStack._loadItem(tag);
9+
auto mItem = itemStack.mItem;
10+
if (mItem) {
11+
mItem->fixupCommon(itemStack);
12+
if (itemStack.getAuxValue() == 0x7FFF) {
13+
itemStack.mAuxValue = 0;
14+
}
15+
}
16+
};
17+
} // namespace lse::api

src/lse/api/ItemHelper.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "mc/world/item/ItemStack.h"
2+
3+
namespace lse::api {
4+
class ItemHelper {
5+
public:
6+
static void load(ItemStack& itemStack, CompoundTag& tag);
7+
};
8+
} // namespace lse::api

xmake.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ add_rules("mode.debug", "mode.release")
33
add_repositories("liteldev-repo https://github.com/LiteLDev/xmake-repo.git")
44

55
if is_config("target_type", "server") then
6-
add_requires("levilamina db34cb99ac8f097bcafde355c1ee703fc8639d68", {configs = {target_type = "server"}})
6+
add_requires("levilamina 12e6017aa0fe8eb3d522c592c7b0653c20a95733", {configs = {target_type = "server"}})
77
else
8-
add_requires("levilamina db34cb99ac8f097bcafde355c1ee703fc8639d68", {configs = {target_type = "client"}})
8+
add_requires("levilamina 12e6017aa0fe8eb3d522c592c7b0653c20a95733", {configs = {target_type = "client"}})
99
end
1010

1111
add_requires("levibuildscript")

0 commit comments

Comments
 (0)