DayNightSpawnManager.java 6.2 KB

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