CharSummonTable.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (C) 2004-2013 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.L2MerchantSummonInstance;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  35. import com.l2jserver.gameserver.model.actor.instance.L2ServitorInstance;
  36. import com.l2jserver.gameserver.model.actor.instance.L2SiegeSummonInstance;
  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.l2skills.L2SkillSummon;
  40. import com.l2jserver.gameserver.network.serverpackets.PetItemList;
  41. /**
  42. * @author Nyaran
  43. */
  44. public class CharSummonTable
  45. {
  46. private static final Logger _log = 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 = L2DatabaseFactory.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. _log.warning(getClass().getSimpleName() + ": Error while loading saved servitor: " + e);
  79. }
  80. }
  81. if (Config.RESTORE_PET_ON_RECONNECT)
  82. {
  83. try (Connection con = L2DatabaseFactory.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. _log.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 = L2DatabaseFactory.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. _log.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. _log.warning(getClass().getSimpleName() + ": Null pet summoning item for: " + activeChar);
  118. return;
  119. }
  120. final L2PetData petData = PetDataTable.getInstance().getPetDataByItemId(item.getItemId());
  121. if (petData == null)
  122. {
  123. _log.warning(getClass().getSimpleName() + ": Null pet data for: " + activeChar + " and summoning item: " + item);
  124. return;
  125. }
  126. final L2NpcTemplate npcTemplate = NpcTable.getInstance().getTemplate(petData.getNpcId());
  127. if (npcTemplate == null)
  128. {
  129. _log.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. _log.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.store();
  151. }
  152. activeChar.setPet(pet);
  153. pet.spawnMe(activeChar.getX() + 50, activeChar.getY() + 100, activeChar.getZ());
  154. pet.startFeed();
  155. item.setEnchantLevel(pet.getLevel());
  156. if (pet.getCurrentFed() <= 0)
  157. {
  158. pet.unSummon(activeChar);
  159. }
  160. else
  161. {
  162. pet.startFeed();
  163. }
  164. pet.setFollowStatus(true);
  165. pet.getOwner().sendPacket(new PetItemList(pet.getInventory().getItems()));
  166. pet.broadcastStatusUpdate();
  167. }
  168. public void restoreServitor(L2PcInstance activeChar)
  169. {
  170. int skillId = _servitors.get(activeChar.getObjectId());
  171. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  172. PreparedStatement ps = con.prepareStatement(LOAD_SUMMON))
  173. {
  174. ps.setInt(1, activeChar.getObjectId());
  175. ps.setInt(2, skillId);
  176. try (ResultSet rs = ps.executeQuery())
  177. {
  178. L2NpcTemplate summonTemplate;
  179. L2ServitorInstance summon;
  180. L2SkillSummon skill;
  181. while (rs.next())
  182. {
  183. int curHp = rs.getInt("curHp");
  184. int curMp = rs.getInt("curMp");
  185. int time = rs.getInt("time");
  186. skill = (L2SkillSummon) SkillTable.getInstance().getInfo(skillId, activeChar.getSkillLevel(skillId));
  187. if (skill == null)
  188. {
  189. removeServitor(activeChar);
  190. return;
  191. }
  192. summonTemplate = NpcTable.getInstance().getTemplate(skill.getNpcId());
  193. if (summonTemplate == null)
  194. {
  195. _log.warning(getClass().getSimpleName() + ": Summon attemp for nonexisting Skill ID:" + skillId);
  196. return;
  197. }
  198. final int id = IdFactory.getInstance().getNextId();
  199. if (summonTemplate.isType("L2SiegeSummon"))
  200. {
  201. summon = new L2SiegeSummonInstance(id, summonTemplate, activeChar, skill);
  202. }
  203. else if (summonTemplate.isType("L2MerchantSummon"))
  204. {
  205. // TODO: Confirm L2Merchant summon = new L2MerchantSummonInstance(id, summonTemplate, activeChar, skill);
  206. summon = new L2ServitorInstance(id, summonTemplate, activeChar, skill);
  207. }
  208. else
  209. {
  210. summon = new L2ServitorInstance(id, summonTemplate, activeChar, skill);
  211. }
  212. summon.setName(summonTemplate.getName());
  213. summon.setTitle(activeChar.getName());
  214. summon.setExpPenalty(skill.getExpPenalty());
  215. summon.setSharedElementals(skill.getInheritElementals());
  216. summon.setSharedElementalsValue(skill.getElementalSharePercent());
  217. if (summon.getLevel() >= ExperienceTable.getInstance().getMaxPetLevel())
  218. {
  219. summon.getStat().setExp(ExperienceTable.getInstance().getExpForLevel(ExperienceTable.getInstance().getMaxPetLevel() - 1));
  220. _log.warning(getClass().getSimpleName() + ": Summon (" + summon.getName() + ") NpcID: " + summon.getNpcId() + " has a level above " + ExperienceTable.getInstance().getMaxPetLevel() + ". Please rectify.");
  221. }
  222. else
  223. {
  224. summon.getStat().setExp(ExperienceTable.getInstance().getExpForLevel(summon.getLevel() % ExperienceTable.getInstance().getMaxPetLevel()));
  225. }
  226. summon.setCurrentHp(curHp);
  227. summon.setCurrentMp(curMp);
  228. summon.setHeading(activeChar.getHeading());
  229. summon.setRunning();
  230. if (!(summon instanceof L2MerchantSummonInstance))
  231. {
  232. activeChar.setPet(summon);
  233. }
  234. summon.setTimeRemaining(time);
  235. summon.spawnMe(activeChar.getX() + 20, activeChar.getY() + 20, activeChar.getZ());
  236. }
  237. }
  238. }
  239. catch (SQLException e)
  240. {
  241. _log.warning(getClass().getSimpleName() + ": Servitor cannot be restored: " + e);
  242. }
  243. }
  244. public void saveSummon(L2ServitorInstance summon)
  245. {
  246. if ((summon == null) || (summon.getTimeRemaining() <= 0))
  247. {
  248. return;
  249. }
  250. _servitors.put(summon.getOwner().getObjectId(), summon.getReferenceSkill());
  251. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  252. PreparedStatement ps = con.prepareStatement(SAVE_SUMMON))
  253. {
  254. ps.setInt(1, summon.getOwner().getObjectId());
  255. ps.setInt(2, summon.getReferenceSkill());
  256. ps.setInt(3, (int) Math.round(summon.getCurrentHp()));
  257. ps.setInt(4, (int) Math.round(summon.getCurrentMp()));
  258. ps.setInt(5, summon.getTimeRemaining());
  259. ps.execute();
  260. }
  261. catch (Exception e)
  262. {
  263. _log.warning(getClass().getSimpleName() + ": Failed to store summon: " + summon + " from " + summon.getOwner() + ", error: " + e);
  264. }
  265. }
  266. public static CharSummonTable getInstance()
  267. {
  268. return SingletonHolder._instance;
  269. }
  270. private static class SingletonHolder
  271. {
  272. protected static final CharSummonTable _instance = new CharSummonTable();
  273. }
  274. }