CharSummonTable.java 10 KB

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