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. protected InstanceManager()
  45. {
  46. // Creates the multiverse.
  47. final Instance multiverse = new Instance(-1);
  48. multiverse.setName("multiverse");
  49. _instanceList.put(-1, multiverse);
  50. _log.info(getClass().getSimpleName() + ": Multiverse Instance created.");
  51. // Creates the universe.
  52. final Instance universe = new Instance(0);
  53. universe.setName("universe");
  54. _instanceList.put(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. Connection con = null;
  106. try
  107. {
  108. con = L2DatabaseFactory.getInstance().getConnection();
  109. PreparedStatement statement = con.prepareStatement(ADD_INSTANCE_TIME);
  110. statement.setInt(1, playerObjId);
  111. statement.setInt(2, id);
  112. statement.setLong(3, time);
  113. statement.setLong(4, time);
  114. statement.execute();
  115. statement.close();
  116. _playerInstanceTimes.get(playerObjId).put(id, time);
  117. }
  118. catch (Exception e)
  119. {
  120. _log.warning(getClass().getSimpleName() + ": Could not insert character instance time data: " + e.getMessage());
  121. }
  122. finally
  123. {
  124. L2DatabaseFactory.close(con);
  125. }
  126. }
  127. /**
  128. * @param playerObjId
  129. * @param id
  130. */
  131. public void deleteInstanceTime(int playerObjId, int id)
  132. {
  133. Connection con = null;
  134. try
  135. {
  136. con = L2DatabaseFactory.getInstance().getConnection();
  137. PreparedStatement statement = con.prepareStatement(DELETE_INSTANCE_TIME);
  138. statement.setInt(1, playerObjId);
  139. statement.setInt(2, id);
  140. statement.execute();
  141. statement.close();
  142. _playerInstanceTimes.get(playerObjId).remove(id);
  143. }
  144. catch (Exception e)
  145. {
  146. _log.warning(getClass().getSimpleName() + ": Could not delete character instance time data: " + e.getMessage());
  147. }
  148. finally
  149. {
  150. L2DatabaseFactory.close(con);
  151. }
  152. }
  153. /**
  154. * @param playerObjId
  155. */
  156. public void restoreInstanceTimes(int playerObjId)
  157. {
  158. if (_playerInstanceTimes.containsKey(playerObjId))
  159. {
  160. return; // already restored
  161. }
  162. _playerInstanceTimes.put(playerObjId, new FastMap<Integer, Long>());
  163. Connection con = null;
  164. try
  165. {
  166. con = L2DatabaseFactory.getInstance().getConnection();
  167. PreparedStatement statement = con.prepareStatement(RESTORE_INSTANCE_TIMES);
  168. statement.setInt(1, playerObjId);
  169. ResultSet rset = statement.executeQuery();
  170. while (rset.next())
  171. {
  172. int id = rset.getInt("instanceId");
  173. long time = rset.getLong("time");
  174. if (time < System.currentTimeMillis())
  175. {
  176. deleteInstanceTime(playerObjId, id);
  177. }
  178. else
  179. {
  180. _playerInstanceTimes.get(playerObjId).put(id, time);
  181. }
  182. }
  183. rset.close();
  184. statement.close();
  185. }
  186. catch (Exception e)
  187. {
  188. _log.warning(getClass().getSimpleName() + ": Could not delete character instance time data: " + e.getMessage());
  189. }
  190. finally
  191. {
  192. L2DatabaseFactory.close(con);
  193. }
  194. }
  195. /**
  196. * @param id
  197. * @return
  198. */
  199. public String getInstanceIdName(int id)
  200. {
  201. if (_instanceIdNames.containsKey(id))
  202. {
  203. return _instanceIdNames.get(id);
  204. }
  205. return ("UnknownInstance");
  206. }
  207. @Override
  208. protected void parseDocument(Document doc)
  209. {
  210. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  211. {
  212. if ("list".equals(n.getNodeName()))
  213. {
  214. NamedNodeMap attrs;
  215. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  216. {
  217. if ("instance".equals(d.getNodeName()))
  218. {
  219. attrs = d.getAttributes();
  220. _instanceIdNames.put(parseInteger(attrs, "id"), attrs.getNamedItem("name").getNodeValue());
  221. }
  222. }
  223. }
  224. }
  225. }
  226. public static class InstanceWorld
  227. {
  228. public int instanceId;
  229. public int templateId = -1;
  230. public FastList<Integer> allowed = new FastList<Integer>();
  231. public volatile int status;
  232. }
  233. /**
  234. * @param world
  235. */
  236. public void addWorld(InstanceWorld world)
  237. {
  238. _instanceWorlds.put(world.instanceId, world);
  239. }
  240. /**
  241. * @param instanceId
  242. * @return
  243. */
  244. public InstanceWorld getWorld(int instanceId)
  245. {
  246. return _instanceWorlds.get(instanceId);
  247. }
  248. /**
  249. * @param player
  250. * @return
  251. */
  252. public InstanceWorld getPlayerWorld(L2PcInstance player)
  253. {
  254. for (InstanceWorld temp : _instanceWorlds.values())
  255. {
  256. if (temp == null)
  257. {
  258. continue;
  259. }
  260. // check if the player have a World Instance where he/she is allowed to enter
  261. if (temp.allowed.contains(player.getObjectId()))
  262. {
  263. return temp;
  264. }
  265. }
  266. return null;
  267. }
  268. /**
  269. * @param instanceid
  270. */
  271. public void destroyInstance(int instanceid)
  272. {
  273. if (instanceid <= 0)
  274. {
  275. return;
  276. }
  277. Instance temp = _instanceList.get(instanceid);
  278. if (temp != null)
  279. {
  280. temp.removeNpcs();
  281. temp.removePlayers();
  282. temp.removeDoors();
  283. temp.cancelTimer();
  284. _instanceList.remove(instanceid);
  285. if (_instanceWorlds.containsKey(instanceid))
  286. {
  287. _instanceWorlds.remove(instanceid);
  288. }
  289. }
  290. }
  291. /**
  292. * @param instanceid
  293. * @return
  294. */
  295. public Instance getInstance(int instanceid)
  296. {
  297. return _instanceList.get(instanceid);
  298. }
  299. /**
  300. * @return
  301. */
  302. public FastMap<Integer, Instance> getInstances()
  303. {
  304. return _instanceList;
  305. }
  306. /**
  307. * @param objectId
  308. * @return
  309. */
  310. public int getPlayerInstance(int objectId)
  311. {
  312. for (Instance temp : _instanceList.values())
  313. {
  314. if (temp == null)
  315. {
  316. continue;
  317. }
  318. // check if the player is in any active instance
  319. if (temp.containsPlayer(objectId))
  320. {
  321. return temp.getId();
  322. }
  323. }
  324. // 0 is default instance aka the world
  325. return 0;
  326. }
  327. /**
  328. * @param id
  329. * @return
  330. */
  331. public boolean createInstance(int id)
  332. {
  333. if (getInstance(id) != null)
  334. {
  335. return false;
  336. }
  337. Instance instance = new Instance(id);
  338. _instanceList.put(id, instance);
  339. return true;
  340. }
  341. /**
  342. * @param id
  343. * @param template
  344. * @return
  345. */
  346. public boolean createInstanceFromTemplate(int id, String template)
  347. {
  348. if (getInstance(id) != null)
  349. {
  350. return false;
  351. }
  352. Instance instance = new Instance(id);
  353. _instanceList.put(id, instance);
  354. instance.loadInstanceTemplate(template);
  355. return true;
  356. }
  357. /**
  358. * Create a new instance with a dynamic instance id based on a template (or null)
  359. * @param template xml file
  360. * @return
  361. */
  362. public int createDynamicInstance(String template)
  363. {
  364. while (getInstance(_dynamic) != null)
  365. {
  366. _dynamic++;
  367. if (_dynamic == Integer.MAX_VALUE)
  368. {
  369. _log.warning(getClass().getSimpleName() + ": More then " + (Integer.MAX_VALUE - 300000) + " instances created");
  370. _dynamic = 300000;
  371. }
  372. }
  373. Instance instance = new Instance(_dynamic);
  374. _instanceList.put(_dynamic, instance);
  375. if (template != null)
  376. {
  377. instance.loadInstanceTemplate(template);
  378. }
  379. return _dynamic;
  380. }
  381. public static final InstanceManager getInstance()
  382. {
  383. return SingletonHolder._instance;
  384. }
  385. private static class SingletonHolder
  386. {
  387. protected static final InstanceManager _instance = new InstanceManager();
  388. }
  389. }