2
0

L2GroupSpawn.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 net.sf.l2j.gameserver.model;
  16. import java.lang.reflect.Constructor;
  17. import net.sf.l2j.Config;
  18. import net.sf.l2j.gameserver.Territory;
  19. import net.sf.l2j.gameserver.idfactory.IdFactory;
  20. import net.sf.l2j.gameserver.model.actor.L2Npc;
  21. import net.sf.l2j.gameserver.templates.chars.L2NpcTemplate;
  22. import net.sf.l2j.util.Rnd;
  23. /**
  24. * @author littlecrow
  25. * A special spawn implementation to spawn controllable mob
  26. */
  27. public class L2GroupSpawn extends L2Spawn
  28. {
  29. private Constructor<?> _constructor;
  30. private L2NpcTemplate _template;
  31. public L2GroupSpawn(L2NpcTemplate mobTemplate) throws SecurityException, ClassNotFoundException, NoSuchMethodException
  32. {
  33. super(mobTemplate);
  34. _constructor = Class.forName("net.sf.l2j.gameserver.model.actor.instance.L2ControllableMobInstance").getConstructors()[0];
  35. _template = mobTemplate;
  36. setAmount(1);
  37. }
  38. public L2Npc doGroupSpawn()
  39. {
  40. L2Npc mob = null;
  41. try
  42. {
  43. if (_template.type.equalsIgnoreCase("L2Pet") ||
  44. _template.type.equalsIgnoreCase("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. _log.finest("spawned Mob ID: "+_template.npcId+" ,at: "
  77. +mob.getX()+" x, "+mob.getY()+" y, "+mob.getZ()+" z");
  78. return mob;
  79. }
  80. catch (Exception e)
  81. {
  82. _log.warning("NPC class not found: " + e);
  83. return null;
  84. }
  85. }
  86. }