L2GroupSpawn.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.model;
  16. import java.lang.reflect.Constructor;
  17. import java.util.logging.Level;
  18. import com.l2jserver.Config;
  19. import com.l2jserver.gameserver.Territory;
  20. import com.l2jserver.gameserver.idfactory.IdFactory;
  21. import com.l2jserver.gameserver.model.actor.L2Npc;
  22. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  23. import com.l2jserver.util.Rnd;
  24. /**
  25. * @author littlecrow
  26. * A special spawn implementation to spawn controllable mob
  27. */
  28. public class L2GroupSpawn extends L2Spawn
  29. {
  30. private Constructor<?> _constructor;
  31. private L2NpcTemplate _template;
  32. public L2GroupSpawn(L2NpcTemplate mobTemplate) throws SecurityException, ClassNotFoundException, NoSuchMethodException
  33. {
  34. super(mobTemplate);
  35. _constructor = Class.forName("com.l2jserver.gameserver.model.actor.instance.L2ControllableMobInstance").getConstructors()[0];
  36. _template = mobTemplate;
  37. setAmount(1);
  38. }
  39. public L2Npc doGroupSpawn()
  40. {
  41. L2Npc mob = null;
  42. try
  43. {
  44. if (_template.isType("L2Pet") || _template.isType("L2Minion"))
  45. return null;
  46. Object[] parameters = {IdFactory.getInstance().getNextId(), _template};
  47. Object tmp = _constructor.newInstance(parameters);
  48. if (!(tmp instanceof L2Npc))
  49. return null;
  50. mob = (L2Npc)tmp;
  51. int newlocx, newlocy, newlocz;
  52. if (getLocx() == 0 && getLocy() == 0)
  53. {
  54. if (getLocation() == 0)
  55. return null;
  56. int p[] = Territory.getInstance().getRandomPoint(getLocation());
  57. newlocx = p[0];
  58. newlocy = p[1];
  59. newlocz = p[2];
  60. }
  61. else
  62. {
  63. newlocx = getLocx();
  64. newlocy = getLocy();
  65. newlocz = getLocz();
  66. }
  67. mob.setCurrentHpMp(mob.getMaxHp(), mob.getMaxMp());
  68. if (getHeading() == -1)
  69. mob.setHeading(Rnd.nextInt(61794));
  70. else
  71. mob.setHeading(getHeading());
  72. mob.setSpawn(this);
  73. mob.spawnMe(newlocx, newlocy, newlocz);
  74. mob.onSpawn();
  75. if (Config.DEBUG)
  76. {
  77. _log.finest("Spawned Mob Id: " + _template.getNpcId() + " ,at: X: " + mob.getX() + " Y: " + mob.getY() + " Z: " + mob.getZ());
  78. }
  79. return mob;
  80. }
  81. catch (Exception e)
  82. {
  83. _log.log(Level.WARNING, "NPC class not found: " + e.getMessage(), e);
  84. return null;
  85. }
  86. }
  87. }