InstanceManager.java 9.7 KB

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