Skip to content

Commit b2df344

Browse files
committed
refactor: replace toNbt with getExtras in ItemMeta and update related logic
1 parent 562a5ac commit b2df344

6 files changed

Lines changed: 29 additions & 16 deletions

File tree

include/endstone/inventory/meta/item_meta.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
#include <vector>
2323

2424
#include "endstone/enchantments/enchantment.h"
25-
#include "endstone/nbt/tag.h"
2625

2726
#define ENDSTONE_ITEM_META_TYPE(type) static constexpr auto MetaType = Type::type;
2827

2928
namespace endstone {
29+
namespace core {
30+
class ItemMetaExtras;
31+
}
3032
/**
3133
* @brief Represents the metadata of a generic item.
3234
*/
@@ -219,7 +221,10 @@ class ItemMeta {
219221
*/
220222
[[nodiscard]] virtual std::unique_ptr<ItemMeta> clone() const = 0;
221223

222-
[[nodiscard]] virtual CompoundTag toNbt() const = 0;
224+
/**
225+
* @internal Internal use only.
226+
*/
227+
[[nodiscard]] virtual const core::ItemMetaExtras &getExtras() const = 0;
223228

224229
template <typename T>
225230
requires std::is_base_of_v<ItemMeta, T>

src/bedrock/nbt/compound_tag.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,16 @@ const Int64Tag *CompoundTag::getInt64Tag(StringView name) const
231231
return nullptr;
232232
}
233233

234+
Int64Tag *CompoundTag::getInt64Tag(StringView name)
235+
{
236+
if (auto *tag = get(name); tag) {
237+
if (tag->getId() == Type::Int64) {
238+
return static_cast<Int64Tag *>(tag);
239+
}
240+
}
241+
return nullptr;
242+
}
243+
234244
std::int16_t CompoundTag::getShort(StringView name) const
235245
{
236246
if (const auto *tag = getShortTag(name)) {

src/endstone/core/inventory/inventory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ int EndstoneInventory::getMaxStackSize() const
3232

3333
std::optional<ItemStack> EndstoneInventory::getItem(int index) const
3434
{
35-
auto item = container_.getItem(index);
35+
const auto &item = container_.getItem(index);
3636
if (item.isNull()) {
3737
return std::nullopt;
3838
}

src/endstone/core/inventory/item_factory.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ bool EndstoneItemFactory::isApplicable(const ItemMeta *meta, const ItemTypeId ty
3636
if (meta == nullptr) {
3737
return false;
3838
}
39-
return static_cast<const EndstoneItemMeta &>(*meta).applicableTo(type);
39+
return meta->getExtras().applicableTo(type);
4040
}
4141

42-
static bool equals0(const EndstoneItemMeta &meta1, const EndstoneItemMeta &meta2)
42+
static bool equals0(const ItemMeta &meta1, const ItemMeta &meta2)
4343
{
44-
return meta1.equalsCommon(meta2) && meta1.notUncommon(meta2) && meta2.notUncommon(meta1);
44+
auto &e1 = meta1.getExtras();
45+
auto &e2 = meta2.getExtras();
46+
return e1.equalsCommon(meta2) && e1.notUncommon(meta2) && e2.notUncommon(meta1);
4547
}
4648

4749
bool EndstoneItemFactory::equals(const ItemMeta *meta1, const ItemMeta *meta2) const
@@ -50,12 +52,12 @@ bool EndstoneItemFactory::equals(const ItemMeta *meta1, const ItemMeta *meta2) c
5052
return true;
5153
}
5254
if (meta1 == nullptr) {
53-
return static_cast<const EndstoneItemMeta *>(meta2)->isEmpty();
55+
return meta2->getExtras().isEmpty();
5456
}
5557
if (meta2 == nullptr) {
56-
return static_cast<const EndstoneItemMeta *>(meta1)->isEmpty();
58+
return meta1->getExtras().isEmpty();
5759
}
58-
return equals0(static_cast<const EndstoneItemMeta &>(*meta1), static_cast<const EndstoneItemMeta &>(*meta2));
60+
return equals0(*meta1, *meta2);
5961
}
6062

6163
std::unique_ptr<ItemMeta> EndstoneItemFactory::asMetaFor(const ItemMeta *meta, ItemTypeId type) const

src/endstone/core/inventory/item_stack.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "endstone/core/nbt.h"
2222

2323
namespace endstone::core {
24+
2425
EndstoneItemStack::EndstoneItemStack(const ::ItemStackBase &item) : item_(item) {}
2526

2627
EndstoneItemStack::EndstoneItemStack(const EndstoneItemStack &other) : item_(other.item_) {}
@@ -200,7 +201,7 @@ bool EndstoneItemStack::setItemMeta(ItemStackBase *item, const ItemMeta *meta)
200201
return true;
201202
}
202203

203-
if (const auto &m = static_cast<EndstoneItemMeta &>(*item_meta); !m.isEmpty()) {
204+
if (auto &m = item_meta->getExtras(); !m.isEmpty()) {
204205
auto tag = item->hasUserData() ? item->getUserData()->clone() : std::make_unique<::CompoundTag>();
205206
m.applyToItem(*tag);
206207
item->setUserData(std::move(tag));

src/endstone/core/inventory/meta/item_meta.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,7 @@ class EndstoneItemMetaBase : public Interface, public ItemMetaExtras {
181181

182182
void setRepairCost(int cost) override { repair_cost_ = cost; }
183183

184-
[[nodiscard]] CompoundTag toNbt() const override
185-
{
186-
auto tag = std::make_unique<::CompoundTag>();
187-
applyToItem(*tag);
188-
return nbt::fromMinecraft(*tag).get<CompoundTag>();
189-
}
184+
[[nodiscard]] const ItemMetaExtras &getExtras() const override { return *this; }
190185

191186
[[nodiscard]] bool isEmpty() const override
192187
{

0 commit comments

Comments
 (0)