L2GroupSpawn.java 3.1 KB

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