Instance.java 23 KB

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