Instance.java 22 KB

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