CharSummonTable.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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.map.hash.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.network.serverpackets.PetItemList;
  34. import com.l2jserver.gameserver.skills.l2skills.L2SkillSummon;
  35. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  36. /**
  37. * @author Nyaran
  38. */
  39. public class CharSummonTable
  40. {
  41. private static Logger _log = Logger.getLogger(CharSummonTable.class.getName());
  42. private static final String INIT_SUMMONS = "SELECT ownerId, summonSkillId FROM character_summons";
  43. private static final String INIT_PET = "SELECT ownerId, item_obj_id FROM pets WHERE restore = 'true'";
  44. private static final String SAVE_SUMMON = "REPLACE INTO character_summons (ownerId,summonSkillId,curHp,curMp,time) VALUES (?,?,?,?,?)";
  45. private static final String LOAD_SUMMON = "SELECT curHp, curMp, time FROM character_summons WHERE ownerId = ? AND summonSkillId = ?";
  46. private static final String REMOVE_SUMMON = "DELETE FROM character_summons WHERE ownerId = ?";
  47. private static final TIntIntHashMap _servitors = new TIntIntHashMap();
  48. private static final TIntIntHashMap _pets = new TIntIntHashMap();
  49. public static CharSummonTable getInstance()
  50. {
  51. return SingletonHolder._instance;
  52. }
  53. public void init()
  54. {
  55. int ownerId;
  56. int refId;
  57. Connection con = null;
  58. if (Config.RESTORE_SERVITOR_ON_RECONNECT)
  59. {
  60. try
  61. {
  62. con = L2DatabaseFactory.getInstance().getConnection();
  63. PreparedStatement statement = con.prepareStatement(INIT_SUMMONS);
  64. ResultSet rset = statement.executeQuery();
  65. while (rset.next())
  66. {
  67. ownerId = rset.getInt("ownerId");
  68. refId = rset.getInt("summonSkillId");
  69. _servitors.put(ownerId, refId);
  70. }
  71. rset.close();
  72. statement.close();
  73. }
  74. catch (Exception e)
  75. {
  76. _log.log(Level.SEVERE, "Error while loading saved summons", e);
  77. }
  78. finally
  79. {
  80. L2DatabaseFactory.close(con);
  81. }
  82. }
  83. if (Config.RESTORE_PET_ON_RECONNECT)
  84. {
  85. try
  86. {
  87. con = L2DatabaseFactory.getInstance().getConnection();
  88. PreparedStatement statement = con.prepareStatement(INIT_PET);
  89. ResultSet rset = statement.executeQuery();
  90. while (rset.next())
  91. {
  92. ownerId = rset.getInt("ownerId");
  93. refId = rset.getInt("item_obj_id");
  94. _pets.put(ownerId, refId);
  95. }
  96. rset.close();
  97. statement.close();
  98. }
  99. catch (Exception e)
  100. {
  101. _log.log(Level.SEVERE, "Error while loading saved summons", e);
  102. }
  103. finally
  104. {
  105. L2DatabaseFactory.close(con);
  106. }
  107. }
  108. }
  109. public TIntIntHashMap getServitors()
  110. {
  111. return _servitors;
  112. }
  113. public TIntIntHashMap getPets()
  114. {
  115. return _pets;
  116. }
  117. public void saveSummon(L2SummonInstance summon)
  118. {
  119. if (summon == null || summon.getTimeRemaining() <= 0)
  120. return;
  121. Connection con = null;
  122. try
  123. {
  124. con = L2DatabaseFactory.getInstance().getConnection();
  125. PreparedStatement statement = con.prepareStatement(SAVE_SUMMON);
  126. statement.setInt(1, summon.getOwner().getObjectId());
  127. statement.setInt(2, summon.getReferenceSkill());
  128. statement.setInt(3, (int) Math.round(summon.getCurrentHp()));
  129. statement.setInt(4, (int) Math.round(summon.getCurrentMp()));
  130. statement.setInt(5, summon.getTimeRemaining());
  131. statement.execute();
  132. statement.close();
  133. _servitors.put(summon.getOwner().getObjectId(), summon.getReferenceSkill());
  134. }
  135. catch (Exception e)
  136. {
  137. _log.log(Level.SEVERE, "Failed to store summon [SummonId: " + summon.getNpcId() + "] from Char [CharId: " + summon.getOwner().getObjectId() + "] data", e);
  138. }
  139. finally
  140. {
  141. L2DatabaseFactory.close(con);
  142. }
  143. }
  144. public void restoreServitor(L2PcInstance activeChar)
  145. {
  146. Connection con = null;
  147. try
  148. {
  149. int skillId = _servitors.get(activeChar.getObjectId());
  150. con = L2DatabaseFactory.getInstance().getConnection();
  151. PreparedStatement statement = con.prepareStatement(LOAD_SUMMON);
  152. statement.setInt(1, activeChar.getObjectId());
  153. statement.setInt(2, skillId);
  154. ResultSet rset = statement.executeQuery();
  155. L2NpcTemplate summonTemplate;
  156. L2SummonInstance summon;
  157. L2SkillSummon skill;
  158. while (rset.next())
  159. {
  160. int curHp = rset.getInt("curHp");
  161. int curMp = rset.getInt("curMp");
  162. int time = rset.getInt("time");
  163. skill = (L2SkillSummon) SkillTable.getInstance().getInfo(skillId, activeChar.getSkillLevel(skillId));
  164. if (skill == null)
  165. {
  166. removeServitor(activeChar);
  167. return;
  168. }
  169. summonTemplate = NpcTable.getInstance().getTemplate(skill.getNpcId());
  170. if (summonTemplate == null)
  171. {
  172. _log.warning("[CharSummonTable] Summon attemp for nonexisting Skill ID:" + skillId);
  173. return;
  174. }
  175. if (summonTemplate.type.equalsIgnoreCase("L2SiegeSummon"))
  176. summon = new L2SiegeSummonInstance(IdFactory.getInstance().getNextId(), summonTemplate, activeChar, skill);
  177. /* TODO: Confirm L2Merchant
  178. else if (summonTemplate.type.equalsIgnoreCase("L2MerchantSummon"))
  179. summon = new L2MerchantSummonInstance(IdFactory.getInstance().getNextId(), summonTemplate, activeChar, skill);*/
  180. else
  181. summon = new L2SummonInstance(IdFactory.getInstance().getNextId(), summonTemplate, activeChar, skill);
  182. summon.setName(summonTemplate.name);
  183. summon.setTitle(activeChar.getName());
  184. summon.setExpPenalty(skill.getExpPenalty());
  185. if (summon.getLevel() >= ExperienceTable.getInstance().getMaxPetLevel())
  186. {
  187. summon.getStat().setExp(ExperienceTable.getInstance().getExpForLevel(ExperienceTable.getInstance().getMaxPetLevel()-1));
  188. _log.warning("Summon (" + summon.getName() + ") NpcID: " + summon.getNpcId() + " has a level above "+ExperienceTable.getInstance().getMaxPetLevel()+". Please rectify.");
  189. }
  190. else
  191. {
  192. summon.getStat().setExp(ExperienceTable.getInstance().getExpForLevel(summon.getLevel() % ExperienceTable.getInstance().getMaxPetLevel()));
  193. }
  194. summon.setCurrentHp(curHp);
  195. summon.setCurrentMp(curMp);
  196. summon.setHeading(activeChar.getHeading());
  197. summon.setRunning();
  198. if (!(summon instanceof L2MerchantSummonInstance))
  199. activeChar.setPet(summon);
  200. summon.setTimeRemaining(time);
  201. //L2World.getInstance().storeObject(summon);
  202. summon.spawnMe(activeChar.getX() + 20, activeChar.getY() + 20, activeChar.getZ());
  203. }
  204. rset.close();
  205. statement.close();
  206. }
  207. catch (SQLException e)
  208. {
  209. _log.log(Level.WARNING, "[CharSummonTable]: Summon cannot be restored: ", e);
  210. }
  211. finally
  212. {
  213. L2DatabaseFactory.close(con);
  214. }
  215. }
  216. public void removeServitor(L2PcInstance activeChar)
  217. {
  218. Connection con = null;
  219. try
  220. {
  221. con = L2DatabaseFactory.getInstance().getConnection();
  222. PreparedStatement statement = con.prepareStatement(REMOVE_SUMMON);
  223. statement.setInt(1, activeChar.getObjectId());
  224. statement.execute();
  225. statement.close();
  226. _servitors.remove(activeChar.getObjectId());
  227. }
  228. catch (SQLException e)
  229. {
  230. _log.log(Level.WARNING, "[CharSummonTable]: Summon cannot be removed: ", e);
  231. }
  232. finally
  233. {
  234. L2DatabaseFactory.close(con);
  235. }
  236. }
  237. public void restorePet(L2PcInstance activeChar)
  238. {
  239. L2ItemInstance item = activeChar.getInventory().getItemByObjectId(_pets.get(activeChar.getObjectId()));
  240. final L2SummonItem sitem = SummonItemsData.getInstance().getSummonItem(item.getItemId());
  241. L2NpcTemplate npcTemplate = NpcTable.getInstance().getTemplate(sitem.getNpcId());
  242. if (npcTemplate == null)
  243. return;
  244. final L2PetInstance petSummon = L2PetInstance.spawnPet(npcTemplate, activeChar, item);
  245. if (petSummon == null)
  246. return;
  247. petSummon.setShowSummonAnimation(true);
  248. petSummon.setTitle(activeChar.getName());
  249. if (!petSummon.isRespawned())
  250. {
  251. petSummon.setCurrentHp(petSummon.getMaxHp());
  252. petSummon.setCurrentMp(petSummon.getMaxMp());
  253. petSummon.getStat().setExp(petSummon.getExpForThisLevel());
  254. petSummon.setCurrentFed(petSummon.getMaxFed());
  255. }
  256. petSummon.setRunning();
  257. if (!petSummon.isRespawned())
  258. petSummon.store();
  259. activeChar.setPet(petSummon);
  260. petSummon.spawnMe(activeChar.getX() + 50, activeChar.getY() + 100, activeChar.getZ());
  261. petSummon.startFeed();
  262. item.setEnchantLevel(petSummon.getLevel());
  263. if (petSummon.getCurrentFed() <= 0)
  264. petSummon.unSummon(activeChar);
  265. else
  266. petSummon.startFeed();
  267. petSummon.setFollowStatus(true);
  268. petSummon.getOwner().sendPacket(new PetItemList(petSummon));
  269. petSummon.broadcastStatusUpdate();
  270. }
  271. @SuppressWarnings("synthetic-access")
  272. private static class SingletonHolder
  273. {
  274. protected static final CharSummonTable _instance = new CharSummonTable();
  275. }
  276. }