CharSummonTable.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.datatables;
  16. import gnu.trove.TIntIntHashMap;
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.L2DatabaseFactory;
  25. import com.l2jserver.gameserver.idfactory.IdFactory;
  26. import com.l2jserver.gameserver.model.L2ItemInstance;
  27. import com.l2jserver.gameserver.model.L2SummonItem;
  28. import com.l2jserver.gameserver.model.actor.instance.L2MerchantSummonInstance;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  31. import com.l2jserver.gameserver.model.actor.instance.L2SiegeSummonInstance;
  32. import com.l2jserver.gameserver.model.actor.instance.L2SummonInstance;
  33. import com.l2jserver.gameserver.model.base.Experience;
  34. import com.l2jserver.gameserver.network.serverpackets.PetItemList;
  35. import com.l2jserver.gameserver.skills.l2skills.L2SkillSummon;
  36. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  37. /**
  38. * @author Nyaran
  39. */
  40. public class CharSummonTable
  41. {
  42. private static Logger _log = Logger.getLogger(CharSummonTable.class.getName());
  43. private static final String INIT_SUMMONS = "SELECT ownerId, summonSkillId FROM character_summons";
  44. private static final String INIT_PET = "SELECT ownerId, item_obj_id FROM pets WHERE restore = 'true'";
  45. private static final String SAVE_SUMMON = "REPLACE INTO character_summons (ownerId,summonSkillId,curHp,curMp,time) VALUES (?,?,?,?,?)";
  46. private static final String LOAD_SUMMON = "SELECT curHp, curMp, time FROM character_summons WHERE ownerId = ? AND summonSkillId = ?";
  47. private static final String REMOVE_SUMMON = "DELETE FROM character_summons WHERE ownerId = ?";
  48. private static final TIntIntHashMap _servitors = new TIntIntHashMap();
  49. private static final TIntIntHashMap _pets = new TIntIntHashMap();
  50. public static CharSummonTable getInstance()
  51. {
  52. return SingletonHolder._instance;
  53. }
  54. public void init()
  55. {
  56. int ownerId;
  57. int refId;
  58. Connection con = null;
  59. if (Config.RESTORE_SERVITOR_ON_RECONNECT)
  60. {
  61. try
  62. {
  63. con = L2DatabaseFactory.getInstance().getConnection();
  64. PreparedStatement statement = con.prepareStatement(INIT_SUMMONS);
  65. ResultSet rset = statement.executeQuery();
  66. while (rset.next())
  67. {
  68. ownerId = rset.getInt("ownerId");
  69. refId = rset.getInt("summonSkillId");
  70. _servitors.put(ownerId, refId);
  71. }
  72. rset.close();
  73. statement.close();
  74. }
  75. catch (Exception e)
  76. {
  77. _log.log(Level.SEVERE, "Error while loading saved summons", e);
  78. }
  79. finally
  80. {
  81. L2DatabaseFactory.close(con);
  82. }
  83. }
  84. if (Config.RESTORE_PET_ON_RECONNECT)
  85. {
  86. try
  87. {
  88. con = L2DatabaseFactory.getInstance().getConnection();
  89. PreparedStatement statement = con.prepareStatement(INIT_PET);
  90. ResultSet rset = statement.executeQuery();
  91. while (rset.next())
  92. {
  93. ownerId = rset.getInt("ownerId");
  94. refId = rset.getInt("item_obj_id");
  95. _pets.put(ownerId, refId);
  96. }
  97. rset.close();
  98. statement.close();
  99. }
  100. catch (Exception e)
  101. {
  102. _log.log(Level.SEVERE, "Error while loading saved summons", e);
  103. }
  104. finally
  105. {
  106. L2DatabaseFactory.close(con);
  107. }
  108. }
  109. }
  110. public TIntIntHashMap getServitors()
  111. {
  112. return _servitors;
  113. }
  114. public TIntIntHashMap getPets()
  115. {
  116. return _pets;
  117. }
  118. public void saveSummon(L2SummonInstance summon)
  119. {
  120. Connection con = null;
  121. try
  122. {
  123. con = L2DatabaseFactory.getInstance().getConnection();
  124. PreparedStatement statement = con.prepareStatement(SAVE_SUMMON);
  125. statement.setInt(1, summon.getOwner().getObjectId());
  126. statement.setInt(2, summon.getReferenceSkill());
  127. statement.setDouble(3, summon.getCurrentHp());
  128. statement.setDouble(4, summon.getCurrentMp());
  129. statement.setInt(5, summon.getTimeRemaining());
  130. statement.execute();
  131. statement.close();
  132. _servitors.put(summon.getOwner().getObjectId(), summon.getReferenceSkill());
  133. }
  134. catch (Exception e)
  135. {
  136. _log.log(Level.SEVERE, "Failed to store summon [SummonId: " + summon.getNpcId() + "] from Char [CharId: " + summon.getOwner().getObjectId() + "] data", e);
  137. }
  138. finally
  139. {
  140. L2DatabaseFactory.close(con);
  141. }
  142. }
  143. public void restoreServitor(L2PcInstance activeChar)
  144. {
  145. Connection con = null;
  146. try
  147. {
  148. int skillId = _servitors.get(activeChar.getObjectId());
  149. con = L2DatabaseFactory.getInstance().getConnection();
  150. PreparedStatement statement = con.prepareStatement(LOAD_SUMMON);
  151. statement.setInt(1, activeChar.getObjectId());
  152. statement.setInt(2, skillId);
  153. ResultSet rset = statement.executeQuery();
  154. L2NpcTemplate summonTemplate;
  155. L2SummonInstance summon;
  156. L2SkillSummon skill;
  157. while (rset.next())
  158. {
  159. int curHp = rset.getInt("curHp");
  160. int curMp = rset.getInt("curMp");
  161. int time = rset.getInt("time");
  162. skill = (L2SkillSummon) SkillTable.getInstance().getInfo(skillId, activeChar.getSkillLevel(skillId));
  163. if (skill == null)
  164. {
  165. removeServitor(activeChar);
  166. return;
  167. }
  168. summonTemplate = NpcTable.getInstance().getTemplate(skill.getNpcId());
  169. if (summonTemplate == null)
  170. {
  171. _log.warning("[CharSummonTable] Summon attemp for nonexisting Skill ID:" + skillId);
  172. return;
  173. }
  174. if (summonTemplate.type.equalsIgnoreCase("L2SiegeSummon"))
  175. summon = new L2SiegeSummonInstance(IdFactory.getInstance().getNextId(), summonTemplate, activeChar, skill);
  176. /* TODO: Confirm L2Merchant
  177. else if (summonTemplate.type.equalsIgnoreCase("L2MerchantSummon"))
  178. summon = new L2MerchantSummonInstance(IdFactory.getInstance().getNextId(), summonTemplate, activeChar, skill);*/
  179. else
  180. summon = new L2SummonInstance(IdFactory.getInstance().getNextId(), summonTemplate, activeChar, skill);
  181. summon.setName(summonTemplate.name);
  182. summon.setTitle(activeChar.getName());
  183. summon.setExpPenalty(skill.getExpPenalty());
  184. if (summon.getLevel() >= Experience.PET_MAX_LEVEL)
  185. {
  186. summon.getStat().setExp(Experience.LEVEL[Experience.PET_MAX_LEVEL - 1]);
  187. _log.warning("Summon (" + summon.getName() + ") NpcID: " + summon.getNpcId() + " has a level above 86. Please rectify.");
  188. }
  189. else
  190. {
  191. summon.getStat().setExp(Experience.LEVEL[(summon.getLevel() % Experience.PET_MAX_LEVEL)]);
  192. }
  193. summon.setCurrentHp(curHp);
  194. summon.setCurrentMp(curMp);
  195. summon.setHeading(activeChar.getHeading());
  196. summon.setRunning();
  197. if (!(summon instanceof L2MerchantSummonInstance))
  198. activeChar.setPet(summon);
  199. summon.setTimeRemaining(time);
  200. //L2World.getInstance().storeObject(summon);
  201. summon.spawnMe(activeChar.getX() + 20, activeChar.getY() + 20, activeChar.getZ());
  202. }
  203. rset.close();
  204. statement.close();
  205. }
  206. catch (SQLException e)
  207. {
  208. _log.log(Level.WARNING, "[CharSummonTable]: Summon cannot be restored: ", e);
  209. }
  210. finally
  211. {
  212. L2DatabaseFactory.close(con);
  213. }
  214. }
  215. public void removeServitor(L2PcInstance activeChar)
  216. {
  217. Connection con = null;
  218. try
  219. {
  220. con = L2DatabaseFactory.getInstance().getConnection();
  221. PreparedStatement statement = con.prepareStatement(REMOVE_SUMMON);
  222. statement.setInt(1, activeChar.getObjectId());
  223. statement.execute();
  224. statement.close();
  225. _servitors.remove(activeChar.getObjectId());
  226. }
  227. catch (SQLException e)
  228. {
  229. _log.log(Level.WARNING, "[CharSummonTable]: Summon cannot be removed: ", e);
  230. }
  231. finally
  232. {
  233. L2DatabaseFactory.close(con);
  234. }
  235. }
  236. public void restorePet(L2PcInstance activeChar)
  237. {
  238. L2ItemInstance item = activeChar.getInventory().getItemByObjectId(_pets.get(activeChar.getObjectId()));
  239. final L2SummonItem sitem = SummonItemsData.getInstance().getSummonItem(item.getItemId());
  240. L2NpcTemplate npcTemplate = NpcTable.getInstance().getTemplate(sitem.getNpcId());
  241. if (npcTemplate == null)
  242. return;
  243. final L2PetInstance petSummon = L2PetInstance.spawnPet(npcTemplate, activeChar, item);
  244. if (petSummon == null)
  245. return;
  246. petSummon.setShowSummonAnimation(true);
  247. petSummon.setTitle(activeChar.getName());
  248. if (!petSummon.isRespawned())
  249. {
  250. petSummon.setCurrentHp(petSummon.getMaxHp());
  251. petSummon.setCurrentMp(petSummon.getMaxMp());
  252. petSummon.getStat().setExp(petSummon.getExpForThisLevel());
  253. petSummon.setCurrentFed(petSummon.getMaxFed());
  254. }
  255. petSummon.setRunning();
  256. if (!petSummon.isRespawned())
  257. petSummon.store();
  258. activeChar.setPet(petSummon);
  259. petSummon.spawnMe(activeChar.getX() + 50, activeChar.getY() + 100, activeChar.getZ());
  260. petSummon.startFeed();
  261. item.setEnchantLevel(petSummon.getLevel());
  262. if (petSummon.getCurrentFed() <= 0)
  263. petSummon.unSummon(activeChar);
  264. else
  265. petSummon.startFeed();
  266. petSummon.setFollowStatus(true);
  267. petSummon.getOwner().sendPacket(new PetItemList(petSummon));
  268. petSummon.broadcastStatusUpdate();
  269. }
  270. @SuppressWarnings("synthetic-access")
  271. private static class SingletonHolder
  272. {
  273. protected static final CharSummonTable _instance = new CharSummonTable();
  274. }
  275. }