InstanceManager.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.concurrent.ConcurrentHashMap;
  26. import org.w3c.dom.Document;
  27. import org.w3c.dom.NamedNodeMap;
  28. import org.w3c.dom.Node;
  29. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  31. import com.l2jserver.gameserver.model.entity.Instance;
  32. import com.l2jserver.gameserver.model.instancezone.InstanceWorld;
  33. import com.l2jserver.util.data.xml.IXmlReader;
  34. /**
  35. * Instance Manager.
  36. * @author evill33t, GodKratos
  37. */
  38. public final class InstanceManager implements IXmlReader
  39. {
  40. private static final Map<Integer, Instance> INSTANCES = new ConcurrentHashMap<>();
  41. private final Map<Integer, InstanceWorld> _instanceWorlds = new ConcurrentHashMap<>();
  42. private int _dynamic = 300000;
  43. // InstanceId Names
  44. private static final Map<Integer, String> _instanceIdNames = new HashMap<>();
  45. private final Map<Integer, Map<Integer, Long>> _playerInstanceTimes = new ConcurrentHashMap<>();
  46. // SQL Queries
  47. private static final String ADD_INSTANCE_TIME = "INSERT INTO character_instance_time (charId,instanceId,time) values (?,?,?) ON DUPLICATE KEY UPDATE time=?";
  48. private static final String RESTORE_INSTANCE_TIMES = "SELECT instanceId,time FROM character_instance_time WHERE charId=?";
  49. private static final String DELETE_INSTANCE_TIME = "DELETE FROM character_instance_time WHERE charId=? AND instanceId=?";
  50. protected InstanceManager()
  51. {
  52. // Creates the multiverse.
  53. INSTANCES.put(-1, new Instance(-1, "multiverse"));
  54. LOGGER.info("{}: Multiverse Instance created.", getClass().getSimpleName());
  55. // Creates the universe.
  56. INSTANCES.put(0, new Instance(0, "universe"));
  57. LOGGER.info("{}: Universe Instance created.", getClass().getSimpleName());
  58. load();
  59. }
  60. @Override
  61. public void load()
  62. {
  63. _instanceIdNames.clear();
  64. parseDatapackFile("data/instancenames.xml");
  65. LOGGER.info("{}: Loaded {} instance names.", getClass().getSimpleName(), _instanceIdNames.size());
  66. }
  67. /**
  68. * @param playerObjId
  69. * @param id
  70. * @return
  71. */
  72. public long getInstanceTime(int playerObjId, int id)
  73. {
  74. if (!_playerInstanceTimes.containsKey(playerObjId))
  75. {
  76. restoreInstanceTimes(playerObjId);
  77. }
  78. if (_playerInstanceTimes.get(playerObjId).containsKey(id))
  79. {
  80. return _playerInstanceTimes.get(playerObjId).get(id);
  81. }
  82. return -1;
  83. }
  84. /**
  85. * @param playerObjId
  86. * @return
  87. */
  88. public Map<Integer, Long> getAllInstanceTimes(int playerObjId)
  89. {
  90. if (!_playerInstanceTimes.containsKey(playerObjId))
  91. {
  92. restoreInstanceTimes(playerObjId);
  93. }
  94. return _playerInstanceTimes.get(playerObjId);
  95. }
  96. /**
  97. * @param playerObjId
  98. * @param id
  99. * @param time
  100. */
  101. public void setInstanceTime(int playerObjId, int id, long time)
  102. {
  103. if (!_playerInstanceTimes.containsKey(playerObjId))
  104. {
  105. restoreInstanceTimes(playerObjId);
  106. }
  107. try (Connection con = ConnectionFactory.getInstance().getConnection();
  108. PreparedStatement ps = con.prepareStatement(ADD_INSTANCE_TIME))
  109. {
  110. ps.setInt(1, playerObjId);
  111. ps.setInt(2, id);
  112. ps.setLong(3, time);
  113. ps.setLong(4, time);
  114. ps.execute();
  115. _playerInstanceTimes.get(playerObjId).put(id, time);
  116. }
  117. catch (Exception e)
  118. {
  119. LOGGER.warn("{}: Could not insert character instance time data!", getClass().getSimpleName(), e);
  120. }
  121. }
  122. /**
  123. * @param playerObjId
  124. * @param id
  125. */
  126. public void deleteInstanceTime(int playerObjId, int id)
  127. {
  128. try (Connection con = ConnectionFactory.getInstance().getConnection();
  129. PreparedStatement ps = con.prepareStatement(DELETE_INSTANCE_TIME))
  130. {
  131. ps.setInt(1, playerObjId);
  132. ps.setInt(2, id);
  133. ps.execute();
  134. _playerInstanceTimes.get(playerObjId).remove(id);
  135. }
  136. catch (Exception e)
  137. {
  138. LOGGER.warn("{}: Could not delete character instance time data!", getClass().getSimpleName(), e);
  139. }
  140. }
  141. /**
  142. * @param playerObjId
  143. */
  144. public void restoreInstanceTimes(int playerObjId)
  145. {
  146. if (_playerInstanceTimes.containsKey(playerObjId))
  147. {
  148. return; // already restored
  149. }
  150. _playerInstanceTimes.put(playerObjId, new ConcurrentHashMap<>());
  151. try (Connection con = ConnectionFactory.getInstance().getConnection();
  152. PreparedStatement ps = con.prepareStatement(RESTORE_INSTANCE_TIMES))
  153. {
  154. ps.setInt(1, playerObjId);
  155. try (ResultSet rs = ps.executeQuery())
  156. {
  157. while (rs.next())
  158. {
  159. int id = rs.getInt("instanceId");
  160. long time = rs.getLong("time");
  161. if (time < System.currentTimeMillis())
  162. {
  163. deleteInstanceTime(playerObjId, id);
  164. }
  165. else
  166. {
  167. _playerInstanceTimes.get(playerObjId).put(id, time);
  168. }
  169. }
  170. }
  171. }
  172. catch (Exception e)
  173. {
  174. LOGGER.warn("{}: Could not delete character instance time data!", getClass().getSimpleName(), e);
  175. }
  176. }
  177. /**
  178. * @param id
  179. * @return
  180. */
  181. public String getInstanceIdName(int id)
  182. {
  183. if (_instanceIdNames.containsKey(id))
  184. {
  185. return _instanceIdNames.get(id);
  186. }
  187. return ("UnknownInstance");
  188. }
  189. @Override
  190. public void parseDocument(Document doc)
  191. {
  192. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  193. {
  194. if ("list".equals(n.getNodeName()))
  195. {
  196. NamedNodeMap attrs;
  197. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  198. {
  199. if ("instance".equals(d.getNodeName()))
  200. {
  201. attrs = d.getAttributes();
  202. _instanceIdNames.put(parseInteger(attrs, "id"), attrs.getNamedItem("name").getNodeValue());
  203. }
  204. }
  205. }
  206. }
  207. }
  208. /**
  209. * @param world
  210. */
  211. public void addWorld(InstanceWorld world)
  212. {
  213. _instanceWorlds.put(world.getInstanceId(), world);
  214. }
  215. /**
  216. * @param instanceId
  217. * @return
  218. */
  219. public InstanceWorld getWorld(int instanceId)
  220. {
  221. return _instanceWorlds.get(instanceId);
  222. }
  223. /**
  224. * Check if the player have a World Instance where it's allowed to enter.
  225. * @param player the player to check
  226. * @return the instance world
  227. */
  228. public InstanceWorld getPlayerWorld(L2PcInstance player)
  229. {
  230. for (InstanceWorld temp : _instanceWorlds.values())
  231. {
  232. if ((temp != null) && (temp.isAllowed(player.getObjectId())))
  233. {
  234. return temp;
  235. }
  236. }
  237. return null;
  238. }
  239. /**
  240. * @param instanceid
  241. */
  242. public void destroyInstance(int instanceid)
  243. {
  244. if (instanceid <= 0)
  245. {
  246. return;
  247. }
  248. final Instance temp = INSTANCES.get(instanceid);
  249. if (temp != null)
  250. {
  251. temp.removeNpcs();
  252. temp.removePlayers();
  253. temp.removeDoors();
  254. temp.cancelTimer();
  255. INSTANCES.remove(instanceid);
  256. _instanceWorlds.remove(instanceid);
  257. }
  258. }
  259. /**
  260. * @param instanceid
  261. * @return
  262. */
  263. public Instance getInstance(int instanceid)
  264. {
  265. return INSTANCES.get(instanceid);
  266. }
  267. /**
  268. * @return
  269. */
  270. public Map<Integer, Instance> getInstances()
  271. {
  272. return INSTANCES;
  273. }
  274. /**
  275. * @param objectId
  276. * @return
  277. */
  278. public int getPlayerInstance(int objectId)
  279. {
  280. for (Instance temp : INSTANCES.values())
  281. {
  282. if (temp == null)
  283. {
  284. continue;
  285. }
  286. // check if the player is in any active instance
  287. if (temp.containsPlayer(objectId))
  288. {
  289. return temp.getId();
  290. }
  291. }
  292. // 0 is default instance aka the world
  293. return 0;
  294. }
  295. /**
  296. * @param id
  297. * @return
  298. */
  299. public boolean createInstance(int id)
  300. {
  301. if (getInstance(id) != null)
  302. {
  303. return false;
  304. }
  305. final Instance instance = new Instance(id);
  306. INSTANCES.put(id, instance);
  307. return true;
  308. }
  309. /**
  310. * @param id
  311. * @param template
  312. * @return
  313. */
  314. public boolean createInstanceFromTemplate(int id, String template)
  315. {
  316. if (getInstance(id) != null)
  317. {
  318. return false;
  319. }
  320. final Instance instance = new Instance(id);
  321. INSTANCES.put(id, instance);
  322. instance.loadInstanceTemplate(template);
  323. return true;
  324. }
  325. /**
  326. * Create a new instance with a dynamic instance id based on a template (or null)
  327. * @param template xml file
  328. * @return
  329. */
  330. public int createDynamicInstance(String template)
  331. {
  332. while (getInstance(_dynamic) != null)
  333. {
  334. _dynamic++;
  335. if (_dynamic == Integer.MAX_VALUE)
  336. {
  337. LOGGER.warn("{}: More then {} instances has been created!", getClass().getSimpleName(), (Integer.MAX_VALUE - 300000));
  338. _dynamic = 300000;
  339. }
  340. }
  341. final Instance instance = new Instance(_dynamic);
  342. INSTANCES.put(_dynamic, instance);
  343. if (template != null)
  344. {
  345. instance.loadInstanceTemplate(template);
  346. }
  347. return _dynamic;
  348. }
  349. /**
  350. * Gets the single instance of {@code InstanceManager}.
  351. * @return single instance of {@code InstanceManager}
  352. */
  353. public static final InstanceManager getInstance()
  354. {
  355. return SingletonHolder._instance;
  356. }
  357. private static class SingletonHolder
  358. {
  359. protected static final InstanceManager _instance = new InstanceManager();
  360. }
  361. }