All pastes #21167 Raw Edit

Anonymous

public text v1 · immutable
#21167 ·published 2005-08-25 23:39 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.util.logging.Level;
import java.util.logging.Logger;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import net.sf.l2j.L2DatabaseFactory;
import net.sf.l2j.gameserver.ClientThread;
import net.sf.l2j.gameserver.serverpackets.SystemMessage;

public class L2Friend
{
	private static final Logger _log = Logger.getLogger(L2Friend.class.getName());
	
	private String _name;
	
	public void friendInvite(L2PcInstance activeChar, String target)
	{
		java.sql.Connection con = null;
		
		L2PcInstance targetObject = L2World.getInstance().getPlayer(target);
			
		try 
		{
		    con = L2DatabaseFactory.getInstance().getConnection();
		    PreparedStatement statement = con.prepareStatement("select char_id, friend_name from character_friends where char_id=? and friend_name=?");
		    statement.setInt(1, activeChar.getObjectId());
		    statement.setString(2, target);
		    ResultSet rset = statement.executeQuery();
		    if (rset.next())
		    {
		    	SystemMessage sm1 = new SystemMessage(SystemMessage.S1_IS_ALREADY_IN_YOUR_FRIEND_LIST);
		    	sm1.addString(target);
		    	activeChar.sendPacket(sm1);
		    } 
		    else 
		    {
		    	statement = con.prepareStatement("select * from characters where char_name=?");
		    	statement.setString(1, target);
		    	rset = statement.executeQuery();
		    	if (!rset.next())
		    	{
		    		SystemMessage sm2 = new SystemMessage(SystemMessage.THE_USER_WHO_REQUESTED_TO_BECOME_FRIENDS_IS_NOT_FOUND_IN_THE_GAME);
		    		activeChar.sendPacket(sm2);
		    	}
		    	else if (rset.getInt("obj_Id") == activeChar.getObjectId())
		    	{
		    		SystemMessage sm3 = new SystemMessage(SystemMessage.YOU_CANNOT_ADD_YOURSELF_TO_YOUR_OWN_FRIEND_LIST);
		    		activeChar.sendPacket(sm3);
		    	} 
		    	else
		    	{
		    		//Send a request to become friends
		    		SystemMessage sm4 = new SystemMessage(SystemMessage.S1_HAS_REQUESTED_TO_BECOME_FRIENDS);
		    		targetObject.sendPacket(sm4);
		    		//sm.addString("Player "+_name+" added to your friendlist.");
		    	}
		    }  
			rset.close();
			statement.close();
		} 
		catch (Exception e)
		{
		    _log.log(Level.WARNING, "Could not add friend objectid: ", e);
		}
		finally
		{
		    try { con.close(); } catch (Exception e) {}
		}
	}
	
	public void friendAdd(L2PcInstance activeChar, String target)
	{
		java.sql.Connection con = null;
		
		try		
		{
			con = L2DatabaseFactory.getInstance().getConnection();
			PreparedStatement statement = con.prepareStatement("insert into character_friends (char_id,friend_name) values(?,?)");
			statement.setInt(1, activeChar.getObjectId());
			statement.setString(2, target);
			statement.execute();
			statement.close();
			SystemMessage sm1 = new SystemMessage(SystemMessage.YOU_HAVE_SUCCEEDED_IN_INVITING_A_FRIEND);
			SystemMessage sm2 = new SystemMessage(SystemMessage.S1_HAS_BEEN_ADDED_TO_YOUR_FRIEND_LIST);
			activeChar.sendPacket(sm1);
			activeChar.sendPacket(sm2);
		}
		catch (Exception e)
		{
		    _log.log(Level.WARNING, "Could not add friend objectid: ", e);
		}
		finally
		{
		    try { con.close(); } catch (Exception e) {}
		}
	}
}