SummonItems.java 8.4 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.actor.templates.L2NpcTemplate;
  37. import com.l2jserver.gameserver.model.entity.TvTEvent;
  38. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  39. import com.l2jserver.gameserver.network.SystemMessageId;
  40. import com.l2jserver.gameserver.network.serverpackets.MagicSkillLaunched;
  41. import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  42. import com.l2jserver.gameserver.network.serverpackets.PetItemList;
  43. import com.l2jserver.gameserver.network.serverpackets.SetupGauge;
  44. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  45. import com.l2jserver.gameserver.util.Broadcast;
  46. public class SummonItems implements IItemHandler
  47. {
  48. @Override
  49. public boolean useItem(L2Playable playable, L2ItemInstance item, boolean forceUse)
  50. {
  51. if (!(playable instanceof L2PcInstance))
  52. return false;
  53. if (!TvTEvent.onItemSummon(playable.getObjectId()))
  54. return false;
  55. final L2PcInstance activeChar = (L2PcInstance) playable;
  56. if (!activeChar.getFloodProtectors().getItemPetSummon().tryPerformAction("summon items"))
  57. return false;
  58. if (activeChar.isSitting())
  59. {
  60. activeChar.sendPacket(SystemMessageId.CANT_MOVE_SITTING);
  61. return false;
  62. }
  63. if(activeChar.getBlockCheckerArena() != -1)
  64. return false;
  65. if (activeChar.inObserverMode())
  66. return false;
  67. if (activeChar.isInOlympiadMode())
  68. {
  69. activeChar.sendPacket(SystemMessageId.THIS_ITEM_IS_NOT_AVAILABLE_FOR_THE_OLYMPIAD_EVENT);
  70. return false;
  71. }
  72. if (activeChar.isAllSkillsDisabled() || activeChar.isCastingNow())
  73. return false;
  74. final L2SummonItem sitem = SummonItemsData.getInstance().getSummonItem(item.getItemId());
  75. if ((activeChar.getPet() != null || activeChar.isMounted()) && sitem.isPetSummon())
  76. {
  77. activeChar.sendPacket(SystemMessageId.YOU_ALREADY_HAVE_A_PET);
  78. return false;
  79. }
  80. if (activeChar.isAttackingNow())
  81. {
  82. activeChar.sendPacket(SystemMessageId.YOU_CANNOT_SUMMON_IN_COMBAT);
  83. return false;
  84. }
  85. final int npcId = sitem.getNpcId();
  86. if (npcId == 0)
  87. return false;
  88. final L2NpcTemplate npcTemplate = NpcTable.getInstance().getTemplate(npcId);
  89. if (npcTemplate == null)
  90. return false;
  91. activeChar.stopMove(null, false);
  92. switch (sitem.getType())
  93. {
  94. case 0: // static summons (like Christmas tree)
  95. try
  96. {
  97. Collection<L2Character> characters = activeChar.getKnownList().getKnownCharactersInRadius(1200);
  98. for (L2Character ch : characters)
  99. {
  100. if (ch instanceof L2XmassTreeInstance && npcTemplate.isSpecialTree())
  101. {
  102. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.CANNOT_SUMMON_S1_AGAIN);
  103. sm.addCharName(ch);
  104. activeChar.sendPacket(sm);
  105. return false;
  106. }
  107. }
  108. if (activeChar.destroyItem("Summon", item.getObjectId(), 1, null, false))
  109. {
  110. final L2Spawn spawn = new L2Spawn(npcTemplate);
  111. spawn.setLocx(activeChar.getX());
  112. spawn.setLocy(activeChar.getY());
  113. spawn.setLocz(activeChar.getZ());
  114. spawn.setInstanceId(activeChar.getInstanceId());
  115. spawn.stopRespawn();
  116. final L2Npc npc = spawn.spawnOne(true);
  117. npc.setTitle(activeChar.getName());
  118. npc.setIsRunning(false); // broadcast info
  119. if (sitem.getDespawnDelay() > 0)
  120. npc.scheduleDespawn(sitem.getDespawnDelay() * 1000L);
  121. }
  122. }
  123. catch (Exception e)
  124. {
  125. activeChar.sendPacket(SystemMessageId.TARGET_CANT_FOUND);
  126. }
  127. break;
  128. case 1: // pet summons
  129. final L2Object oldTarget = activeChar.getTarget();
  130. activeChar.setTarget(activeChar);
  131. Broadcast.toSelfAndKnownPlayers(activeChar, new MagicSkillUse(activeChar, 2046, 1, 5000, 0));
  132. activeChar.setTarget(oldTarget);
  133. activeChar.sendPacket(new SetupGauge(0, 5000));
  134. activeChar.sendPacket(SystemMessageId.SUMMON_A_PET);
  135. activeChar.setIsCastingNow(true);
  136. ThreadPoolManager.getInstance().scheduleGeneral(new PetSummonFinalizer(activeChar, npcTemplate, item), 5000);
  137. break;
  138. case 2: // wyvern
  139. activeChar.mount(sitem.getNpcId(), item.getObjectId(), true);
  140. break;
  141. case 3: // Great Wolf
  142. activeChar.mount(sitem.getNpcId(), item.getObjectId(), false);
  143. break;
  144. }
  145. return true;
  146. }
  147. static class PetSummonFeedWait implements Runnable
  148. {
  149. private final L2PcInstance _activeChar;
  150. private final L2PetInstance _petSummon;
  151. PetSummonFeedWait(L2PcInstance activeChar, L2PetInstance petSummon)
  152. {
  153. _activeChar = activeChar;
  154. _petSummon = petSummon;
  155. }
  156. @Override
  157. public void run()
  158. {
  159. try
  160. {
  161. if (_petSummon.getCurrentFed() <= 0)
  162. _petSummon.unSummon(_activeChar);
  163. else
  164. _petSummon.startFeed();
  165. }
  166. catch (Exception e)
  167. {
  168. _log.log(Level.SEVERE, "", e);
  169. }
  170. }
  171. }
  172. // TODO: this should be inside skill handler
  173. static class PetSummonFinalizer implements Runnable
  174. {
  175. private final L2PcInstance _activeChar;
  176. private final L2ItemInstance _item;
  177. private final L2NpcTemplate _npcTemplate;
  178. PetSummonFinalizer(L2PcInstance activeChar, L2NpcTemplate npcTemplate, L2ItemInstance item)
  179. {
  180. _activeChar = activeChar;
  181. _npcTemplate = npcTemplate;
  182. _item = item;
  183. }
  184. @Override
  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. }