SummonItems.java 8.5 KB

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