From 42c36429d7cd1e6c3f879b6749e18e912deefe0d Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 19 May 2014 20:40:56 +0100 Subject: Added client translation to achievements --- src/CompositeChat.cpp | 22 ++++++++++++++++++++++ src/CompositeChat.h | 15 +++++++++++++++ src/Entities/Player.cpp | 8 ++++---- src/Protocol/Protocol17x.cpp | 29 +++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/CompositeChat.cpp b/src/CompositeChat.cpp index c70ef1070..d3b7595b7 100644 --- a/src/CompositeChat.cpp +++ b/src/CompositeChat.cpp @@ -189,6 +189,15 @@ void cCompositeChat::AddSuggestCommandPart(const AString & a_Text, const AString +void cCompositeChat::AddShowAchievementPart(const AString & a_PlayerName, const AString & a_Achievement, const AString & a_Style) +{ + m_Parts.push_back(new cShowAchievementPart(a_PlayerName, a_Achievement, a_Style)); +} + + + + + void cCompositeChat::ParseText(const AString & a_ParseText) { size_t len = a_ParseText.length(); @@ -476,3 +485,16 @@ cCompositeChat::cSuggestCommandPart::cSuggestCommandPart(const AString & a_Text, + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// cCompositeChat::cShowAchievementPart: + +cCompositeChat::cShowAchievementPart::cShowAchievementPart(const AString & a_PlayerName, const AString & a_Achievement, const AString & a_Style) : + super(ptShowAchievement, a_Achievement, a_Style), + m_PlayerName(a_PlayerName) +{ +} + + + + diff --git a/src/CompositeChat.h b/src/CompositeChat.h index 5b9c5f612..a0264d864 100644 --- a/src/CompositeChat.h +++ b/src/CompositeChat.h @@ -38,6 +38,7 @@ public: ptUrl, ptRunCommand, ptSuggestCommand, + ptShowAchievement, } ; class cBasePart @@ -106,6 +107,15 @@ public: public: cSuggestCommandPart(const AString & a_Text, const AString & a_Command, const AString & a_Style = ""); } ; + + class cShowAchievementPart : + public cBasePart + { + typedef cBasePart super; + public: + AString m_PlayerName; + cShowAchievementPart(const AString & a_PlayerName, const AString & a_Achievement, const AString & a_Style = ""); + } ; typedef std::vector cParts; @@ -148,6 +158,11 @@ public: /** Adds a part that suggests a command (enters it into the chat message area, but doesn't send) when clicked. The default style is underlined yellow text. */ void AddSuggestCommandPart(const AString & a_Text, const AString & a_SuggestedCommand, const AString & a_Style = "u@b"); + + /** Adds a part that fully formats a specified achievement using client translatable strings + Takes achievement name and player awarded to. Displays as {player} has earned the achievement {achievement_name}. + */ + void AddShowAchievementPart(const AString & a_PlayerName, const AString & a_Achievement, const AString & a_Style = ""); /** Parses text into various parts, adds those. Recognizes "http:" and "https:" URLs and @color-codes. */ diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index c3b763278..3a32bfb2e 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -1169,8 +1169,8 @@ unsigned int cPlayer::AwardAchievement(const eStatistic a_Ach) { // First time, announce it cCompositeChat Msg; - Msg.AddTextPart(m_PlayerName + " has just earned the achievement "); - Msg.AddTextPart(cStatInfo::GetName(a_Ach)); // TODO 2014-05-12 xdot: Use the proper cCompositeChat part (cAchievement) + Msg.SetMessageType(mtSuccess); + Msg.AddShowAchievementPart(GetName(), cStatInfo::GetName(a_Ach)); m_World->BroadcastChat(Msg); // Increment the statistic @@ -1788,7 +1788,7 @@ bool cPlayer::SaveToDisk() // Save the player stats. // We use the default world name (like bukkit) because stats are shared between dimensions/worlds. - cStatSerializer StatSerializer(cRoot::Get()->GetDefaultWorld()->GetName(), m_PlayerName, &m_Stats); + cStatSerializer StatSerializer(cRoot::Get()->GetDefaultWorld()->GetName(), GetName(), &m_Stats); if (!StatSerializer.Save()) { LOGERROR("Could not save stats for player %s", m_PlayerName.c_str()); @@ -1963,7 +1963,7 @@ void cPlayer::UpdateMovementStats(const Vector3d & a_DeltaPos) BLOCKTYPE Block; NIBBLETYPE Meta; - if (!m_World->GetBlockTypeMeta(PosX, PosY, PosZ, Block, Meta)) + if ((PosY < 0) || (PosY > cChunkDef::Height) || !m_World->GetBlockTypeMeta(PosX, PosY, PosZ, Block, Meta)) { return; } diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 39feee16f..3061d132b 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -289,6 +289,35 @@ void cProtocol172::SendChat(const cCompositeChat & a_Message) AddChatPartStyle(Part, p.m_Style); break; } + + case cCompositeChat::ptShowAchievement: + { + const cCompositeChat::cShowAchievementPart & p = (const cCompositeChat::cShowAchievementPart &)**itr; + Part["translate"] = "chat.type.achievement"; + + Json::Value Ach; + Ach["action"] = "show_achievement"; + Ach["value"] = p.m_Text; + + Json::Value AchColourAndName; + AchColourAndName["color"] = "green"; + AchColourAndName["translate"] = p.m_Text; + AchColourAndName["hoverEvent"] = Ach; + + Json::Value Extra; + Extra.append(AchColourAndName); + + Json::Value Name; + Name["text"] = p.m_PlayerName; + + Json::Value With; + With.append(Name); + With.append(Extra); + + Part["with"] = With; + AddChatPartStyle(Part, p.m_Style); + break; + } } msg["extra"].append(Part); } // for itr - Parts[] -- cgit v1.2.3 From 3a9543178abb7500aff65e09f054c28200ca6cac Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 19 May 2014 21:16:29 +0100 Subject: cCompositeChat message type is now formatted --- src/ClientHandle.h | 4 ++-- src/CompositeChat.cpp | 3 ++- src/CompositeChat.h | 9 ++++++++- src/Protocol/Protocol17x.cpp | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/ClientHandle.h b/src/ClientHandle.h index 9f1245be5..659c67658 100644 --- a/src/ClientHandle.h +++ b/src/ClientHandle.h @@ -80,9 +80,9 @@ public: static AString GenerateOfflineUUID(const AString & a_Username); // tolua_export /** Formats the type of message with the proper color and prefix for sending to the client. **/ - AString FormatMessageType(bool ShouldAppendChatPrefixes, eMessageType a_ChatPrefix, const AString & a_AdditionalData); + static AString FormatMessageType(bool ShouldAppendChatPrefixes, eMessageType a_ChatPrefix, const AString & a_AdditionalData); - AString FormatChatPrefix(bool ShouldAppendChatPrefixes, AString a_ChatPrefixS, AString m_Color1, AString m_Color2); + static AString FormatChatPrefix(bool ShouldAppendChatPrefixes, AString a_ChatPrefixS, AString m_Color1, AString m_Color2); void Kick(const AString & a_Reason); // tolua_export void Authenticate(const AString & a_Name, const AString & a_UUID); // Called by cAuthenticator when the user passes authentication diff --git a/src/CompositeChat.cpp b/src/CompositeChat.cpp index d3b7595b7..a3612983a 100644 --- a/src/CompositeChat.cpp +++ b/src/CompositeChat.cpp @@ -299,9 +299,10 @@ void cCompositeChat::ParseText(const AString & a_ParseText) -void cCompositeChat::SetMessageType(eMessageType a_MessageType) +void cCompositeChat::SetMessageType(eMessageType a_MessageType, const AString & a_AdditionalMessageTypeData) { m_MessageType = a_MessageType; + m_AdditionalMessageTypeData = a_AdditionalMessageTypeData; } diff --git a/src/CompositeChat.h b/src/CompositeChat.h index a0264d864..209d9d925 100644 --- a/src/CompositeChat.h +++ b/src/CompositeChat.h @@ -47,6 +47,7 @@ public: ePartType m_PartType; AString m_Text; AString m_Style; + AString m_AdditionalStyleData; cBasePart(ePartType a_PartType, const AString & a_Text, const AString & a_Style = ""); @@ -169,7 +170,7 @@ public: void ParseText(const AString & a_ParseText); /** Sets the message type, which is indicated by prefixes added to the message when serializing. */ - void SetMessageType(eMessageType a_MessageType); + void SetMessageType(eMessageType a_MessageType, const AString & a_AdditionalMessageTypeata = ""); /** Adds the "underline" style to each part that is an URL. */ void UnderlineUrls(void); @@ -178,6 +179,9 @@ public: /** Returns the message type set previously by SetMessageType(). */ eMessageType GetMessageType(void) const { return m_MessageType; } + + /** Returns additional data pertaining to message type, for example, the name of a mtPrivateMsg sender */ + AString GetAdditionalMessageTypeData(void) const { return m_AdditionalMessageTypeData; } /** Returns the text from the parts that comprises the human-readable data. Used for older protocols that don't support composite chat @@ -199,6 +203,9 @@ protected: /** The message type, as indicated by prefixes. */ eMessageType m_MessageType; + /** Additional data pertaining to message type, for example, the name of a mtPrivateMsg sender */ + AString m_AdditionalMessageTypeData; + /** Adds a_AddStyle to a_Style; overwrites the existing style if appropriate. If the style already contains something that a_AddStyle overrides, it is erased first. */ diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 3061d132b..7c526d103 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -234,7 +234,7 @@ void cProtocol172::SendChat(const cCompositeChat & a_Message) // Compose the complete Json string to send: Json::Value msg; - msg["text"] = ""; // The client crashes without this + msg["text"] = cClientHandle::FormatMessageType(m_Client->GetPlayer()->GetWorld()->ShouldUseChatPrefixes(), a_Message.GetMessageType(), a_Message.GetAdditionalMessageTypeData()); // The client crashes without this field being present const cCompositeChat::cParts & Parts = a_Message.GetParts(); for (cCompositeChat::cParts::const_iterator itr = Parts.begin(), end = Parts.end(); itr != end; ++itr) { -- cgit v1.2.3 From 4008af692556ffb0f819c9a570267ffbaa871cad Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 19 May 2014 21:17:28 +0100 Subject: Simplified cacti conditions --- src/Entities/Entity.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index 15f456332..ed06e76b9 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -1024,9 +1024,9 @@ void cEntity::DetectCacti() float w = m_Width / 2; if ( (((X + 1) - GetPosX() < w) && (GetWorld()->GetBlock(X + 1, Y, Z) == E_BLOCK_CACTUS)) || - (((GetPosX() - (X - 1)) - 1 < w) && (GetWorld()->GetBlock(X - 1, Y, Z) == E_BLOCK_CACTUS)) || + ((GetPosX() - X < w) && (GetWorld()->GetBlock(X - 1, Y, Z) == E_BLOCK_CACTUS)) || (((Z + 1) - GetPosZ() < w) && (GetWorld()->GetBlock(X, Y, Z + 1) == E_BLOCK_CACTUS)) || - (((GetPosZ() - (Z - 1)) - 1 < w) && (GetWorld()->GetBlock(X, Y, Z - 1) == E_BLOCK_CACTUS)) || + ((GetPosZ() - Z < w) && (GetWorld()->GetBlock(X, Y, Z - 1) == E_BLOCK_CACTUS)) || (((Y > 0) && (Y < cChunkDef::Height)) && ((GetPosY() - Y < 1) && (GetWorld()->GetBlock(X, Y, Z) == E_BLOCK_CACTUS))) ) { -- cgit v1.2.3 From 2bfe962e2831316f651aa22c4e7b7ea68021c978 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 19 May 2014 22:15:39 +0100 Subject: Fixed a cactus Y position issue --- src/Entities/Entity.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index ed06e76b9..8a584d2ca 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -1023,11 +1023,12 @@ void cEntity::DetectCacti() int X = POSX_TOINT, Y = POSY_TOINT, Z = POSZ_TOINT; float w = m_Width / 2; if ( - (((X + 1) - GetPosX() < w) && (GetWorld()->GetBlock(X + 1, Y, Z) == E_BLOCK_CACTUS)) || + ((Y > 0) && (Y < cChunkDef::Height)) && + ((((X + 1) - GetPosX() < w) && (GetWorld()->GetBlock(X + 1, Y, Z) == E_BLOCK_CACTUS)) || ((GetPosX() - X < w) && (GetWorld()->GetBlock(X - 1, Y, Z) == E_BLOCK_CACTUS)) || (((Z + 1) - GetPosZ() < w) && (GetWorld()->GetBlock(X, Y, Z + 1) == E_BLOCK_CACTUS)) || ((GetPosZ() - Z < w) && (GetWorld()->GetBlock(X, Y, Z - 1) == E_BLOCK_CACTUS)) || - (((Y > 0) && (Y < cChunkDef::Height)) && ((GetPosY() - Y < 1) && (GetWorld()->GetBlock(X, Y, Z) == E_BLOCK_CACTUS))) + (((GetPosY() - Y < 1) && (GetWorld()->GetBlock(X, Y, Z) == E_BLOCK_CACTUS)))) ) { TakeDamage(dtCactusContact, NULL, 1, 0); -- cgit v1.2.3 From 507df718453483e6ca74ca897a78eed679222f06 Mon Sep 17 00:00:00 2001 From: Alexander Harkness Date: Tue, 20 May 2014 13:35:39 +0100 Subject: Update Player.cpp --- src/Entities/Player.cpp | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index 3a32bfb2e..c27456397 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -377,7 +377,7 @@ short cPlayer::DeltaExperience(short a_Xp_delta) } LOGD("Player \"%s\" gained/lost %d experience, total is now: %d", - m_PlayerName.c_str(), a_Xp_delta, m_CurrentXp); + GetName().c_str(), a_Xp_delta, m_CurrentXp); // Set experience to be updated m_bDirtyExperience = true; @@ -391,7 +391,7 @@ short cPlayer::DeltaExperience(short a_Xp_delta) void cPlayer::StartChargingBow(void) { - LOGD("Player \"%s\" started charging their bow", m_PlayerName.c_str()); + LOGD("Player \"%s\" started charging their bow", GetName().c_str()); m_IsChargingBow = true; m_BowCharge = 0; } @@ -402,7 +402,7 @@ void cPlayer::StartChargingBow(void) int cPlayer::FinishChargingBow(void) { - LOGD("Player \"%s\" finished charging their bow at a charge of %d", m_PlayerName.c_str(), m_BowCharge); + LOGD("Player \"%s\" finished charging their bow at a charge of %d", GetName().c_str(), m_BowCharge); int res = m_BowCharge; m_IsChargingBow = false; m_BowCharge = 0; @@ -415,7 +415,7 @@ int cPlayer::FinishChargingBow(void) void cPlayer::CancelChargingBow(void) { - LOGD("Player \"%s\" cancelled charging their bow at a charge of %d", m_PlayerName.c_str(), m_BowCharge); + LOGD("Player \"%s\" cancelled charging their bow at a charge of %d", GetName().c_str(), m_BowCharge); m_IsChargingBow = false; m_BowCharge = 0; } @@ -1305,7 +1305,7 @@ void cPlayer::AddToGroup( const AString & a_GroupName ) { cGroup* Group = cRoot::Get()->GetGroupManager()->GetGroup( a_GroupName ); m_Groups.push_back( Group ); - LOGD("Added %s to group %s", m_PlayerName.c_str(), a_GroupName.c_str() ); + LOGD("Added %s to group %s", GetName().c_str(), a_GroupName.c_str() ); ResolveGroups(); ResolvePermissions(); } @@ -1329,13 +1329,13 @@ void cPlayer::RemoveFromGroup( const AString & a_GroupName ) if( bRemoved ) { - LOGD("Removed %s from group %s", m_PlayerName.c_str(), a_GroupName.c_str() ); + LOGD("Removed %s from group %s", GetName().c_str(), a_GroupName.c_str() ); ResolveGroups(); ResolvePermissions(); } else { - LOGWARN("Tried to remove %s from group %s but was not in that group", m_PlayerName.c_str(), a_GroupName.c_str() ); + LOGWARN("Tried to remove %s from group %s but was not in that group", GetName().c_str(), a_GroupName.c_str() ); } } @@ -1441,7 +1441,7 @@ void cPlayer::ResolveGroups() if( AllGroups.find( CurrentGroup ) != AllGroups.end() ) { LOGWARNING("ERROR: Player \"%s\" is in the group multiple times (\"%s\"). Please fix your settings in users.ini!", - m_PlayerName.c_str(), CurrentGroup->GetName().c_str() + GetName().c_str(), CurrentGroup->GetName().c_str() ); } else @@ -1453,7 +1453,7 @@ void cPlayer::ResolveGroups() { if( AllGroups.find( *itr ) != AllGroups.end() ) { - LOGERROR("ERROR: Player %s is in the same group multiple times due to inheritance (%s). FIX IT!", m_PlayerName.c_str(), (*itr)->GetName().c_str() ); + LOGERROR("ERROR: Player %s is in the same group multiple times due to inheritance (%s). FIX IT!", GetName().c_str(), (*itr)->GetName().c_str() ); continue; } ToIterate.push_back( *itr ); @@ -1605,19 +1605,19 @@ void cPlayer::LoadPermissionsFromDisk() cIniFile IniFile; if (IniFile.ReadFile("users.ini")) { - AString Groups = IniFile.GetValueSet(m_PlayerName, "Groups", "Default"); + AString Groups = IniFile.GetValueSet(GetName(), "Groups", "Default"); AStringVector Split = StringSplitAndTrim(Groups, ","); for (AStringVector::const_iterator itr = Split.begin(), end = Split.end(); itr != end; ++itr) { if (!cRoot::Get()->GetGroupManager()->ExistsGroup(*itr)) { - LOGWARNING("The group %s for player %s was not found!", itr->c_str(), m_PlayerName.c_str()); + LOGWARNING("The group %s for player %s was not found!", itr->c_str(), GetName().c_str()); } AddToGroup(*itr); } - AString Color = IniFile.GetValue(m_PlayerName, "Color", "-"); + AString Color = IniFile.GetValue(GetName(), "Color", "-"); if (!Color.empty()) { m_Color = Color[0]; @@ -1626,7 +1626,7 @@ void cPlayer::LoadPermissionsFromDisk() else { cGroupManager::GenerateDefaultUsersIni(IniFile); - IniFile.AddValue("Groups", m_PlayerName, "Default"); + IniFile.AddValue("Groups", GetName(), "Default"); AddToGroup("Default"); } IniFile.WriteFile("users.ini"); @@ -1641,14 +1641,14 @@ bool cPlayer::LoadFromDisk() LoadPermissionsFromDisk(); // Log player permissions, cause it's what the cool kids do - LOGINFO("Player %s has permissions:", m_PlayerName.c_str() ); + LOGINFO("Player %s has permissions:", GetName().c_str() ); for( PermissionMap::iterator itr = m_ResolvedPermissions.begin(); itr != m_ResolvedPermissions.end(); ++itr ) { if( itr->second ) LOG(" - %s", itr->first.c_str() ); } AString SourceFile; - Printf(SourceFile, "players/%s.json", m_PlayerName.c_str() ); + Printf(SourceFile, "players/%s.json", GetName().c_str() ); cFile f; if (!f.Open(SourceFile, cFile::fmRead)) @@ -1716,7 +1716,7 @@ bool cPlayer::LoadFromDisk() StatSerializer.Load(); LOGD("Player \"%s\" was read from file, spawning at {%.2f, %.2f, %.2f} in world \"%s\"", - m_PlayerName.c_str(), GetPosX(), GetPosY(), GetPosZ(), m_LoadedWorldName.c_str() + GetName().c_str(), GetPosX(), GetPosY(), GetPosZ(), m_LoadedWorldName.c_str() ); return true; @@ -1772,12 +1772,12 @@ bool cPlayer::SaveToDisk() std::string JsonData = writer.write(root); AString SourceFile; - Printf(SourceFile, "players/%s.json", m_PlayerName.c_str() ); + Printf(SourceFile, "players/%s.json", GetName().c_str() ); cFile f; if (!f.Open(SourceFile, cFile::fmWrite)) { - LOGERROR("ERROR WRITING PLAYER \"%s\" TO FILE \"%s\" - cannot open file", m_PlayerName.c_str(), SourceFile.c_str()); + LOGERROR("ERROR WRITING PLAYER \"%s\" TO FILE \"%s\" - cannot open file", GetName().c_str(), SourceFile.c_str()); return false; } if (f.Write(JsonData.c_str(), JsonData.size()) != (int)JsonData.size()) @@ -1791,7 +1791,7 @@ bool cPlayer::SaveToDisk() cStatSerializer StatSerializer(cRoot::Get()->GetDefaultWorld()->GetName(), GetName(), &m_Stats); if (!StatSerializer.Save()) { - LOGERROR("Could not save stats for player %s", m_PlayerName.c_str()); + LOGERROR("Could not save stats for player %s", GetName().c_str()); return false; } -- cgit v1.2.3 From 576e17ae08ddd0dcff625134325bfceeab956906 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Tue, 20 May 2014 22:49:21 +0100 Subject: Update CompositeChat.h --- src/CompositeChat.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/CompositeChat.h b/src/CompositeChat.h index 209d9d925..1ad196f1d 100644 --- a/src/CompositeChat.h +++ b/src/CompositeChat.h @@ -169,8 +169,10 @@ public: Recognizes "http:" and "https:" URLs and @color-codes. */ void ParseText(const AString & a_ParseText); - /** Sets the message type, which is indicated by prefixes added to the message when serializing. */ - void SetMessageType(eMessageType a_MessageType, const AString & a_AdditionalMessageTypeata = ""); + /** Sets the message type, which is indicated by prefixes added to the message when serializing + Takes optional AdditionalMessageTypeData to set m_AdditionalMessageTypeData. See said variable for more documentation. + */ + void SetMessageType(eMessageType a_MessageType, const AString & a_AdditionalMessageTypeData = ""); /** Adds the "underline" style to each part that is an URL. */ void UnderlineUrls(void); -- cgit v1.2.3