CharSummonTable.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.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. Connection con = null;
  120. try
  121. {
  122. con = L2DatabaseFactory.getInstance().getConnection();
  123. PreparedStatement statement = con.prepareStatement(SAVE_SUMMON);
  124. statement.setInt(1, summon.getOwner().getObjectId());
  125. statement.setInt(2, summon.getReferenceSkill());
  126. statement.setInt(3, (int) Math.round(summon.getCurrentHp()));
  127. statement.setInt(4, (int) Math.round(summon.getCurrentMp()));
  128. statement.setInt(5, summon.getTimeRemaining());
  129. statement.execute();
  130. statement.close();
  131. _servitors.put(summon.getOwner().getObjectId(), summon.getReferenceSkill());
  132. }
  133. catch (Exception e)
  134. {
  135. _log.log(Level.SEVERE, "Failed to store summon [SummonId: " + summon.getNpcId() + "] from Char [CharId: " + summon.getOwner().getObjectId() + "] data", e);
  136. }
  137. finally
  138. {
  139. L2DatabaseFactory.close(con);
  140. }
  141. }
  142. public void restoreServitor(L2PcInstance activeChar)
  143. {
  144. Connection con = null;
  145. try
  146. {
  147. int skillId = _servitors.get(activeChar.getObjectId());
  148. con = L2DatabaseFactory.getInstance().getConnection();
  149. PreparedStatement statement = con.prepareStatement(LOAD_SUMMON);
  150. statement.setInt(1, activeChar.getObjectId());
  151. statement.setInt(2, skillId);
  152. ResultSet rset = statement.executeQuery();
  153. L2NpcTemplate summonTemplate;
  154. L2SummonInstance summon;
  155. L2SkillSummon skill;
  156. while (rset.next())
  157. {
  158. int curHp = rset.getInt("curHp");
  159. int curMp = rset.getInt("curMp");
  160. int time = rset.getInt("time");
  161. skill = (L2SkillSummon) SkillTable.getInstance().getInfo(skillId, activeChar.getSkillLevel(skillId));
  162. if (skill == null)
  163. {
  164. removeServitor(activeChar);
  165. return;
  166. }
  167. summonTemplate = NpcTable.getInstance().getTemplate(skill.getNpcId());
  168. if (summonTemplate == null)
  169. {
  170. _log.warning("[CharSummonTable] Summon attemp for nonexisting Skill ID:" + skillId);
  171. return;
  172. }
  173. if (summonTemplate.type.equalsIgnoreCase("L2SiegeSummon"))
  174. summon = new L2SiegeSummonInstance(IdFactory.getInstance().getNextId(), summonTemplate, activeChar, skill);
  175. /* TODO: Confirm L2Merchant
  176. else if (summonTemplate.type.equalsIgnoreCase("L2MerchantSummon"))
  177. summon = new L2MerchantSummonInstance(IdFactory.getInstance().getNextId(), summonTemplate, activeChar, skill);*/
  178. else
  179. summon = new L2SummonInstance(IdFactory.getInstance().getNextId(), summonTemplate, activeChar, skill);
  180. summon.setName(summonTemplate.name);
  181. summon.setTitle(activeChar.getName());
  182. summon.setExpPenalty(skill.getExpPenalty());
  183. if (summon.getLevel() >= ExperienceTable.getInstance().getMaxPetLevel())
  184. {
  185. summon.getStat().setExp(ExperienceTable.getInstance().getExpForLevel(ExperienceTable.getInstance().getMaxPetLevel()-1));
  186. _log.warning("Summon (" + summon.getName() + ") NpcID: " + summon.getNpcId() + " has a level above "+ExperienceTable.getInstance().getMaxPetLevel()+". Please rectify.");
  187. }
  188. else
  189. {
  190. summon.getStat().setExp(ExperienceTable.getInstance().getExpForLevel(summon.getLevel() % ExperienceTable.getInstance().getMaxPetLevel()));
  191. }
  192. summon.setCurrentHp(curHp);
  193. summon.setCurrentMp(curMp);
  194. summon.setHeading(activeChar.getHeading());
  195. summon.setRunning();
  196. if (!(summon instanceof L2MerchantSummonInstance))
  197. activeChar.setPet(summon);
  198. summon.setTimeRemaining(time);
  199. //L2World.getInstance().storeObject(summon);
  200. summon.spawnMe(activeChar.getX() + 20, activeChar.getY() + 20, activeChar.getZ());
  201. }
  202. rset.close();
  203. statement.close();
  204. }
  205. catch (SQLException e)
  206. {
  207. _log.log(Level.WARNING, "[CharSummonTable]: Summon cannot be restored: ", e);
  208. }
  209. finally
  210. {
  211. L2DatabaseFactory.close(con);
  212. }
  213. }
  214. public void removeServitor(L2PcInstance activeChar)
  215. {
  216. Connection con = null;
  217. try
  218. {
  219. con = L2DatabaseFactory.getInstance().getConnection();
  220. PreparedStatement statement = con.prepareStatement(REMOVE_SUMMON);
  221. statement.setInt(1, activeChar.getObjectId());
  222. statement.execute();
  223. statement.close();
  224. _servitors.remove(activeChar.getObjectId());
  225. }
  226. catch (SQLException e)
  227. {
  228. _log.log(Level.WARNING, "[CharSummonTable]: Summon cannot be removed: ", e);
  229. }
  230. finally
  231. {
  232. L2DatabaseFactory.close(con);
  233. }
  234. }
  235. public void restorePet(L2PcInstance activeChar)
  236. {
  237. L2ItemInstance item = activeChar.getInventory().getItemByObjectId(_pets.get(activeChar.getObjectId()));
  238. final L2SummonItem sitem = SummonItemsData.getInstance().getSummonItem(item.getItemId());
  239. L2NpcTemplate npcTemplate = NpcTable.getInstance().getTemplate(sitem.getNpcId());
  240. if (npcTemplate == null)
  241. return;
  242. final L2PetInstance petSummon = L2PetInstance.spawnPet(npcTemplate, activeChar, item);
  243. if (petSummon == null)
  244. return;
  245. petSummon.setShowSummonAnimation(true);
  246. petSummon.setTitle(activeChar.getName());
  247. if (!petSummon.isRespawned())
  248. {
  249. petSummon.setCurrentHp(petSummon.getMaxHp());
  250. petSummon.setCurrentMp(petSummon.getMaxMp());
  251. petSummon.getStat().setExp(petSummon.getExpForThisLevel());
  252. petSummon.setCurrentFed(petSummon.getMaxFed());
  253. }
  254. petSummon.setRunning();
  255. if (!petSummon.isRespawned())
  256. petSummon.store();
  257. activeChar.setPet(petSummon);
  258. petSummon.spawnMe(activeChar.getX() + 50, activeChar.getY() + 100, activeChar.getZ());
  259. petSummon.startFeed();
  260. item.setEnchantLevel(petSummon.getLevel());
  261. if (petSummon.getCurrentFed() <= 0)
  262. petSummon.unSummon(activeChar);
  263. else
  264. petSummon.startFeed();
  265. petSummon.setFollowStatus(true);
  266. petSummon.getOwner().sendPacket(new PetItemList(petSummon));
  267. petSummon.broadcastStatusUpdate();
  268. }
  269. @SuppressWarnings("synthetic-access")
  270. private static class SingletonHolder
  271. {
  272. protected static final CharSummonTable _instance = new CharSummonTable();
  273. }
  274. }