CharSummonTable.java 9.7 KB

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