CharSummonTable.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (C) 2004-2014 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.datatables;
  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.L2DatabaseFactory;
  30. import com.l2jserver.gameserver.model.L2PetData;
  31. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  33. import com.l2jserver.gameserver.model.actor.instance.L2ServitorInstance;
  34. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  35. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  36. import com.l2jserver.gameserver.model.skills.L2Skill;
  37. import com.l2jserver.gameserver.network.serverpackets.PetItemList;
  38. /**
  39. * @author Nyaran
  40. */
  41. public class CharSummonTable
  42. {
  43. private static final Logger _log = Logger.getLogger(CharSummonTable.class.getName());
  44. private static final Map<Integer, Integer> _pets = new ConcurrentHashMap<>();
  45. private static final Map<Integer, Integer> _servitors = new ConcurrentHashMap<>();
  46. // SQL
  47. private static final String INIT_PET = "SELECT ownerId, item_obj_id FROM pets WHERE restore = 'true'";
  48. private static final String INIT_SUMMONS = "SELECT ownerId, summonSkillId FROM character_summons";
  49. private static final String LOAD_SUMMON = "SELECT curHp, curMp, time FROM character_summons WHERE ownerId = ? AND summonSkillId = ?";
  50. private static final String REMOVE_SUMMON = "DELETE FROM character_summons WHERE ownerId = ?";
  51. private static final String SAVE_SUMMON = "REPLACE INTO character_summons (ownerId,summonSkillId,curHp,curMp,time) VALUES (?,?,?,?,?)";
  52. public Map<Integer, Integer> getPets()
  53. {
  54. return _pets;
  55. }
  56. public Map<Integer, Integer> getServitors()
  57. {
  58. return _servitors;
  59. }
  60. public void init()
  61. {
  62. if (Config.RESTORE_SERVITOR_ON_RECONNECT)
  63. {
  64. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  65. Statement s = con.createStatement();
  66. ResultSet rs = s.executeQuery(INIT_SUMMONS))
  67. {
  68. while (rs.next())
  69. {
  70. _servitors.put(rs.getInt("ownerId"), rs.getInt("summonSkillId"));
  71. }
  72. }
  73. catch (Exception e)
  74. {
  75. _log.warning(getClass().getSimpleName() + ": Error while loading saved servitor: " + e);
  76. }
  77. }
  78. if (Config.RESTORE_PET_ON_RECONNECT)
  79. {
  80. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  81. Statement s = con.createStatement();
  82. ResultSet rs = s.executeQuery(INIT_PET))
  83. {
  84. while (rs.next())
  85. {
  86. _pets.put(rs.getInt("ownerId"), rs.getInt("item_obj_id"));
  87. }
  88. }
  89. catch (Exception e)
  90. {
  91. _log.warning(getClass().getSimpleName() + ": Error while loading saved pet: " + e);
  92. }
  93. }
  94. }
  95. public void removeServitor(L2PcInstance activeChar)
  96. {
  97. _servitors.remove(activeChar.getObjectId());
  98. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  99. PreparedStatement ps = con.prepareStatement(REMOVE_SUMMON))
  100. {
  101. ps.setInt(1, activeChar.getObjectId());
  102. ps.execute();
  103. }
  104. catch (SQLException e)
  105. {
  106. _log.warning(getClass().getSimpleName() + ": Summon cannot be removed: " + e);
  107. }
  108. }
  109. public void restorePet(L2PcInstance activeChar)
  110. {
  111. final L2ItemInstance item = activeChar.getInventory().getItemByObjectId(_pets.get(activeChar.getObjectId()));
  112. if (item == null)
  113. {
  114. _log.warning(getClass().getSimpleName() + ": Null pet summoning item for: " + activeChar);
  115. return;
  116. }
  117. final L2PetData petData = PetDataTable.getInstance().getPetDataByItemId(item.getId());
  118. if (petData == null)
  119. {
  120. _log.warning(getClass().getSimpleName() + ": Null pet data for: " + activeChar + " and summoning item: " + item);
  121. return;
  122. }
  123. final L2NpcTemplate npcTemplate = NpcData.getInstance().getTemplate(petData.getNpcId());
  124. if (npcTemplate == null)
  125. {
  126. _log.warning(getClass().getSimpleName() + ": Null pet NPC template for: " + activeChar + " and pet Id:" + petData.getNpcId());
  127. return;
  128. }
  129. final L2PetInstance pet = L2PetInstance.spawnPet(npcTemplate, activeChar, item);
  130. if (pet == null)
  131. {
  132. _log.warning(getClass().getSimpleName() + ": Null pet instance for: " + activeChar + " and pet NPC template:" + npcTemplate);
  133. return;
  134. }
  135. pet.setShowSummonAnimation(true);
  136. pet.setTitle(activeChar.getName());
  137. if (!pet.isRespawned())
  138. {
  139. pet.setCurrentHp(pet.getMaxHp());
  140. pet.setCurrentMp(pet.getMaxMp());
  141. pet.getStat().setExp(pet.getExpForThisLevel());
  142. pet.setCurrentFed(pet.getMaxFed());
  143. }
  144. pet.setRunning();
  145. if (!pet.isRespawned())
  146. {
  147. pet.storeMe();
  148. }
  149. activeChar.setPet(pet);
  150. pet.spawnMe(activeChar.getX() + 50, activeChar.getY() + 100, activeChar.getZ());
  151. pet.startFeed();
  152. item.setEnchantLevel(pet.getLevel());
  153. if (pet.getCurrentFed() <= 0)
  154. {
  155. pet.unSummon(activeChar);
  156. }
  157. else
  158. {
  159. pet.startFeed();
  160. }
  161. pet.setFollowStatus(true);
  162. pet.getOwner().sendPacket(new PetItemList(pet.getInventory().getItems()));
  163. pet.broadcastStatusUpdate();
  164. }
  165. public void restoreServitor(L2PcInstance activeChar)
  166. {
  167. int skillId = _servitors.get(activeChar.getObjectId());
  168. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  169. PreparedStatement ps = con.prepareStatement(LOAD_SUMMON))
  170. {
  171. ps.setInt(1, activeChar.getObjectId());
  172. ps.setInt(2, skillId);
  173. try (ResultSet rs = ps.executeQuery())
  174. {
  175. L2Skill skill;
  176. while (rs.next())
  177. {
  178. int curHp = rs.getInt("curHp");
  179. int curMp = rs.getInt("curMp");
  180. int time = rs.getInt("time");
  181. skill = SkillTable.getInstance().getInfo(skillId, activeChar.getSkillLevel(skillId));
  182. if (skill == null)
  183. {
  184. removeServitor(activeChar);
  185. return;
  186. }
  187. skill.applyEffects(activeChar, activeChar);
  188. if (activeChar.hasServitor())
  189. {
  190. final L2ServitorInstance summon = (L2ServitorInstance) activeChar.getSummon();
  191. summon.setCurrentHp(curHp);
  192. summon.setCurrentMp(curMp);
  193. summon.setLifeTimeRemaining(time);
  194. }
  195. }
  196. }
  197. }
  198. catch (SQLException e)
  199. {
  200. _log.warning(getClass().getSimpleName() + ": Servitor cannot be restored: " + e);
  201. }
  202. }
  203. public void saveSummon(L2ServitorInstance summon)
  204. {
  205. if ((summon == null) || (summon.getLifeTimeRemaining() <= 0))
  206. {
  207. return;
  208. }
  209. _servitors.put(summon.getOwner().getObjectId(), summon.getReferenceSkill());
  210. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  211. PreparedStatement ps = con.prepareStatement(SAVE_SUMMON))
  212. {
  213. ps.setInt(1, summon.getOwner().getObjectId());
  214. ps.setInt(2, summon.getReferenceSkill());
  215. ps.setInt(3, (int) Math.round(summon.getCurrentHp()));
  216. ps.setInt(4, (int) Math.round(summon.getCurrentMp()));
  217. ps.setInt(5, summon.getLifeTimeRemaining());
  218. ps.execute();
  219. }
  220. catch (Exception e)
  221. {
  222. _log.warning(getClass().getSimpleName() + ": Failed to store summon: " + summon + " from " + summon.getOwner() + ", error: " + e);
  223. }
  224. }
  225. public static CharSummonTable getInstance()
  226. {
  227. return SingletonHolder._instance;
  228. }
  229. private static class SingletonHolder
  230. {
  231. protected static final CharSummonTable _instance = new CharSummonTable();
  232. }
  233. }