BlockState: store properties

This commit is contained in:
DankParrot 2020-08-13 18:47:19 -07:00
parent 2db8c240c1
commit 2b06f0c149
3 changed files with 49 additions and 3 deletions

View File

@ -4,21 +4,35 @@
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <functional> #include <functional>
#include <map>
namespace Feather namespace Feather
{ {
class BlockState class BlockState
{ {
friend struct std::hash<BlockState>;
// PLACEHOLDER // PLACEHOLDER
std::string_view m_name; std::string_view m_name;
std::map<std::string_view, std::string_view> m_properties;
public: public:
BlockState(std::string_view name) : m_name(name) {} BlockState(std::string_view name) : m_name(name) {}
inline const std::string_view GetName() const { return m_name; } inline const std::string_view GetName() const { return m_name; }
inline void AddProperty(std::string_view key, std::string_view value) { m_properties.insert({ key, value }); }
inline bool operator ==(const BlockState that) const inline bool operator ==(const BlockState that) const
{ {
return m_name == that.m_name; if (m_name != that.m_name) return false;
for (auto& [key, value] : that.m_properties)
{
if (!m_properties.contains(key) || m_properties.at(key) != value)
return false;
}
return true;
} }
}; };
} }
@ -31,6 +45,13 @@ struct std::hash<Feather::BlockState>
using std::hash; using std::hash;
using std::string_view; using std::string_view;
return hash<string_view>()(state.GetName()); size_t accum = 0;
accum += hash<string_view>()(state.GetName());
// pretty basic hash, sum of all keys and values + block name
for (auto& [key, value] : state.m_properties)
accum += hash<string_view>()(key) + hash<string_view>()(value);
return accum;
} }
}; };

View File

@ -42,13 +42,28 @@ namespace Feather
for (auto& block : doc.GetObject()) for (auto& block : doc.GetObject())
{ {
//Log::Info("{}: {} states", m.name.GetString(), m.value["states"].Size()); // TODO
//if (block.value.HasMember("properties")) {}
//Log::Info("{}: {} states", block.name.GetString(), block.value["states"].Size());
for (auto& stateData : block.value["states"].GetArray()) for (auto& stateData : block.value["states"].GetArray())
{ {
std::string* name = new std::string(block.name.GetString()); std::string* name = new std::string(block.name.GetString());
BlockState state(*name); BlockState state(*name);
if (stateData.HasMember("properties"))
{
for (auto& prop : stateData["properties"].GetObject())
{
std::string* propName = new std::string(prop.name.GetString());
std::string* propValue = new std::string(prop.value.GetString());
state.AddProperty(*propName, *propValue);
}
}
BLOCK_STATES.AddMapping(stateData["id"].GetInt(), state); BLOCK_STATES.AddMapping(stateData["id"].GetInt(), state);
} }
} }
Log::Info("Loaded {} block states.", BLOCK_STATES.Size()); Log::Info("Loaded {} block states.", BLOCK_STATES.Size());

View File

@ -55,6 +55,16 @@ namespace Feather
const char* name = state.Get<StringTag>("Name").GetValue(); const char* name = state.Get<StringTag>("Name").GetValue();
BlockState stateLookup(name); BlockState stateLookup(name);
CompoundTag properties = state.GetCompound("Properties");
if (properties)
{
for (auto& prop : properties)
{
stateLookup.AddProperty(prop.GetName(), prop.GetValueString());
}
}
int id; int id;
// TODO: handle this in IdMapper // TODO: handle this in IdMapper