diff --git a/api/logic/minecraft/World.cpp b/api/logic/minecraft/World.cpp index b39f940e..5da562aa 100644 --- a/api/logic/minecraft/World.cpp +++ b/api/logic/minecraft/World.cpp @@ -30,6 +30,26 @@ #include #include +#include + +QString gameTypeToString(GameType type) +{ + switch (type) + { + case GameType::Survival: + return QCoreApplication::translate("GameType", "Survival"); + case GameType::Creative: + return QCoreApplication::translate("GameType", "Creative"); + case GameType::Adventure: + return QCoreApplication::translate("GameType", "Adventure"); + case GameType::Spectator: + return QCoreApplication::translate("GameType", "Spectator"); + default: + break; + } + return QObject::tr("Unknown"); +} + std::unique_ptr parseLevelDat(QByteArray data) { QByteArray output; @@ -303,6 +323,32 @@ static int64_t read_long (nbt::value& parent, const char * name, const int64_t & } } +static int read_int (nbt::value& parent, const char * name, const int & fallback = 0) +{ + try + { + auto &namedValue = parent.at(name); + if(namedValue.get_type() != nbt::tag_type::Int) + { + return fallback; + } + auto & tag_str = namedValue.as(); + return tag_str.get(); + } + catch (const std::out_of_range &e) + { + // fallback for old world formats + qWarning() << "Int NBT tag" << name << "could not be found. Defaulting to" << fallback; + return fallback; + } + catch (const std::bad_cast &e) + { + // type mismatch + qWarning() << "NBT tag" << name << "could not be converted to int. Defaulting to" << fallback; + return fallback; + } +} + void World::loadFromLevelDat(QByteArray data) { try @@ -332,11 +378,14 @@ void World::loadFromLevelDat(QByteArray data) m_lastPlayed = QDateTime::fromMSecsSinceEpoch(temp); } + m_gameType = (GameType) read_int(val, "GameType", 0); + m_randomSeed = read_long(val, "RandomSeed", 0); qDebug() << "World Name:" << m_actualName; qDebug() << "Last Played:" << m_lastPlayed.toString(); qDebug() << "Seed:" << m_randomSeed; + qDebug() << "GameMode:" << m_randomSeed; } catch (const nbt::io::input_error &e) { diff --git a/api/logic/minecraft/World.h b/api/logic/minecraft/World.h index 2cce85a2..7cb694a7 100644 --- a/api/logic/minecraft/World.h +++ b/api/logic/minecraft/World.h @@ -19,6 +19,15 @@ #include "multimc_logic_export.h" +enum class GameType +{ + Survival, + Creative, + Adventure, + Spectator +}; +QString MULTIMC_LOGIC_EXPORT gameTypeToString(GameType type); + class MULTIMC_LOGIC_EXPORT World { public: @@ -35,6 +44,10 @@ public: { return m_lastPlayed; } + GameType gameType() const + { + return m_gameType; + } int64_t seed() const { return m_randomSeed; @@ -79,5 +92,6 @@ protected: QDateTime levelDatTime; QDateTime m_lastPlayed; int64_t m_randomSeed = 0; + GameType m_gameType = GameType::Survival; bool is_valid = false; }; diff --git a/api/logic/minecraft/WorldList.cpp b/api/logic/minecraft/WorldList.cpp index 79a5bf38..827b2d63 100644 --- a/api/logic/minecraft/WorldList.cpp +++ b/api/logic/minecraft/WorldList.cpp @@ -138,7 +138,7 @@ bool WorldList::deleteWorlds(int first, int last) int WorldList::columnCount(const QModelIndex &parent) const { - return 2; + return 3; } QVariant WorldList::data(const QModelIndex &index, int role) const @@ -161,6 +161,9 @@ QVariant WorldList::data(const QModelIndex &index, int role) const case NameColumn: return world.name(); + case GameModeColumn: + return gameTypeToString(world.gameType()); + case LastPlayedColumn: return world.lastPlayed(); @@ -206,6 +209,8 @@ QVariant WorldList::headerData(int section, Qt::Orientation orientation, int rol { case NameColumn: return tr("Name"); + case GameModeColumn: + return tr("Game Mode"); case LastPlayedColumn: return tr("Last Played"); default: @@ -217,6 +222,8 @@ QVariant WorldList::headerData(int section, Qt::Orientation orientation, int rol { case NameColumn: return tr("The name of the world."); + case GameModeColumn: + return tr("Game mode of the world."); case LastPlayedColumn: return tr("Date and time the world was last played."); default: diff --git a/api/logic/minecraft/WorldList.h b/api/logic/minecraft/WorldList.h index a1cd8f51..433d354b 100644 --- a/api/logic/minecraft/WorldList.h +++ b/api/logic/minecraft/WorldList.h @@ -33,6 +33,7 @@ public: enum Columns { NameColumn, + GameModeColumn, LastPlayedColumn }; @@ -42,6 +43,7 @@ public: FolderRole, SeedRole, NameRole, + GameModeRole, LastPlayedRole }; diff --git a/application/pages/instance/WorldListPage.ui b/application/pages/instance/WorldListPage.ui index 0018ddf3..bc104854 100644 --- a/application/pages/instance/WorldListPage.ui +++ b/application/pages/instance/WorldListPage.ui @@ -126,6 +126,9 @@ QAbstractItemView::DragDrop + + true + true