[7645] Fixed problems wit temporary unsummoned pets and cleanup code.
[AHbot.git] / src / game / GuildHandler.cpp
blobf9bde88496a884469e7cc5ccde0c80dab5b026e7
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "WorldPacket.h"
21 #include "WorldSession.h"
22 #include "World.h"
23 #include "ObjectMgr.h"
24 #include "Log.h"
25 #include "Opcodes.h"
26 #include "Guild.h"
27 #include "GossipDef.h"
28 #include "SocialMgr.h"
30 void WorldSession::HandleGuildQueryOpcode(WorldPacket& recvPacket)
32 CHECK_PACKET_SIZE(recvPacket, 4);
34 uint32 guildId;
35 Guild *guild;
37 //sLog.outDebug("WORLD: Received CMSG_GUILD_QUERY");
39 recvPacket >> guildId;
41 guild = objmgr.GetGuildById(guildId);
42 if(!guild)
44 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
45 return;
48 guild->Query(this);
51 void WorldSession::HandleGuildCreateOpcode(WorldPacket& recvPacket)
53 CHECK_PACKET_SIZE(recvPacket, 1);
55 std::string gname;
57 //sLog.outDebug("WORLD: Received CMSG_GUILD_CREATE");
59 recvPacket >> gname;
61 if(GetPlayer()->GetGuildId())
62 return;
64 Guild *guild = new Guild;
65 if(!guild->create(GetPlayer(),gname))
67 delete guild;
68 return;
71 objmgr.AddGuild(guild);
74 void WorldSession::HandleGuildInviteOpcode(WorldPacket& recvPacket)
76 CHECK_PACKET_SIZE(recvPacket, 1);
78 std::string Invitedname, plname;
80 //sLog.outDebug("WORLD: Received CMSG_GUILD_INVITE");
82 Player * player = NULL;
84 recvPacket >> Invitedname;
86 if(normalizePlayerName(Invitedname))
87 player = ObjectAccessor::Instance().FindPlayerByName(Invitedname.c_str());
89 if(!player)
91 SendGuildCommandResult(GUILD_INVITE_S, Invitedname, GUILD_PLAYER_NOT_FOUND);
92 return;
95 Guild *guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
96 if(!guild)
98 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
99 return;
102 // OK result but not send invite
103 if(player->GetSocial()->HasIgnore(GetPlayer()->GetGUIDLow()))
104 return;
106 // not let enemies sign guild charter
107 if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_GUILD) && player->GetTeam() != GetPlayer()->GetTeam())
109 SendGuildCommandResult(GUILD_INVITE_S, Invitedname, GUILD_NOT_ALLIED);
110 return;
113 if(player->GetGuildId())
115 plname = player->GetName();
116 SendGuildCommandResult(GUILD_INVITE_S, plname, ALREADY_IN_GUILD);
117 return;
120 if(player->GetGuildIdInvited())
122 plname = player->GetName();
123 SendGuildCommandResult(GUILD_INVITE_S, plname, ALREADY_INVITED_TO_GUILD);
124 return;
127 if(!guild->HasRankRight(GetPlayer()->GetRank(), GR_RIGHT_INVITE))
129 SendGuildCommandResult(GUILD_INVITE_S, "", GUILD_PERMISSIONS);
130 return;
133 sLog.outDebug("Player %s Invited %s to Join his Guild", GetPlayer()->GetName(), Invitedname.c_str());
135 player->SetGuildIdInvited(GetPlayer()->GetGuildId());
136 // Put record into guildlog
137 guild->LogGuildEvent(GUILD_EVENT_LOG_INVITE_PLAYER, GetPlayer()->GetGUIDLow(), player->GetGUIDLow(), 0);
139 WorldPacket data(SMSG_GUILD_INVITE, (8+10)); // guess size
140 data << GetPlayer()->GetName();
141 data << guild->GetName();
142 player->GetSession()->SendPacket(&data);
144 //sLog.outDebug("WORLD: Sent (SMSG_GUILD_INVITE)");
147 void WorldSession::HandleGuildRemoveOpcode(WorldPacket& recvPacket)
149 CHECK_PACKET_SIZE(recvPacket, 1);
151 std::string plName;
153 //sLog.outDebug("WORLD: Received CMSG_GUILD_REMOVE");
155 recvPacket >> plName;
157 if(!normalizePlayerName(plName))
158 return;
160 Guild* guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
161 if(!guild)
163 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
164 return;
167 if(!guild->HasRankRight(GetPlayer()->GetRank(), GR_RIGHT_REMOVE))
169 SendGuildCommandResult(GUILD_INVITE_S, "", GUILD_PERMISSIONS);
170 return;
173 uint64 plGuid;
174 MemberSlot* slot = guild->GetMemberSlot(plName, plGuid);
175 if(!slot)
177 SendGuildCommandResult(GUILD_INVITE_S, plName, GUILD_PLAYER_NOT_IN_GUILD_S);
178 return;
181 if(slot->RankId == GR_GUILDMASTER)
183 SendGuildCommandResult(GUILD_QUIT_S, "", GUILD_LEADER_LEAVE);
184 return;
187 guild->DelMember(plGuid);
188 // Put record into guildlog
189 guild->LogGuildEvent(GUILD_EVENT_LOG_UNINVITE_PLAYER, GetPlayer()->GetGUIDLow(), GUID_LOPART(plGuid), 0);
191 WorldPacket data(SMSG_GUILD_EVENT, (2+20)); // guess size
192 data << (uint8)GE_REMOVED;
193 data << (uint8)2; // strings count
194 data << plName;
195 data << GetPlayer()->GetName();
196 guild->BroadcastPacket(&data);
199 void WorldSession::HandleGuildAcceptOpcode(WorldPacket& /*recvPacket*/)
201 Guild *guild;
202 Player *player = GetPlayer();
204 //sLog.outDebug("WORLD: Received CMSG_GUILD_ACCEPT");
206 guild = objmgr.GetGuildById(player->GetGuildIdInvited());
207 if(!guild || player->GetGuildId())
208 return;
210 // not let enemies sign guild charter
211 if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_GUILD) && player->GetTeam() != objmgr.GetPlayerTeamByGUID(guild->GetLeader()))
212 return;
214 if(!guild->AddMember(GetPlayer()->GetGUID(),guild->GetLowestRank()))
215 return;
216 // Put record into guildlog
217 guild->LogGuildEvent(GUILD_EVENT_LOG_JOIN_GUILD, GetPlayer()->GetGUIDLow(), 0, 0);
219 WorldPacket data(SMSG_GUILD_EVENT, (2+10)); // guess size
220 data << (uint8)GE_JOINED;
221 data << (uint8)1;
222 data << player->GetName();
223 guild->BroadcastPacket(&data);
225 //sLog.outDebug("WORLD: Sent (SMSG_GUILD_EVENT)");
228 void WorldSession::HandleGuildDeclineOpcode(WorldPacket& /*recvPacket*/)
230 //sLog.outDebug("WORLD: Received CMSG_GUILD_DECLINE");
232 GetPlayer()->SetGuildIdInvited(0);
233 GetPlayer()->SetInGuild(0);
236 void WorldSession::HandleGuildInfoOpcode(WorldPacket& /*recvPacket*/)
238 Guild *guild;
239 //sLog.outDebug("WORLD: Received CMSG_GUILD_INFO");
241 guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
242 if(!guild)
244 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
245 return;
248 WorldPacket data(SMSG_GUILD_INFO, (5*4 + guild->GetName().size() + 1));
249 data << guild->GetName();
250 data << guild->GetCreatedDay();
251 data << guild->GetCreatedMonth();
252 data << guild->GetCreatedYear();
253 data << guild->GetMemberSize();
254 data << guild->GetMemberSize();
256 SendPacket(&data);
259 void WorldSession::HandleGuildRosterOpcode(WorldPacket& /*recvPacket*/)
261 //sLog.outDebug("WORLD: Received CMSG_GUILD_ROSTER");
263 Guild* guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
264 if(!guild)
265 return;
267 guild->Roster(this);
270 void WorldSession::HandleGuildPromoteOpcode(WorldPacket& recvPacket)
272 CHECK_PACKET_SIZE(recvPacket, 1);
274 std::string plName;
276 //sLog.outDebug("WORLD: Received CMSG_GUILD_PROMOTE");
278 recvPacket >> plName;
280 if(!normalizePlayerName(plName))
281 return;
283 Guild* guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
284 if(!guild)
286 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
287 return;
289 if(!guild->HasRankRight(GetPlayer()->GetRank(), GR_RIGHT_PROMOTE))
291 SendGuildCommandResult(GUILD_INVITE_S, "", GUILD_PERMISSIONS);
292 return;
295 uint64 plGuid;
296 MemberSlot* slot = guild->GetMemberSlot(plName, plGuid);
298 if(!slot)
300 SendGuildCommandResult(GUILD_INVITE_S, plName, GUILD_PLAYER_NOT_IN_GUILD_S);
301 return;
304 if(plGuid == GetPlayer()->GetGUID())
306 SendGuildCommandResult(GUILD_INVITE_S, "", GUILD_NAME_INVALID);
307 return;
310 if(slot->RankId < 2 || (slot->RankId-1) < GetPlayer()->GetRank())
311 return;
313 uint32 newRankId = slot->RankId < guild->GetNrRanks() ? slot->RankId-1 : guild->GetNrRanks()-1;
315 guild->ChangeRank(plGuid, newRankId);
316 // Put record into guildlog
317 guild->LogGuildEvent(GUILD_EVENT_LOG_PROMOTE_PLAYER, GetPlayer()->GetGUIDLow(), GUID_LOPART(plGuid), newRankId);
319 WorldPacket data(SMSG_GUILD_EVENT, (2+30)); // guess size
320 data << (uint8)GE_PROMOTION;
321 data << (uint8)3;
322 data << GetPlayer()->GetName();
323 data << plName;
324 data << guild->GetRankName(newRankId);
325 guild->BroadcastPacket(&data);
328 void WorldSession::HandleGuildDemoteOpcode(WorldPacket& recvPacket)
330 CHECK_PACKET_SIZE(recvPacket, 1);
332 std::string plName;
334 //sLog.outDebug("WORLD: Received CMSG_GUILD_DEMOTE");
336 recvPacket >> plName;
338 if(!normalizePlayerName(plName))
339 return;
341 Guild* guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
343 if(!guild)
345 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
346 return;
349 if(!guild->HasRankRight(GetPlayer()->GetRank(), GR_RIGHT_DEMOTE))
351 SendGuildCommandResult(GUILD_INVITE_S, "", GUILD_PERMISSIONS);
352 return;
355 uint64 plGuid;
356 MemberSlot* slot = guild->GetMemberSlot(plName, plGuid);
358 if (!slot)
360 SendGuildCommandResult(GUILD_INVITE_S, plName, GUILD_PLAYER_NOT_IN_GUILD_S);
361 return;
364 if(plGuid == GetPlayer()->GetGUID())
366 SendGuildCommandResult(GUILD_INVITE_S, "", GUILD_NAME_INVALID);
367 return;
370 if((slot->RankId+1) >= guild->GetNrRanks() || slot->RankId <= GetPlayer()->GetRank())
371 return;
373 guild->ChangeRank(plGuid, (slot->RankId+1));
374 // Put record into guildlog
375 guild->LogGuildEvent(GUILD_EVENT_LOG_DEMOTE_PLAYER, GetPlayer()->GetGUIDLow(), GUID_LOPART(plGuid), slot->RankId);
377 WorldPacket data(SMSG_GUILD_EVENT, (2+30)); // guess size
378 data << (uint8)GE_DEMOTION;
379 data << (uint8)3;
380 data << GetPlayer()->GetName();
381 data << plName;
382 data << guild->GetRankName(slot->RankId);
383 guild->BroadcastPacket(&data);
386 void WorldSession::HandleGuildLeaveOpcode(WorldPacket& /*recvPacket*/)
388 std::string plName;
389 Guild *guild;
391 //sLog.outDebug("WORLD: Received CMSG_GUILD_LEAVE");
393 guild = objmgr.GetGuildById(_player->GetGuildId());
394 if(!guild)
396 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
397 return;
399 if(_player->GetGUID() == guild->GetLeader() && guild->GetMemberSize() > 1)
401 SendGuildCommandResult(GUILD_QUIT_S, "", GUILD_LEADER_LEAVE);
402 return;
405 if(_player->GetGUID() == guild->GetLeader())
407 guild->Disband();
408 return;
411 plName = _player->GetName();
413 guild->DelMember(_player->GetGUID());
414 // Put record into guildlog
415 guild->LogGuildEvent(GUILD_EVENT_LOG_LEAVE_GUILD, _player->GetGUIDLow(), 0, 0);
417 WorldPacket data(SMSG_GUILD_EVENT, (2+10)); // guess size
418 data << (uint8)GE_LEFT;
419 data << (uint8)1;
420 data << plName;
421 guild->BroadcastPacket(&data);
423 //sLog.outDebug("WORLD: Sent (SMSG_GUILD_EVENT)");
425 SendGuildCommandResult(GUILD_QUIT_S, guild->GetName(), GUILD_PLAYER_NO_MORE_IN_GUILD);
428 void WorldSession::HandleGuildDisbandOpcode(WorldPacket& /*recvPacket*/)
430 std::string name;
431 Guild *guild;
433 //sLog.outDebug("WORLD: Received CMSG_GUILD_DISBAND");
435 guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
436 if(!guild)
438 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
439 return;
441 if(GetPlayer()->GetGUID() != guild->GetLeader())
443 SendGuildCommandResult(GUILD_INVITE_S, "", GUILD_PERMISSIONS);
444 return;
447 guild->Disband();
449 //sLog.outDebug("WORLD: Guild Sucefully Disbanded");
452 void WorldSession::HandleGuildLeaderOpcode(WorldPacket& recvPacket)
454 CHECK_PACKET_SIZE(recvPacket, 1);
456 std::string name;
457 Player *oldLeader = GetPlayer();
458 Guild *guild;
460 //sLog.outDebug("WORLD: Received CMSG_GUILD_LEADER");
462 recvPacket >> name;
464 if(!normalizePlayerName(name))
465 return;
467 guild = objmgr.GetGuildById(oldLeader->GetGuildId());
469 if (!guild)
471 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
472 return;
475 if (oldLeader->GetGUID() != guild->GetLeader())
477 SendGuildCommandResult(GUILD_INVITE_S, "", GUILD_PERMISSIONS);
478 return;
481 uint64 newLeaderGUID;
482 MemberSlot* slot = guild->GetMemberSlot(name, newLeaderGUID);
484 if (!slot)
486 SendGuildCommandResult(GUILD_INVITE_S, name, GUILD_PLAYER_NOT_IN_GUILD_S);
487 return;
490 guild->SetLeader(newLeaderGUID);
491 guild->ChangeRank(oldLeader->GetGUID(), GR_OFFICER);
493 WorldPacket data(SMSG_GUILD_EVENT, (2+20)); // guess size
494 data << (uint8)GE_LEADER_CHANGED;
495 data << (uint8)2;
496 data << oldLeader->GetName();
497 data << name.c_str();
498 guild->BroadcastPacket(&data);
500 //sLog.outDebug("WORLD: Sent (SMSG_GUILD_EVENT)");
503 void WorldSession::HandleGuildMOTDOpcode(WorldPacket& recvPacket)
505 Guild *guild;
506 std::string MOTD;
508 //sLog.outDebug("WORLD: Received CMSG_GUILD_MOTD");
510 guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
511 if(!guild)
513 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
514 return;
516 if(!guild->HasRankRight(GetPlayer()->GetRank(), GR_RIGHT_SETMOTD))
518 SendGuildCommandResult(GUILD_INVITE_S, "", GUILD_PERMISSIONS);
519 return;
522 if(!recvPacket.empty())
523 recvPacket >> MOTD;
524 else
525 MOTD = "";
527 guild->SetMOTD(MOTD);
529 WorldPacket data(SMSG_GUILD_EVENT, (2+MOTD.size()+1));
530 data << (uint8)GE_MOTD;
531 data << (uint8)1;
532 data << MOTD;
533 guild->BroadcastPacket(&data);
535 //sLog.outDebug("WORLD: Sent (SMSG_GUILD_EVENT)");
538 void WorldSession::HandleGuildSetPublicNoteOpcode(WorldPacket& recvPacket)
540 CHECK_PACKET_SIZE(recvPacket, 1);
542 std::string name,PNOTE;
544 //sLog.outDebug("WORLD: Received CMSG_GUILD_SET_PUBLIC_NOTE");
546 recvPacket >> name;
548 if(!normalizePlayerName(name))
549 return;
551 Guild* guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
553 if (!guild)
555 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
556 return;
559 if (!guild->HasRankRight(GetPlayer()->GetRank(), GR_RIGHT_EPNOTE))
561 SendGuildCommandResult(GUILD_INVITE_S, "", GUILD_PERMISSIONS);
562 return;
565 uint64 plGuid;
566 MemberSlot* slot = guild->GetMemberSlot(name, plGuid);
568 if (!slot)
570 SendGuildCommandResult(GUILD_INVITE_S, name, GUILD_PLAYER_NOT_IN_GUILD_S);
571 return;
574 recvPacket >> PNOTE;
575 guild->SetPNOTE(plGuid, PNOTE);
577 guild->Roster(this);
580 void WorldSession::HandleGuildSetOfficerNoteOpcode(WorldPacket& recvPacket)
582 CHECK_PACKET_SIZE(recvPacket, 1);
584 std::string plName, OFFNOTE;
586 //sLog.outDebug("WORLD: Received CMSG_GUILD_SET_OFFICER_NOTE");
588 recvPacket >> plName;
590 if (!normalizePlayerName(plName))
591 return;
593 Guild* guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
595 if (!guild)
597 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
598 return;
600 if (!guild->HasRankRight(GetPlayer()->GetRank(), GR_RIGHT_EOFFNOTE))
602 SendGuildCommandResult(GUILD_INVITE_S, "", GUILD_PERMISSIONS);
603 return;
606 uint64 plGuid;
607 MemberSlot* slot = guild->GetMemberSlot(plName, plGuid);
609 if (!slot)
611 SendGuildCommandResult(GUILD_INVITE_S, plName, GUILD_PLAYER_NOT_IN_GUILD_S);
612 return;
615 recvPacket >> OFFNOTE;
616 guild->SetOFFNOTE(plGuid, OFFNOTE);
618 guild->Roster(this);
621 void WorldSession::HandleGuildRankOpcode(WorldPacket& recvPacket)
623 CHECK_PACKET_SIZE(recvPacket, 4+4+1+4*13);
624 //recvPacket.hexlike();
626 Guild *guild;
627 std::string rankname;
628 uint32 rankId;
629 uint32 rights, MoneyPerDay;
630 uint32 BankRights;
631 uint32 BankSlotPerDay;
633 //sLog.outDebug("WORLD: Received CMSG_GUILD_RANK");
635 guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
636 if(!guild)
638 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
639 return;
642 else if(GetPlayer()->GetGUID() != guild->GetLeader())
644 SendGuildCommandResult(GUILD_INVITE_S, "", GUILD_PERMISSIONS);
645 return;
648 recvPacket >> rankId;
649 recvPacket >> rights;
650 recvPacket >> rankname;
651 recvPacket >> MoneyPerDay;
653 for (int i = 0; i < GUILD_BANK_MAX_TABS; ++i)
655 recvPacket >> BankRights;
656 recvPacket >> BankSlotPerDay;
657 guild->SetBankRightsAndSlots(rankId, uint8(i), uint16(BankRights & 0xFF), uint16(BankSlotPerDay), true);
659 sLog.outDebug("WORLD: Changed RankName to %s , Rights to 0x%.4X", rankname.c_str(), rights);
661 guild->SetBankMoneyPerDay(rankId, MoneyPerDay);
662 guild->SetRankName(rankId, rankname);
664 if(rankId==GR_GUILDMASTER) // prevent loss leader rights
665 rights = GR_RIGHT_ALL;
667 guild->SetRankRights(rankId, rights);
669 guild->Query(this);
670 guild->Roster(this);
673 void WorldSession::HandleGuildAddRankOpcode(WorldPacket& recvPacket)
675 CHECK_PACKET_SIZE(recvPacket, 1);
677 Guild *guild;
678 std::string rankname;
680 //sLog.outDebug("WORLD: Received CMSG_GUILD_ADD_RANK");
682 guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
683 if(!guild)
685 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
686 return;
689 if(GetPlayer()->GetGUID() != guild->GetLeader())
691 SendGuildCommandResult(GUILD_INVITE_S, "", GUILD_PERMISSIONS);
692 return;
695 if(guild->GetNrRanks() >= GUILD_MAX_RANKS) // client not let create more 10 than ranks
696 return;
698 recvPacket >> rankname;
700 guild->CreateRank(rankname, GR_RIGHT_GCHATLISTEN | GR_RIGHT_GCHATSPEAK);
702 guild->Query(this);
703 guild->Roster(this);
706 void WorldSession::HandleGuildDelRankOpcode(WorldPacket& /*recvPacket*/)
708 Guild *guild;
709 std::string rankname;
711 //sLog.outDebug("WORLD: Received CMSG_GUILD_DEL_RANK");
713 guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
714 if(!guild)
716 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
717 return;
720 else if(GetPlayer()->GetGUID() != guild->GetLeader())
722 SendGuildCommandResult(GUILD_INVITE_S, "", GUILD_PERMISSIONS);
723 return;
726 guild->DelRank();
728 guild->Query(this);
729 guild->Roster(this);
732 void WorldSession::SendGuildCommandResult(uint32 typecmd, const std::string& str,uint32 cmdresult)
734 WorldPacket data(SMSG_GUILD_COMMAND_RESULT, (8+str.size()+1));
735 data << typecmd;
736 data << str;
737 data << cmdresult;
738 SendPacket(&data);
740 //sLog.outDebug("WORLD: Sent (SMSG_GUILD_COMMAND_RESULT)");
743 void WorldSession::HandleGuildChangeInfoOpcode(WorldPacket& recvPacket)
745 CHECK_PACKET_SIZE(recvPacket, 1);
747 //sLog.outDebug("WORLD: Received CMSG_GUILD_INFO_TEXT");
749 std::string GINFO;
751 recvPacket >> GINFO;
753 Guild *guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
754 if(!guild)
756 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PLAYER_NOT_IN_GUILD);
757 return;
760 if(!guild->HasRankRight(GetPlayer()->GetRank(), GR_RIGHT_MODIFY_GUILD_INFO))
762 SendGuildCommandResult(GUILD_CREATE_S, "", GUILD_PERMISSIONS);
763 return;
766 guild->SetGINFO(GINFO);
769 void WorldSession::HandleGuildSaveEmblemOpcode(WorldPacket& recvPacket)
771 CHECK_PACKET_SIZE(recvPacket, 8+4+4+4+4+4);
773 //sLog.outDebug("WORLD: Received MSG_SAVE_GUILD_EMBLEM");
775 uint64 vendorGuid;
777 uint32 EmblemStyle;
778 uint32 EmblemColor;
779 uint32 BorderStyle;
780 uint32 BorderColor;
781 uint32 BackgroundColor;
783 recvPacket >> vendorGuid;
785 Creature *pCreature = ObjectAccessor::GetNPCIfCanInteractWith(*_player, vendorGuid,UNIT_NPC_FLAG_TABARDDESIGNER);
786 if (!pCreature)
788 //"That's not an emblem vendor!"
789 SendSaveGuildEmblem(ERR_GUILDEMBLEM_INVALIDVENDOR);
790 sLog.outDebug("WORLD: HandleGuildSaveEmblemOpcode - Unit (GUID: %u) not found or you can't interact with him.", GUID_LOPART(vendorGuid));
791 return;
794 // remove fake death
795 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
796 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
798 recvPacket >> EmblemStyle;
799 recvPacket >> EmblemColor;
800 recvPacket >> BorderStyle;
801 recvPacket >> BorderColor;
802 recvPacket >> BackgroundColor;
804 Guild *guild = objmgr.GetGuildById(GetPlayer()->GetGuildId());
805 if(!guild)
807 //"You are not part of a guild!";
808 SendSaveGuildEmblem(ERR_GUILDEMBLEM_NOGUILD);
809 return;
812 if (guild->GetLeader() != GetPlayer()->GetGUID())
814 //"Only guild leaders can create emblems."
815 SendSaveGuildEmblem(ERR_GUILDEMBLEM_NOTGUILDMASTER);
816 return;
819 if(GetPlayer()->GetMoney() < 10*GOLD)
821 //"You can't afford to do that."
822 SendSaveGuildEmblem(ERR_GUILDEMBLEM_NOTENOUGHMONEY);
823 return;
826 GetPlayer()->ModifyMoney(-10*GOLD);
827 guild->SetEmblem(EmblemStyle, EmblemColor, BorderStyle, BorderColor, BackgroundColor);
829 //"Guild Emblem saved."
830 SendSaveGuildEmblem(ERR_GUILDEMBLEM_SUCCESS);
832 guild->Query(this);
835 void WorldSession::HandleGuildEventLogOpcode(WorldPacket& /* recvPacket */)
837 // empty
838 sLog.outDebug("WORLD: Received (MSG_GUILD_EVENT_LOG_QUERY)");
839 //recvPacket.hexlike();
841 uint32 GuildId = GetPlayer()->GetGuildId();
842 if (GuildId == 0)
843 return;
845 Guild *pGuild = objmgr.GetGuildById(GuildId);
846 if(!pGuild)
847 return;
849 pGuild->DisplayGuildEventlog(this);
852 /****** GUILD BANK *******/
854 void WorldSession::HandleGuildBankGetMoneyAmount( WorldPacket & /* recv_data */ )
856 sLog.outDebug("WORLD: Received (MSG_GUILD_BANK_MONEY_WITHDRAWN)");
857 //recv_data.hexlike();
859 uint32 GuildId = GetPlayer()->GetGuildId();
860 if (GuildId == 0)
861 return;
863 Guild *pGuild = objmgr.GetGuildById(GuildId);
864 if(!pGuild)
865 return;
867 pGuild->SendMoneyInfo(this, GetPlayer()->GetGUIDLow());
870 void WorldSession::HandleGuildBankGetRights( WorldPacket& /* recv_data */ )
872 sLog.outDebug("WORLD: Received (MSG_GUILD_PERMISSIONS)");
874 uint32 GuildId = GetPlayer()->GetGuildId();
875 if (GuildId == 0)
876 return;
878 Guild *pGuild = objmgr.GetGuildById(GuildId);
879 if(!pGuild)
880 return;
882 uint32 rankId = GetPlayer()->GetRank();
884 WorldPacket data(MSG_GUILD_PERMISSIONS, 4*15+1);
885 data << uint32(rankId); // guild rank id
886 data << uint32(pGuild->GetRankRights(rankId)); // rank rights
887 // money per day left
888 data << uint32(pGuild->GetMemberMoneyWithdrawRem(GetPlayer()->GetGUIDLow()));
889 data << uint8(pGuild->GetPurchasedTabs()); // tabs count
890 for(int i = 0; i < GUILD_BANK_MAX_TABS; ++i)
892 data << uint32(pGuild->GetBankRights(rankId, uint8(i)));
893 data << uint32(pGuild->GetMemberSlotWithdrawRem(GetPlayer()->GetGUIDLow(), uint8(i)));
895 SendPacket(&data);
896 sLog.outDebug("WORLD: Sent (MSG_GUILD_PERMISSIONS)");
899 /* Called when clicking on Guild bank gameobject */
900 void WorldSession::HandleGuildBankQuery( WorldPacket & recv_data )
902 sLog.outDebug("WORLD: Received (CMSG_GUILD_BANKER_ACTIVATE)");
903 CHECK_PACKET_SIZE(recv_data,8+1);
904 uint64 GoGuid;
905 uint8 unk;
906 recv_data >> GoGuid >> unk;
908 if (!objmgr.IsGuildVaultGameObject(_player, GoGuid))
909 return;
911 if (uint32 GuildId = GetPlayer()->GetGuildId())
913 if(Guild *pGuild = objmgr.GetGuildById(GuildId))
915 pGuild->DisplayGuildBankTabsInfo(this);
916 return;
920 SendGuildCommandResult(GUILD_BANK_S, "", GUILD_PLAYER_NOT_IN_GUILD);
923 /* Called when opening guild bank tab only (first one) */
924 void WorldSession::HandleGuildBankTabColon( WorldPacket & recv_data )
926 sLog.outDebug("WORLD: Received (CMSG_GUILD_BANK_QUERY_TAB)");
927 CHECK_PACKET_SIZE(recv_data,8+1+1);
928 uint64 GoGuid;
929 uint8 TabId,unk1;
930 recv_data >> GoGuid >> TabId >> unk1;
932 if (!objmgr.IsGuildVaultGameObject(_player, GoGuid))
933 return;
935 uint32 GuildId = GetPlayer()->GetGuildId();
936 if (GuildId == 0)
937 return;
939 Guild *pGuild = objmgr.GetGuildById(GuildId);
940 if(!pGuild)
941 return;
943 // Let's update the amount of gold the player can withdraw before displaying the content
944 // This is usefull if money withdraw right has changed
945 pGuild->SendMoneyInfo(this, GetPlayer()->GetGUIDLow());
947 pGuild->DisplayGuildBankContent(this, TabId);
950 void WorldSession::HandleGuildBankDeposit( WorldPacket & recv_data )
952 sLog.outDebug("WORLD: Received (CMSG_GUILD_BANK_DEPOSIT_MONEY)");
953 CHECK_PACKET_SIZE(recv_data,8+4);
954 uint64 GoGuid;
955 uint32 money;
956 recv_data >> GoGuid >> money;
958 if (!money)
959 return;
961 if (!objmgr.IsGuildVaultGameObject(_player, GoGuid))
962 return;
964 uint32 GuildId = GetPlayer()->GetGuildId();
965 if (GuildId == 0)
966 return;
968 Guild *pGuild = objmgr.GetGuildById(GuildId);
969 if(!pGuild)
970 return;
972 if (GetPlayer()->GetMoney() < money)
973 return;
975 CharacterDatabase.BeginTransaction();
977 pGuild->SetBankMoney(pGuild->GetGuildBankMoney()+money);
978 GetPlayer()->ModifyMoney(-int(money));
979 GetPlayer()->SaveDataFieldToDB(); //contains money
981 CharacterDatabase.CommitTransaction();
983 // logging money
984 if(_player->GetSession()->GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE))
986 sLog.outCommand(_player->GetSession()->GetAccountId(),"GM %s (Account: %u) deposit money (Amount: %u) to guild bank (Guild ID %u)",
987 _player->GetName(),_player->GetSession()->GetAccountId(),money,GuildId);
990 // log
991 pGuild->LogBankEvent(GUILD_BANK_LOG_DEPOSIT_MONEY, uint8(0), GetPlayer()->GetGUIDLow(), money);
993 pGuild->DisplayGuildBankTabsInfo(this);
994 pGuild->DisplayGuildBankContent(this, 0);
995 pGuild->DisplayGuildBankMoneyUpdate();
998 void WorldSession::HandleGuildBankWithdraw( WorldPacket & recv_data )
1000 sLog.outDebug("WORLD: Received (CMSG_GUILD_BANK_WITHDRAW_MONEY)");
1001 CHECK_PACKET_SIZE(recv_data,8+4);
1002 uint64 GoGuid;
1003 uint32 money;
1004 recv_data >> GoGuid >> money;
1006 if (!money)
1007 return;
1009 if (!objmgr.IsGuildVaultGameObject(_player, GoGuid))
1010 return;
1012 uint32 GuildId = GetPlayer()->GetGuildId();
1013 if (GuildId == 0)
1014 return;
1016 Guild *pGuild = objmgr.GetGuildById(GuildId);
1017 if(!pGuild)
1018 return;
1020 if (pGuild->GetGuildBankMoney()<money) // not enough money in bank
1021 return;
1023 if (!pGuild->HasRankRight(GetPlayer()->GetRank(), GR_RIGHT_WITHDRAW_GOLD))
1024 return;
1026 CharacterDatabase.BeginTransaction();
1028 if (!pGuild->MemberMoneyWithdraw(money, GetPlayer()->GetGUIDLow()))
1030 CharacterDatabase.RollbackTransaction();
1031 return;
1034 GetPlayer()->ModifyMoney(money);
1035 GetPlayer()->SaveDataFieldToDB(); // contains money
1037 CharacterDatabase.CommitTransaction();
1039 // Log
1040 pGuild->LogBankEvent(GUILD_BANK_LOG_WITHDRAW_MONEY, uint8(0), GetPlayer()->GetGUIDLow(), money);
1042 pGuild->SendMoneyInfo(this, GetPlayer()->GetGUIDLow());
1043 pGuild->DisplayGuildBankTabsInfo(this);
1044 pGuild->DisplayGuildBankContent(this, 0);
1045 pGuild->DisplayGuildBankMoneyUpdate();
1048 void WorldSession::HandleGuildBankDepositItem( WorldPacket & recv_data )
1050 sLog.outDebug("WORLD: Received (CMSG_GUILD_BANK_SWAP_ITEMS)");
1051 //recv_data.hexlike();
1053 uint64 GoGuid;
1054 uint8 BankToBank;
1056 uint8 BankTab, BankTabSlot, AutoStore, AutoStoreCount, PlayerSlot, PlayerBag, SplitedAmount = 0;
1057 uint8 BankTabDst, BankTabSlotDst, unk2, ToChar = 1;
1058 uint32 ItemEntry, unk1;
1060 CHECK_PACKET_SIZE(recv_data,8+1);
1061 recv_data >> GoGuid >> BankToBank;
1062 if (BankToBank)
1064 // recheck
1065 CHECK_PACKET_SIZE(recv_data, recv_data.rpos()+1+1+4+1+1+4+1+1);
1066 recv_data >> BankTabDst;
1067 recv_data >> BankTabSlotDst;
1068 recv_data >> unk1; // always 0
1069 recv_data >> BankTab;
1070 recv_data >> BankTabSlot;
1071 recv_data >> ItemEntry;
1072 recv_data >> unk2; // always 0
1073 recv_data >> SplitedAmount;
1075 if (BankTabSlotDst >= GUILD_BANK_MAX_SLOTS)
1076 return;
1077 if (BankTabDst == BankTab && BankTabSlotDst == BankTabSlot)
1078 return;
1080 else
1082 // recheck
1083 CHECK_PACKET_SIZE(recv_data, recv_data.rpos()+1+1+4+1);
1084 recv_data >> BankTab;
1085 recv_data >> BankTabSlot;
1086 recv_data >> ItemEntry;
1087 recv_data >> AutoStore;
1088 if (AutoStore)
1090 // recheck
1091 CHECK_PACKET_SIZE(recv_data, recv_data.rpos()+1);
1092 recv_data >> AutoStoreCount;
1094 // recheck
1095 CHECK_PACKET_SIZE(recv_data, recv_data.rpos()+1+1);
1096 recv_data >> PlayerBag;
1097 recv_data >> PlayerSlot;
1098 if (!AutoStore)
1100 // recheck
1101 CHECK_PACKET_SIZE(recv_data, recv_data.rpos()+1+1);
1102 recv_data >> ToChar;
1103 recv_data >> SplitedAmount;
1106 if (BankTabSlot >= GUILD_BANK_MAX_SLOTS && BankTabSlot != 0xFF)
1107 return;
1110 if (!objmgr.IsGuildVaultGameObject(_player, GoGuid))
1111 return;
1113 uint32 GuildId = GetPlayer()->GetGuildId();
1114 if (GuildId == 0)
1115 return;
1117 Guild *pGuild = objmgr.GetGuildById(GuildId);
1118 if(!pGuild)
1119 return;
1121 Player *pl = GetPlayer();
1123 // Bank <-> Bank
1124 if (BankToBank)
1126 // empty operation
1127 if(BankTab==BankTabDst && BankTabSlot==BankTabSlotDst)
1128 return;
1130 Item *pItemSrc = pGuild->GetItem(BankTab, BankTabSlot);
1131 if (!pItemSrc) // may prevent crash
1132 return;
1134 if(SplitedAmount > pItemSrc->GetCount())
1135 return; // cheating?
1136 else if(SplitedAmount == pItemSrc->GetCount())
1137 SplitedAmount = 0; // no split
1139 Item *pItemDst = pGuild->GetItem(BankTabDst, BankTabSlotDst);
1141 if(BankTab!=BankTabDst)
1143 // check dest pos rights (if different tabs)
1144 if(!pGuild->IsMemberHaveRights(pl->GetGUIDLow(), BankTabDst, GUILD_BANK_RIGHT_DEPOSIT_ITEM))
1145 return;
1147 // check source pos rights (if different tabs)
1148 uint32 remRight = pGuild->GetMemberSlotWithdrawRem(pl->GetGUIDLow(), BankTab);
1149 if(remRight <= 0)
1150 return;
1153 if (SplitedAmount)
1154 { // Bank -> Bank item split (in empty or non empty slot
1155 GuildItemPosCountVec dest;
1156 uint8 msg = pGuild->CanStoreItem(BankTabDst,BankTabSlotDst,dest,SplitedAmount,pItemSrc,false);
1157 if( msg != EQUIP_ERR_OK )
1159 pl->SendEquipError( msg, pItemSrc, NULL );
1160 return;
1163 Item *pNewItem = pItemSrc->CloneItem( SplitedAmount );
1164 if( !pNewItem )
1166 pl->SendEquipError( EQUIP_ERR_ITEM_NOT_FOUND, pItemSrc, NULL );
1167 return;
1170 CharacterDatabase.BeginTransaction();
1171 pGuild->LogBankEvent(GUILD_BANK_LOG_MOVE_ITEM, BankTab, pl->GetGUIDLow(), pItemSrc->GetEntry(), SplitedAmount, BankTabDst);
1173 pl->ItemRemovedQuestCheck( pItemSrc->GetEntry(), SplitedAmount );
1174 pItemSrc->SetCount( pItemSrc->GetCount() - SplitedAmount );
1175 pItemSrc->FSetState(ITEM_CHANGED);
1176 pItemSrc->SaveToDB(); // not in inventory and can be save standalone
1177 pGuild->StoreItem(BankTabDst,dest,pNewItem);
1178 CharacterDatabase.CommitTransaction();
1180 else // non split
1182 GuildItemPosCountVec gDest;
1183 uint8 msg = pGuild->CanStoreItem(BankTabDst,BankTabSlotDst,gDest,pItemSrc->GetCount(),pItemSrc,false);
1184 if( msg == EQUIP_ERR_OK ) // merge to
1186 CharacterDatabase.BeginTransaction();
1187 pGuild->LogBankEvent(GUILD_BANK_LOG_MOVE_ITEM, BankTab, pl->GetGUIDLow(), pItemSrc->GetEntry(), pItemSrc->GetCount(), BankTabDst);
1189 pGuild->RemoveItem(BankTab, BankTabSlot);
1190 pGuild->StoreItem(BankTabDst, gDest, pItemSrc);
1191 CharacterDatabase.CommitTransaction();
1193 else // swap
1195 gDest.clear();
1196 uint8 msg = pGuild->CanStoreItem(BankTabDst,BankTabSlotDst,gDest,pItemSrc->GetCount(),pItemSrc,true);
1197 if( msg != EQUIP_ERR_OK )
1199 pl->SendEquipError( msg, pItemSrc, NULL );
1200 return;
1203 GuildItemPosCountVec gSrc;
1204 msg = pGuild->CanStoreItem(BankTab,BankTabSlot,gSrc,pItemDst->GetCount(),pItemDst,true);
1205 if( msg != EQUIP_ERR_OK )
1207 pl->SendEquipError( msg, pItemDst, NULL );
1208 return;
1211 if(BankTab!=BankTabDst)
1213 // check source pos rights (item swapped to src)
1214 if(!pGuild->IsMemberHaveRights(pl->GetGUIDLow(), BankTab, GUILD_BANK_RIGHT_DEPOSIT_ITEM))
1215 return;
1217 // check dest pos rights (item swapped to src)
1218 uint32 remRightDst = pGuild->GetMemberSlotWithdrawRem(pl->GetGUIDLow(), BankTabDst);
1219 if(remRightDst <= 0)
1220 return;
1223 CharacterDatabase.BeginTransaction();
1224 pGuild->LogBankEvent(GUILD_BANK_LOG_MOVE_ITEM, BankTab, pl->GetGUIDLow(), pItemSrc->GetEntry(), pItemSrc->GetCount(), BankTabDst);
1225 pGuild->LogBankEvent(GUILD_BANK_LOG_MOVE_ITEM, BankTabDst, pl->GetGUIDLow(), pItemDst->GetEntry(), pItemDst->GetCount(), BankTab);
1227 pGuild->RemoveItem(BankTab, BankTabSlot);
1228 pGuild->RemoveItem(BankTabDst, BankTabSlotDst);
1229 pGuild->StoreItem(BankTab, gSrc, pItemDst);
1230 pGuild->StoreItem(BankTabDst, gDest, pItemSrc);
1231 CharacterDatabase.CommitTransaction();
1234 pGuild->DisplayGuildBankContentUpdate(BankTab,BankTabSlot,BankTab==BankTabDst ? BankTabSlotDst : -1);
1235 if(BankTab!=BankTabDst)
1236 pGuild->DisplayGuildBankContentUpdate(BankTabDst,BankTabSlotDst);
1237 return;
1240 // Player <-> Bank
1242 // char->bank autostore click return BankTabSlot = 255 = NULL_SLOT
1243 // do similar for bank->char
1244 if(AutoStore && ToChar)
1246 PlayerBag = NULL_BAG;
1247 PlayerSlot = NULL_SLOT;
1250 // allow work with inventory only
1251 if(!Player::IsInventoryPos(PlayerBag,PlayerSlot) && !(PlayerBag == NULL_BAG && PlayerSlot == NULL_SLOT) )
1253 _player->SendEquipError( EQUIP_ERR_NONE, NULL, NULL );
1254 return;
1257 Item *pItemBank = pGuild->GetItem(BankTab, BankTabSlot);
1258 Item *pItemChar = GetPlayer()->GetItemByPos(PlayerBag, PlayerSlot);
1259 if (!pItemChar && !pItemBank) // Nothing to do
1260 return;
1262 if (!pItemChar && !ToChar) // Problem to get item from player
1263 return;
1265 if (!pItemBank && ToChar) // Problem to get bank item
1266 return;
1268 // BankToChar swap or char to bank remaining
1270 if (ToChar) // Bank -> Char cases
1272 if(SplitedAmount > pItemBank->GetCount())
1273 return; // cheating?
1274 else if(SplitedAmount == pItemBank->GetCount())
1275 SplitedAmount = 0; // no split
1277 if (SplitedAmount)
1278 { // Bank -> Char split to slot (patly move)
1279 Item *pNewItem = pItemBank->CloneItem( SplitedAmount );
1280 if( !pNewItem )
1282 pl->SendEquipError( EQUIP_ERR_ITEM_NOT_FOUND, pItemBank, NULL );
1283 return;
1286 ItemPosCountVec dest;
1287 uint8 msg = pl->CanStoreItem(PlayerBag, PlayerSlot, dest, pNewItem, false);
1288 if( msg != EQUIP_ERR_OK )
1290 pl->SendEquipError( msg, pNewItem, NULL );
1291 delete pNewItem;
1292 return;
1295 // check source pos rights (item moved to inventory)
1296 uint32 remRight = pGuild->GetMemberSlotWithdrawRem(pl->GetGUIDLow(), BankTab);
1297 if(remRight <= 0)
1299 delete pNewItem;
1300 return;
1303 CharacterDatabase.BeginTransaction();
1304 pGuild->LogBankEvent(GUILD_BANK_LOG_WITHDRAW_ITEM, BankTab, pl->GetGUIDLow(), pItemBank->GetEntry(), SplitedAmount);
1306 pItemBank->SetCount(pItemBank->GetCount()-SplitedAmount);
1307 pItemBank->FSetState(ITEM_CHANGED);
1308 pItemBank->SaveToDB(); // not in inventory and can be save standalone
1309 pl->MoveItemToInventory(dest,pNewItem,true);
1310 pl->SaveInventoryAndGoldToDB();
1312 pGuild->MemberItemWithdraw(BankTab, pl->GetGUIDLow());
1313 CharacterDatabase.CommitTransaction();
1315 else // Bank -> Char swap with slot (move)
1317 ItemPosCountVec dest;
1318 uint8 msg = pl->CanStoreItem(PlayerBag, PlayerSlot, dest, pItemBank, false);
1319 if( msg == EQUIP_ERR_OK ) // merge case
1321 // check source pos rights (item moved to inventory)
1322 uint32 remRight = pGuild->GetMemberSlotWithdrawRem(pl->GetGUIDLow(), BankTab);
1323 if(remRight <= 0)
1324 return;
1326 CharacterDatabase.BeginTransaction();
1327 pGuild->LogBankEvent(GUILD_BANK_LOG_WITHDRAW_ITEM, BankTab, pl->GetGUIDLow(), pItemBank->GetEntry(), pItemBank->GetCount());
1329 pGuild->RemoveItem(BankTab, BankTabSlot);
1330 pl->MoveItemToInventory(dest,pItemBank,true);
1331 pl->SaveInventoryAndGoldToDB();
1333 pGuild->MemberItemWithdraw(BankTab, pl->GetGUIDLow());
1334 CharacterDatabase.CommitTransaction();
1336 else // Bank <-> Char swap items
1338 // check source pos rights (item swapped to bank)
1339 if(!pGuild->IsMemberHaveRights(pl->GetGUIDLow(), BankTab, GUILD_BANK_RIGHT_DEPOSIT_ITEM))
1340 return;
1342 if(pItemChar)
1344 if(!pItemChar->CanBeTraded())
1346 _player->SendEquipError( EQUIP_ERR_ITEMS_CANT_BE_SWAPPED, pItemChar, NULL );
1347 return;
1351 ItemPosCountVec iDest;
1352 msg = pl->CanStoreItem(PlayerBag, PlayerSlot, iDest, pItemBank, true);
1353 if( msg != EQUIP_ERR_OK )
1355 pl->SendEquipError( msg, pItemBank, NULL );
1356 return;
1359 GuildItemPosCountVec gDest;
1360 if(pItemChar)
1362 msg = pGuild->CanStoreItem(BankTab,BankTabSlot,gDest,pItemChar->GetCount(),pItemChar,true);
1363 if( msg != EQUIP_ERR_OK )
1365 pl->SendEquipError( msg, pItemChar, NULL );
1366 return;
1370 // check source pos rights (item moved to inventory)
1371 uint32 remRight = pGuild->GetMemberSlotWithdrawRem(pl->GetGUIDLow(), BankTab);
1372 if(remRight <= 0)
1373 return;
1375 if(pItemChar)
1377 // logging item move to bank
1378 if(_player->GetSession()->GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE))
1380 sLog.outCommand(_player->GetSession()->GetAccountId(),"GM %s (Account: %u) deposit item: %s (Entry: %d Count: %u) to guild bank (Guild ID: %u )",
1381 _player->GetName(),_player->GetSession()->GetAccountId(),
1382 pItemChar->GetProto()->Name1,pItemChar->GetEntry(),pItemChar->GetCount(),
1383 GuildId);
1387 CharacterDatabase.BeginTransaction();
1388 pGuild->LogBankEvent(GUILD_BANK_LOG_WITHDRAW_ITEM, BankTab, pl->GetGUIDLow(), pItemBank->GetEntry(), pItemBank->GetCount());
1389 if(pItemChar)
1390 pGuild->LogBankEvent(GUILD_BANK_LOG_DEPOSIT_ITEM, BankTab, pl->GetGUIDLow(), pItemChar->GetEntry(), pItemChar->GetCount());
1392 pGuild->RemoveItem(BankTab, BankTabSlot);
1393 if(pItemChar)
1395 pl->MoveItemFromInventory(PlayerBag, PlayerSlot, true);
1396 pItemChar->DeleteFromInventoryDB();
1399 if(pItemChar)
1400 pGuild->StoreItem(BankTab, gDest, pItemChar);
1401 pl->MoveItemToInventory(iDest,pItemBank,true);
1402 pl->SaveInventoryAndGoldToDB();
1404 pGuild->MemberItemWithdraw(BankTab, pl->GetGUIDLow());
1405 CharacterDatabase.CommitTransaction();
1408 pGuild->DisplayGuildBankContentUpdate(BankTab,BankTabSlot);
1409 return;
1410 } // End "To char" part
1412 // Char -> Bank cases
1414 if(!pItemChar->CanBeTraded())
1416 _player->SendEquipError( EQUIP_ERR_ITEMS_CANT_BE_SWAPPED, pItemChar, NULL );
1417 return;
1420 // check source pos rights (item moved to bank)
1421 if(!pGuild->IsMemberHaveRights(pl->GetGUIDLow(), BankTab, GUILD_BANK_RIGHT_DEPOSIT_ITEM))
1422 return;
1424 if(SplitedAmount > pItemChar->GetCount())
1425 return; // cheating?
1426 else if(SplitedAmount == pItemChar->GetCount())
1427 SplitedAmount = 0; // no split
1429 if (SplitedAmount)
1430 { // Char -> Bank split to empty or non-empty slot (partly move)
1431 GuildItemPosCountVec dest;
1432 uint8 msg = pGuild->CanStoreItem(BankTab,BankTabSlot,dest,SplitedAmount,pItemChar,false);
1433 if( msg != EQUIP_ERR_OK )
1435 pl->SendEquipError( msg, pItemChar, NULL );
1436 return;
1439 Item *pNewItem = pItemChar->CloneItem( SplitedAmount );
1440 if( !pNewItem )
1442 pl->SendEquipError( EQUIP_ERR_ITEM_NOT_FOUND, pItemChar, NULL );
1443 return;
1446 // logging item move to bank (before items merge
1447 if(_player->GetSession()->GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE))
1449 sLog.outCommand(_player->GetSession()->GetAccountId(),"GM %s (Account: %u) deposit item: %s (Entry: %d Count: %u) to guild bank (Guild ID: %u )",
1450 _player->GetName(),_player->GetSession()->GetAccountId(),
1451 pItemChar->GetProto()->Name1,pItemChar->GetEntry(),SplitedAmount,GuildId);
1454 CharacterDatabase.BeginTransaction();
1455 pGuild->LogBankEvent(GUILD_BANK_LOG_DEPOSIT_ITEM, BankTab, pl->GetGUIDLow(), pItemChar->GetEntry(), SplitedAmount);
1457 pl->ItemRemovedQuestCheck( pItemChar->GetEntry(), SplitedAmount );
1458 pItemChar->SetCount(pItemChar->GetCount()-SplitedAmount);
1459 pItemChar->SetState(ITEM_CHANGED);
1460 pl->SaveInventoryAndGoldToDB();
1461 pGuild->StoreItem(BankTab, dest, pNewItem);
1462 CharacterDatabase.CommitTransaction();
1464 pGuild->DisplayGuildBankContentUpdate(BankTab,dest);
1466 else // Char -> Bank swap with empty or non-empty (move)
1468 GuildItemPosCountVec dest;
1469 uint8 msg = pGuild->CanStoreItem(BankTab,BankTabSlot,dest,pItemChar->GetCount(),pItemChar,false);
1470 if( msg == EQUIP_ERR_OK ) // merge
1472 // logging item move to bank
1473 if(_player->GetSession()->GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE))
1475 sLog.outCommand(_player->GetSession()->GetAccountId(),"GM %s (Account: %u) deposit item: %s (Entry: %d Count: %u) to guild bank (Guild ID: %u )",
1476 _player->GetName(),_player->GetSession()->GetAccountId(),
1477 pItemChar->GetProto()->Name1,pItemChar->GetEntry(),pItemChar->GetCount(),
1478 GuildId);
1481 CharacterDatabase.BeginTransaction();
1482 pGuild->LogBankEvent(GUILD_BANK_LOG_DEPOSIT_ITEM, BankTab, pl->GetGUIDLow(), pItemChar->GetEntry(), pItemChar->GetCount());
1484 pl->MoveItemFromInventory(PlayerBag, PlayerSlot, true);
1485 pItemChar->DeleteFromInventoryDB();
1487 pGuild->StoreItem(BankTab,dest,pItemChar);
1488 pl->SaveInventoryAndGoldToDB();
1489 CharacterDatabase.CommitTransaction();
1491 pGuild->DisplayGuildBankContentUpdate(BankTab,dest);
1493 else // Char <-> Bank swap items (posible NULL bank item)
1495 ItemPosCountVec iDest;
1496 if(pItemBank)
1498 msg = pl->CanStoreItem(PlayerBag, PlayerSlot, iDest, pItemBank, true);
1499 if( msg != EQUIP_ERR_OK )
1501 pl->SendEquipError( msg, pItemBank, NULL );
1502 return;
1506 GuildItemPosCountVec gDest;
1507 msg = pGuild->CanStoreItem(BankTab,BankTabSlot,gDest,pItemChar->GetCount(),pItemChar,true);
1508 if( msg != EQUIP_ERR_OK )
1510 pl->SendEquipError( msg, pItemChar, NULL );
1511 return;
1514 if(pItemBank)
1516 // check bank pos rights (item swapped with inventory)
1517 uint32 remRight = pGuild->GetMemberSlotWithdrawRem(pl->GetGUIDLow(), BankTab);
1518 if(remRight <= 0)
1519 return;
1522 // logging item move to bank
1523 if(_player->GetSession()->GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE))
1525 sLog.outCommand(_player->GetSession()->GetAccountId(),"GM %s (Account: %u) deposit item: %s (Entry: %d Count: %u) to guild bank (Guild ID: %u )",
1526 _player->GetName(),_player->GetSession()->GetAccountId(),
1527 pItemChar->GetProto()->Name1,pItemChar->GetEntry(),pItemChar->GetCount(),
1528 GuildId);
1531 CharacterDatabase.BeginTransaction();
1532 if(pItemBank)
1533 pGuild->LogBankEvent(GUILD_BANK_LOG_WITHDRAW_ITEM, BankTab, pl->GetGUIDLow(), pItemBank->GetEntry(), pItemBank->GetCount());
1534 pGuild->LogBankEvent(GUILD_BANK_LOG_DEPOSIT_ITEM, BankTab, pl->GetGUIDLow(), pItemChar->GetEntry(), pItemChar->GetCount());
1536 pl->MoveItemFromInventory(PlayerBag, PlayerSlot, true);
1537 pItemChar->DeleteFromInventoryDB();
1538 if(pItemBank)
1539 pGuild->RemoveItem(BankTab, BankTabSlot);
1541 pGuild->StoreItem(BankTab,gDest,pItemChar);
1542 if(pItemBank)
1543 pl->MoveItemToInventory(iDest,pItemBank,true);
1544 pl->SaveInventoryAndGoldToDB();
1545 if(pItemBank)
1546 pGuild->MemberItemWithdraw(BankTab, pl->GetGUIDLow());
1547 CharacterDatabase.CommitTransaction();
1549 pGuild->DisplayGuildBankContentUpdate(BankTab,gDest);
1554 void WorldSession::HandleGuildBankBuyTab( WorldPacket & recv_data )
1556 sLog.outDebug("WORLD: Received (CMSG_GUILD_BANK_BUY_TAB)");
1557 CHECK_PACKET_SIZE(recv_data, 8+1);
1558 //recv_data.hexlike();
1559 uint64 GoGuid;
1560 uint8 TabId;
1562 recv_data >> GoGuid;
1563 recv_data >> TabId;
1565 if (!objmgr.IsGuildVaultGameObject(_player, GoGuid))
1566 return;
1568 uint32 GuildId = GetPlayer()->GetGuildId();
1569 if (GuildId==0)
1570 return;
1572 Guild *pGuild = objmgr.GetGuildById(GuildId);
1573 if(!pGuild)
1574 return;
1576 uint32 TabCost = objmgr.GetGuildBankTabPrice(TabId) * GOLD;
1577 if (!TabCost)
1578 return;
1580 if (pGuild->GetPurchasedTabs() >= GUILD_BANK_MAX_TABS)
1581 return;
1583 if (TabId != pGuild->GetPurchasedTabs()) // purchased_tabs = 0 when buying Tab 0, that is why this check can be made
1585 sLog.outError("Error: trying to buy a tab non contigous to owned ones");
1586 return;
1589 if (GetPlayer()->GetMoney() < TabCost) // Should not happen, this is checked by client
1590 return;
1592 // Go on with creating tab
1593 pGuild->CreateNewBankTab();
1594 GetPlayer()->ModifyMoney(-int(TabCost));
1595 pGuild->SetBankMoneyPerDay(GetPlayer()->GetRank(), WITHDRAW_MONEY_UNLIMITED);
1596 pGuild->SetBankRightsAndSlots(GetPlayer()->GetRank(), TabId, GUILD_BANK_RIGHT_FULL, WITHDRAW_SLOT_UNLIMITED, true);
1597 pGuild->Roster(this);
1598 pGuild->DisplayGuildBankTabsInfo(this);
1601 void WorldSession::HandleGuildBankModifyTab( WorldPacket & recv_data )
1603 sLog.outDebug("WORLD: Received (CMSG_GUILD_BANK_UPDATE_TAB)");
1604 //recv_data.hexlike();
1605 CHECK_PACKET_SIZE(recv_data, 8+1+1+1);
1606 uint64 GoGuid;
1607 uint8 TabId;
1608 std::string Name;
1609 std::string IconIndex;
1611 recv_data >> GoGuid;
1612 recv_data >> TabId;
1613 recv_data >> Name;
1614 recv_data >> IconIndex;
1616 if(Name.empty())
1617 return;
1619 if(IconIndex.empty())
1620 return;
1622 if (!objmgr.IsGuildVaultGameObject(_player, GoGuid))
1623 return;
1625 uint32 GuildId = GetPlayer()->GetGuildId();
1626 if (GuildId==0)
1627 return;
1629 Guild *pGuild = objmgr.GetGuildById(GuildId);
1630 if(!pGuild)
1631 return;
1633 pGuild->SetGuildBankTabInfo(TabId, Name, IconIndex);
1634 pGuild->DisplayGuildBankTabsInfo(this);
1635 pGuild->DisplayGuildBankContent(this, TabId);
1638 void WorldSession::HandleGuildBankLog( WorldPacket & recv_data )
1640 sLog.outDebug("WORLD: Received (MSG_GUILD_BANK_LOG_QUERY)");
1641 CHECK_PACKET_SIZE(recv_data, 1);
1643 uint32 GuildId = GetPlayer()->GetGuildId();
1644 if (GuildId == 0)
1645 return;
1647 Guild *pGuild = objmgr.GetGuildById(GuildId);
1648 if(!pGuild)
1649 return;
1651 uint8 TabId;
1652 recv_data >> TabId;
1654 pGuild->DisplayGuildBankLogs(this, TabId);
1657 void WorldSession::HandleGuildBankTabText(WorldPacket &recv_data)
1659 sLog.outDebug("WORLD: Received MSG_QUERY_GUILD_BANK_TEXT");
1660 CHECK_PACKET_SIZE(recv_data, 1);
1662 uint32 GuildId = GetPlayer()->GetGuildId();
1663 if (GuildId == 0)
1664 return;
1666 Guild *pGuild = objmgr.GetGuildById(GuildId);
1667 if(!pGuild)
1668 return;
1670 uint8 TabId;
1671 recv_data >> TabId;
1673 pGuild->SendGuildBankTabText(this, TabId);
1676 void WorldSession::HandleGuildBankSetTabText(WorldPacket &recv_data)
1678 sLog.outDebug("WORLD: Received CMSG_SET_GUILD_BANK_TEXT");
1679 CHECK_PACKET_SIZE(recv_data, 1+1);
1681 uint32 GuildId = GetPlayer()->GetGuildId();
1682 if (GuildId == 0)
1683 return;
1685 Guild *pGuild = objmgr.GetGuildById(GuildId);
1686 if(!pGuild)
1687 return;
1689 uint8 TabId;
1690 std::string Text;
1691 recv_data >> TabId;
1692 recv_data >> Text;
1694 pGuild->SetGuildBankTabText(TabId, Text);
1697 void WorldSession::SendSaveGuildEmblem( uint32 msg )
1699 WorldPacket data(MSG_SAVE_GUILD_EMBLEM, 4);
1700 data << uint32(msg); // not part of guild
1701 SendPacket( &data );