Skip to content

Commit

Permalink
refactor: move methods from ItemRegistry to ItemRegistryRef
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Aug 29, 2024
1 parent b91932f commit ce23437
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 26 deletions.
13 changes: 6 additions & 7 deletions include/bedrock/world/item/registry/item_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,23 @@
#include "bedrock/core/hashed_string.h"
#include "bedrock/core/memory.h"

class ItemRegistryRef;

class ItemRegistry : public std::enable_shared_from_this<ItemRegistry> {
public:
[[nodiscard]] WeakPtr<Item> getItem(int id) const
{
return id_to_item_map_.at(id);
}

[[nodiscard]] int getItemCount() const
[[nodiscard]] WeakPtr<Item> getItem(const HashedString &name) const
{
return items_.size();
}

[[nodiscard]] const std::unordered_map<HashedString, WeakPtr<Item>> &getNameToItemMap() const
{
return name_to_item_map_;
return name_to_item_map_.at(name);
}

private:
friend ItemRegistryRef;

std::vector<SharedPtr<Item>> items_; // +16
std::unordered_map<int, WeakPtr<Item>> id_to_item_map_; // +40
std::unordered_map<HashedString, WeakPtr<Item>> name_to_item_map_; // +104 (+80)
Expand Down
11 changes: 10 additions & 1 deletion include/bedrock/world/item/registry/item_registry_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@

#include <memory>

#include "bedrock/core/hashed_string.h"
#include "bedrock/world/item/item.h"
#include "bedrock/world/item/registry/item_registry.h"

class ItemRegistryRef {
public:
std::weak_ptr<ItemRegistry> weak_registry;
[[nodiscard]] WeakPtr<Item> getItem(const HashedString &name) const;
[[nodiscard]] WeakPtr<Item> getItem(int id) const;
[[nodiscard]] const std::unordered_map<HashedString, WeakPtr<Item>> &getNameToItemMap() const;

private:
[[nodiscard]] std::shared_ptr<ItemRegistry> _lockRegistry() const; // NOLINT

std::weak_ptr<ItemRegistry> weak_registry_;
};
33 changes: 15 additions & 18 deletions src/endstone_devtools/vanilla_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@

#include "endstone/detail/devtools/vanilla_data.h"

#include <entt/entt.hpp>
#include <magic_enum/magic_enum.hpp>

#include "bedrock/common/util/bytes_data_output.h"
#include "bedrock/nbt/nbt_io.h"
#include "bedrock/network/packet/crafting_data_packet.h"
#include "bedrock/world/item/registry/creative_item_registry.h"
#include "bedrock/world/level/dimension/vanilla_dimensions.h"
#include "bedrock/world/level/block/actor/furnace_block_actor.h"
#include "bedrock/world/level/dimension/vanilla_dimensions.h"
#include "endstone/detail/base64.h"
#include "endstone/detail/devtools/imgui/imgui_json.h"
#include "endstone/detail/level/level.h"
Expand Down Expand Up @@ -116,7 +115,7 @@ void dumpBlockData(VanillaData &data, ::Level &level)
{
auto overworld = level.getDimension(VanillaDimensions::Overworld);
auto &region = overworld->getBlockSourceFromMainChunkSource();
auto item_registry = level.getItemRegistry().weak_registry.lock();
auto item_registry = level.getItemRegistry();

BlockTypeRegistry::forEachBlock([&](const BlockLegacy &block_legacy) {
const auto &material = block_legacy.getMaterial();
Expand Down Expand Up @@ -154,7 +153,7 @@ void dumpBlockData(VanillaData &data, ::Level &level)
}

nlohmann::json special_tools;
for (const auto &[key, item] : item_registry->getNameToItemMap()) {
for (const auto &[key, item] : item_registry.getNameToItemMap()) {
if (item->canDestroySpecial(*block_legacy.getDefaultState())) {
special_tools.push_back(item->getFullItemName());
}
Expand Down Expand Up @@ -200,8 +199,8 @@ void dumpBlockData(VanillaData &data, ::Level &level)

void dumpItemData(VanillaData &data, ::Level &level)
{
auto item_registry = level.getItemRegistry().weak_registry.lock();
for (const auto &[key, item] : item_registry->getNameToItemMap()) {
auto item_registry = level.getItemRegistry();
for (const auto &[key, item] : item_registry.getNameToItemMap()) {
const auto &name = item->getFullItemName();
nlohmann::json tags;
for (const auto &tag : item->getTags()) {
Expand All @@ -214,17 +213,15 @@ void dumpItemData(VanillaData &data, ::Level &level)
data.item_tags[tag_name].push_back(name);
}

data.items[name] = {
{"id", item->getId()},
{"attackDamage", item->getAttackDamage()},
{"armorValue", item->getArmorValue()},
{"toughnessValue", item->getToughnessValue()},
{"maxDamage", item->getMaxDamage()},
{"isDamageable", item->isDamageable()},
{"maxStackSize", item->getMaxStackSize({})},
{"furnaceBurnDuration", FurnaceBlockActor::getBurnDuration(*ItemStack::create(*item), 200)},
{"furnaceXPMultiplier", item->getFurnaceXPmultiplier(nullptr)}
};
data.items[name] = {{"id", item->getId()},
{"attackDamage", item->getAttackDamage()},
{"armorValue", item->getArmorValue()},
{"toughnessValue", item->getToughnessValue()},
{"maxDamage", item->getMaxDamage()},
{"isDamageable", item->isDamageable()},
{"maxStackSize", item->getMaxStackSize({})},
{"furnaceBurnDuration", FurnaceBlockActor::getBurnDuration(*ItemStack::create(*item), 200)},
{"furnaceXPMultiplier", item->getFurnaceXPmultiplier(nullptr)}};
if (!tags.is_null()) {
data.items[name]["tags"] = tags;
}
Expand Down Expand Up @@ -278,7 +275,7 @@ void dumpRecipes(VanillaData &data, ::Level &level)
{
auto packet = CraftingDataPacket::prepareFromRecipes(level.getRecipes(), false);
auto id_to_name = [&level](int id) {
return level.getItemRegistry().weak_registry.lock()->getItem(id)->getFullItemName();
return level.getItemRegistry().getItem(id)->getFullItemName();
};

for (const auto &entry : packet->crafting_entries) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "bedrock/world/item/registry/item_registry_ref.h"

WeakPtr<Item> ItemRegistryRef::getItem(const HashedString &name) const
{
return _lockRegistry()->name_to_item_map_.at(name);
}

WeakPtr<Item> ItemRegistryRef::getItem(int id) const
{
return _lockRegistry()->id_to_item_map_.at(id);
}

const std::unordered_map<HashedString, WeakPtr<Item>> &ItemRegistryRef::getNameToItemMap() const
{
return _lockRegistry()->name_to_item_map_;
}

std::shared_ptr<ItemRegistry> ItemRegistryRef::_lockRegistry() const
{
return weak_registry_.lock();
}

0 comments on commit ce23437

Please sign in to comment.