SummonNpc.java 5.7 KB

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