SummonItems.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /**
  16. *
  17. * @author FBIagent
  18. *
  19. */
  20. package handlers.itemhandlers;
  21. import java.util.Collection;
  22. import java.util.logging.Level;
  23. import com.l2jserver.gameserver.ThreadPoolManager;
  24. import com.l2jserver.gameserver.datatables.NpcTable;
  25. import com.l2jserver.gameserver.datatables.SummonItemsData;
  26. import com.l2jserver.gameserver.handler.IItemHandler;
  27. import com.l2jserver.gameserver.model.L2Object;
  28. import com.l2jserver.gameserver.model.L2Spawn;
  29. import com.l2jserver.gameserver.model.L2SummonItem;
  30. import com.l2jserver.gameserver.model.actor.L2Character;
  31. import com.l2jserver.gameserver.model.actor.L2Npc;
  32. import com.l2jserver.gameserver.model.actor.L2Playable;
  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.L2XmassTreeInstance;
  36. import com.l2jserver.gameserver.model.entity.TvTEvent;
  37. import com.l2jserver.gameserver.model.item.instance.L2ItemInstance;
  38. import com.l2jserver.gameserver.network.SystemMessageId;
  39. import com.l2jserver.gameserver.network.serverpackets.MagicSkillLaunched;
  40. import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  41. import com.l2jserver.gameserver.network.serverpackets.PetItemList;
  42. import com.l2jserver.gameserver.network.serverpackets.SetupGauge;
  43. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  44. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  45. import com.l2jserver.gameserver.util.Broadcast;
  46. public class SummonItems implements IItemHandler
  47. {
  48. /**
  49. *
  50. * @see com.l2jserver.gameserver.handler.IItemHandler#useItem(com.l2jserver.gameserver.model.actor.L2Playable, com.l2jserver.gameserver.model.L2ItemInstance, boolean)
  51. */
  52. public void useItem(L2Playable playable, L2ItemInstance item, boolean forceUse)
  53. {
  54. if (!(playable instanceof L2PcInstance))
  55. return;
  56. if (!TvTEvent.onItemSummon(playable.getObjectId()))
  57. return;
  58. final L2PcInstance activeChar = (L2PcInstance) playable;
  59. if (!activeChar.getFloodProtectors().getItemPetSummon().tryPerformAction("summon items"))
  60. return;
  61. if (activeChar.isSitting())
  62. {
  63. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.CANT_MOVE_SITTING));
  64. return;
  65. }
  66. if(activeChar.getBlockCheckerArena() != -1)
  67. return;
  68. if (activeChar.inObserverMode())
  69. return;
  70. if (activeChar.isInOlympiadMode())
  71. {
  72. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.THIS_ITEM_IS_NOT_AVAILABLE_FOR_THE_OLYMPIAD_EVENT));
  73. return;
  74. }
  75. if (activeChar.isAllSkillsDisabled() || activeChar.isCastingNow())
  76. return;
  77. final L2SummonItem sitem = SummonItemsData.getInstance().getSummonItem(item.getItemId());
  78. if ((activeChar.getPet() != null || activeChar.isMounted()) && sitem.isPetSummon())
  79. {
  80. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.YOU_ALREADY_HAVE_A_PET));
  81. return;
  82. }
  83. if (activeChar.isAttackingNow())
  84. {
  85. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.YOU_CANNOT_SUMMON_IN_COMBAT));
  86. return;
  87. }
  88. final int npcId = sitem.getNpcId();
  89. if (npcId == 0)
  90. return;
  91. final L2NpcTemplate npcTemplate = NpcTable.getInstance().getTemplate(npcId);
  92. if (npcTemplate == null)
  93. return;
  94. activeChar.stopMove(null, false);
  95. switch (sitem.getType())
  96. {
  97. case 0: // static summons (like Christmas tree)
  98. try
  99. {
  100. Collection<L2Character> characters = activeChar.getKnownList().getKnownCharactersInRadius(1200);
  101. for (L2Character ch : characters)
  102. {
  103. if (ch instanceof L2XmassTreeInstance && npcTemplate.isSpecialTree())
  104. {
  105. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.CANNOT_SUMMON_S1_AGAIN);
  106. sm.addCharName(ch);
  107. activeChar.sendPacket(sm);
  108. return;
  109. }
  110. }
  111. if (activeChar.destroyItem("Summon", item.getObjectId(), 1, null, false))
  112. {
  113. final L2Spawn spawn = new L2Spawn(npcTemplate);
  114. spawn.setLocx(activeChar.getX());
  115. spawn.setLocy(activeChar.getY());
  116. spawn.setLocz(activeChar.getZ());
  117. spawn.setInstanceId(activeChar.getInstanceId());
  118. spawn.stopRespawn();
  119. final L2Npc npc = spawn.spawnOne(true);
  120. npc.setTitle(activeChar.getName());
  121. npc.setIsRunning(false); // broadcast info
  122. if (sitem.getDespawnDelay() > 0)
  123. npc.scheduleDespawn(sitem.getDespawnDelay() * 1000L);
  124. }
  125. }
  126. catch (Exception e)
  127. {
  128. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.TARGET_CANT_FOUND));
  129. }
  130. break;
  131. case 1: // pet summons
  132. final L2Object oldTarget = activeChar.getTarget();
  133. activeChar.setTarget(activeChar);
  134. Broadcast.toSelfAndKnownPlayers(activeChar, new MagicSkillUse(activeChar, 2046, 1, 5000, 0));
  135. activeChar.setTarget(oldTarget);
  136. activeChar.sendPacket(new SetupGauge(0, 5000));
  137. activeChar.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.SUMMON_A_PET));
  138. activeChar.setIsCastingNow(true);
  139. ThreadPoolManager.getInstance().scheduleGeneral(new PetSummonFinalizer(activeChar, npcTemplate, item), 5000);
  140. break;
  141. case 2: // wyvern
  142. activeChar.mount(sitem.getNpcId(), item.getObjectId(), true);
  143. break;
  144. case 3: // Great Wolf
  145. activeChar.mount(sitem.getNpcId(), item.getObjectId(), false);
  146. break;
  147. }
  148. }
  149. static class PetSummonFeedWait implements Runnable
  150. {
  151. private final L2PcInstance _activeChar;
  152. private final L2PetInstance _petSummon;
  153. PetSummonFeedWait(L2PcInstance activeChar, L2PetInstance petSummon)
  154. {
  155. _activeChar = activeChar;
  156. _petSummon = petSummon;
  157. }
  158. public void run()
  159. {
  160. try
  161. {
  162. if (_petSummon.getCurrentFed() <= 0)
  163. _petSummon.unSummon(_activeChar);
  164. else
  165. _petSummon.startFeed();
  166. }
  167. catch (Exception e)
  168. {
  169. _log.log(Level.SEVERE, "", e);
  170. }
  171. }
  172. }
  173. // TODO: this should be inside skill handler
  174. static class PetSummonFinalizer implements Runnable
  175. {
  176. private final L2PcInstance _activeChar;
  177. private final L2ItemInstance _item;
  178. private final L2NpcTemplate _npcTemplate;
  179. PetSummonFinalizer(L2PcInstance activeChar, L2NpcTemplate npcTemplate, L2ItemInstance item)
  180. {
  181. _activeChar = activeChar;
  182. _npcTemplate = npcTemplate;
  183. _item = item;
  184. }
  185. public void run()
  186. {
  187. try
  188. {
  189. _activeChar.sendPacket(new MagicSkillLaunched(_activeChar, 2046, 1));
  190. _activeChar.setIsCastingNow(false);
  191. // check for summon item validity
  192. if (_item == null
  193. || _item.getOwnerId() != _activeChar.getObjectId()
  194. || _item.getLocation() != L2ItemInstance.ItemLocation.INVENTORY)
  195. return;
  196. final L2PetInstance petSummon = L2PetInstance.spawnPet(_npcTemplate, _activeChar, _item);
  197. if (petSummon == null)
  198. return;
  199. petSummon.setShowSummonAnimation(true);
  200. petSummon.setTitle(_activeChar.getName());
  201. if (!petSummon.isRespawned())
  202. {
  203. petSummon.setCurrentHp(petSummon.getMaxHp());
  204. petSummon.setCurrentMp(petSummon.getMaxMp());
  205. petSummon.getStat().setExp(petSummon.getExpForThisLevel());
  206. petSummon.setCurrentFed(petSummon.getMaxFed());
  207. }
  208. petSummon.setRunning();
  209. if (!petSummon.isRespawned())
  210. petSummon.store();
  211. _activeChar.setPet(petSummon);
  212. //JIV remove - done on spawn
  213. //L2World.getInstance().storeObject(petSummon);
  214. petSummon.spawnMe(_activeChar.getX() + 50, _activeChar.getY() + 100, _activeChar.getZ());
  215. petSummon.startFeed();
  216. _item.setEnchantLevel(petSummon.getLevel());
  217. if (petSummon.getCurrentFed() <= 0)
  218. ThreadPoolManager.getInstance().scheduleGeneral(new PetSummonFeedWait(_activeChar, petSummon), 60000);
  219. else
  220. petSummon.startFeed();
  221. petSummon.setFollowStatus(true);
  222. petSummon.sendPacket(new PetItemList(petSummon));
  223. petSummon.broadcastStatusUpdate();
  224. }
  225. catch (Exception e)
  226. {
  227. _log.log(Level.SEVERE, "", e);
  228. }
  229. }
  230. }
  231. }