All pastes #24264 Raw Edit

Miscellany

public text v1 · immutable
#24264 ·published 2005-09-30 00:30 UTC
rendered paste body
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package net.sf.l2j.gameserver.model;

import java.io.File;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Logger;

import net.sf.l2j.Config;
import net.sf.l2j.L2DatabaseFactory;
import net.sf.l2j.gameserver.serverpackets.PledgeShowMemberListAll;
import net.sf.l2j.gameserver.serverpackets.PledgeStatusChanged;
import net.sf.l2j.gameserver.serverpackets.ServerBasePacket;
import net.sf.l2j.gameserver.serverpackets.SystemMessage;

public class L2Alliance
{
    private static final Logger _log = Logger.getLogger(L2Clan.class.getName());

    private String _name;
    private int _allyId;
    private L2Clan _leader;
    private boolean _hasCrest;
    private Map<String, L2Clan> _members = new TreeMap<String, L2Clan>();
    
    /**
     * called if a alliance is referenced only by id.
     * in this case all other data needs to be fetched from db
     * @param allyId
     */
    public L2Alliance (int allyId)
    {
        _allyId = allyId;
        restore();
        File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Alliance_"+_allyId+".bmp");
        _hasCrest = crestFile.exists();
    }
    
    /**
     * this is only called if a new alliance is created
     * @param allyId
     * @param clanName
     * @param leader
     */
    public L2Alliance(int allyId, String allianceName, L2Clan leader)
    {
        _allyId = allyId;
        _name = allianceName;
        setLeader(leader);
    }
    
    /**
     * @return Returns the clanId.
     */
    public int getAllianceId()
    {
        return _allyId;
    }
    
    /**
     * @param clanId The clanId to set.
     */
    public void setAllianceId(int allianceId)
    {
        _allyId = allianceId;
    }
    
    /**
     * @return Returns the leaderId.
     */
    public int getLeaderId()
    {
        return _leader.getClanId();
    }
    
    /**
     * @param leaderId The leaderId to set.
     */
    public void setLeader(L2Clan leader)
    {
        _leader = leader;
        _members.put(leader.getName(), leader);
    }
    
    /**
     * @return Returns the name.
     */
    public String getName()
    {
        return _name;
    }
    
    /**
     * @param name The name to set.
     */
    public void setName(String name)
    {
        _name = name;
    }
    
    public void addAllyMember(L2Clan newClan)
    {
        _members.put(newClan.getName(), newClan);
        // store in db
        storeNewMembersInDatabase(newClan);
    }
    
    private void storeNewMembersInDatabase(L2Clan member)
    {
        java.sql.Connection con = null;
        L2ClanMember[] members = member.getMembers();
        
        for (int i = 0; i < members.length; i++)
        {    
            try
            {
                con = L2DatabaseFactory.getInstance().getConnection();
                PreparedStatement statement = con.prepareStatement("UPDATE characters SET allyid=? WHERE obj_Id=?");
                statement.setInt(1, getAllianceId());
                statement.setInt(2, members[i].getObjectId());
                statement.execute();
                statement.close();                  
                if (Config.DEBUG) _log.fine("New clan member saved in db: "+members[i].getObjectId());
            }
            catch (Exception e)
            {
                _log.warning("error while saving new clan member to db "+e);
            }
            finally
            {
                try { con.close(); } catch (Exception e) {}
            }
        }
    }
    
    public void removeAlliance()
    {
        broadcastToOnlineMembers(new SystemMessage(SystemMessage.CLAN_MEMBERSHIP_TERMINATED));
        for (L2ClanMember member : getMembers())
        {
            removeClanMember(member.getName());
        }
        for (L2ItemInstance item : _warehouse.listItems())
        {
            _warehouse.destroyItem(item.getItemId(),item.getCount());
        }
        
        java.sql.Connection con = null;
        try
        {
            con = L2DatabaseFactory.getInstance().getConnection();
            PreparedStatement statement = con.prepareStatement("DELETE FROM clan_data WHERE clan_id=?");
            statement.setInt(1, getClanId());
            statement.execute();
            statement.close();                  
            if (Config.DEBUG) _log.fine("clan removed in db: "+getClanId());
        }
        catch (Exception e)
        {
            _log.warning("error while removing clan in db "+e);
        }
        finally
        {
            try { con.close(); } catch (Exception e) {}
        }
    }
}