DayNightSpawnManager.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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.instancemanager;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import javolution.util.FastMap;
  22. import com.l2jserver.gameserver.GameTimeController;
  23. import com.l2jserver.gameserver.model.L2Spawn;
  24. import com.l2jserver.gameserver.model.actor.L2Npc;
  25. import com.l2jserver.gameserver.model.actor.instance.L2RaidBossInstance;
  26. /**
  27. *
  28. * @version $Revision: $ $Date: $
  29. * @author godson
  30. */
  31. public class DayNightSpawnManager
  32. {
  33. private static Logger _log = Logger.getLogger(DayNightSpawnManager.class.getName());
  34. private List<L2Spawn> _dayCreatures;
  35. private List<L2Spawn> _nightCreatures;
  36. private Map<L2Spawn, L2RaidBossInstance> _bosses;
  37. //private static int _currentState; // 0 = Day, 1 = Night
  38. public static DayNightSpawnManager getInstance()
  39. {
  40. return SingletonHolder._instance;
  41. }
  42. private DayNightSpawnManager()
  43. {
  44. _dayCreatures = new ArrayList<L2Spawn>();
  45. _nightCreatures = new ArrayList<L2Spawn>();
  46. _bosses = new FastMap<L2Spawn, L2RaidBossInstance>();
  47. _log.info("DayNightSpawnManager: Day/Night handler initialized");
  48. }
  49. public void addDayCreature(L2Spawn spawnDat)
  50. {
  51. _dayCreatures.add(spawnDat);
  52. }
  53. public void addNightCreature(L2Spawn spawnDat)
  54. {
  55. _nightCreatures.add(spawnDat);
  56. }
  57. /**
  58. * Spawn Day Creatures, and Unspawn Night Creatures
  59. */
  60. public void spawnDayCreatures()
  61. {
  62. spawnCreatures(_nightCreatures, _dayCreatures, "night", "day");
  63. }
  64. /**
  65. * Spawn Night Creatures, and Unspawn Day Creatures
  66. */
  67. public void spawnNightCreatures()
  68. {
  69. spawnCreatures(_dayCreatures, _nightCreatures, "day", "night");
  70. }
  71. /**
  72. * Manage Spawn/Respawn
  73. * Arg 1 : List with spawns must be unspawned
  74. * Arg 2 : List with spawns must be spawned
  75. * Arg 3 : String for log info for unspawned L2NpcInstance
  76. * Arg 4 : String for log info for spawned L2NpcInstance
  77. */
  78. private void spawnCreatures(List<L2Spawn> unSpawnCreatures, List<L2Spawn> spawnCreatures, String UnspawnLogInfo,
  79. String SpawnLogInfo)
  80. {
  81. try
  82. {
  83. if (!unSpawnCreatures.isEmpty())
  84. {
  85. int i = 0;
  86. for (L2Spawn spawn: unSpawnCreatures)
  87. {
  88. if (spawn == null)
  89. continue;
  90. spawn.stopRespawn();
  91. L2Npc last = spawn.getLastSpawn();
  92. if (last != null)
  93. {
  94. last.deleteMe();
  95. i++;
  96. }
  97. }
  98. _log.info("DayNightSpawnManager: Removed " + i + " " + UnspawnLogInfo + " creatures");
  99. }
  100. int i = 0;
  101. for (L2Spawn spawnDat : spawnCreatures)
  102. {
  103. if (spawnDat == null)
  104. continue;
  105. spawnDat.startRespawn();
  106. spawnDat.doSpawn();
  107. i++;
  108. }
  109. _log.info("DayNightSpawnManager: Spawned " + i + " " + SpawnLogInfo + " creatures");
  110. }
  111. catch (Exception e)
  112. {
  113. _log.log(Level.WARNING, "Error while spawning creatures: " + e.getMessage(), e);
  114. }
  115. }
  116. private void changeMode(int mode)
  117. {
  118. if (_nightCreatures.isEmpty() && _dayCreatures.isEmpty())
  119. return;
  120. switch (mode)
  121. {
  122. case 0:
  123. spawnDayCreatures();
  124. specialNightBoss(0);
  125. break;
  126. case 1:
  127. spawnNightCreatures();
  128. specialNightBoss(1);
  129. break;
  130. default:
  131. _log.warning("DayNightSpawnManager: Wrong mode sent");
  132. break;
  133. }
  134. }
  135. public DayNightSpawnManager trim()
  136. {
  137. ((ArrayList<?>)_nightCreatures).trimToSize();
  138. ((ArrayList<?>)_dayCreatures).trimToSize();
  139. return this;
  140. }
  141. public void notifyChangeMode()
  142. {
  143. try
  144. {
  145. if (GameTimeController.getInstance().isNowNight())
  146. changeMode(1);
  147. else
  148. changeMode(0);
  149. }
  150. catch (Exception e)
  151. {
  152. _log.log(Level.WARNING, "Error while notifyChangeMode(): " + e.getMessage(), e);
  153. }
  154. }
  155. public void cleanUp()
  156. {
  157. _nightCreatures.clear();
  158. _dayCreatures.clear();
  159. _bosses.clear();
  160. }
  161. private void specialNightBoss(int mode)
  162. {
  163. try
  164. {
  165. for (L2Spawn spawn : _bosses.keySet())
  166. {
  167. L2RaidBossInstance boss = _bosses.get(spawn);
  168. if (boss == null && mode == 1)
  169. {
  170. boss = (L2RaidBossInstance) spawn.doSpawn();
  171. RaidBossSpawnManager.getInstance().notifySpawnNightBoss(boss);
  172. _bosses.remove(spawn);
  173. _bosses.put(spawn, boss);
  174. continue;
  175. }
  176. if (boss == null && mode == 0)
  177. continue;
  178. if (boss.getNpcId() == 25328 && boss.getRaidStatus().equals(RaidBossSpawnManager.StatusEnum.ALIVE))
  179. handleHellmans(boss, mode);
  180. return;
  181. }
  182. }
  183. catch (Exception e)
  184. {
  185. _log.log(Level.WARNING, "Error while specialNoghtBoss(): " + e.getMessage(), e);
  186. }
  187. }
  188. private void handleHellmans(L2RaidBossInstance boss, int mode)
  189. {
  190. switch (mode)
  191. {
  192. case 0:
  193. boss.deleteMe();
  194. _log.info("DayNightSpawnManager: Deleting Hellman raidboss");
  195. break;
  196. case 1:
  197. boss.spawnMe();
  198. _log.info("DayNightSpawnManager: Spawning Hellman raidboss");
  199. break;
  200. }
  201. }
  202. public L2RaidBossInstance handleBoss(L2Spawn spawnDat)
  203. {
  204. if (_bosses.containsKey(spawnDat))
  205. return _bosses.get(spawnDat);
  206. if (GameTimeController.getInstance().isNowNight())
  207. {
  208. L2RaidBossInstance raidboss = (L2RaidBossInstance) spawnDat.doSpawn();
  209. _bosses.put(spawnDat, raidboss);
  210. return raidboss;
  211. }
  212. else
  213. _bosses.put(spawnDat, null);
  214. return null;
  215. }
  216. @SuppressWarnings("synthetic-access")
  217. private static class SingletonHolder
  218. {
  219. protected static final DayNightSpawnManager _instance = new DayNightSpawnManager();
  220. }
  221. }