Instance.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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.model.entity;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.time.DayOfWeek;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.concurrent.ConcurrentHashMap;
  29. import java.util.concurrent.CopyOnWriteArrayList;
  30. import java.util.concurrent.ScheduledFuture;
  31. import java.util.logging.Level;
  32. import java.util.logging.Logger;
  33. import javax.xml.parsers.DocumentBuilderFactory;
  34. import org.w3c.dom.Document;
  35. import org.w3c.dom.NamedNodeMap;
  36. import org.w3c.dom.Node;
  37. import com.l2jserver.Config;
  38. import com.l2jserver.gameserver.ThreadPoolManager;
  39. import com.l2jserver.gameserver.data.xml.impl.DoorData;
  40. import com.l2jserver.gameserver.enums.InstanceReenterType;
  41. import com.l2jserver.gameserver.enums.InstanceRemoveBuffType;
  42. import com.l2jserver.gameserver.instancemanager.InstanceManager;
  43. import com.l2jserver.gameserver.model.L2Spawn;
  44. import com.l2jserver.gameserver.model.L2World;
  45. import com.l2jserver.gameserver.model.L2WorldRegion;
  46. import com.l2jserver.gameserver.model.Location;
  47. import com.l2jserver.gameserver.model.StatsSet;
  48. import com.l2jserver.gameserver.model.TeleportWhereType;
  49. import com.l2jserver.gameserver.model.actor.L2Attackable;
  50. import com.l2jserver.gameserver.model.actor.L2Character;
  51. import com.l2jserver.gameserver.model.actor.L2Npc;
  52. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  53. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  54. import com.l2jserver.gameserver.model.actor.templates.L2DoorTemplate;
  55. import com.l2jserver.gameserver.model.holders.InstanceReenterTimeHolder;
  56. import com.l2jserver.gameserver.model.instancezone.InstanceWorld;
  57. import com.l2jserver.gameserver.network.SystemMessageId;
  58. import com.l2jserver.gameserver.network.clientpackets.Say2;
  59. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  60. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  61. import com.l2jserver.gameserver.util.Broadcast;
  62. /**
  63. * Main class for game instances.
  64. * @author evill33t, GodKratos
  65. */
  66. public final class Instance
  67. {
  68. private static final Logger _log = Logger.getLogger(Instance.class.getName());
  69. private final int _id;
  70. private String _name;
  71. private int _ejectTime = Config.EJECT_DEAD_PLAYER_TIME;
  72. /** Allow random walk for NPCs, global parameter. */
  73. private boolean _allowRandomWalk = true;
  74. private final List<Integer> _players = new CopyOnWriteArrayList<>();
  75. private final List<L2Npc> _npcs = new CopyOnWriteArrayList<>();
  76. private final Map<Integer, L2DoorInstance> _doors = new ConcurrentHashMap<>();
  77. private final Map<String, List<L2Spawn>> _manualSpawn = new HashMap<>();
  78. private Location _spawnLoc = null;
  79. private boolean _allowSummon = true;
  80. private long _emptyDestroyTime = -1;
  81. private long _lastLeft = -1;
  82. private long _instanceStartTime = -1;
  83. private long _instanceEndTime = -1;
  84. private boolean _isPvPInstance = false;
  85. private boolean _showTimer = false;
  86. private boolean _isTimerIncrease = true;
  87. private String _timerText = "";
  88. // Instance reset data
  89. private InstanceReenterType _type = InstanceReenterType.NONE;
  90. private final List<InstanceReenterTimeHolder> _resetData = new ArrayList<>();
  91. // Instance remove buffs data
  92. private InstanceRemoveBuffType _removeBuffType = InstanceRemoveBuffType.NONE;
  93. private final List<Integer> _exceptionList = new ArrayList<>();
  94. protected ScheduledFuture<?> _checkTimeUpTask = null;
  95. protected final Map<Integer, ScheduledFuture<?>> _ejectDeadTasks = new ConcurrentHashMap<>();
  96. public Instance(int id)
  97. {
  98. _id = id;
  99. _instanceStartTime = System.currentTimeMillis();
  100. }
  101. public Instance(int id, String name)
  102. {
  103. _id = id;
  104. _name = name;
  105. _instanceStartTime = System.currentTimeMillis();
  106. }
  107. /**
  108. * @return the ID of this instance.
  109. */
  110. public int getId()
  111. {
  112. return _id;
  113. }
  114. /**
  115. * @return the name of this instance
  116. */
  117. public String getName()
  118. {
  119. return _name;
  120. }
  121. public void setName(String name)
  122. {
  123. _name = name;
  124. }
  125. /**
  126. * @return the eject time
  127. */
  128. public int getEjectTime()
  129. {
  130. return _ejectTime;
  131. }
  132. /**
  133. * @param ejectTime the player eject time upon death
  134. */
  135. public void setEjectTime(int ejectTime)
  136. {
  137. _ejectTime = ejectTime;
  138. }
  139. /**
  140. * @return whether summon friend type skills are allowed for this instance
  141. */
  142. public boolean isSummonAllowed()
  143. {
  144. return _allowSummon;
  145. }
  146. /**
  147. * Sets the status for the instance for summon friend type skills
  148. * @param b
  149. */
  150. public void setAllowSummon(boolean b)
  151. {
  152. _allowSummon = b;
  153. }
  154. /**
  155. * Returns true if entire instance is PvP zone
  156. * @return
  157. */
  158. public boolean isPvPInstance()
  159. {
  160. return _isPvPInstance;
  161. }
  162. /**
  163. * Sets PvP zone status of the instance
  164. * @param b
  165. */
  166. public void setPvPInstance(boolean b)
  167. {
  168. _isPvPInstance = b;
  169. }
  170. /**
  171. * Set the instance duration task
  172. * @param duration in milliseconds
  173. */
  174. public void setDuration(int duration)
  175. {
  176. if (_checkTimeUpTask != null)
  177. {
  178. _checkTimeUpTask.cancel(true);
  179. }
  180. _checkTimeUpTask = ThreadPoolManager.getInstance().scheduleGeneral(new CheckTimeUp(duration), 500);
  181. _instanceEndTime = System.currentTimeMillis() + duration + 500;
  182. }
  183. /**
  184. * Set time before empty instance will be removed
  185. * @param time in milliseconds
  186. */
  187. public void setEmptyDestroyTime(long time)
  188. {
  189. _emptyDestroyTime = time;
  190. }
  191. /**
  192. * Checks if the player exists within this instance
  193. * @param objectId
  194. * @return true if player exists in instance
  195. */
  196. public boolean containsPlayer(int objectId)
  197. {
  198. return _players.contains(objectId);
  199. }
  200. /**
  201. * Adds the specified player to the instance
  202. * @param objectId Players object ID
  203. */
  204. public void addPlayer(int objectId)
  205. {
  206. _players.add(objectId);
  207. }
  208. /**
  209. * Removes the specified player from the instance list.
  210. * @param objectId the player's object Id
  211. */
  212. public void removePlayer(Integer objectId)
  213. {
  214. _players.remove(objectId);
  215. if (_players.isEmpty() && (_emptyDestroyTime >= 0))
  216. {
  217. _lastLeft = System.currentTimeMillis();
  218. setDuration((int) (_instanceEndTime - System.currentTimeMillis() - 500));
  219. }
  220. }
  221. public void addNpc(L2Npc npc)
  222. {
  223. _npcs.add(npc);
  224. }
  225. public void removeNpc(L2Npc npc)
  226. {
  227. if (npc.getSpawn() != null)
  228. {
  229. npc.getSpawn().stopRespawn();
  230. }
  231. _npcs.remove(npc);
  232. }
  233. /**
  234. * Adds a door into the instance
  235. * @param doorId - from doors.xml
  236. * @param set - StatsSet for initializing door
  237. */
  238. public void addDoor(int doorId, StatsSet set)
  239. {
  240. if (_doors.containsKey(doorId))
  241. {
  242. _log.warning("Door ID " + doorId + " already exists in instance " + getId());
  243. return;
  244. }
  245. final L2DoorInstance newdoor = new L2DoorInstance(new L2DoorTemplate(set));
  246. newdoor.setInstanceId(getId());
  247. newdoor.setCurrentHp(newdoor.getMaxHp());
  248. newdoor.spawnMe(newdoor.getTemplate().getX(), newdoor.getTemplate().getY(), newdoor.getTemplate().getZ());
  249. _doors.put(doorId, newdoor);
  250. }
  251. public List<Integer> getPlayers()
  252. {
  253. return _players;
  254. }
  255. public List<L2Npc> getNpcs()
  256. {
  257. return _npcs;
  258. }
  259. public Collection<L2DoorInstance> getDoors()
  260. {
  261. return _doors.values();
  262. }
  263. public L2DoorInstance getDoor(int id)
  264. {
  265. return _doors.get(id);
  266. }
  267. public long getInstanceEndTime()
  268. {
  269. return _instanceEndTime;
  270. }
  271. public long getInstanceStartTime()
  272. {
  273. return _instanceStartTime;
  274. }
  275. public boolean isShowTimer()
  276. {
  277. return _showTimer;
  278. }
  279. public boolean isTimerIncrease()
  280. {
  281. return _isTimerIncrease;
  282. }
  283. public String getTimerText()
  284. {
  285. return _timerText;
  286. }
  287. /**
  288. * @return the spawn location for this instance to be used when leaving the instance
  289. */
  290. public Location getSpawnLoc()
  291. {
  292. return _spawnLoc;
  293. }
  294. /**
  295. * Sets the spawn location for this instance to be used when leaving the instance
  296. * @param loc
  297. */
  298. public void setSpawnLoc(Location loc)
  299. {
  300. _spawnLoc = loc;
  301. }
  302. public void removePlayers()
  303. {
  304. for (Integer objectId : _players)
  305. {
  306. final L2PcInstance player = L2World.getInstance().getPlayer(objectId);
  307. if ((player != null) && (player.getInstanceId() == getId()))
  308. {
  309. player.setInstanceId(0);
  310. if (getSpawnLoc() != null)
  311. {
  312. player.teleToLocation(getSpawnLoc(), true);
  313. }
  314. else
  315. {
  316. player.teleToLocation(TeleportWhereType.TOWN);
  317. }
  318. }
  319. }
  320. _players.clear();
  321. }
  322. public void removeNpcs()
  323. {
  324. for (L2Npc mob : _npcs)
  325. {
  326. if (mob != null)
  327. {
  328. if (mob.getSpawn() != null)
  329. {
  330. mob.getSpawn().stopRespawn();
  331. }
  332. mob.deleteMe();
  333. }
  334. }
  335. _npcs.clear();
  336. _manualSpawn.clear();
  337. }
  338. public void removeDoors()
  339. {
  340. for (L2DoorInstance door : _doors.values())
  341. {
  342. if (door != null)
  343. {
  344. L2WorldRegion region = door.getWorldRegion();
  345. door.decayMe();
  346. if (region != null)
  347. {
  348. region.removeVisibleObject(door);
  349. }
  350. door.getKnownList().removeAllKnownObjects();
  351. L2World.getInstance().removeObject(door);
  352. }
  353. }
  354. _doors.clear();
  355. }
  356. /**
  357. * Spawns group of instance NPC's
  358. * @param groupName - name of group from XML definition to spawn
  359. * @return list of spawned NPC's
  360. */
  361. public List<L2Npc> spawnGroup(String groupName)
  362. {
  363. List<L2Npc> ret = null;
  364. if (_manualSpawn.containsKey(groupName))
  365. {
  366. final List<L2Spawn> manualSpawn = _manualSpawn.get(groupName);
  367. ret = new ArrayList<>(manualSpawn.size());
  368. for (L2Spawn spawnDat : manualSpawn)
  369. {
  370. ret.add(spawnDat.doSpawn());
  371. }
  372. }
  373. else
  374. {
  375. _log.warning(getName() + " instance: cannot spawn NPC's, wrong group name: " + groupName);
  376. }
  377. return ret;
  378. }
  379. public void loadInstanceTemplate(String filename)
  380. {
  381. Document doc = null;
  382. File xml = new File(Config.DATAPACK_ROOT, "data/instances/" + filename);
  383. try
  384. {
  385. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  386. factory.setValidating(false);
  387. factory.setIgnoringComments(true);
  388. doc = factory.newDocumentBuilder().parse(xml);
  389. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  390. {
  391. if ("instance".equalsIgnoreCase(n.getNodeName()))
  392. {
  393. parseInstance(n);
  394. }
  395. }
  396. }
  397. catch (IOException e)
  398. {
  399. _log.log(Level.WARNING, "Instance: can not find " + xml.getAbsolutePath() + " ! " + e.getMessage(), e);
  400. }
  401. catch (Exception e)
  402. {
  403. _log.log(Level.WARNING, "Instance: error while loading " + xml.getAbsolutePath() + " ! " + e.getMessage(), e);
  404. }
  405. }
  406. private void parseInstance(Node n) throws Exception
  407. {
  408. _name = n.getAttributes().getNamedItem("name").getNodeValue();
  409. Node a = n.getAttributes().getNamedItem("ejectTime");
  410. if (a != null)
  411. {
  412. _ejectTime = 1000 * Integer.parseInt(a.getNodeValue());
  413. }
  414. a = n.getAttributes().getNamedItem("allowRandomWalk");
  415. if (a != null)
  416. {
  417. _allowRandomWalk = Boolean.parseBoolean(a.getNodeValue());
  418. }
  419. Node first = n.getFirstChild();
  420. for (n = first; n != null; n = n.getNextSibling())
  421. {
  422. if ("activityTime".equalsIgnoreCase(n.getNodeName()))
  423. {
  424. a = n.getAttributes().getNamedItem("val");
  425. if (a != null)
  426. {
  427. _checkTimeUpTask = ThreadPoolManager.getInstance().scheduleGeneral(new CheckTimeUp(Integer.parseInt(a.getNodeValue()) * 60000), 15000);
  428. _instanceEndTime = System.currentTimeMillis() + (Long.parseLong(a.getNodeValue()) * 60000) + 15000;
  429. }
  430. }
  431. // @formatter:off
  432. /*
  433. else if ("timeDelay".equalsIgnoreCase(n.getNodeName()))
  434. {
  435. a = n.getAttributes().getNamedItem("val");
  436. if (a != null)
  437. {
  438. instance.setTimeDelay(Integer.parseInt(a.getNodeValue()));
  439. }
  440. }
  441. */
  442. // @formatter:on
  443. else if ("allowSummon".equalsIgnoreCase(n.getNodeName()))
  444. {
  445. a = n.getAttributes().getNamedItem("val");
  446. if (a != null)
  447. {
  448. setAllowSummon(Boolean.parseBoolean(a.getNodeValue()));
  449. }
  450. }
  451. else if ("emptyDestroyTime".equalsIgnoreCase(n.getNodeName()))
  452. {
  453. a = n.getAttributes().getNamedItem("val");
  454. if (a != null)
  455. {
  456. _emptyDestroyTime = Long.parseLong(a.getNodeValue()) * 1000;
  457. }
  458. }
  459. else if ("showTimer".equalsIgnoreCase(n.getNodeName()))
  460. {
  461. a = n.getAttributes().getNamedItem("val");
  462. if (a != null)
  463. {
  464. _showTimer = Boolean.parseBoolean(a.getNodeValue());
  465. }
  466. a = n.getAttributes().getNamedItem("increase");
  467. if (a != null)
  468. {
  469. _isTimerIncrease = Boolean.parseBoolean(a.getNodeValue());
  470. }
  471. a = n.getAttributes().getNamedItem("text");
  472. if (a != null)
  473. {
  474. _timerText = a.getNodeValue();
  475. }
  476. }
  477. else if ("PvPInstance".equalsIgnoreCase(n.getNodeName()))
  478. {
  479. a = n.getAttributes().getNamedItem("val");
  480. if (a != null)
  481. {
  482. setPvPInstance(Boolean.parseBoolean(a.getNodeValue()));
  483. }
  484. }
  485. else if ("doorlist".equalsIgnoreCase(n.getNodeName()))
  486. {
  487. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  488. {
  489. int doorId = 0;
  490. if ("door".equalsIgnoreCase(d.getNodeName()))
  491. {
  492. doorId = Integer.parseInt(d.getAttributes().getNamedItem("doorId").getNodeValue());
  493. StatsSet set = new StatsSet();
  494. set.add(DoorData.getInstance().getDoorTemplate(doorId));
  495. for (Node bean = d.getFirstChild(); bean != null; bean = bean.getNextSibling())
  496. {
  497. if ("set".equalsIgnoreCase(bean.getNodeName()))
  498. {
  499. NamedNodeMap attrs = bean.getAttributes();
  500. String setname = attrs.getNamedItem("name").getNodeValue();
  501. String value = attrs.getNamedItem("val").getNodeValue();
  502. set.set(setname, value);
  503. }
  504. }
  505. addDoor(doorId, set);
  506. }
  507. }
  508. }
  509. else if ("spawnlist".equalsIgnoreCase(n.getNodeName()))
  510. {
  511. for (Node group = n.getFirstChild(); group != null; group = group.getNextSibling())
  512. {
  513. if ("group".equalsIgnoreCase(group.getNodeName()))
  514. {
  515. String spawnGroup = group.getAttributes().getNamedItem("name").getNodeValue();
  516. List<L2Spawn> manualSpawn = new ArrayList<>();
  517. for (Node d = group.getFirstChild(); d != null; d = d.getNextSibling())
  518. {
  519. int npcId = 0, x = 0, y = 0, z = 0, heading = 0, respawn = 0, respawnRandom = 0, delay = -1;
  520. Boolean allowRandomWalk = null;
  521. if ("spawn".equalsIgnoreCase(d.getNodeName()))
  522. {
  523. npcId = Integer.parseInt(d.getAttributes().getNamedItem("npcId").getNodeValue());
  524. x = Integer.parseInt(d.getAttributes().getNamedItem("x").getNodeValue());
  525. y = Integer.parseInt(d.getAttributes().getNamedItem("y").getNodeValue());
  526. z = Integer.parseInt(d.getAttributes().getNamedItem("z").getNodeValue());
  527. heading = Integer.parseInt(d.getAttributes().getNamedItem("heading").getNodeValue());
  528. respawn = Integer.parseInt(d.getAttributes().getNamedItem("respawn").getNodeValue());
  529. if (d.getAttributes().getNamedItem("onKillDelay") != null)
  530. {
  531. delay = Integer.parseInt(d.getAttributes().getNamedItem("onKillDelay").getNodeValue());
  532. }
  533. if (d.getAttributes().getNamedItem("respawnRandom") != null)
  534. {
  535. respawnRandom = Integer.parseInt(d.getAttributes().getNamedItem("respawnRandom").getNodeValue());
  536. }
  537. if (d.getAttributes().getNamedItem("allowRandomWalk") != null)
  538. {
  539. allowRandomWalk = Boolean.valueOf(d.getAttributes().getNamedItem("allowRandomWalk").getNodeValue());
  540. }
  541. final L2Spawn spawnDat = new L2Spawn(npcId);
  542. spawnDat.setX(x);
  543. spawnDat.setY(y);
  544. spawnDat.setZ(z);
  545. spawnDat.setAmount(1);
  546. spawnDat.setHeading(heading);
  547. spawnDat.setRespawnDelay(respawn, respawnRandom);
  548. if (respawn == 0)
  549. {
  550. spawnDat.stopRespawn();
  551. }
  552. else
  553. {
  554. spawnDat.startRespawn();
  555. }
  556. spawnDat.setInstanceId(getId());
  557. if (allowRandomWalk == null)
  558. {
  559. spawnDat.setIsNoRndWalk(!_allowRandomWalk);
  560. }
  561. else
  562. {
  563. spawnDat.setIsNoRndWalk(!allowRandomWalk);
  564. }
  565. if (spawnGroup.equals("general"))
  566. {
  567. L2Npc spawned = spawnDat.doSpawn();
  568. if ((delay >= 0) && (spawned instanceof L2Attackable))
  569. {
  570. ((L2Attackable) spawned).setOnKillDelay(delay);
  571. }
  572. }
  573. else
  574. {
  575. manualSpawn.add(spawnDat);
  576. }
  577. }
  578. }
  579. if (!manualSpawn.isEmpty())
  580. {
  581. _manualSpawn.put(spawnGroup, manualSpawn);
  582. }
  583. }
  584. }
  585. }
  586. else if ("spawnpoint".equalsIgnoreCase(n.getNodeName()))
  587. {
  588. try
  589. {
  590. int x = Integer.parseInt(n.getAttributes().getNamedItem("spawnX").getNodeValue());
  591. int y = Integer.parseInt(n.getAttributes().getNamedItem("spawnY").getNodeValue());
  592. int z = Integer.parseInt(n.getAttributes().getNamedItem("spawnZ").getNodeValue());
  593. _spawnLoc = new Location(x, y, z);
  594. }
  595. catch (Exception e)
  596. {
  597. _log.log(Level.WARNING, "Error parsing instance xml: " + e.getMessage(), e);
  598. _spawnLoc = null;
  599. }
  600. }
  601. else if ("reenter".equalsIgnoreCase(n.getNodeName()))
  602. {
  603. a = n.getAttributes().getNamedItem("additionStyle");
  604. if (a != null)
  605. {
  606. _type = InstanceReenterType.valueOf(a.getNodeValue());
  607. }
  608. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  609. {
  610. long time = -1;
  611. DayOfWeek day = null;
  612. int hour = -1;
  613. int minute = -1;
  614. if ("reset".equalsIgnoreCase(d.getNodeName()))
  615. {
  616. a = d.getAttributes().getNamedItem("time");
  617. if (a != null)
  618. {
  619. time = Long.parseLong(a.getNodeValue());
  620. if (time > 0)
  621. {
  622. _resetData.add(new InstanceReenterTimeHolder(time));
  623. break;
  624. }
  625. }
  626. else if (time == -1)
  627. {
  628. a = d.getAttributes().getNamedItem("day");
  629. if (a != null)
  630. {
  631. day = DayOfWeek.valueOf(a.getNodeValue().toUpperCase());
  632. }
  633. a = d.getAttributes().getNamedItem("hour");
  634. if (a != null)
  635. {
  636. hour = Integer.parseInt(a.getNodeValue());
  637. }
  638. a = d.getAttributes().getNamedItem("minute");
  639. if (a != null)
  640. {
  641. minute = Integer.parseInt(a.getNodeValue());
  642. }
  643. _resetData.add(new InstanceReenterTimeHolder(day, hour, minute));
  644. }
  645. }
  646. }
  647. }
  648. else if ("removeBuffs".equalsIgnoreCase(n.getNodeName()))
  649. {
  650. a = n.getAttributes().getNamedItem("type");
  651. if (a != null)
  652. {
  653. _removeBuffType = InstanceRemoveBuffType.valueOf(a.getNodeValue().toUpperCase());
  654. }
  655. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  656. {
  657. if ("skill".equalsIgnoreCase(d.getNodeName()))
  658. {
  659. a = d.getAttributes().getNamedItem("id");
  660. if (a != null)
  661. {
  662. _exceptionList.add(Integer.parseInt(a.getNodeValue()));
  663. }
  664. }
  665. }
  666. }
  667. }
  668. }
  669. protected void doCheckTimeUp(int remaining)
  670. {
  671. CreatureSay cs = null;
  672. int timeLeft;
  673. int interval;
  674. if (_players.isEmpty() && (_emptyDestroyTime == 0))
  675. {
  676. remaining = 0;
  677. interval = 500;
  678. }
  679. else if (_players.isEmpty() && (_emptyDestroyTime > 0))
  680. {
  681. Long emptyTimeLeft = (_lastLeft + _emptyDestroyTime) - System.currentTimeMillis();
  682. if (emptyTimeLeft <= 0)
  683. {
  684. interval = 0;
  685. remaining = 0;
  686. }
  687. else if ((remaining > 300000) && (emptyTimeLeft > 300000))
  688. {
  689. interval = 300000;
  690. remaining = remaining - 300000;
  691. }
  692. else if ((remaining > 60000) && (emptyTimeLeft > 60000))
  693. {
  694. interval = 60000;
  695. remaining = remaining - 60000;
  696. }
  697. else if ((remaining > 30000) && (emptyTimeLeft > 30000))
  698. {
  699. interval = 30000;
  700. remaining = remaining - 30000;
  701. }
  702. else
  703. {
  704. interval = 10000;
  705. remaining = remaining - 10000;
  706. }
  707. }
  708. else if (remaining > 300000)
  709. {
  710. timeLeft = remaining / 60000;
  711. interval = 300000;
  712. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.DUNGEON_EXPIRES_IN_S1_MINUTES);
  713. sm.addString(Integer.toString(timeLeft));
  714. Broadcast.toPlayersInInstance(sm, getId());
  715. remaining = remaining - 300000;
  716. }
  717. else if (remaining > 60000)
  718. {
  719. timeLeft = remaining / 60000;
  720. interval = 60000;
  721. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.DUNGEON_EXPIRES_IN_S1_MINUTES);
  722. sm.addString(Integer.toString(timeLeft));
  723. Broadcast.toPlayersInInstance(sm, getId());
  724. remaining = remaining - 60000;
  725. }
  726. else if (remaining > 30000)
  727. {
  728. timeLeft = remaining / 1000;
  729. interval = 30000;
  730. cs = new CreatureSay(0, Say2.ALLIANCE, "Notice", timeLeft + " seconds left.");
  731. remaining = remaining - 30000;
  732. }
  733. else
  734. {
  735. timeLeft = remaining / 1000;
  736. interval = 10000;
  737. cs = new CreatureSay(0, Say2.ALLIANCE, "Notice", timeLeft + " seconds left.");
  738. remaining = remaining - 10000;
  739. }
  740. if (cs != null)
  741. {
  742. for (Integer objectId : _players)
  743. {
  744. final L2PcInstance player = L2World.getInstance().getPlayer(objectId);
  745. if ((player != null) && (player.getInstanceId() == getId()))
  746. {
  747. player.sendPacket(cs);
  748. }
  749. }
  750. }
  751. cancelTimer();
  752. if (remaining >= 10000)
  753. {
  754. _checkTimeUpTask = ThreadPoolManager.getInstance().scheduleGeneral(new CheckTimeUp(remaining), interval);
  755. }
  756. else
  757. {
  758. _checkTimeUpTask = ThreadPoolManager.getInstance().scheduleGeneral(new TimeUp(), interval);
  759. }
  760. }
  761. public void cancelTimer()
  762. {
  763. if (_checkTimeUpTask != null)
  764. {
  765. _checkTimeUpTask.cancel(true);
  766. }
  767. }
  768. public void cancelEjectDeadPlayer(L2PcInstance player)
  769. {
  770. final ScheduledFuture<?> task = _ejectDeadTasks.remove(player.getObjectId());
  771. if (task != null)
  772. {
  773. task.cancel(true);
  774. }
  775. }
  776. public void addEjectDeadTask(L2PcInstance player)
  777. {
  778. if ((player != null))
  779. {
  780. _ejectDeadTasks.put(player.getObjectId(), ThreadPoolManager.getInstance().scheduleGeneral(() ->
  781. {
  782. if (player.isDead() && (player.getInstanceId() == getId()))
  783. {
  784. player.setInstanceId(0);
  785. if (getSpawnLoc() != null)
  786. {
  787. player.teleToLocation(getSpawnLoc(), true);
  788. }
  789. else
  790. {
  791. player.teleToLocation(TeleportWhereType.TOWN);
  792. }
  793. }
  794. }, _ejectTime));
  795. }
  796. }
  797. /**
  798. * @param killer the character that killed the {@code victim}
  799. * @param victim the character that was killed by the {@code killer}
  800. */
  801. public final void notifyDeath(L2Character killer, L2Character victim)
  802. {
  803. final InstanceWorld instance = InstanceManager.getInstance().getPlayerWorld(victim.getActingPlayer());
  804. if (instance != null)
  805. {
  806. instance.onDeath(killer, victim);
  807. }
  808. }
  809. public class CheckTimeUp implements Runnable
  810. {
  811. private final int _remaining;
  812. public CheckTimeUp(int remaining)
  813. {
  814. _remaining = remaining;
  815. }
  816. @Override
  817. public void run()
  818. {
  819. doCheckTimeUp(_remaining);
  820. }
  821. }
  822. public class TimeUp implements Runnable
  823. {
  824. @Override
  825. public void run()
  826. {
  827. InstanceManager.getInstance().destroyInstance(getId());
  828. }
  829. }
  830. public InstanceReenterType getReenterType()
  831. {
  832. return _type;
  833. }
  834. public void setReenterType(InstanceReenterType type)
  835. {
  836. _type = type;
  837. }
  838. public List<InstanceReenterTimeHolder> getReenterData()
  839. {
  840. return _resetData;
  841. }
  842. public boolean isRemoveBuffEnabled()
  843. {
  844. return getRemoveBuffType() != InstanceRemoveBuffType.NONE;
  845. }
  846. public InstanceRemoveBuffType getRemoveBuffType()
  847. {
  848. return _removeBuffType;
  849. }
  850. public List<Integer> getBuffExceptionList()
  851. {
  852. return _exceptionList;
  853. }
  854. }