Instance.java 24 KB

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