Skip to content

Commit

Permalink
feat: dump shapeless recipe data in DevTools
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jul 2, 2024
1 parent 6063407 commit e08087b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 10 deletions.
69 changes: 66 additions & 3 deletions include/bedrock/world/item/item_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,77 @@

#pragma once

#include <map>
#include <optional>

#include "bedrock/deps/jsoncpp/value.h"
#include "bedrock/nbt/compound_tag.h"

class Item;

class ItemDescriptor {
class BaseDescriptor {};
enum class InternalType;

public:
ItemDescriptor() = default;
struct ItemEntry {
const Item *item;
std::int16_t aux_value;
};

class BaseDescriptor {
public:
[[nodiscard]] virtual std::unique_ptr<ItemDescriptor::BaseDescriptor> clone() const = 0;
[[nodiscard]] virtual bool sameItems(ItemDescriptor::BaseDescriptor const &, bool) const = 0;
[[nodiscard]] virtual bool sameItem(ItemDescriptor::ItemEntry const &, bool) const = 0;
[[nodiscard]] virtual std::string const &getFullName() const = 0;
[[nodiscard]] virtual ItemDescriptor::ItemEntry getItem() const = 0;
[[nodiscard]] virtual bool forEachItemUntil(std::function<bool(Item const &, short)> func) const = 0;
[[nodiscard]] virtual std::map<std::string, std::string> toMap() const = 0;
[[nodiscard]] virtual std::optional<CompoundTag> save() const = 0;
virtual void serialize(Json::Value &val) const = 0;
virtual void serialize(BinaryStream &stream) const = 0;
[[nodiscard]] virtual ItemDescriptor::InternalType getType() const = 0;
[[nodiscard]] virtual bool isValid() const = 0;
[[nodiscard]] virtual std::size_t getHash() const = 0;
[[nodiscard]] virtual bool shouldResolve() const = 0;
[[nodiscard]] virtual std::unique_ptr<ItemDescriptor::BaseDescriptor> resolve() const = 0;
virtual ~BaseDescriptor() = 0;
};

ItemDescriptor() = default;
virtual ~ItemDescriptor() = default;

[[nodiscard]] std::string const &getFullName() const
{
static std::string empty;
if (!impl_) {
return empty;
}
return impl_->getFullName();
}

[[nodiscard]] const Item *getItem() const
{
if (!impl_) {
return nullptr;
}
if (impl_->shouldResolve()) {
impl_ = std::move(impl_->resolve());
}
return impl_->getItem().item;
}

[[nodiscard]] std::int16_t getAuxValue() const
{
if (!impl_) {
return 0x7FFF;
}
if (impl_->shouldResolve()) {
impl_ = std::move(impl_->resolve());
}
return impl_->getItem().aux_value;
}

private:
std::unique_ptr<ItemDescriptor::BaseDescriptor> impl_;
mutable std::unique_ptr<ItemDescriptor::BaseDescriptor> impl_;
};
5 changes: 5 additions & 0 deletions include/bedrock/world/item/item_stack_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ class ItemStackBase {
return block_;
}

[[nodiscard]] std::uint8_t getCount() const // Endstone
{
return count_;
}

private:
inline const static std::string TAG_DISPLAY = "display"; // NOLINT(*-identifier-naming)
inline const static std::string TAG_DISPLAY_NAME = "Name"; // NOLINT(*-identifier-naming)
Expand Down
17 changes: 10 additions & 7 deletions src/endstone_devtools/vanilla_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,24 +255,27 @@ void dumpRecipes(VanillaData &data, ::Level &level)
case ShapelessRecipe: {
nlohmann::json input;
for (const auto &ingredient : entry.recipe->getIngredients()) {
input.push_back({
// {"id"},
// {"count"},
// {"type"},
// {"auxValue"},
}); // TODO: ...
nlohmann::json json = {
{"id", ingredient.getFullName()},
{"count", ingredient.getStackSize()},
};
if (ingredient.getAuxValue() != 0x7FFF) {
json["damage"] = ingredient.getAuxValue();
}
input.push_back(json);
}

nlohmann::json output;
for (const auto &result_item : entry.recipe->getResultItems()) {
output.push_back({
{"id", result_item.getItem()->getFullItemName()},
// {"count", }, // TODO: ...
{"count", result_item.getCount()},
});
}

data.recipes.shapeless.push_back({
{"id", entry.recipe->getRecipeId()},
{"netId", entry.recipe->getNetId().raw_id},
{"input", input},
{"output", output},
{"uuid", entry.recipe->getId().toEndstone().str()},
Expand Down

0 comments on commit e08087b

Please sign in to comment.