SummonNpc.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (C) 2004-2013 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.NpcTable;
  21. import com.l2jserver.gameserver.idfactory.IdFactory;
  22. import com.l2jserver.gameserver.model.L2Spawn;
  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.skills.BuffInfo;
  32. import com.l2jserver.gameserver.model.skills.targets.L2TargetType;
  33. import com.l2jserver.gameserver.util.Point3D;
  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 final 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 = getParameters().getInt("despawnDelay", 20000);
  50. _npcId = getParameters().getInt("npcId", 0);
  51. _npcCount = getParameters().getInt("npcCount", 1);
  52. _randomOffset = getParameters().getBoolean("randomOffset", false);
  53. _isSummonSpawn = getParameters().getBoolean("isSummonSpawn", false);
  54. }
  55. @Override
  56. public boolean isInstant()
  57. {
  58. return true;
  59. }
  60. @Override
  61. public boolean onStart(BuffInfo info)
  62. {
  63. if ((info.getEffected() == null) || !info.getEffected().isPlayer() || info.getEffected().isAlikeDead() || info.getEffected().getActingPlayer().inObserverMode())
  64. {
  65. return false;
  66. }
  67. if ((_npcId <= 0) || (_npcCount <= 0))
  68. {
  69. _log.warning(SummonNpc.class.getSimpleName() + ": Invalid NPC ID or count skill ID: " + info.getSkill().getId());
  70. return false;
  71. }
  72. final L2PcInstance player = info.getEffected().getActingPlayer();
  73. if (player.isMounted())
  74. {
  75. return false;
  76. }
  77. final L2NpcTemplate npcTemplate = NpcTable.getInstance().getTemplate(_npcId);
  78. if (npcTemplate == null)
  79. {
  80. _log.warning(SummonNpc.class.getSimpleName() + ": Spawn of the nonexisting NPC ID: " + _npcId + ", skill ID:" + info.getSkill().getId());
  81. return false;
  82. }
  83. switch (npcTemplate.getType())
  84. {
  85. case "L2Decoy":
  86. {
  87. final L2DecoyInstance decoy = new L2DecoyInstance(IdFactory.getInstance().getNextId(), npcTemplate, player, _despawnDelay);
  88. decoy.setCurrentHp(decoy.getMaxHp());
  89. decoy.setCurrentMp(decoy.getMaxMp());
  90. decoy.setHeading(player.getHeading());
  91. decoy.setInstanceId(player.getInstanceId());
  92. decoy.setSummoner(player);
  93. decoy.spawnMe(player.getX(), player.getY(), player.getZ());
  94. player.setDecoy(decoy);
  95. break;
  96. }
  97. case "L2EffectPoint": // TODO: Implement proper signet skills.
  98. {
  99. final L2EffectPointInstance effectPoint = new L2EffectPointInstance(IdFactory.getInstance().getNextId(), npcTemplate, player);
  100. effectPoint.setCurrentHp(effectPoint.getMaxHp());
  101. effectPoint.setCurrentMp(effectPoint.getMaxMp());
  102. int x = player.getX();
  103. int y = player.getY();
  104. int z = player.getZ();
  105. if (info.getSkill().getTargetType() == L2TargetType.GROUND)
  106. {
  107. final Point3D wordPosition = player.getActingPlayer().getCurrentSkillWorldPosition();
  108. if (wordPosition != null)
  109. {
  110. x = wordPosition.getX();
  111. y = wordPosition.getY();
  112. z = wordPosition.getZ();
  113. }
  114. }
  115. effectPoint.setIsInvul(true);
  116. effectPoint.setSummoner(player);
  117. effectPoint.spawnMe(x, y, z);
  118. if (_despawnDelay > 0)
  119. {
  120. effectPoint.scheduleDespawn(_despawnDelay);
  121. }
  122. break;
  123. }
  124. default:
  125. {
  126. L2Spawn spawn;
  127. try
  128. {
  129. spawn = new L2Spawn(npcTemplate);
  130. }
  131. catch (Exception e)
  132. {
  133. _log.warning(SummonNpc.class.getSimpleName() + ": " + e.getMessage());
  134. return false;
  135. }
  136. int x = player.getX();
  137. int y = player.getY();
  138. if (_randomOffset)
  139. {
  140. x += (Rnd.nextBoolean() ? Rnd.get(20, 50) : Rnd.get(-50, -20));
  141. y += (Rnd.nextBoolean() ? Rnd.get(20, 50) : Rnd.get(-50, -20));
  142. }
  143. spawn.setX(x);
  144. spawn.setY(y);
  145. spawn.setZ(player.getZ());
  146. spawn.setHeading(player.getHeading());
  147. spawn.stopRespawn();
  148. final L2Npc npc = spawn.doSpawn(_isSummonSpawn);
  149. npc.setSummoner(player);
  150. npc.setName(npcTemplate.getName());
  151. npc.setTitle(npcTemplate.getName());
  152. if (_despawnDelay > 0)
  153. {
  154. npc.scheduleDespawn(_despawnDelay);
  155. }
  156. npc.setIsRunning(false); // TODO: Fix broadcast info.
  157. }
  158. }
  159. return true;
  160. }
  161. }