2
0

InstanceManager.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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.FastList;
  22. import javolution.util.FastMap;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. import com.l2jserver.L2DatabaseFactory;
  27. import com.l2jserver.gameserver.engines.DocumentParser;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.gameserver.model.entity.Instance;
  30. /**
  31. * @author evill33t, GodKratos
  32. */
  33. public class InstanceManager extends DocumentParser
  34. {
  35. private static final FastMap<Integer, Instance> _instanceList = new FastMap<>();
  36. private final FastMap<Integer, InstanceWorld> _instanceWorlds = new FastMap<>();
  37. private int _dynamic = 300000;
  38. // InstanceId Names
  39. private final static Map<Integer, String> _instanceIdNames = new HashMap<>();
  40. private final Map<Integer, Map<Integer, Long>> _playerInstanceTimes = new FastMap<>();
  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. private InstanceManager()
  45. {
  46. _instanceIdNames.clear();
  47. parseDatapackFile("data/instancenames.xml");
  48. _log.info(getClass().getSimpleName() + ": Loaded " + _instanceIdNames.size() + " instance names.");
  49. createWorld();
  50. }
  51. /**
  52. * @param playerObjId
  53. * @param id
  54. * @return
  55. */
  56. public long getInstanceTime(int playerObjId, int id)
  57. {
  58. if (!_playerInstanceTimes.containsKey(playerObjId))
  59. {
  60. restoreInstanceTimes(playerObjId);
  61. }
  62. if (_playerInstanceTimes.get(playerObjId).containsKey(id))
  63. {
  64. return _playerInstanceTimes.get(playerObjId).get(id);
  65. }
  66. return -1;
  67. }
  68. /**
  69. * @param playerObjId
  70. * @return
  71. */
  72. public Map<Integer, Long> getAllInstanceTimes(int playerObjId)
  73. {
  74. if (!_playerInstanceTimes.containsKey(playerObjId))
  75. {
  76. restoreInstanceTimes(playerObjId);
  77. }
  78. return _playerInstanceTimes.get(playerObjId);
  79. }
  80. /**
  81. * @param playerObjId
  82. * @param id
  83. * @param time
  84. */
  85. public void setInstanceTime(int playerObjId, int id, long time)
  86. {
  87. if (!_playerInstanceTimes.containsKey(playerObjId))
  88. {
  89. restoreInstanceTimes(playerObjId);
  90. }
  91. Connection con = null;
  92. try
  93. {
  94. con = L2DatabaseFactory.getInstance().getConnection();
  95. PreparedStatement statement = con.prepareStatement(ADD_INSTANCE_TIME);
  96. statement.setInt(1, playerObjId);
  97. statement.setInt(2, id);
  98. statement.setLong(3, time);
  99. statement.setLong(4, time);
  100. statement.execute();
  101. statement.close();
  102. _playerInstanceTimes.get(playerObjId).put(id, time);
  103. }
  104. catch (Exception e)
  105. {
  106. _log.warning(getClass().getSimpleName() + ": Could not insert character instance time data: " + e.getMessage());
  107. }
  108. finally
  109. {
  110. L2DatabaseFactory.close(con);
  111. }
  112. }
  113. /**
  114. * @param playerObjId
  115. * @param id
  116. */
  117. public void deleteInstanceTime(int playerObjId, int id)
  118. {
  119. Connection con = null;
  120. try
  121. {
  122. con = L2DatabaseFactory.getInstance().getConnection();
  123. PreparedStatement statement = con.prepareStatement(DELETE_INSTANCE_TIME);
  124. statement.setInt(1, playerObjId);
  125. statement.setInt(2, id);
  126. statement.execute();
  127. statement.close();
  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. finally
  135. {
  136. L2DatabaseFactory.close(con);
  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. Connection con = null;
  150. try
  151. {
  152. con = L2DatabaseFactory.getInstance().getConnection();
  153. PreparedStatement statement = con.prepareStatement(RESTORE_INSTANCE_TIMES);
  154. statement.setInt(1, playerObjId);
  155. ResultSet rset = statement.executeQuery();
  156. while (rset.next())
  157. {
  158. int id = rset.getInt("instanceId");
  159. long time = rset.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. rset.close();
  170. statement.close();
  171. }
  172. catch (Exception e)
  173. {
  174. _log.warning(getClass().getSimpleName() + ": Could not delete character instance time data: " + e.getMessage());
  175. }
  176. finally
  177. {
  178. L2DatabaseFactory.close(con);
  179. }
  180. }
  181. /**
  182. * @param id
  183. * @return
  184. */
  185. public String getInstanceIdName(int id)
  186. {
  187. if (_instanceIdNames.containsKey(id))
  188. {
  189. return _instanceIdNames.get(id);
  190. }
  191. return ("UnknownInstance");
  192. }
  193. @Override
  194. protected void parseDocument(Document doc)
  195. {
  196. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  197. {
  198. if ("list".equals(n.getNodeName()))
  199. {
  200. NamedNodeMap attrs;
  201. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  202. {
  203. if ("instance".equals(d.getNodeName()))
  204. {
  205. attrs = d.getAttributes();
  206. _instanceIdNames.put(parseInteger(attrs, "id"), attrs.getNamedItem("name").getNodeValue());
  207. }
  208. }
  209. }
  210. }
  211. }
  212. public static class InstanceWorld
  213. {
  214. public int instanceId;
  215. public int templateId = -1;
  216. public FastList<Integer> allowed = new FastList<Integer>();
  217. public volatile int status;
  218. }
  219. /**
  220. * @param world
  221. */
  222. public void addWorld(InstanceWorld world)
  223. {
  224. _instanceWorlds.put(world.instanceId, world);
  225. }
  226. /**
  227. * @param instanceId
  228. * @return
  229. */
  230. public InstanceWorld getWorld(int instanceId)
  231. {
  232. return _instanceWorlds.get(instanceId);
  233. }
  234. /**
  235. * @param player
  236. * @return
  237. */
  238. public InstanceWorld getPlayerWorld(L2PcInstance player)
  239. {
  240. for (InstanceWorld temp : _instanceWorlds.values())
  241. {
  242. if (temp == null)
  243. {
  244. continue;
  245. }
  246. // check if the player have a World Instance where he/she is allowed to enter
  247. if (temp.allowed.contains(player.getObjectId()))
  248. {
  249. return temp;
  250. }
  251. }
  252. return null;
  253. }
  254. private void createWorld()
  255. {
  256. Instance themultiverse = new Instance(-1);
  257. themultiverse.setName("multiverse");
  258. _instanceList.put(-1, themultiverse);
  259. _log.info("Multiverse Instance created");
  260. Instance universe = new Instance(0);
  261. universe.setName("universe");
  262. _instanceList.put(0, universe);
  263. _log.info("Universe Instance created");
  264. }
  265. /**
  266. * @param instanceid
  267. */
  268. public void destroyInstance(int instanceid)
  269. {
  270. if (instanceid <= 0)
  271. {
  272. return;
  273. }
  274. Instance temp = _instanceList.get(instanceid);
  275. if (temp != null)
  276. {
  277. temp.removeNpcs();
  278. temp.removePlayers();
  279. temp.removeDoors();
  280. temp.cancelTimer();
  281. _instanceList.remove(instanceid);
  282. if (_instanceWorlds.containsKey(instanceid))
  283. {
  284. _instanceWorlds.remove(instanceid);
  285. }
  286. }
  287. }
  288. /**
  289. * @param instanceid
  290. * @return
  291. */
  292. public Instance getInstance(int instanceid)
  293. {
  294. return _instanceList.get(instanceid);
  295. }
  296. /**
  297. * @return
  298. */
  299. public FastMap<Integer, Instance> getInstances()
  300. {
  301. return _instanceList;
  302. }
  303. /**
  304. * @param objectId
  305. * @return
  306. */
  307. public int getPlayerInstance(int objectId)
  308. {
  309. for (Instance temp : _instanceList.values())
  310. {
  311. if (temp == null)
  312. {
  313. continue;
  314. }
  315. // check if the player is in any active instance
  316. if (temp.containsPlayer(objectId))
  317. {
  318. return temp.getId();
  319. }
  320. }
  321. // 0 is default instance aka the world
  322. return 0;
  323. }
  324. /**
  325. * @param id
  326. * @return
  327. */
  328. public boolean createInstance(int id)
  329. {
  330. if (getInstance(id) != null)
  331. {
  332. return false;
  333. }
  334. Instance instance = new Instance(id);
  335. _instanceList.put(id, instance);
  336. return true;
  337. }
  338. /**
  339. * @param id
  340. * @param template
  341. * @return
  342. */
  343. public boolean createInstanceFromTemplate(int id, String template)
  344. {
  345. if (getInstance(id) != null)
  346. {
  347. return false;
  348. }
  349. Instance instance = new Instance(id);
  350. _instanceList.put(id, instance);
  351. instance.loadInstanceTemplate(template);
  352. return true;
  353. }
  354. /**
  355. * Create a new instance with a dynamic instance id based on a template (or null)
  356. * @param template xml file
  357. * @return
  358. */
  359. public int createDynamicInstance(String template)
  360. {
  361. while (getInstance(_dynamic) != null)
  362. {
  363. _dynamic++;
  364. if (_dynamic == Integer.MAX_VALUE)
  365. {
  366. _log.warning("InstanceManager: More then " + (Integer.MAX_VALUE - 300000) + " instances created");
  367. _dynamic = 300000;
  368. }
  369. }
  370. Instance instance = new Instance(_dynamic);
  371. _instanceList.put(_dynamic, instance);
  372. if (template != null)
  373. {
  374. instance.loadInstanceTemplate(template);
  375. }
  376. return _dynamic;
  377. }
  378. public static final InstanceManager getInstance()
  379. {
  380. return SingletonHolder._instance;
  381. }
  382. @SuppressWarnings("synthetic-access")
  383. private static class SingletonHolder
  384. {
  385. protected static final InstanceManager _instance = new InstanceManager();
  386. }
  387. }