SummonNpc.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2004-2014 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack 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 DataPack 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 handlers.effecthandlers;
  20. import com.l2jserver.gameserver.datatables.NpcData;
  21. import com.l2jserver.gameserver.idfactory.IdFactory;
  22. import com.l2jserver.gameserver.model.L2Spawn;
  23. import com.l2jserver.gameserver.model.Location;
  24. import com.l2jserver.gameserver.model.StatsSet;
  25. import com.l2jserver.gameserver.model.actor.L2Npc;
  26. import com.l2jserver.gameserver.model.actor.instance.L2DecoyInstance;
  27. import com.l2jserver.gameserver.model.actor.instance.L2EffectPointInstance;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  30. import com.l2jserver.gameserver.model.conditions.Condition;
  31. import com.l2jserver.gameserver.model.effects.AbstractEffect;
  32. import com.l2jserver.gameserver.model.effects.L2EffectType;
  33. import com.l2jserver.gameserver.model.skills.BuffInfo;
  34. import com.l2jserver.gameserver.model.skills.targets.L2TargetType;
  35. import com.l2jserver.util.Rnd;
  36. /**
  37. * Summon Npc effect implementation.
  38. * @author Zoey76
  39. */
  40. public final class SummonNpc extends AbstractEffect
  41. {
  42. private int _despawnDelay;
  43. private final int _npcId;
  44. private final int _npcCount;
  45. private final boolean _randomOffset;
  46. private final boolean _isSummonSpawn;
  47. public SummonNpc(Condition attachCond, Condition applyCond, StatsSet set, StatsSet params)
  48. {
  49. super(attachCond, applyCond, set, params);
  50. _despawnDelay = params.getInt("despawnDelay", 20000);
  51. _npcId = params.getInt("npcId", 0);
  52. _npcCount = params.getInt("npcCount", 1);
  53. _randomOffset = params.getBoolean("randomOffset", false);
  54. _isSummonSpawn = params.getBoolean("isSummonSpawn", false);
  55. }
  56. @Override
  57. public L2EffectType getEffectType()
  58. {
  59. return L2EffectType.SUMMON_NPC;
  60. }
  61. @Override
  62. public boolean isInstant()
  63. {
  64. return true;
  65. }
  66. @Override
  67. public void onStart(BuffInfo info)
  68. {
  69. if ((info.getEffected() == null) || !info.getEffected().isPlayer() || info.getEffected().isAlikeDead() || info.getEffected().getActingPlayer().inObserverMode())
  70. {
  71. return;
  72. }
  73. if ((_npcId <= 0) || (_npcCount <= 0))
  74. {
  75. _log.warning(SummonNpc.class.getSimpleName() + ": Invalid NPC ID or count skill ID: " + info.getSkill().getId());
  76. return;
  77. }
  78. final L2PcInstance player = info.getEffected().getActingPlayer();
  79. if (player.isMounted())
  80. {
  81. return;
  82. }
  83. final L2NpcTemplate npcTemplate = NpcData.getInstance().getTemplate(_npcId);
  84. if (npcTemplate == null)
  85. {
  86. _log.warning(SummonNpc.class.getSimpleName() + ": Spawn of the nonexisting NPC ID: " + _npcId + ", skill ID:" + info.getSkill().getId());
  87. return;
  88. }
  89. switch (npcTemplate.getType())
  90. {
  91. case "L2Decoy":
  92. {
  93. final L2DecoyInstance decoy = new L2DecoyInstance(IdFactory.getInstance().getNextId(), npcTemplate, player, _despawnDelay);
  94. decoy.setCurrentHp(decoy.getMaxHp());
  95. decoy.setCurrentMp(decoy.getMaxMp());
  96. decoy.setHeading(player.getHeading());
  97. decoy.setInstanceId(player.getInstanceId());
  98. decoy.setSummoner(player);
  99. decoy.spawnMe(player.getX(), player.getY(), player.getZ());
  100. player.setDecoy(decoy);
  101. break;
  102. }
  103. case "L2EffectPoint": // TODO: Implement proper signet skills.
  104. {
  105. final L2EffectPointInstance effectPoint = new L2EffectPointInstance(IdFactory.getInstance().getNextId(), npcTemplate, player);
  106. effectPoint.setCurrentHp(effectPoint.getMaxHp());
  107. effectPoint.setCurrentMp(effectPoint.getMaxMp());
  108. int x = player.getX();
  109. int y = player.getY();
  110. int z = player.getZ();
  111. if (info.getSkill().getTargetType() == L2TargetType.GROUND)
  112. {
  113. final Location wordPosition = player.getActingPlayer().getCurrentSkillWorldPosition();
  114. if (wordPosition != null)
  115. {
  116. x = wordPosition.getX();
  117. y = wordPosition.getY();
  118. z = wordPosition.getZ();
  119. }
  120. }
  121. effectPoint.setIsInvul(true);
  122. effectPoint.setSummoner(player);
  123. effectPoint.spawnMe(x, y, z);
  124. _despawnDelay = NpcData.getInstance().getTemplate(_npcId).getParameters().getInt("despawn_time") * 1000;
  125. if (_despawnDelay > 0)
  126. {
  127. effectPoint.scheduleDespawn(_despawnDelay);
  128. }
  129. break;
  130. }
  131. default:
  132. {
  133. L2Spawn spawn;
  134. try
  135. {
  136. spawn = new L2Spawn(npcTemplate);
  137. }
  138. catch (Exception e)
  139. {
  140. _log.warning(SummonNpc.class.getSimpleName() + ": " + e.getMessage());
  141. return;
  142. }
  143. int x = player.getX();
  144. int y = player.getY();
  145. if (_randomOffset)
  146. {
  147. x += (Rnd.nextBoolean() ? Rnd.get(20, 50) : Rnd.get(-50, -20));
  148. y += (Rnd.nextBoolean() ? Rnd.get(20, 50) : Rnd.get(-50, -20));
  149. }
  150. spawn.setX(x);
  151. spawn.setY(y);
  152. spawn.setZ(player.getZ());
  153. spawn.setHeading(player.getHeading());
  154. spawn.stopRespawn();
  155. final L2Npc npc = spawn.doSpawn(_isSummonSpawn);
  156. npc.setSummoner(player);
  157. npc.setName(npcTemplate.getName());
  158. npc.setTitle(npcTemplate.getName());
  159. if (_despawnDelay > 0)
  160. {
  161. npc.scheduleDespawn(_despawnDelay);
  162. }
  163. npc.setIsRunning(false); // TODO: Fix broadcast info.
  164. }
  165. }
  166. }
  167. }