DayNightSpawnManager.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. protected DayNightSpawnManager()
  41. {
  42. _dayCreatures = new ArrayList<>();
  43. _nightCreatures = new ArrayList<>();
  44. _bosses = new FastMap<>();
  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. * @param unSpawnCreatures List with spawns must be unspawned
  72. * @param spawnCreatures List with spawns must be spawned
  73. * @param UnspawnLogInfo String for log info for unspawned L2NpcInstance
  74. * @param SpawnLogInfo 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. L2RaidBossInstance boss;
  164. for (L2Spawn spawn : _bosses.keySet())
  165. {
  166. boss = _bosses.get(spawn);
  167. if ((boss == null) && (mode == 1))
  168. {
  169. boss = (L2RaidBossInstance) spawn.doSpawn();
  170. RaidBossSpawnManager.getInstance().notifySpawnNightBoss(boss);
  171. _bosses.remove(spawn);
  172. _bosses.put(spawn, boss);
  173. continue;
  174. }
  175. if ((boss == null) && (mode == 0))
  176. {
  177. continue;
  178. }
  179. if ((boss != null) && (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. _bosses.put(spawnDat, null);
  214. return null;
  215. }
  216. private static class SingletonHolder
  217. {
  218. protected static final DayNightSpawnManager _instance = new DayNightSpawnManager();
  219. }
  220. }