add ItemStack::fields
This commit is contained in:
+31
-5
@@ -3,13 +3,18 @@
|
||||
#include "content/Content.hpp"
|
||||
#include "ItemDef.hpp"
|
||||
|
||||
ItemStack::ItemStack(itemid_t item, itemcount_t count)
|
||||
: item(item), count(count) {
|
||||
ItemStack::ItemStack(itemid_t item, itemcount_t count, dv::value data)
|
||||
: item(item), count(count), fields(std::move(data)) {
|
||||
}
|
||||
|
||||
void ItemStack::set(const ItemStack& item) {
|
||||
set(ItemStack(item));
|
||||
}
|
||||
|
||||
void ItemStack::set(ItemStack&& item) {
|
||||
this->item = item.item;
|
||||
this->count = item.count;
|
||||
this->fields = std::move(item.fields);
|
||||
if (count == 0) {
|
||||
this->item = 0;
|
||||
}
|
||||
@@ -22,14 +27,14 @@ bool ItemStack::accepts(const ItemStack& other) const {
|
||||
if (isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
return item == other.getItemId();
|
||||
return item == other.getItemId() && other.fields == nullptr;
|
||||
}
|
||||
|
||||
void ItemStack::move(ItemStack& item, const ContentIndices& indices) {
|
||||
auto& def = indices.items.require(item.getItemId());
|
||||
itemcount_t count = std::min(item.count, def.stackSize - this->count);
|
||||
if (isEmpty()) {
|
||||
set(ItemStack(item.getItemId(), count));
|
||||
set(ItemStack(item.getItemId(), count, std::move(item.fields)));
|
||||
} else {
|
||||
setCount(this->count + count);
|
||||
}
|
||||
@@ -39,6 +44,27 @@ void ItemStack::move(ItemStack& item, const ContentIndices& indices) {
|
||||
void ItemStack::setCount(itemcount_t count) {
|
||||
this->count = count;
|
||||
if (count == 0) {
|
||||
item = 0;
|
||||
clear();
|
||||
}
|
||||
}
|
||||
|
||||
void ItemStack::setField(std::string_view name, dv::value value) {
|
||||
if (fields == nullptr) {
|
||||
if (value == nullptr) {
|
||||
return;
|
||||
}
|
||||
fields = dv::object();
|
||||
}
|
||||
if (value == nullptr) {
|
||||
fields.erase(std::string(name));
|
||||
return;
|
||||
}
|
||||
fields[std::string(name)] = std::move(value);
|
||||
}
|
||||
|
||||
dv::value* ItemStack::getField(const std::string& name) const {
|
||||
if (fields == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
return fields.at(name).ptr;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user