ZoneManager.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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.instancemanager;
  20. import java.io.File;
  21. import java.lang.reflect.Constructor;
  22. import java.util.ArrayList;
  23. import java.util.Collection;
  24. import java.util.HashMap;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.logging.Level;
  29. import org.w3c.dom.Document;
  30. import org.w3c.dom.NamedNodeMap;
  31. import org.w3c.dom.Node;
  32. import com.l2jserver.gameserver.model.L2Object;
  33. import com.l2jserver.gameserver.model.L2World;
  34. import com.l2jserver.gameserver.model.L2WorldRegion;
  35. import com.l2jserver.gameserver.model.actor.L2Character;
  36. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  37. import com.l2jserver.gameserver.model.zone.AbstractZoneSettings;
  38. import com.l2jserver.gameserver.model.zone.L2ZoneForm;
  39. import com.l2jserver.gameserver.model.zone.L2ZoneRespawn;
  40. import com.l2jserver.gameserver.model.zone.L2ZoneType;
  41. import com.l2jserver.gameserver.model.zone.form.ZoneCuboid;
  42. import com.l2jserver.gameserver.model.zone.form.ZoneCylinder;
  43. import com.l2jserver.gameserver.model.zone.form.ZoneNPoly;
  44. import com.l2jserver.gameserver.model.zone.type.L2ArenaZone;
  45. import com.l2jserver.gameserver.model.zone.type.L2OlympiadStadiumZone;
  46. import com.l2jserver.gameserver.model.zone.type.L2RespawnZone;
  47. import com.l2jserver.gameserver.model.zone.type.NpcSpawnTerritory;
  48. import com.l2jserver.util.data.xml.IXmlReader;
  49. /**
  50. * This class manages the zones
  51. * @author durgus
  52. */
  53. public final class ZoneManager implements IXmlReader
  54. {
  55. private static final Map<String, AbstractZoneSettings> _settings = new HashMap<>();
  56. private final Map<Class<? extends L2ZoneType>, Map<Integer, ? extends L2ZoneType>> _classZones = new HashMap<>();
  57. private final Map<String, NpcSpawnTerritory> _spawnTerritories = new HashMap<>();
  58. private int _lastDynamicId = 300000;
  59. private List<L2ItemInstance> _debugItems;
  60. /**
  61. * Instantiates a new zone manager.
  62. */
  63. protected ZoneManager()
  64. {
  65. load();
  66. }
  67. /**
  68. * Reload.
  69. */
  70. public void reload()
  71. {
  72. // Get the world regions
  73. int count = 0;
  74. final L2WorldRegion[][] worldRegions = L2World.getInstance().getWorldRegions();
  75. // Backup old zone settings
  76. for (Map<Integer, ? extends L2ZoneType> map : _classZones.values())
  77. {
  78. for (L2ZoneType zone : map.values())
  79. {
  80. if (zone.getSettings() != null)
  81. {
  82. _settings.put(zone.getName(), zone.getSettings());
  83. }
  84. }
  85. }
  86. // Clear zones
  87. for (L2WorldRegion[] worldRegion : worldRegions)
  88. {
  89. for (L2WorldRegion element : worldRegion)
  90. {
  91. element.getZones().clear();
  92. count++;
  93. }
  94. }
  95. GrandBossManager.getInstance().getZones().clear();
  96. LOGGER.info(getClass().getSimpleName() + ": Removed zones in " + count + " regions.");
  97. // Load the zones
  98. load();
  99. // Re-validate all characters in zones
  100. for (L2Object obj : L2World.getInstance().getVisibleObjects())
  101. {
  102. if (obj instanceof L2Character)
  103. {
  104. ((L2Character) obj).revalidateZone(true);
  105. }
  106. }
  107. _settings.clear();
  108. }
  109. @Override
  110. public void parseDocument(Document doc, File f)
  111. {
  112. // Get the world regions
  113. final L2WorldRegion[][] worldRegions = L2World.getInstance().getWorldRegions();
  114. NamedNodeMap attrs;
  115. Node attribute;
  116. String zoneName;
  117. int[][] coords;
  118. int zoneId, minZ, maxZ;
  119. String zoneType, zoneShape;
  120. final List<int[]> rs = new ArrayList<>();
  121. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  122. {
  123. if ("list".equalsIgnoreCase(n.getNodeName()))
  124. {
  125. attrs = n.getAttributes();
  126. attribute = attrs.getNamedItem("enabled");
  127. if ((attribute != null) && !Boolean.parseBoolean(attribute.getNodeValue()))
  128. {
  129. continue;
  130. }
  131. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  132. {
  133. if ("zone".equalsIgnoreCase(d.getNodeName()))
  134. {
  135. attrs = d.getAttributes();
  136. attribute = attrs.getNamedItem("type");
  137. if (attribute != null)
  138. {
  139. zoneType = attribute.getNodeValue();
  140. }
  141. else
  142. {
  143. LOGGER.warning("ZoneData: Missing type for zone in file: " + f.getName());
  144. continue;
  145. }
  146. attribute = attrs.getNamedItem("id");
  147. if (attribute != null)
  148. {
  149. zoneId = Integer.parseInt(attribute.getNodeValue());
  150. }
  151. else
  152. {
  153. zoneId = zoneType.equalsIgnoreCase("NpcSpawnTerritory") ? 0 : _lastDynamicId++;
  154. }
  155. attribute = attrs.getNamedItem("name");
  156. if (attribute != null)
  157. {
  158. zoneName = attribute.getNodeValue();
  159. }
  160. else
  161. {
  162. zoneName = null;
  163. }
  164. // Check zone name for NpcSpawnTerritory. Must exist and to be unique
  165. if (zoneType.equalsIgnoreCase("NpcSpawnTerritory"))
  166. {
  167. if (zoneName == null)
  168. {
  169. LOGGER.warning("ZoneData: Missing name for NpcSpawnTerritory in file: " + f.getName() + ", skipping zone");
  170. continue;
  171. }
  172. else if (_spawnTerritories.containsKey(zoneName))
  173. {
  174. LOGGER.warning("ZoneData: Name " + zoneName + " already used for another zone, check file: " + f.getName() + ". Skipping zone");
  175. continue;
  176. }
  177. }
  178. minZ = parseInteger(attrs, "minZ");
  179. maxZ = parseInteger(attrs, "maxZ");
  180. zoneType = parseString(attrs, "type");
  181. zoneShape = parseString(attrs, "shape");
  182. // Get the zone shape from xml
  183. L2ZoneForm zoneForm = null;
  184. try
  185. {
  186. for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  187. {
  188. if ("node".equalsIgnoreCase(cd.getNodeName()))
  189. {
  190. attrs = cd.getAttributes();
  191. int[] point = new int[2];
  192. point[0] = parseInteger(attrs, "X");
  193. point[1] = parseInteger(attrs, "Y");
  194. rs.add(point);
  195. }
  196. }
  197. coords = rs.toArray(new int[rs.size()][2]);
  198. rs.clear();
  199. if ((coords == null) || (coords.length == 0))
  200. {
  201. LOGGER.warning(getClass().getSimpleName() + ": ZoneData: missing data for zone: " + zoneId + " XML file: " + f.getName());
  202. continue;
  203. }
  204. // Create this zone. Parsing for cuboids is a
  205. // bit different than for other polygons
  206. // cuboids need exactly 2 points to be defined.
  207. // Other polygons need at least 3 (one per
  208. // vertex)
  209. if (zoneShape.equalsIgnoreCase("Cuboid"))
  210. {
  211. if (coords.length == 2)
  212. {
  213. zoneForm = new ZoneCuboid(coords[0][0], coords[1][0], coords[0][1], coords[1][1], minZ, maxZ);
  214. }
  215. else
  216. {
  217. LOGGER.warning(getClass().getSimpleName() + ": ZoneData: Missing cuboid vertex in sql data for zone: " + zoneId + " in file: " + f.getName());
  218. continue;
  219. }
  220. }
  221. else if (zoneShape.equalsIgnoreCase("NPoly"))
  222. {
  223. // nPoly needs to have at least 3 vertices
  224. if (coords.length > 2)
  225. {
  226. final int[] aX = new int[coords.length];
  227. final int[] aY = new int[coords.length];
  228. for (int i = 0; i < coords.length; i++)
  229. {
  230. aX[i] = coords[i][0];
  231. aY[i] = coords[i][1];
  232. }
  233. zoneForm = new ZoneNPoly(aX, aY, minZ, maxZ);
  234. }
  235. else
  236. {
  237. LOGGER.warning(getClass().getSimpleName() + ": ZoneData: Bad data for zone: " + zoneId + " in file: " + f.getName());
  238. continue;
  239. }
  240. }
  241. else if (zoneShape.equalsIgnoreCase("Cylinder"))
  242. {
  243. // A Cylinder zone requires a center point
  244. // at x,y and a radius
  245. attrs = d.getAttributes();
  246. final int zoneRad = Integer.parseInt(attrs.getNamedItem("rad").getNodeValue());
  247. if ((coords.length == 1) && (zoneRad > 0))
  248. {
  249. zoneForm = new ZoneCylinder(coords[0][0], coords[0][1], minZ, maxZ, zoneRad);
  250. }
  251. else
  252. {
  253. LOGGER.warning(getClass().getSimpleName() + ": ZoneData: Bad data for zone: " + zoneId + " in file: " + f.getName());
  254. continue;
  255. }
  256. }
  257. else
  258. {
  259. LOGGER.warning(getClass().getSimpleName() + ": ZoneData: Unknown shape: \"" + zoneShape + "\" for zone: " + zoneId + " in file: " + f.getName());
  260. continue;
  261. }
  262. }
  263. catch (Exception e)
  264. {
  265. LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": ZoneData: Failed to load zone " + zoneId + " coordinates: " + e.getMessage(), e);
  266. }
  267. // No further parameters needed, if NpcSpawnTerritory is loading
  268. if (zoneType.equalsIgnoreCase("NpcSpawnTerritory"))
  269. {
  270. _spawnTerritories.put(zoneName, new NpcSpawnTerritory(zoneName, zoneForm));
  271. continue;
  272. }
  273. // Create the zone
  274. Class<?> newZone = null;
  275. Constructor<?> zoneConstructor = null;
  276. L2ZoneType temp;
  277. try
  278. {
  279. newZone = Class.forName("com.l2jserver.gameserver.model.zone.type.L2" + zoneType);
  280. zoneConstructor = newZone.getConstructor(int.class);
  281. temp = (L2ZoneType) zoneConstructor.newInstance(zoneId);
  282. temp.setZone(zoneForm);
  283. }
  284. catch (Exception e)
  285. {
  286. LOGGER.warning(getClass().getSimpleName() + ": ZoneData: No such zone type: " + zoneType + " in file: " + f.getName());
  287. continue;
  288. }
  289. // Check for additional parameters
  290. for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  291. {
  292. if ("stat".equalsIgnoreCase(cd.getNodeName()))
  293. {
  294. attrs = cd.getAttributes();
  295. String name = attrs.getNamedItem("name").getNodeValue();
  296. String val = attrs.getNamedItem("val").getNodeValue();
  297. temp.setParameter(name, val);
  298. }
  299. else if ("spawn".equalsIgnoreCase(cd.getNodeName()) && (temp instanceof L2ZoneRespawn))
  300. {
  301. attrs = cd.getAttributes();
  302. int spawnX = Integer.parseInt(attrs.getNamedItem("X").getNodeValue());
  303. int spawnY = Integer.parseInt(attrs.getNamedItem("Y").getNodeValue());
  304. int spawnZ = Integer.parseInt(attrs.getNamedItem("Z").getNodeValue());
  305. Node val = attrs.getNamedItem("type");
  306. ((L2ZoneRespawn) temp).parseLoc(spawnX, spawnY, spawnZ, val == null ? null : val.getNodeValue());
  307. }
  308. else if ("race".equalsIgnoreCase(cd.getNodeName()) && (temp instanceof L2RespawnZone))
  309. {
  310. attrs = cd.getAttributes();
  311. String race = attrs.getNamedItem("name").getNodeValue();
  312. String point = attrs.getNamedItem("point").getNodeValue();
  313. ((L2RespawnZone) temp).addRaceRespawnPoint(race, point);
  314. }
  315. }
  316. if (checkId(zoneId))
  317. {
  318. LOGGER.config(getClass().getSimpleName() + ": Caution: Zone (" + zoneId + ") from file: " + f.getName() + " overrides previos definition.");
  319. }
  320. if ((zoneName != null) && !zoneName.isEmpty())
  321. {
  322. temp.setName(zoneName);
  323. }
  324. addZone(zoneId, temp);
  325. // Register the zone into any world region it
  326. // intersects with...
  327. // currently 11136 test for each zone :>
  328. int ax, ay, bx, by;
  329. for (int x = 0; x < worldRegions.length; x++)
  330. {
  331. for (int y = 0; y < worldRegions[x].length; y++)
  332. {
  333. ax = (x - L2World.OFFSET_X) << L2World.SHIFT_BY;
  334. bx = ((x + 1) - L2World.OFFSET_X) << L2World.SHIFT_BY;
  335. ay = (y - L2World.OFFSET_Y) << L2World.SHIFT_BY;
  336. by = ((y + 1) - L2World.OFFSET_Y) << L2World.SHIFT_BY;
  337. if (temp.getZone().intersectsRectangle(ax, bx, ay, by))
  338. {
  339. worldRegions[x][y].addZone(temp);
  340. }
  341. }
  342. }
  343. }
  344. }
  345. }
  346. }
  347. }
  348. @Override
  349. public final void load()
  350. {
  351. _classZones.clear();
  352. _spawnTerritories.clear();
  353. parseDatapackDirectory("data/zones", false);
  354. parseDatapackDirectory("data/zones/npcSpawnTerritories", false);
  355. LOGGER.info(getClass().getSimpleName() + ": Loaded " + _classZones.size() + " zone classes and " + getSize() + " zones.");
  356. LOGGER.info(getClass().getSimpleName() + ": Loaded " + _spawnTerritories.size() + " NPC spawn territoriers.");
  357. }
  358. /**
  359. * Gets the size.
  360. * @return the size
  361. */
  362. public int getSize()
  363. {
  364. int i = 0;
  365. for (Map<Integer, ? extends L2ZoneType> map : _classZones.values())
  366. {
  367. i += map.size();
  368. }
  369. return i;
  370. }
  371. /**
  372. * Check id.
  373. * @param id the id
  374. * @return true, if successful
  375. */
  376. public boolean checkId(int id)
  377. {
  378. for (Map<Integer, ? extends L2ZoneType> map : _classZones.values())
  379. {
  380. if (map.containsKey(id))
  381. {
  382. return true;
  383. }
  384. }
  385. return false;
  386. }
  387. /**
  388. * Add new zone.
  389. * @param <T> the generic type
  390. * @param id the id
  391. * @param zone the zone
  392. */
  393. @SuppressWarnings("unchecked")
  394. public <T extends L2ZoneType> void addZone(Integer id, T zone)
  395. {
  396. Map<Integer, T> map = (Map<Integer, T>) _classZones.get(zone.getClass());
  397. if (map == null)
  398. {
  399. map = new HashMap<>();
  400. map.put(id, zone);
  401. _classZones.put(zone.getClass(), map);
  402. }
  403. else
  404. {
  405. map.put(id, zone);
  406. }
  407. }
  408. /**
  409. * Returns all zones registered with the ZoneManager. To minimize iteration processing retrieve zones from L2WorldRegion for a specific location instead.
  410. * @return zones
  411. * @see #getAllZones(Class)
  412. */
  413. @Deprecated
  414. public Collection<L2ZoneType> getAllZones()
  415. {
  416. final List<L2ZoneType> zones = new ArrayList<>();
  417. for (Map<Integer, ? extends L2ZoneType> map : _classZones.values())
  418. {
  419. zones.addAll(map.values());
  420. }
  421. return zones;
  422. }
  423. /**
  424. * Return all zones by class type.
  425. * @param <T> the generic type
  426. * @param zoneType Zone class
  427. * @return Collection of zones
  428. */
  429. @SuppressWarnings("unchecked")
  430. public <T extends L2ZoneType> Collection<T> getAllZones(Class<T> zoneType)
  431. {
  432. return (Collection<T>) _classZones.get(zoneType).values();
  433. }
  434. /**
  435. * Get zone by ID.
  436. * @param id the id
  437. * @return the zone by id
  438. * @see #getZoneById(int, Class)
  439. */
  440. public L2ZoneType getZoneById(int id)
  441. {
  442. for (Map<Integer, ? extends L2ZoneType> map : _classZones.values())
  443. {
  444. if (map.containsKey(id))
  445. {
  446. return map.get(id);
  447. }
  448. }
  449. return null;
  450. }
  451. /**
  452. * Get zone by ID and zone class.
  453. * @param <T> the generic type
  454. * @param id the id
  455. * @param zoneType the zone type
  456. * @return zone
  457. */
  458. @SuppressWarnings("unchecked")
  459. public <T extends L2ZoneType> T getZoneById(int id, Class<T> zoneType)
  460. {
  461. return (T) _classZones.get(zoneType).get(id);
  462. }
  463. /**
  464. * Returns all zones from where the object is located.
  465. * @param object the object
  466. * @return zones
  467. */
  468. public List<L2ZoneType> getZones(L2Object object)
  469. {
  470. return getZones(object.getX(), object.getY(), object.getZ());
  471. }
  472. /**
  473. * Gets the zone.
  474. * @param <T> the generic type
  475. * @param object the object
  476. * @param type the type
  477. * @return zone from where the object is located by type
  478. */
  479. public <T extends L2ZoneType> T getZone(L2Object object, Class<T> type)
  480. {
  481. if (object == null)
  482. {
  483. return null;
  484. }
  485. return getZone(object.getX(), object.getY(), object.getZ(), type);
  486. }
  487. /**
  488. * Returns all zones from given coordinates (plane).
  489. * @param x the x
  490. * @param y the y
  491. * @return zones
  492. */
  493. public List<L2ZoneType> getZones(int x, int y)
  494. {
  495. final L2WorldRegion region = L2World.getInstance().getRegion(x, y);
  496. final List<L2ZoneType> temp = new ArrayList<>();
  497. for (L2ZoneType zone : region.getZones())
  498. {
  499. if (zone.isInsideZone(x, y))
  500. {
  501. temp.add(zone);
  502. }
  503. }
  504. return temp;
  505. }
  506. /**
  507. * Returns all zones from given coordinates.
  508. * @param x the x
  509. * @param y the y
  510. * @param z the z
  511. * @return zones
  512. */
  513. public List<L2ZoneType> getZones(int x, int y, int z)
  514. {
  515. final L2WorldRegion region = L2World.getInstance().getRegion(x, y);
  516. final List<L2ZoneType> temp = new ArrayList<>();
  517. for (L2ZoneType zone : region.getZones())
  518. {
  519. if (zone.isInsideZone(x, y, z))
  520. {
  521. temp.add(zone);
  522. }
  523. }
  524. return temp;
  525. }
  526. /**
  527. * Gets the zone.
  528. * @param <T> the generic type
  529. * @param x the x
  530. * @param y the y
  531. * @param z the z
  532. * @param type the type
  533. * @return zone from given coordinates
  534. */
  535. @SuppressWarnings("unchecked")
  536. public <T extends L2ZoneType> T getZone(int x, int y, int z, Class<T> type)
  537. {
  538. final L2WorldRegion region = L2World.getInstance().getRegion(x, y);
  539. for (L2ZoneType zone : region.getZones())
  540. {
  541. if (zone.isInsideZone(x, y, z) && type.isInstance(zone))
  542. {
  543. return (T) zone;
  544. }
  545. }
  546. return null;
  547. }
  548. /**
  549. * Get spawm territory by name
  550. * @param name name of territory to search
  551. * @return link to zone form
  552. */
  553. public NpcSpawnTerritory getSpawnTerritory(String name)
  554. {
  555. return _spawnTerritories.containsKey(name) ? _spawnTerritories.get(name) : null;
  556. }
  557. /**
  558. * Returns all spawm territories from where the object is located
  559. * @param object
  560. * @return zones
  561. */
  562. public List<NpcSpawnTerritory> getSpawnTerritories(L2Object object)
  563. {
  564. List<NpcSpawnTerritory> temp = new ArrayList<>();
  565. for (NpcSpawnTerritory territory : _spawnTerritories.values())
  566. {
  567. if (territory.isInsideZone(object.getX(), object.getY(), object.getZ()))
  568. {
  569. temp.add(territory);
  570. }
  571. }
  572. return temp;
  573. }
  574. /**
  575. * Gets the arena.
  576. * @param character the character
  577. * @return the arena
  578. */
  579. public final L2ArenaZone getArena(L2Character character)
  580. {
  581. if (character == null)
  582. {
  583. return null;
  584. }
  585. for (L2ZoneType temp : ZoneManager.getInstance().getZones(character.getX(), character.getY(), character.getZ()))
  586. {
  587. if ((temp instanceof L2ArenaZone) && temp.isCharacterInZone(character))
  588. {
  589. return ((L2ArenaZone) temp);
  590. }
  591. }
  592. return null;
  593. }
  594. /**
  595. * Gets the olympiad stadium.
  596. * @param character the character
  597. * @return the olympiad stadium
  598. */
  599. public final L2OlympiadStadiumZone getOlympiadStadium(L2Character character)
  600. {
  601. if (character == null)
  602. {
  603. return null;
  604. }
  605. for (L2ZoneType temp : ZoneManager.getInstance().getZones(character.getX(), character.getY(), character.getZ()))
  606. {
  607. if ((temp instanceof L2OlympiadStadiumZone) && temp.isCharacterInZone(character))
  608. {
  609. return ((L2OlympiadStadiumZone) temp);
  610. }
  611. }
  612. return null;
  613. }
  614. /**
  615. * For testing purposes only.
  616. * @param <T> the generic type
  617. * @param obj the obj
  618. * @param type the type
  619. * @return the closest zone
  620. */
  621. @SuppressWarnings("unchecked")
  622. public <T extends L2ZoneType> T getClosestZone(L2Object obj, Class<T> type)
  623. {
  624. T zone = getZone(obj, type);
  625. if (zone == null)
  626. {
  627. double closestdis = Double.MAX_VALUE;
  628. for (T temp : (Collection<T>) _classZones.get(type).values())
  629. {
  630. double distance = temp.getDistanceToZone(obj);
  631. if (distance < closestdis)
  632. {
  633. closestdis = distance;
  634. zone = temp;
  635. }
  636. }
  637. }
  638. return zone;
  639. }
  640. /**
  641. * General storage for debug items used for visualizing zones.
  642. * @return list of items
  643. */
  644. public List<L2ItemInstance> getDebugItems()
  645. {
  646. if (_debugItems == null)
  647. {
  648. _debugItems = new ArrayList<>();
  649. }
  650. return _debugItems;
  651. }
  652. /**
  653. * Remove all debug items from l2world.
  654. */
  655. public void clearDebugItems()
  656. {
  657. if (_debugItems != null)
  658. {
  659. final Iterator<L2ItemInstance> it = _debugItems.iterator();
  660. while (it.hasNext())
  661. {
  662. final L2ItemInstance item = it.next();
  663. if (item != null)
  664. {
  665. item.decayMe();
  666. }
  667. it.remove();
  668. }
  669. }
  670. }
  671. /**
  672. * Gets the settings.
  673. * @param name the name
  674. * @return the settings
  675. */
  676. public static AbstractZoneSettings getSettings(String name)
  677. {
  678. return _settings.get(name);
  679. }
  680. /**
  681. * Gets the single instance of ZoneManager.
  682. * @return single instance of ZoneManager
  683. */
  684. public static final ZoneManager getInstance()
  685. {
  686. return SingletonHolder._instance;
  687. }
  688. private static class SingletonHolder
  689. {
  690. protected static final ZoneManager _instance = new ZoneManager();
  691. }
  692. }