Author Topic: Interlude CWH items problem after changing leader  (Read 239 times)

Offline ver

  • Heir
  • **
  • Posts: 22
CWH items problem after changing leader
« on: January 10, 2021, 01:16:10 PM »
Hello,

I had a situation when a clan leader of a clan was changed to a clan member with full cwh rights. Leadership has been changed without any problem - old leader lost his access to cwh. However all items in DB were still assigned to the old leader (owner_id was still set to old leaders char ID).

After the restart of game server all itemscontainer (or whatever its called) was restored. Unfortunatelly there were no items in CWH because all of'em were still assigned to old leader.

As I have traced the code I have found that here
java/org/l2jmobius/gameserver/model/clan/Clan.java
Code: [Select]

        public boolean setLeader(ClanMember member)
        {
                if (member == null)
                {
                        return false;
                }

                final ClanMember oldLeader = _leader;
                _leader = member;
                _members.put(member.getName(), member);

                // refresh oldleader and new leader info
                if (oldLeader != null)
                {
                        final PlayerInstance exLeader = oldLeader.getPlayerInstance();
                        exLeader.setClan(this);
                        exLeader.setPledgeClass(exLeader.getClan().getClanMember(exLeader.getObjectId()).calculatePledgeClass(exLeader));
                        exLeader.setClanPrivileges(CP_NOTHING);

                        exLeader.broadcastUserInfo();

                        CrownManager.getInstance().checkCrowns(exLeader);
                        // -------------------------------------------------------------------------------------
                        // HERE something to assing CLANWH items from exLeader to CLANWH items of member (new leader)
                }

                updateClanInDB();

                if (member.getPlayerInstance() != null)
                {
                        final PlayerInstance newLeader = member.getPlayerInstance();
                        newLeader.setClan(this);
                        newLeader.setPledgeClass(member.calculatePledgeClass(newLeader));
                        newLeader.setClanPrivileges(CP_ALL);

                        newLeader.broadcastUserInfo();
                }

                broadcastClanStatus();

                CrownManager.getInstance().checkCrowns(member.getPlayerInstance());

                return true;
        }


 

Offline ver

  • Heir
  • **
  • Posts: 22
Re: CWH items problem after changing leader
« Reply #1 on: January 19, 2021, 11:59:06 PM »
I made a simple method that I call while CL is changed.

java/org/l2jmobius/gameserver/model/clan/Clan.java  (manual patch)
Code: [Select]
        public void setNewLeader(ClanMember member, PlayerInstance player)
        {
                if (player.isRiding() || player.isFlying())
                {
                        player.sendPacket(ActionFailed.STATIC_PACKET);
                        return;
                }
                if (!_leader.isOnline())
                {
                        return;
                }

                if (member == null)
                {
                        return;
                }

                if (!member.isOnline())
                {
                        return;
                }

                if (setLeader(member))
                {
+                        transferCWHToNewLeader(member.getPlayerInstance().getObjectId(), player.getObjectId());

                        final SystemMessage sm = new SystemMessage(SystemMessageId.CLAN_LORD_PRIVILEGES_HAVE_BEEN_TRANSFERRED_TO_S1);
                        sm.addString(member.getName());
                        broadcastToOnlineMembers(sm);
                }
        }

+
+        public void transferCWHToNewLeader(int newLeaderId, int oldLeaderId)
+        {
+                 // no need to update anything...
+                 if (newLeaderId == oldLeaderId)
+                 {
+                    return;
+                 }
+
+                // update cwh in DB
+               try (Connection con = DatabaseFactory.getConnection())
+                {
+                        final PreparedStatement statement = con.prepareStatement("UPDATE items SET owner_id = ? WHERE loc = 'CLANWH' AND owner_id = ?");
+                        statement.setInt(1, newLeaderId);
+                        statement.setInt(2, oldLeaderId);
+                        statement.execute();
+                        statement.close();
+                }
+                catch (Exception e)
+                {
+                        LOGGER.warning("error while transferring cwh " + e);
+                }
+        }
+

Maybe not so pretty, but at least works.

Offline G-hamsteR

  • Inner Circle
  • Baron
  • *****
  • Posts: 220
Re: CWH items problem after changing leader
« Reply #2 on: January 20, 2021, 07:10:26 PM »
Are in-game warehouse items instantly transferring to the new clan owner? This makes the change to the database. Thank you for sharing!

Offline ver

  • Heir
  • **
  • Posts: 22
Re: CWH items problem after changing leader
« Reply #3 on: January 20, 2021, 09:32:51 PM »
The thing is after CL change all items and privs were working pretty much normal. This "problem" appeared after restart where items in clanwh were still binded to old leader (were not changed).
With that method I didn't see any problems.

However I might be wrong... I'm still noob in that l2j java thing ;) maybe additional loop on items already loaded from cwh would be good.

Online Mobius

  • Administrator
  • Distinguished King
  • *****
  • Posts: 8794