GH-2374 fix missing alternating backgrounds in worlds, add gametype column

This commit is contained in:
Petr Mrázek 2018-12-11 23:53:14 +01:00
parent de568b32b8
commit 13b293dd65
5 changed files with 76 additions and 1 deletions

View File

@ -30,6 +30,26 @@
#include <quazipfile.h> #include <quazipfile.h>
#include <quazipdir.h> #include <quazipdir.h>
#include <QCoreApplication>
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 <nbt::tag_compound> parseLevelDat(QByteArray data) std::unique_ptr <nbt::tag_compound> parseLevelDat(QByteArray data)
{ {
QByteArray output; 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<nbt::tag_int>();
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) void World::loadFromLevelDat(QByteArray data)
{ {
try try
@ -332,11 +378,14 @@ void World::loadFromLevelDat(QByteArray data)
m_lastPlayed = QDateTime::fromMSecsSinceEpoch(temp); m_lastPlayed = QDateTime::fromMSecsSinceEpoch(temp);
} }
m_gameType = (GameType) read_int(val, "GameType", 0);
m_randomSeed = read_long(val, "RandomSeed", 0); m_randomSeed = read_long(val, "RandomSeed", 0);
qDebug() << "World Name:" << m_actualName; qDebug() << "World Name:" << m_actualName;
qDebug() << "Last Played:" << m_lastPlayed.toString(); qDebug() << "Last Played:" << m_lastPlayed.toString();
qDebug() << "Seed:" << m_randomSeed; qDebug() << "Seed:" << m_randomSeed;
qDebug() << "GameMode:" << m_randomSeed;
} }
catch (const nbt::io::input_error &e) catch (const nbt::io::input_error &e)
{ {

View File

@ -19,6 +19,15 @@
#include "multimc_logic_export.h" #include "multimc_logic_export.h"
enum class GameType
{
Survival,
Creative,
Adventure,
Spectator
};
QString MULTIMC_LOGIC_EXPORT gameTypeToString(GameType type);
class MULTIMC_LOGIC_EXPORT World class MULTIMC_LOGIC_EXPORT World
{ {
public: public:
@ -35,6 +44,10 @@ public:
{ {
return m_lastPlayed; return m_lastPlayed;
} }
GameType gameType() const
{
return m_gameType;
}
int64_t seed() const int64_t seed() const
{ {
return m_randomSeed; return m_randomSeed;
@ -79,5 +92,6 @@ protected:
QDateTime levelDatTime; QDateTime levelDatTime;
QDateTime m_lastPlayed; QDateTime m_lastPlayed;
int64_t m_randomSeed = 0; int64_t m_randomSeed = 0;
GameType m_gameType = GameType::Survival;
bool is_valid = false; bool is_valid = false;
}; };

View File

@ -138,7 +138,7 @@ bool WorldList::deleteWorlds(int first, int last)
int WorldList::columnCount(const QModelIndex &parent) const int WorldList::columnCount(const QModelIndex &parent) const
{ {
return 2; return 3;
} }
QVariant WorldList::data(const QModelIndex &index, int role) const QVariant WorldList::data(const QModelIndex &index, int role) const
@ -161,6 +161,9 @@ QVariant WorldList::data(const QModelIndex &index, int role) const
case NameColumn: case NameColumn:
return world.name(); return world.name();
case GameModeColumn:
return gameTypeToString(world.gameType());
case LastPlayedColumn: case LastPlayedColumn:
return world.lastPlayed(); return world.lastPlayed();
@ -206,6 +209,8 @@ QVariant WorldList::headerData(int section, Qt::Orientation orientation, int rol
{ {
case NameColumn: case NameColumn:
return tr("Name"); return tr("Name");
case GameModeColumn:
return tr("Game Mode");
case LastPlayedColumn: case LastPlayedColumn:
return tr("Last Played"); return tr("Last Played");
default: default:
@ -217,6 +222,8 @@ QVariant WorldList::headerData(int section, Qt::Orientation orientation, int rol
{ {
case NameColumn: case NameColumn:
return tr("The name of the world."); return tr("The name of the world.");
case GameModeColumn:
return tr("Game mode of the world.");
case LastPlayedColumn: case LastPlayedColumn:
return tr("Date and time the world was last played."); return tr("Date and time the world was last played.");
default: default:

View File

@ -33,6 +33,7 @@ public:
enum Columns enum Columns
{ {
NameColumn, NameColumn,
GameModeColumn,
LastPlayedColumn LastPlayedColumn
}; };
@ -42,6 +43,7 @@ public:
FolderRole, FolderRole,
SeedRole, SeedRole,
NameRole, NameRole,
GameModeRole,
LastPlayedRole LastPlayedRole
}; };

View File

@ -126,6 +126,9 @@
<property name="dragDropMode"> <property name="dragDropMode">
<enum>QAbstractItemView::DragDrop</enum> <enum>QAbstractItemView::DragDrop</enum>
</property> </property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="sortingEnabled"> <property name="sortingEnabled">
<bool>true</bool> <bool>true</bool>
</property> </property>