Instance.java 22 KB

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