DayNightSpawnManager.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright © 2004-2019 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 org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  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. private static final Logger LOG = LoggerFactory.getLogger(DayNightSpawnManager.class);
  35. private final List<L2Spawn> _dayCreatures = new ArrayList<>();
  36. private final List<L2Spawn> _nightCreatures = new ArrayList<>();
  37. private final Map<L2Spawn, L2RaidBossInstance> _bosses = new ConcurrentHashMap<>();
  38. protected DayNightSpawnManager() {
  39. // Prevent external initialization.
  40. }
  41. public void addDayCreature(L2Spawn spawnDat) {
  42. _dayCreatures.add(spawnDat);
  43. }
  44. public void addNightCreature(L2Spawn spawnDat) {
  45. _nightCreatures.add(spawnDat);
  46. }
  47. /**
  48. * Spawn Day Creatures, and Unspawn Night Creatures
  49. */
  50. public void spawnDayCreatures() {
  51. spawnCreatures(_nightCreatures, _dayCreatures, "night", "day");
  52. }
  53. /**
  54. * Spawn Night Creatures, and Unspawn Day Creatures
  55. */
  56. public void spawnNightCreatures() {
  57. spawnCreatures(_dayCreatures, _nightCreatures, "day", "night");
  58. }
  59. /**
  60. * Manage Spawn/Respawn
  61. * @param unSpawnCreatures List with spawns must be unspawned
  62. * @param spawnCreatures List with spawns must be spawned
  63. * @param unspawnLogInfo String for log info for unspawned L2NpcInstance
  64. * @param spawnLogInfo String for log info for spawned L2NpcInstance
  65. */
  66. private void spawnCreatures(List<L2Spawn> unSpawnCreatures, List<L2Spawn> spawnCreatures, String unspawnLogInfo, String spawnLogInfo) {
  67. try {
  68. if (!unSpawnCreatures.isEmpty()) {
  69. int i = 0;
  70. for (L2Spawn spawn : unSpawnCreatures) {
  71. if (spawn == null) {
  72. continue;
  73. }
  74. spawn.stopRespawn();
  75. L2Npc last = spawn.getLastSpawn();
  76. if (last != null) {
  77. last.deleteMe();
  78. i++;
  79. }
  80. }
  81. LOG.info("Removed {} {} creatures.", i, unspawnLogInfo);
  82. }
  83. int i = 0;
  84. for (L2Spawn spawnDat : spawnCreatures) {
  85. if (spawnDat == null) {
  86. continue;
  87. }
  88. spawnDat.startRespawn();
  89. spawnDat.doSpawn();
  90. i++;
  91. }
  92. LOG.info("Spawned {} {} creatures.", i, spawnLogInfo);
  93. } catch (Exception ex) {
  94. LOG.warn("There has been an error while spawning creatures!", ex);
  95. }
  96. }
  97. private void changeMode(int mode) {
  98. if (_nightCreatures.isEmpty() && _dayCreatures.isEmpty() && _bosses.isEmpty()) {
  99. return;
  100. }
  101. switch (mode) {
  102. case 0:
  103. spawnDayCreatures();
  104. specialNightBoss(0);
  105. break;
  106. case 1:
  107. spawnNightCreatures();
  108. specialNightBoss(1);
  109. break;
  110. }
  111. }
  112. public DayNightSpawnManager trim() {
  113. ((ArrayList<?>) _nightCreatures).trimToSize();
  114. ((ArrayList<?>) _dayCreatures).trimToSize();
  115. return this;
  116. }
  117. public void notifyChangeMode() {
  118. if (GameTimeController.getInstance().isNight()) {
  119. changeMode(1);
  120. } else {
  121. changeMode(0);
  122. }
  123. }
  124. public void cleanUp() {
  125. _nightCreatures.clear();
  126. _dayCreatures.clear();
  127. _bosses.clear();
  128. }
  129. private void specialNightBoss(int mode) {
  130. try {
  131. for (L2Spawn spawn : _bosses.keySet()) {
  132. var boss = _bosses.get(spawn);
  133. if ((boss == null) && (mode == 1)) {
  134. boss = (L2RaidBossInstance) spawn.doSpawn();
  135. RaidBossSpawnManager.getInstance().notifySpawnNightBoss(boss);
  136. _bosses.put(spawn, boss);
  137. continue;
  138. }
  139. if ((boss == null) && (mode == 0)) {
  140. continue;
  141. }
  142. if ((boss != null) && (boss.getId() == 25328) && boss.getRaidStatus().equals(RaidBossSpawnManager.StatusEnum.ALIVE)) {
  143. handleHellmans(boss, mode);
  144. }
  145. return;
  146. }
  147. } catch (Exception ex) {
  148. LOG.warn("There has been an error while spawning special night boss!", ex);
  149. }
  150. }
  151. private void handleHellmans(L2RaidBossInstance boss, int mode) {
  152. switch (mode) {
  153. case 0:
  154. boss.deleteMe();
  155. LOG.info("Deleting Hellman raidboss.");
  156. break;
  157. case 1:
  158. if (!boss.isVisible()) {
  159. boss.spawnMe();
  160. }
  161. LOG.info("Spawning Hellman raidboss.");
  162. break;
  163. }
  164. }
  165. public L2RaidBossInstance handleBoss(L2Spawn bossSpawn) {
  166. if (_bosses.containsKey(bossSpawn)) {
  167. return _bosses.get(bossSpawn);
  168. }
  169. if (GameTimeController.getInstance().isNight()) {
  170. L2RaidBossInstance raidboss = (L2RaidBossInstance) bossSpawn.doSpawn();
  171. _bosses.put(bossSpawn, raidboss);
  172. return raidboss;
  173. }
  174. return null;
  175. }
  176. public static DayNightSpawnManager getInstance() {
  177. return SingletonHolder.INSTANCE;
  178. }
  179. private static class SingletonHolder {
  180. protected static final DayNightSpawnManager INSTANCE = new DayNightSpawnManager();
  181. }
  182. }