MCPcopy Create free account
hub / github.com/cuberite/cuberite / SaveToDisk

Method SaveToDisk

src/Entities/Player.cpp:1645–1705  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1643
1644
1645bool cPlayer::SaveToDisk()
1646{
1647 cFile::CreateFolder(FILE_IO_PREFIX + AString("players"));
1648
1649 // create the JSON data
1650 Json::Value JSON_PlayerPosition;
1651 JSON_PlayerPosition.append(Json::Value(GetPosX()));
1652 JSON_PlayerPosition.append(Json::Value(GetPosY()));
1653 JSON_PlayerPosition.append(Json::Value(GetPosZ()));
1654
1655 Json::Value JSON_PlayerRotation;
1656 JSON_PlayerRotation.append(Json::Value(GetYaw()));
1657 JSON_PlayerRotation.append(Json::Value(GetPitch()));
1658 JSON_PlayerRotation.append(Json::Value(GetRoll()));
1659
1660 Json::Value JSON_Inventory;
1661 m_Inventory.SaveToJson(JSON_Inventory);
1662
1663 Json::Value root;
1664 root["position"] = JSON_PlayerPosition;
1665 root["rotation"] = JSON_PlayerRotation;
1666 root["inventory"] = JSON_Inventory;
1667 root["health"] = m_Health;
1668 root["xpTotal"] = m_LifetimeTotalXp;
1669 root["xpCurrent"] = m_CurrentXp;
1670 root["air"] = m_AirLevel;
1671 root["food"] = m_FoodLevel;
1672 root["foodSaturation"] = m_FoodSaturationLevel;
1673 root["foodTickTimer"] = m_FoodTickTimer;
1674 root["foodExhaustion"] = m_FoodExhaustionLevel;
1675 root["world"] = GetWorld()->GetName();
1676 root["isflying"] = IsFlying();
1677
1678 if (m_GameMode == GetWorld()->GetGameMode())
1679 {
1680 root["gamemode"] = (int) eGameMode_NotSet;
1681 }
1682 else
1683 {
1684 root["gamemode"] = (int) m_GameMode;
1685 }
1686
1687 Json::StyledWriter writer;
1688 std::string JsonData = writer.write(root);
1689
1690 AString SourceFile;
1691 Printf(SourceFile, "players/%s.json", m_PlayerName.c_str() );
1692
1693 cFile f;
1694 if (!f.Open(SourceFile, cFile::fmWrite))
1695 {
1696 LOGERROR("ERROR WRITING PLAYER \"%s\" TO FILE \"%s\" - cannot open file", m_PlayerName.c_str(), SourceFile.c_str());
1697 return false;
1698 }
1699 if (f.Write(JsonData.c_str(), JsonData.size()) != (int)JsonData.size())
1700 {
1701 LOGERROR("ERROR WRITING PLAYER JSON TO FILE \"%s\"", SourceFile.c_str());
1702 return false;

Callers

nothing calls this directly

Calls 11

PrintfFunction · 0.85
LOGERRORFunction · 0.85
writeMethod · 0.80
c_strMethod · 0.80
sizeMethod · 0.80
ValueClass · 0.50
SaveToJsonMethod · 0.45
GetNameMethod · 0.45
GetGameModeMethod · 0.45
OpenMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected