All pastes #25222 Raw Edit

Miscellany

public java v1 · immutable
#25222 ·published 2005-10-11 14:23 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 allianceName     * @param leader     */    public L2Alliance(int allyId, String allianceName, L2Clan leader)    {        _allyId = allyId;        _name = allianceName;        setLeader(leader);    }        /**     * @return Returns the allianceId.     */    public int getAllianceId()    {        return _allyId;    }        /**     * @param allianceId 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 addAllianceMember(L2Clan newClan)    {        _members.put(newClan.getName(), newClan);        // store in db        storeNewMembersInDatabase(newClan);    }        public void removeAlliance()    {        broadcastToOnlineMembers(new SystemMessage(SystemMessage.CLAN_MEMBERSHIP_TERMINATED));                _members.clear();                java.sql.Connection con = null;        try        {            con = L2DatabaseFactory.getInstance().getConnection();            PreparedStatement statement = con.prepareStatement("DELETE FROM alliance_data WHERE ally_id=?");            statement.setInt(1, getAllianceId());            statement.execute();            statement.close();                              if (Config.DEBUG) _log.fine("Alliance removed in db: "+getAllianceId());        }        catch (Exception e)        {            _log.warning("Error while removing alliance in db "+e);        }        finally        {            try { con.close(); } catch (Exception e) {}        }    }            private void storeNewMembersInDatabase(L2Clan member)    {        java.sql.Connection con = null;            try            {                con = L2DatabaseFactory.getInstance().getConnection();                PreparedStatement statement = con.prepareStatement("UPDATE clan_data SET ally_id=? WHERE obj_Id=?");                statement.setInt(1, getAllianceId());                statement.setInt(2, member.getClanId());                statement.execute();                statement.close();                                                  //Get all clan members and update their allyId in database                        for (L2ClanMember clanMember : member.getMembers())                {                    statement = con.prepareStatement("UPDATE characters SET allyId=? WHERE obj_Id=?");                    statement.setInt(1, getAllianceId());                    statement.setInt(2, clanMember.getObjectId());                    statement.execute();                    statement.close();                }                if (Config.DEBUG) _log.fine("New alliance member saved in db: "+member.getClanId());            }            catch (Exception e)            {                _log.warning("Error while saving new alliance member to db "+e);            }            finally            {                try { con.close(); } catch (Exception e) {}            }    }        private void removeMemberInDatabase(L2Clan member)    {        java.sql.Connection con = null;        try        {            con = L2DatabaseFactory.getInstance().getConnection();            PreparedStatement statement = con.prepareStatement("UPDATE clan_data SET ally_id=0, WHERE clan_Id=?");            statement.setString(1, "");            statement.setInt(2, member.getClanId());            statement.execute();            statement.close();                        for (L2ClanMember clanMember : member.getMembers())            {                statement = con.prepareStatement("UPDATE characters SET allyId=0 WHERE obj_Id=?");                statement.setString(1, "");                statement.setInt(2, clanMember.getObjectId());                statement.execute();                statement.close();            }            if (Config.DEBUG) _log.fine("Alliance member removed in db: "+member.getClanId());        }        catch (Exception e)        {            _log.warning("Error while removing clan member in db "+e);        }        finally        {            try { con.close(); } catch (Exception e) {}        }    }        public void store()    {        java.sql.Connection con = null;        try        {            con = L2DatabaseFactory.getInstance().getConnection();            PreparedStatement statement = con.prepareStatement("INSERT INTO alliance_data (alliance_id,alliance_name,leader_id) values (?,?,?)");            statement.setInt(1, getAllianceId());            statement.setString(2, getName());            statement.setInt(3, getLeaderId());            statement.execute();            statement.close();                        statement = con.prepareStatement("UPDATE clan_table SET ally_id=? WHERE clan_id=?");            statement.setInt(1, getAllianceId());            statement.setInt(2, getLeaderId());            statement.execute();            statement.close();                        statement = con.prepareStatement("UPDATE characters SET allyId=? WHERE obj_Id=?");            statement.setInt(1, getAllianceId());            statement.setInt(2, getLeaderId());            statement.execute();            statement.close();                                    if (Config.DEBUG) _log.fine("New alliance saved in db: "+getAllianceId());        }        catch (Exception e)        {            _log.warning("Error while saving new alliance to db "+e);        }        finally        {            try { con.close(); } catch (Exception e) {}        }    }        private void restore()    {        java.sql.Connection con = null;        try        {                        con = L2DatabaseFactory.getInstance().getConnection();            PreparedStatement statement = con.prepareStatement("SELECT ally_name,leader_id FROM alliance_data where ally_id=?");            statement.setInt(1, getAllianceId());            ResultSet allianceData = statement.executeQuery();                        if (allianceData.next())            {                setName(allianceData.getString("ally_name"));                int leaderId = (allianceData.getInt("leader_id"));                                PreparedStatement statement2 = con.prepareStatement("SELECT clan_id FROM clan_data WHERE allyid=?");                statement2.setInt(1, getAllianceId());                ResultSet allianceMembers = statement2.executeQuery();                                while (allianceMembers.next())                {                   L2Clan member = new L2Clan(allianceMembers.getInt("clan_id"));                   addAllianceMember(member);                                      if (member.getClanId() == leaderId)                   {                       setLeader(member);                   }                   else                   {                       addAllianceMember(member);                   }                }                                allianceMembers.close();                statement2.close();            }            allianceData.close();            statement.close();                        if (getName() != null)                _log.config("Restored alliance data for \"" + getName() + "\" from database.");        }        catch (Exception e)        {            _log.warning("Error while restoring alliance "+e);        }        finally        {            try { con.close(); } catch (Exception e) {}        }    }        public void broadcastToOnlineMembers(ServerBasePacket packet)    {            }}