InstanceManager.java 9.4 KB

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