DayNightSpawnManager.java 6.1 KB

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