CharSummonTable.java 9.6 KB

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