CharSummonTable.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.sql.Statement;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import com.l2jserver.Config;
  25. import com.l2jserver.L2DatabaseFactory;
  26. import com.l2jserver.gameserver.idfactory.IdFactory;
  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.L2ServitorInstance;
  32. import com.l2jserver.gameserver.model.actor.instance.L2SiegeSummonInstance;
  33. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  34. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  35. import com.l2jserver.gameserver.model.skills.l2skills.L2SkillSummon;
  36. import com.l2jserver.gameserver.network.serverpackets.PetItemList;
  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. if (Config.RESTORE_SERVITOR_ON_RECONNECT)
  57. {
  58. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  59. Statement s = con.createStatement();
  60. ResultSet rs = s.executeQuery(INIT_SUMMONS))
  61. {
  62. while (rs.next())
  63. {
  64. _servitors.put(rs.getInt("ownerId"), rs.getInt("summonSkillId"));
  65. }
  66. }
  67. catch (Exception e)
  68. {
  69. _log.log(Level.SEVERE, "Error while loading saved summons", e);
  70. }
  71. }
  72. if (Config.RESTORE_PET_ON_RECONNECT)
  73. {
  74. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  75. Statement s = con.createStatement();
  76. ResultSet rs = s.executeQuery(INIT_PET))
  77. {
  78. while (rs.next())
  79. {
  80. _pets.put(rs.getInt("ownerId"), rs.getInt("item_obj_id"));
  81. }
  82. }
  83. catch (Exception e)
  84. {
  85. _log.log(Level.SEVERE, "Error while loading saved summons", e);
  86. }
  87. }
  88. }
  89. public TIntIntHashMap getServitors()
  90. {
  91. return _servitors;
  92. }
  93. public TIntIntHashMap getPets()
  94. {
  95. return _pets;
  96. }
  97. public void saveSummon(L2ServitorInstance summon)
  98. {
  99. if (summon == null || summon.getTimeRemaining() <= 0)
  100. return;
  101. _servitors.put(summon.getOwner().getObjectId(), summon.getReferenceSkill());
  102. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  103. PreparedStatement ps = con.prepareStatement(SAVE_SUMMON))
  104. {
  105. ps.setInt(1, summon.getOwner().getObjectId());
  106. ps.setInt(2, summon.getReferenceSkill());
  107. ps.setInt(3, (int) Math.round(summon.getCurrentHp()));
  108. ps.setInt(4, (int) Math.round(summon.getCurrentMp()));
  109. ps.setInt(5, summon.getTimeRemaining());
  110. ps.execute();
  111. }
  112. catch (Exception e)
  113. {
  114. _log.log(Level.SEVERE, "Failed to store summon [SummonId: " + summon.getNpcId() + "] from Char [CharId: " + summon.getOwner().getObjectId() + "] data", e);
  115. }
  116. }
  117. public void restoreServitor(L2PcInstance activeChar)
  118. {
  119. int skillId = _servitors.get(activeChar.getObjectId());
  120. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  121. PreparedStatement ps = con.prepareStatement(LOAD_SUMMON))
  122. {
  123. ps.setInt(1, activeChar.getObjectId());
  124. ps.setInt(2, skillId);
  125. try (ResultSet rs = ps.executeQuery())
  126. {
  127. L2NpcTemplate summonTemplate;
  128. L2ServitorInstance summon;
  129. L2SkillSummon skill;
  130. while (rs.next())
  131. {
  132. int curHp = rs.getInt("curHp");
  133. int curMp = rs.getInt("curMp");
  134. int time = rs.getInt("time");
  135. skill = (L2SkillSummon) SkillTable.getInstance().getInfo(skillId, activeChar.getSkillLevel(skillId));
  136. if (skill == null)
  137. {
  138. removeServitor(activeChar);
  139. return;
  140. }
  141. summonTemplate = NpcTable.getInstance().getTemplate(skill.getNpcId());
  142. if (summonTemplate == null)
  143. {
  144. _log.warning("[CharSummonTable] Summon attemp for nonexisting Skill ID:" + skillId);
  145. return;
  146. }
  147. final int id = IdFactory.getInstance().getNextId();
  148. if (summonTemplate.isType("L2SiegeSummon"))
  149. {
  150. summon = new L2SiegeSummonInstance(id, summonTemplate, activeChar, skill);
  151. }
  152. else if (summonTemplate.isType("L2MerchantSummon"))
  153. {
  154. // TODO: Confirm L2Merchant summon = new L2MerchantSummonInstance(id, summonTemplate, activeChar, skill);
  155. summon = new L2ServitorInstance(id, summonTemplate, activeChar, skill);
  156. }
  157. else
  158. {
  159. summon = new L2ServitorInstance(id, summonTemplate, activeChar, skill);
  160. }
  161. summon.setName(summonTemplate.getName());
  162. summon.setTitle(activeChar.getName());
  163. summon.setExpPenalty(skill.getExpPenalty());
  164. summon.setSharedElementals(skill.getInheritElementals());
  165. summon.setSharedElementalsValue(skill.getElementalSharePercent());
  166. if (summon.getLevel() >= ExperienceTable.getInstance().getMaxPetLevel())
  167. {
  168. summon.getStat().setExp(ExperienceTable.getInstance().getExpForLevel(ExperienceTable.getInstance().getMaxPetLevel()-1));
  169. _log.warning("Summon (" + summon.getName() + ") NpcID: " + summon.getNpcId() + " has a level above "+ExperienceTable.getInstance().getMaxPetLevel()+". Please rectify.");
  170. }
  171. else
  172. {
  173. summon.getStat().setExp(ExperienceTable.getInstance().getExpForLevel(summon.getLevel() % ExperienceTable.getInstance().getMaxPetLevel()));
  174. }
  175. summon.setCurrentHp(curHp);
  176. summon.setCurrentMp(curMp);
  177. summon.setHeading(activeChar.getHeading());
  178. summon.setRunning();
  179. if (!(summon instanceof L2MerchantSummonInstance))
  180. activeChar.setPet(summon);
  181. summon.setTimeRemaining(time);
  182. //L2World.getInstance().storeObject(summon);
  183. summon.spawnMe(activeChar.getX() + 20, activeChar.getY() + 20, activeChar.getZ());
  184. }
  185. }
  186. }
  187. catch (SQLException e)
  188. {
  189. _log.log(Level.WARNING, "[CharSummonTable]: Summon cannot be restored: ", e);
  190. }
  191. }
  192. public void removeServitor(L2PcInstance activeChar)
  193. {
  194. _servitors.remove(activeChar.getObjectId());
  195. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  196. PreparedStatement ps = con.prepareStatement(REMOVE_SUMMON))
  197. {
  198. ps.setInt(1, activeChar.getObjectId());
  199. ps.execute();
  200. }
  201. catch (SQLException e)
  202. {
  203. _log.log(Level.WARNING, "[CharSummonTable]: Summon cannot be removed: ", e);
  204. }
  205. }
  206. public void restorePet(L2PcInstance activeChar)
  207. {
  208. L2ItemInstance item = activeChar.getInventory().getItemByObjectId(_pets.get(activeChar.getObjectId()));
  209. final L2SummonItem sitem = SummonItemsData.getInstance().getSummonItem(item.getItemId());
  210. L2NpcTemplate npcTemplate = NpcTable.getInstance().getTemplate(sitem.getNpcId());
  211. if (npcTemplate == null)
  212. return;
  213. final L2PetInstance petSummon = L2PetInstance.spawnPet(npcTemplate, activeChar, item);
  214. if (petSummon == null)
  215. return;
  216. petSummon.setShowSummonAnimation(true);
  217. petSummon.setTitle(activeChar.getName());
  218. if (!petSummon.isRespawned())
  219. {
  220. petSummon.setCurrentHp(petSummon.getMaxHp());
  221. petSummon.setCurrentMp(petSummon.getMaxMp());
  222. petSummon.getStat().setExp(petSummon.getExpForThisLevel());
  223. petSummon.setCurrentFed(petSummon.getMaxFed());
  224. }
  225. petSummon.setRunning();
  226. if (!petSummon.isRespawned())
  227. petSummon.store();
  228. activeChar.setPet(petSummon);
  229. petSummon.spawnMe(activeChar.getX() + 50, activeChar.getY() + 100, activeChar.getZ());
  230. petSummon.startFeed();
  231. item.setEnchantLevel(petSummon.getLevel());
  232. if (petSummon.getCurrentFed() <= 0)
  233. petSummon.unSummon(activeChar);
  234. else
  235. petSummon.startFeed();
  236. petSummon.setFollowStatus(true);
  237. petSummon.getOwner().sendPacket(new PetItemList(petSummon));
  238. petSummon.broadcastStatusUpdate();
  239. }
  240. private static class SingletonHolder
  241. {
  242. protected static final CharSummonTable _instance = new CharSummonTable();
  243. }
  244. }