InstanceManager.java 9.7 KB

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