DayNightSpawnManager.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. spawn.decreaseCount(null);
  98. }
  99. _log.info("DayNightSpawnManager: Removed " + i + " " + UnspawnLogInfo + " creatures");
  100. }
  101. int i = 0;
  102. for (L2Spawn spawnDat : spawnCreatures)
  103. {
  104. if (spawnDat == null)
  105. continue;
  106. spawnDat.startRespawn();
  107. spawnDat.doSpawn();
  108. i++;
  109. }
  110. _log.info("DayNightSpawnManager: Spawned " + i + " " + SpawnLogInfo + " creatures");
  111. }
  112. catch (Exception e)
  113. {
  114. _log.log(Level.WARNING, "Error while spawning creatures: " + e.getMessage(), e);
  115. }
  116. }
  117. private void changeMode(int mode)
  118. {
  119. if (_nightCreatures.isEmpty() && _dayCreatures.isEmpty())
  120. return;
  121. switch (mode)
  122. {
  123. case 0:
  124. spawnDayCreatures();
  125. specialNightBoss(0);
  126. break;
  127. case 1:
  128. spawnNightCreatures();
  129. specialNightBoss(1);
  130. break;
  131. default:
  132. _log.warning("DayNightSpawnManager: Wrong mode sent");
  133. break;
  134. }
  135. }
  136. public DayNightSpawnManager trim()
  137. {
  138. ((ArrayList<?>)_nightCreatures).trimToSize();
  139. ((ArrayList<?>)_dayCreatures).trimToSize();
  140. return this;
  141. }
  142. public void notifyChangeMode()
  143. {
  144. try
  145. {
  146. if (GameTimeController.getInstance().isNowNight())
  147. changeMode(1);
  148. else
  149. changeMode(0);
  150. }
  151. catch (Exception e)
  152. {
  153. _log.log(Level.WARNING, "Error while notifyChangeMode(): " + e.getMessage(), e);
  154. }
  155. }
  156. public void cleanUp()
  157. {
  158. _nightCreatures.clear();
  159. _dayCreatures.clear();
  160. _bosses.clear();
  161. }
  162. private void specialNightBoss(int mode)
  163. {
  164. try
  165. {
  166. for (L2Spawn spawn : _bosses.keySet())
  167. {
  168. L2RaidBossInstance boss = _bosses.get(spawn);
  169. if (boss == null && mode == 1)
  170. {
  171. boss = (L2RaidBossInstance) spawn.doSpawn();
  172. RaidBossSpawnManager.getInstance().notifySpawnNightBoss(boss);
  173. _bosses.remove(spawn);
  174. _bosses.put(spawn, boss);
  175. continue;
  176. }
  177. if (boss == null && mode == 0)
  178. continue;
  179. if (boss.getNpcId() == 25328 && boss.getRaidStatus().equals(RaidBossSpawnManager.StatusEnum.ALIVE))
  180. handleHellmans(boss, mode);
  181. return;
  182. }
  183. }
  184. catch (Exception e)
  185. {
  186. _log.log(Level.WARNING, "Error while specialNoghtBoss(): " + e.getMessage(), e);
  187. }
  188. }
  189. private void handleHellmans(L2RaidBossInstance boss, int mode)
  190. {
  191. switch (mode)
  192. {
  193. case 0:
  194. boss.deleteMe();
  195. _log.info("DayNightSpawnManager: Deleting Hellman raidboss");
  196. break;
  197. case 1:
  198. boss.spawnMe();
  199. _log.info("DayNightSpawnManager: Spawning Hellman raidboss");
  200. break;
  201. }
  202. }
  203. public L2RaidBossInstance handleBoss(L2Spawn spawnDat)
  204. {
  205. if (_bosses.containsKey(spawnDat))
  206. return _bosses.get(spawnDat);
  207. if (GameTimeController.getInstance().isNowNight())
  208. {
  209. L2RaidBossInstance raidboss = (L2RaidBossInstance) spawnDat.doSpawn();
  210. _bosses.put(spawnDat, raidboss);
  211. return raidboss;
  212. }
  213. else
  214. _bosses.put(spawnDat, null);
  215. return null;
  216. }
  217. @SuppressWarnings("synthetic-access")
  218. private static class SingletonHolder
  219. {
  220. protected static final DayNightSpawnManager _instance = new DayNightSpawnManager();
  221. }
  222. }