CharSummonTable.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.data.sql.impl;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.Map;
  26. import java.util.concurrent.ConcurrentHashMap;
  27. import java.util.logging.Logger;
  28. import com.l2jserver.Config;
  29. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  30. import com.l2jserver.gameserver.data.xml.impl.NpcData;
  31. import com.l2jserver.gameserver.data.xml.impl.PetDataTable;
  32. import com.l2jserver.gameserver.datatables.SkillData;
  33. import com.l2jserver.gameserver.model.L2PetData;
  34. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  35. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  36. import com.l2jserver.gameserver.model.actor.instance.L2ServitorInstance;
  37. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  38. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  39. import com.l2jserver.gameserver.model.skills.Skill;
  40. import com.l2jserver.gameserver.network.serverpackets.PetItemList;
  41. /**
  42. * @author Nyaran
  43. */
  44. public class CharSummonTable
  45. {
  46. private static final Logger LOGGER = Logger.getLogger(CharSummonTable.class.getName());
  47. private static final Map<Integer, Integer> _pets = new ConcurrentHashMap<>();
  48. private static final Map<Integer, Integer> _servitors = new ConcurrentHashMap<>();
  49. // SQL
  50. private static final String INIT_PET = "SELECT ownerId, item_obj_id FROM pets WHERE restore = 'true'";
  51. private static final String INIT_SUMMONS = "SELECT ownerId, summonSkillId FROM character_summons";
  52. private static final String LOAD_SUMMON = "SELECT curHp, curMp, time FROM character_summons WHERE ownerId = ? AND summonSkillId = ?";
  53. private static final String REMOVE_SUMMON = "DELETE FROM character_summons WHERE ownerId = ?";
  54. private static final String SAVE_SUMMON = "REPLACE INTO character_summons (ownerId,summonSkillId,curHp,curMp,time) VALUES (?,?,?,?,?)";
  55. public Map<Integer, Integer> getPets()
  56. {
  57. return _pets;
  58. }
  59. public Map<Integer, Integer> getServitors()
  60. {
  61. return _servitors;
  62. }
  63. public void init()
  64. {
  65. if (Config.RESTORE_SERVITOR_ON_RECONNECT)
  66. {
  67. try (Connection con = ConnectionFactory.getInstance().getConnection();
  68. Statement s = con.createStatement();
  69. ResultSet rs = s.executeQuery(INIT_SUMMONS))
  70. {
  71. while (rs.next())
  72. {
  73. _servitors.put(rs.getInt("ownerId"), rs.getInt("summonSkillId"));
  74. }
  75. }
  76. catch (Exception e)
  77. {
  78. LOGGER.warning(getClass().getSimpleName() + ": Error while loading saved servitor: " + e);
  79. }
  80. }
  81. if (Config.RESTORE_PET_ON_RECONNECT)
  82. {
  83. try (Connection con = ConnectionFactory.getInstance().getConnection();
  84. Statement s = con.createStatement();
  85. ResultSet rs = s.executeQuery(INIT_PET))
  86. {
  87. while (rs.next())
  88. {
  89. _pets.put(rs.getInt("ownerId"), rs.getInt("item_obj_id"));
  90. }
  91. }
  92. catch (Exception e)
  93. {
  94. LOGGER.warning(getClass().getSimpleName() + ": Error while loading saved pet: " + e);
  95. }
  96. }
  97. }
  98. public void removeServitor(L2PcInstance activeChar)
  99. {
  100. _servitors.remove(activeChar.getObjectId());
  101. try (Connection con = ConnectionFactory.getInstance().getConnection();
  102. PreparedStatement ps = con.prepareStatement(REMOVE_SUMMON))
  103. {
  104. ps.setInt(1, activeChar.getObjectId());
  105. ps.execute();
  106. }
  107. catch (SQLException e)
  108. {
  109. LOGGER.warning(getClass().getSimpleName() + ": Summon cannot be removed: " + e);
  110. }
  111. }
  112. public void restorePet(L2PcInstance activeChar)
  113. {
  114. final L2ItemInstance item = activeChar.getInventory().getItemByObjectId(_pets.get(activeChar.getObjectId()));
  115. if (item == null)
  116. {
  117. LOGGER.warning(getClass().getSimpleName() + ": Null pet summoning item for: " + activeChar);
  118. return;
  119. }
  120. final L2PetData petData = PetDataTable.getInstance().getPetDataByItemId(item.getId());
  121. if (petData == null)
  122. {
  123. LOGGER.warning(getClass().getSimpleName() + ": Null pet data for: " + activeChar + " and summoning item: " + item);
  124. return;
  125. }
  126. final L2NpcTemplate npcTemplate = NpcData.getInstance().getTemplate(petData.getNpcId());
  127. if (npcTemplate == null)
  128. {
  129. LOGGER.warning(getClass().getSimpleName() + ": Null pet NPC template for: " + activeChar + " and pet Id:" + petData.getNpcId());
  130. return;
  131. }
  132. final L2PetInstance pet = L2PetInstance.spawnPet(npcTemplate, activeChar, item);
  133. if (pet == null)
  134. {
  135. LOGGER.warning(getClass().getSimpleName() + ": Null pet instance for: " + activeChar + " and pet NPC template:" + npcTemplate);
  136. return;
  137. }
  138. pet.setShowSummonAnimation(true);
  139. pet.setTitle(activeChar.getName());
  140. if (!pet.isRespawned())
  141. {
  142. pet.setCurrentHp(pet.getMaxHp());
  143. pet.setCurrentMp(pet.getMaxMp());
  144. pet.getStat().setExp(pet.getExpForThisLevel());
  145. pet.setCurrentFed(pet.getMaxFed());
  146. }
  147. pet.setRunning();
  148. if (!pet.isRespawned())
  149. {
  150. pet.storeMe();
  151. }
  152. item.setEnchantLevel(pet.getLevel());
  153. activeChar.setPet(pet);
  154. pet.spawnMe(activeChar.getX() + 50, activeChar.getY() + 100, activeChar.getZ());
  155. pet.startFeed();
  156. pet.setFollowStatus(true);
  157. pet.getOwner().sendPacket(new PetItemList(pet.getInventory().getItems()));
  158. pet.broadcastStatusUpdate();
  159. }
  160. public void restoreServitor(L2PcInstance activeChar)
  161. {
  162. int skillId = _servitors.get(activeChar.getObjectId());
  163. try (Connection con = ConnectionFactory.getInstance().getConnection();
  164. PreparedStatement ps = con.prepareStatement(LOAD_SUMMON))
  165. {
  166. ps.setInt(1, activeChar.getObjectId());
  167. ps.setInt(2, skillId);
  168. try (ResultSet rs = ps.executeQuery())
  169. {
  170. Skill skill;
  171. while (rs.next())
  172. {
  173. int curHp = rs.getInt("curHp");
  174. int curMp = rs.getInt("curMp");
  175. int time = rs.getInt("time");
  176. skill = SkillData.getInstance().getSkill(skillId, activeChar.getSkillLevel(skillId));
  177. if (skill == null)
  178. {
  179. removeServitor(activeChar);
  180. return;
  181. }
  182. skill.applyEffects(activeChar, activeChar);
  183. if (activeChar.hasServitor())
  184. {
  185. final L2ServitorInstance summon = (L2ServitorInstance) activeChar.getSummon();
  186. summon.setCurrentHp(curHp);
  187. summon.setCurrentMp(curMp);
  188. summon.setLifeTimeRemaining(time);
  189. }
  190. }
  191. }
  192. }
  193. catch (SQLException e)
  194. {
  195. LOGGER.warning(getClass().getSimpleName() + ": Servitor cannot be restored: " + e);
  196. }
  197. }
  198. public void saveSummon(L2ServitorInstance summon)
  199. {
  200. if ((summon == null) || (summon.getLifeTimeRemaining() <= 0))
  201. {
  202. return;
  203. }
  204. _servitors.put(summon.getOwner().getObjectId(), summon.getReferenceSkill());
  205. try (Connection con = ConnectionFactory.getInstance().getConnection();
  206. PreparedStatement ps = con.prepareStatement(SAVE_SUMMON))
  207. {
  208. ps.setInt(1, summon.getOwner().getObjectId());
  209. ps.setInt(2, summon.getReferenceSkill());
  210. ps.setInt(3, (int) Math.round(summon.getCurrentHp()));
  211. ps.setInt(4, (int) Math.round(summon.getCurrentMp()));
  212. ps.setInt(5, summon.getLifeTimeRemaining());
  213. ps.execute();
  214. }
  215. catch (Exception e)
  216. {
  217. LOGGER.warning(getClass().getSimpleName() + ": Failed to store summon: " + summon + " from " + summon.getOwner() + ", error: " + e);
  218. }
  219. }
  220. public static CharSummonTable getInstance()
  221. {
  222. return SingletonHolder._instance;
  223. }
  224. private static class SingletonHolder
  225. {
  226. protected static final CharSummonTable _instance = new CharSummonTable();
  227. }
  228. }