L2SkillSpawn.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. package com.l2jserver.gameserver.skills.l2skills;
  16. import java.util.logging.Level;
  17. import com.l2jserver.gameserver.datatables.NpcTable;
  18. import com.l2jserver.gameserver.model.L2Object;
  19. import com.l2jserver.gameserver.model.L2Skill;
  20. import com.l2jserver.gameserver.model.L2Spawn;
  21. import com.l2jserver.gameserver.model.actor.L2Character;
  22. import com.l2jserver.gameserver.model.actor.L2Npc;
  23. import com.l2jserver.gameserver.templates.StatsSet;
  24. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  25. import com.l2jserver.util.Rnd;
  26. public class L2SkillSpawn extends L2Skill
  27. {
  28. private final int _npcId;
  29. private final int _despawnDelay;
  30. private final boolean _summonSpawn;
  31. private final boolean _randomOffset;
  32. public L2SkillSpawn(StatsSet set)
  33. {
  34. super(set);
  35. _npcId = set.getInteger("npcId", 0);
  36. _despawnDelay = set.getInteger("despawnDelay", 0);
  37. _summonSpawn = set.getBool("isSummonSpawn", false);
  38. _randomOffset = set.getBool("randomOffset", true);
  39. }
  40. @Override
  41. public void useSkill(L2Character caster, L2Object[] targets)
  42. {
  43. if (caster.isAlikeDead())
  44. return;
  45. if (_npcId == 0)
  46. {
  47. _log.warning("NPC ID not defined for skill ID:"+this.getId());
  48. return;
  49. }
  50. final L2NpcTemplate template = NpcTable.getInstance().getTemplate(_npcId);
  51. if (template == null)
  52. {
  53. _log.warning("Spawn of the nonexisting NPC ID:"+_npcId+", skill ID:"+this.getId());
  54. return;
  55. }
  56. try
  57. {
  58. final L2Spawn spawn = new L2Spawn(template);
  59. spawn.setInstanceId(caster.getInstanceId());
  60. spawn.setHeading(-1);
  61. if (_randomOffset)
  62. {
  63. spawn.setLocx(caster.getX() + (Rnd.nextBoolean() ? Rnd.get(20, 50) : Rnd.get(-50, -20)));
  64. spawn.setLocy(caster.getY() + (Rnd.nextBoolean() ? Rnd.get(20, 50) : Rnd.get(-50, -20)));
  65. }
  66. else
  67. {
  68. spawn.setLocx(caster.getX());
  69. spawn.setLocy(caster.getY());
  70. }
  71. spawn.setLocz(caster.getZ() + 20);
  72. spawn.stopRespawn();
  73. L2Npc npc = spawn.spawnOne(_summonSpawn);
  74. if (_despawnDelay > 0)
  75. npc.scheduleDespawn(_despawnDelay);
  76. }
  77. catch (Exception e)
  78. {
  79. _log.log(Level.WARNING, "Exception while spawning NPC ID: " + _npcId + ", skill ID: " + this.getId() + ", exception: " + e.getMessage(), e);
  80. }
  81. }
  82. }