ClientThread.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
net.sf.l2j.gameserver |
![]() |
![]() |
L2J |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* $Header: /cvsroot/l2j/L2_Gameserver/java/net/sf/l2j/gameserver/ClientThread.java,v 1.21 2004/11/19 08:54:43 l2chef Exp $
*
* $Author: l2chef $
* $Date: 2004/11/19 08:54:43 $
* $Revision: 1.21 $
* $Log: ClientThread.java,v $
* Revision 1.21 2004/11/19 08:54:43 l2chef
* database is now used
*
* Revision 1.20 2004/11/14 20:31:01 dethx
* minor mp bonus fixes (DETH)
*
* Revision 1.19 2004/11/07 13:00:38 l2chef
* basemp and effectivemp are now used
*
* Revision 1.18 2004/10/17 06:46:23 l2chef
* no more direct access to Item collection to avoid wrong usage
*
* Revision 1.17 2004/09/16 00:03:04 l2chef
* fistweapon is set on restore (Deth)
*
* Revision 1.16 2004/08/14 22:30:19 l2chef
* unknown methods renamed
*
* Revision 1.15 2004/08/10 00:47:00 l2chef
* clan reference is set on load, automatic expel from clan added
*
* Revision 1.14 2004/08/08 22:57:00 l2chef
* resetting active char is now possible
*
* Revision 1.13 2004/08/08 16:33:34 l2chef
* deleteme method of L2pcinstance is used to cleanup
*
* Revision 1.12 2004/07/25 23:00:38 l2chef
* pet system started (whatev)
*
* Revision 1.11 2004/07/25 00:36:49 l2chef
* warehouse file is now also deleted when a char is deleted (Deth)
*
* Revision 1.10 2004/07/23 01:42:35 l2chef
* all object spawn and delete is now handeld in L2PcInstance
*
* Revision 1.9 2004/07/19 02:02:56 l2chef
* party code completed (whatev)
* some exception cases fixed
*
* Revision 1.8 2004/07/17 12:15:43 l2chef
* logging removed (NuocNam)
*
* Revision 1.7 2004/07/13 23:17:05 l2chef
* log message added and empty blocks commented
*
* Revision 1.6 2004/07/12 20:53:03 l2chef
* warehouses added (nuocnam)
* char data is now stored in subfolder data/accounts
*
* Revision 1.5 2004/07/11 23:41:22 l2chef
* chars are always reloaded from disk and stored every 15mins (whatev)
*
* Revision 1.4 2004/07/05 23:03:31 l2chef
* access levels are stored for logins
*
* Revision 1.3 2004/06/30 21:51:33 l2chef
* using jdk logger instead of println
*
* Revision 1.2 2004/06/27 08:51:43 jeichhorn
* Added copyright notice
*
*
* 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;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.logging.Logger;
import net.sf.l2j.L2DatabaseFactory;
import net.sf.l2j.gameserver.model.L2Clan;
import net.sf.l2j.gameserver.model.L2ItemInstance;
import net.sf.l2j.gameserver.model.L2PcInstance;
import net.sf.l2j.gameserver.model.L2ShortCut;
import net.sf.l2j.gameserver.model.L2Skill;
import net.sf.l2j.gameserver.model.L2World;
import net.sf.l2j.gameserver.templates.L2Item;
import net.sf.l2j.loginserver.LoginController;
/**
* This class ...
*
* @version $Revision: 1.21 $ $Date: 2004/11/19 08:54:43 $
*/
public class ClientThread extends Thread
{
private static Logger _log = Logger.getLogger(ClientThread.class.getName());
private String _loginName;
private L2PcInstance _activeChar;
private int _sessionId;
private byte[] _filter;
private byte[] _cryptkey = {
(byte)0x94, (byte)0x35, (byte)0x00, (byte)0x00,
(byte)0xa1, (byte)0x6c, (byte)0x54, (byte)0x87 // these 4 bytes are fixed
};
private File _userFolder;
private File _charFolder;
private long _autoSaveTime;
private Connection _connection;
private PacketHandler _handler;
private L2World _world;
public ClientThread(Socket client) throws IOException
{
_connection = new Connection(client, _cryptkey);
_sessionId = 0x12345678;
_handler = new PacketHandler(this);
_world = L2World.getInstance();
_autoSaveTime = 60000 * 15;//15 min
start();
}
public void run()
{
_log.fine("thread[C] started");
long starttime = System.currentTimeMillis();
boolean checksumOk = false;
try
{
while (true)
{
if ((_activeChar != null) && (_autoSaveTime < (System.currentTimeMillis() - starttime)))
{
saveCharToDisk(_activeChar);
starttime = System.currentTimeMillis();
}
byte[] decrypt = _connection.getPacket();
_handler.handlePacket(decrypt);
}
}
catch (IOException io)
{
// this happens when the client disconnects
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (_activeChar != null) // this should only happen on connection loss
{
// notify the world about our disconnect
_activeChar.deleteMe();
try
{
saveCharToDisk(_activeChar);
}
catch (Exception e2)
{
// ignore any problems here
}
}
_connection.close();
}
catch (Exception e1)
{
_log.warning(e1.toString());
}
finally
{
// remove the account
LoginController.getInstance().removeGameServerLogin(getLoginName());
}
}
_log.fine("gameserver thread[C] stopped");
}
/**
*
*/
public void saveCharToDisk(L2PcInstance cha)
{
storeChar(cha);
storeInventory(cha);
storeSkills(cha);
storeShortcuts(cha);
storeWarehouse(cha);
IdFactory.getInstance().saveCurrentState();
}
/**
* @param cha
* @param saveFile
*/
private void storeShortcuts(L2PcInstance cha)
{
try
{
L2ShortCut[] scs = cha.getAllShortCuts();
java.sql.Connection con = null;
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("DELETE FROM character_shortcuts WHERE char_obj_id=?");
statement.setInt(1, cha.getObjectId());
statement.execute();
statement.close();
con.close();
for (int x =0 ; x < scs.length;x++)
{
statement = con.prepareStatement("INSERT INTO character_shortcuts SET char_obj_id=?,slot=?,type=?,shortcut_id=?,level=?,unknown=?");
statement.setInt(1, cha.getObjectId());
statement.setInt(2, scs[x].getSlot());
statement.setInt(3, scs[x].getType());
statement.setInt(4, scs[x].getId());
statement.setInt(5, scs[x].getLevel());
statement.setInt(6, scs[x].getUnk());
statement.execute();
statement.close();
}
}
catch (Exception e)
{
_log.warning("could not store shortcuts:" + e.toString());
e.printStackTrace();
}
}
public void deleteChar(int charslot) throws Exception
{
//have to make sure active character must be nulled
if (getActiveChar() != null)
{
saveCharToDisk (getActiveChar());
_log.fine("active Char saved");
_activeChar = null;
}
int objid = 0;
String charname="";
java.sql.Connection con = null;
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("SELECT obj_id FROM characters WHERE account_name=? ORDER BY char_name");
statement.setString(1, _loginName);
ResultSet result = statement.executeQuery();
while (result.next())
{
if (result.getRow() == charslot+1)
{
objid = result.getInt("obj_id");
break;
}
}
result.close();
statement.close();
statement = con.prepareStatement("DELETE FROM characters WHERE obj_Id=?");
statement.setInt(1, objid);
statement.execute();
statement.close();
statement = con.prepareStatement("DELETE FROM character_shortcuts WHERE char_obj_id=?");
statement.setInt(1, objid);
statement.execute();
statement.close();
statement = con.prepareStatement("DELETE FROM character_inventory WHERE obj_Id=?");
statement.setInt(1, objid);
statement.execute();
statement.close();
statement = con.prepareStatement("DELETE FROM character_warehouse WHERE obj_Id=?");
statement.setInt(1, objid);
statement.execute();
statement.close();
statement = con.prepareStatement("DELETE FROM character_skills WHERE obj_Id=?");
statement.setInt(1, objid);
statement.execute();
statement.close();
con.close();
}
public L2PcInstance loadCharFromDisk(int charslot)
{
L2PcInstance character = new L2PcInstance();
character = restoreChar(charslot);
if (character != null)
{
restoreInventory(character);
restoreSkills(character);
restoreShortCuts(character);
restoreWarehouse(character);
if (character.getClanId() != 0)
{
L2Clan clan = ClanTable.getInstance().getClan(character.getClanId());
if (!clan.isMember(character.getName()))
{
// char has been kicked from clan
character.setClanId(0);
character.setTitle("");
}
else
{
character.setClan(clan);
character.setIsClanLeader(clan.getLeaderId() == character.getObjectId());
}
}
}
else
{
_log.warning("could not restore in slot:"+ charslot);
}
//setCharacter(character);
return character;
}
/**
* @param file
* @param restored
*/
private void restoreShortCuts(L2PcInstance restored)
{
try
{
java.sql.Connection con = null;
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("SELECT * FROM character_shortcuts WHERE char_obj_id=?");
statement.setInt(1, restored.getObjectId());
ResultSet rset = statement.executeQuery();
while (rset.next())
{
int slot = rset.getInt("slot");
int type = rset.getInt("type");
int id = rset.getInt("shortcut_id");
int level = rset.getInt("level");
int unk = rset.getInt("unknown");
L2ShortCut sc = new L2ShortCut(slot, type, id, level, unk);
restored.registerShortCut(sc);
}
rset.close();
statement.close();
}
catch (Exception e)
{
_log.warning("could not restore shortcuts:"+e);
}
}
/**
* @param cha
* @param saveFile
*/
private void storeInventory(L2PcInstance cha)
{
try
{
java.sql.Connection con = null;
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("DELETE FROM character_inventory WHERE char_obj_id=?");
statement.setInt(1, cha.getObjectId());
statement.execute();
statement.close();
L2ItemInstance[] inv = cha.getInventory().getItems();
L2ItemInstance temp;
for (int x =0 ; x < inv.length;x++)
{
temp = inv[x];
statement = con.prepareStatement("INSERT INTO character_inventory SET char_obj_id=?,object_id=?,item_id=?,name=?,count=?,price=?,equipSlot=?");
statement.setInt(1, cha.getObjectId());
statement.setInt(2, temp.getObjectId());
statement.setInt(3, temp.getItemId());
statement.setString(4, temp.getItem().getName());
statement.setInt(5, temp.getCount());
statement.setInt(6, temp.getPrice());
statement.setInt(7, temp.getEquipSlot());
statement.execute();
statement.close();
}
con.close();
}
catch (Exception e)
{
_log.warning("could not store characters inventory:"+e);
}
}
/**
* @param cha
* @param saveFile
*/
private void storeSkills(L2PcInstance cha)
{
try
{
java.sql.Connection con = null;
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("DELETE FROM character_skills WHERE char_obj_id=?");
statement.setInt(1, cha.getObjectId());
statement.execute();
statement.close();
L2Skill[] skills = cha.getAllSkills();
for (int count=0; count < skills.length; count++)
{
statement = con.prepareStatement("INSERT INTO character_skills SET char_obj_id=?,skill_id=?,skill_level=?,skill_name=?");
statement.setInt(1, cha.getObjectId());
statement.setInt(2, skills[count].getId());
statement.setInt(3, skills[count].getLevel());
statement.setString(4, skills[count].getName());
statement.execute();
statement.close();
}
con.close();
}
catch (Exception e)
{
_log.warning("could not store skills:"+e);
e.printStackTrace();
}
}
private void storeChar(L2PcInstance cha)
{
try
{
java.sql.Connection con = null;
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("DELETE FROM characters WHERE obj_id=?");
statement.setInt(1, cha.getObjectId());
statement.execute();
statement.close();
statement = con.prepareStatement("INSERT INTO characters SET account_name=?,obj_Id=?,char_name=?,level=?,maxHp=?,curHp=?,maxMp=?,curMp=?,acc=?,crit=?,evasion=?,mAtk=?,mDef=?,mSpd=?,pAtk=?,pDef=?,pSpd=?,runSpd=?,walkSpd=?,str=?,con=?,dex=?,_int=?,men=?,wit=?,face=?,hairStyle=?,hairColor=?,sex=?,heading=?,x=?,y=?,z=?,movement_multiplier=?,attack_speed_multiplier=?,colRad=?,colHeight=?,exp=?,sp=?,karma=?,pvpkills=?,pkkills=?,clanid=?,maxload=?,race=?,classid=?,deletetime=?,cancraft=?,title=?,allyId=?,accesslevel=?");
statement.setString(1, _loginName);
statement.setInt(2, cha.getObjectId());
statement.setString(3, cha.getName());
statement.setInt(4, cha.getLevel());
statement.setInt(5, cha.getMaxHp());
statement.setDouble(6, cha.getCurrentHp());
statement.setInt(7, cha.getMaxBaseMp());
statement.setDouble(8, cha.getCurrentMp());
statement.setInt(9, cha.getAccuracy());
statement.setInt(10, cha.getCriticalHit());
statement.setInt(11, cha.getEvasionRate());
statement.setInt(12, cha.getMagicalAttack());
statement.setInt(13, cha.getMagicalDefense());
statement.setInt(14, cha.getMagicalSpeed());
statement.setInt(15, cha.getPhysicalAttack());
statement.setInt(16, cha.getPhysicalDefense());
statement.setInt(17, cha.getPhysicalSpeed());
statement.setInt(18, cha.getRunSpeed());
statement.setInt(19, cha.getWalkSpeed());
statement.setInt(20, cha.getStr());
statement.setInt(21, cha.getCon());
statement.setInt(22, cha.getDex());
statement.setInt(23, cha.getInt());
statement.setInt(24, cha.getMen());
statement.setInt(25, cha.getWit());
statement.setInt(26, cha.getFace());
statement.setInt(27, cha.getHairStyle());
statement.setInt(28, cha.getHairColor());
statement.setInt(29, cha.getSex());
statement.setInt(30, cha.getHeading());
statement.setInt(31, cha.getX());
statement.setInt(32, cha.getY());
statement.setInt(33, cha.getZ());
statement.setDouble(34, cha.getMovementMultiplier());
statement.setDouble(35, cha.getAttackSpeedMultiplier());
statement.setDouble(36, cha.getCollisionRadius());
statement.setDouble(37, cha.getCollisionHeight());
statement.setInt(38, cha.getExp());
statement.setInt(39, cha.getSp());
statement.setInt(40, cha.getKarma());
statement.setInt(41, cha.getPvpKills());
statement.setInt(42, cha.getPkKills());
statement.setInt(43, cha.getClanId());
statement.setInt(44, cha.getMaxLoad());
statement.setInt(45, cha.getRace());
statement.setInt(46, cha.getClassId());
statement.setInt(47, cha.getDeleteTimer());
statement.setInt(48, cha.getCanCraft());
statement.setString(49, cha.getTitle());
statement.setInt(50, cha.getAllyId());
statement.setInt(51, cha.getAccessLevel());
statement.execute();
statement.close();
con.close();
}
catch (Exception e)
{
_log.warning("could not store char data:"+e);
}
}
private void restoreWarehouse(L2PcInstance cha)
{
try
{
java.sql.Connection con = null;
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("SELECT * FROM character_warehouse WHERE char_obj_id=?");
statement.setInt(1, cha.getObjectId());
ResultSet rset = statement.executeQuery();
L2ItemInstance item;
while (rset.next())
{
item = new L2ItemInstance();
item.setObjectId(rset.getInt(2));
int itemId = rset.getInt(3);
L2Item itemTemp = ItemTable.getInstance().getTemplate(itemId);
item.setItem(itemTemp);
item.setCount(rset.getInt(5));
cha.getWarehouse().addItem(item);
// add this item to the world
_world.storeObject(item);
}
rset.close();
statement.close();
con.close();
}
catch (Exception e)
{
_log.warning("could not restore warehouse:"+e);
}
}
private void storeWarehouse(L2PcInstance cha)
{
try
{
java.sql.Connection con = null;
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("DELETE FROM character_warehouse WHERE char_obj_id=?");
statement.setInt(1, cha.getObjectId());
statement.execute();
statement.close();
ArrayList wh = cha.getWarehouse().getItems();
L2ItemInstance temp;
for (int x =0 ; x < wh.size();x++)
{
temp = (L2ItemInstance) wh.get(x);
statement = con.prepareStatement("INSERT INTO character_warehouse SET char_obj_id=?,object_id=?,item_id=?,name=?,count=?");
statement.setInt(1, cha.getObjectId());
statement.setInt(2, temp.getObjectId());
statement.setInt(3, temp.getItemId());
statement.setString(4, temp.getItem().getName());
statement.setInt(5, temp.getCount());
statement.execute();
statement.close();
_log.fine("warehouse saved item:" + temp.getItem().getName());
}
con.close();
}
catch (Exception e)
{
_log.warning("could not store characters warehouse:"+e);
}
}
/**
* @param string
*/
private void restoreInventory(L2PcInstance cha)
{
try
{
java.sql.Connection con = null;
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("SELECT * FROM character_inventory WHERE char_obj_id=? ORDER BY object_id DESC");
statement.setInt(1, cha.getObjectId());
ResultSet inv = statement.executeQuery();
L2ItemInstance item;
while (inv.next())
{
item = new L2ItemInstance();
item.setObjectId(inv.getInt(2));
int itemId = inv.getInt(3);
L2Item itemTemp = ItemTable.getInstance().getTemplate(itemId);
item.setItem(itemTemp);
item.setCount(inv.getInt(5));
item.setPrice(inv.getInt(6));
item.setEquipSlot(inv.getInt(7));
cha.getInventory().addItem(item);
if (item.isEquipped())
{
cha.getInventory().equipItem(item);
}
// add this item to the world
_world.storeObject(item);
}
inv.close();
statement.close();
con.close();
cha.updateMpBonus();
}
catch (Exception e)
{
_log.warning("could not restore inventory:"+e);
}
finally
{
}
}
/**
* @param string
*/
private void restoreSkills(L2PcInstance cha)
{
try
{
java.sql.Connection con = null;
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("SELECT * FROM character_skills WHERE char_obj_id=?");
statement.setInt(1, cha.getObjectId());
ResultSet rset = statement.executeQuery();
while (rset.next())
{
int id = rset.getInt(2);
int level = rset.getInt(3);
L2Skill skill = SkillTable.getInstance().getInfo(id, level);
cha.addSkill(skill);
}
rset.close();
statement.close();
con.close();
}
catch (Exception e)
{
_log.warning("count not restore skills:"+e);
}
}
/**
* @param line
* @return
*/
private L2PcInstance restoreChar(int charslot)
{
L2PcInstance oldChar = new L2PcInstance();
try
{
java.sql.Connection con = null;
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("SELECT * FROM characters WHERE account_name=? ORDER BY char_name");
statement.setString(1, _loginName);
ResultSet rset = statement.executeQuery();
while (rset.next())
{
if (rset.getRow() != charslot+1)
{
continue;
}
oldChar.setObjectId(rset.getInt(2));
oldChar.setName(rset.getString(3));
oldChar.setLevel(rset.getInt(4));
oldChar.setMaxHp(rset.getInt(5));
oldChar.setCurrentHp(rset.getDouble(6));
oldChar.setMaxBaseMp(rset.getInt(7));
oldChar.setCurrentMp(rset.getDouble(8));
oldChar.setAccuracy(rset.getInt(9));
oldChar.setCriticalHit(rset.getInt(10));
oldChar.setEvasionRate(rset.getInt(11));
oldChar.setMagicalAttack(rset.getInt(12));
oldChar.setMagicalDefense(rset.getInt(13));
oldChar.setMagicalSpeed(rset.getInt(14));
oldChar.setPhysicalAttack(rset.getInt(15));
oldChar.setPhysicalDefense(rset.getInt(16));
oldChar.setPhysicalSpeed(rset.getInt(17));
oldChar.setRunSpeed(rset.getInt(18));
oldChar.setWalkSpeed(rset.getInt(19));
oldChar.setStr(rset.getInt(20));
oldChar.setCon(rset.getInt(21));
oldChar.setDex(rset.getInt(22));
oldChar.setInt(rset.getInt(23));
oldChar.setMen(rset.getInt(24));
oldChar.setWit(rset.getInt(25));
oldChar.setFace(rset.getInt(26));
oldChar.setHairStyle(rset.getInt(27));
oldChar.setHairColor(rset.getInt(28));
oldChar.setSex(rset.getInt(29));
oldChar.setHeading(rset.getInt(30));
oldChar.setX(rset.getInt(31));
oldChar.setY(rset.getInt(32));
oldChar.setZ(rset.getInt(33));
oldChar.setMovementMultiplier(rset.getDouble(34));
oldChar.setAttackSpeedMultiplier(rset.getDouble(35));
oldChar.setCollisionRadius(rset.getDouble(36));
oldChar.setCollisionHeight(rset.getDouble(37));
oldChar.setExp(rset.getInt(38));
oldChar.setSp(rset.getInt(39));
oldChar.setKarma(rset.getInt(40));
oldChar.setPvpKills(rset.getInt(41));
oldChar.setPkKills(rset.getInt(42));
oldChar.setClanId(rset.getInt(43));
oldChar.setMaxLoad(rset.getInt(44));
oldChar.setRace(rset.getInt(45));
oldChar.setClassId(rset.getInt(46));
oldChar.setDeleteTimer(rset.getInt(47));
oldChar.setCanCraft(rset.getInt(48));
oldChar.setTitle(rset.getString(49));
//oldChar.setCrestId(rset.getInt());
oldChar.setAllyId(rset.getInt(50));
oldChar.setAccessLevel(rset.getInt(51));
L2World.getInstance().storeObject(oldChar);
}
rset.close();
statement.close();
con.close();
}
catch (Exception e)
{
_log.warning("could not restore char data:"+e);
e.printStackTrace();
}
return oldChar;
}
/**
* @return
*/
public Connection getConnection()
{
return _connection;
}
/**
* @return
*/
public L2PcInstance getActiveChar()
{
return _activeChar;
}
/**
* @return Returns the sessionId.
*/
public int getSessionId()
{
return _sessionId;
}
public String getLoginName()
{
return _loginName;
}
public void setLoginFolder(String folder)
{
_charFolder = new File("data/accounts",_loginName);
_charFolder.mkdirs();
}
public void setLoginName(String loginName)
{
_loginName = loginName;
}
/**
* @param cha
*/
public void setActiveChar(L2PcInstance cha)
{
_activeChar = cha;
if (cha != null)
{
// we store the connection in the player object so that external
// events can directly send events to the players client
// might be changed later to use a central event management and distribution system
_activeChar.setNetConnection(_connection);
// update world data
_world.storeObject(_activeChar);
}
}
}
The table below shows all metrics for ClientThread.java.




