CharSummonTable.java 9.5 KB

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