InstanceManager.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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.NamedNodeMap;
  24. import org.w3c.dom.Node;
  25. import com.l2jserver.L2DatabaseFactory;
  26. import com.l2jserver.gameserver.engines.DocumentParser;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.entity.Instance;
  29. /**
  30. * @author evill33t, GodKratos
  31. */
  32. public class InstanceManager extends DocumentParser
  33. {
  34. private static final FastMap<Integer, Instance> _instanceList = new FastMap<>();
  35. private final FastMap<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. private static final String ADD_INSTANCE_TIME = "INSERT INTO character_instance_time (charId,instanceId,time) values (?,?,?) ON DUPLICATE KEY UPDATE time=?";
  41. private static final String RESTORE_INSTANCE_TIMES = "SELECT instanceId,time FROM character_instance_time WHERE charId=?";
  42. private static final String DELETE_INSTANCE_TIME = "DELETE FROM character_instance_time WHERE charId=? AND instanceId=?";
  43. protected InstanceManager()
  44. {
  45. // Creates the multiverse.
  46. final Instance multiverse = new Instance(-1);
  47. multiverse.setName("multiverse");
  48. _instanceList.put(-1, multiverse);
  49. _log.info(getClass().getSimpleName() + ": Multiverse Instance created.");
  50. // Creates the universe.
  51. final Instance universe = new Instance(0);
  52. universe.setName("universe");
  53. _instanceList.put(0, universe);
  54. _log.info(getClass().getSimpleName() + ": Universe Instance created.");
  55. load();
  56. }
  57. @Override
  58. public void load()
  59. {
  60. _instanceIdNames.clear();
  61. parseDatapackFile("data/instancenames.xml");
  62. _log.info(getClass().getSimpleName() + ": Loaded " + _instanceIdNames.size() + " instance names.");
  63. }
  64. /**
  65. * @param playerObjId
  66. * @param id
  67. * @return
  68. */
  69. public long getInstanceTime(int playerObjId, int id)
  70. {
  71. if (!_playerInstanceTimes.containsKey(playerObjId))
  72. {
  73. restoreInstanceTimes(playerObjId);
  74. }
  75. if (_playerInstanceTimes.get(playerObjId).containsKey(id))
  76. {
  77. return _playerInstanceTimes.get(playerObjId).get(id);
  78. }
  79. return -1;
  80. }
  81. /**
  82. * @param playerObjId
  83. * @return
  84. */
  85. public Map<Integer, Long> getAllInstanceTimes(int playerObjId)
  86. {
  87. if (!_playerInstanceTimes.containsKey(playerObjId))
  88. {
  89. restoreInstanceTimes(playerObjId);
  90. }
  91. return _playerInstanceTimes.get(playerObjId);
  92. }
  93. /**
  94. * @param playerObjId
  95. * @param id
  96. * @param time
  97. */
  98. public void setInstanceTime(int playerObjId, int id, long time)
  99. {
  100. if (!_playerInstanceTimes.containsKey(playerObjId))
  101. {
  102. restoreInstanceTimes(playerObjId);
  103. }
  104. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  105. {
  106. PreparedStatement statement = con.prepareStatement(ADD_INSTANCE_TIME);
  107. statement.setInt(1, playerObjId);
  108. statement.setInt(2, id);
  109. statement.setLong(3, time);
  110. statement.setLong(4, time);
  111. statement.execute();
  112. statement.close();
  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. {
  128. PreparedStatement statement = con.prepareStatement(DELETE_INSTANCE_TIME);
  129. statement.setInt(1, playerObjId);
  130. statement.setInt(2, id);
  131. statement.execute();
  132. statement.close();
  133. _playerInstanceTimes.get(playerObjId).remove(id);
  134. }
  135. catch (Exception e)
  136. {
  137. _log.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 FastMap<Integer, Long>());
  150. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  151. {
  152. PreparedStatement statement = con.prepareStatement(RESTORE_INSTANCE_TIMES);
  153. statement.setInt(1, playerObjId);
  154. ResultSet rset = statement.executeQuery();
  155. while (rset.next())
  156. {
  157. int id = rset.getInt("instanceId");
  158. long time = rset.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. rset.close();
  169. statement.close();
  170. }
  171. catch (Exception e)
  172. {
  173. _log.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. protected void parseDocument()
  190. {
  191. for (Node n = getCurrentDocument().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. public static class InstanceWorld
  208. {
  209. public int instanceId;
  210. public int templateId = -1;
  211. public FastList<Integer> allowed = new FastList<>();
  212. public volatile int status;
  213. }
  214. /**
  215. * @param world
  216. */
  217. public void addWorld(InstanceWorld world)
  218. {
  219. _instanceWorlds.put(world.instanceId, world);
  220. }
  221. /**
  222. * @param instanceId
  223. * @return
  224. */
  225. public InstanceWorld getWorld(int instanceId)
  226. {
  227. return _instanceWorlds.get(instanceId);
  228. }
  229. /**
  230. * @param player
  231. * @return
  232. */
  233. public InstanceWorld getPlayerWorld(L2PcInstance player)
  234. {
  235. for (InstanceWorld temp : _instanceWorlds.values())
  236. {
  237. if (temp == null)
  238. {
  239. continue;
  240. }
  241. // check if the player have a World Instance where he/she is allowed to enter
  242. if (temp.allowed.contains(player.getObjectId()))
  243. {
  244. return temp;
  245. }
  246. }
  247. return null;
  248. }
  249. /**
  250. * @param instanceid
  251. */
  252. public void destroyInstance(int instanceid)
  253. {
  254. if (instanceid <= 0)
  255. {
  256. return;
  257. }
  258. Instance temp = _instanceList.get(instanceid);
  259. if (temp != null)
  260. {
  261. temp.removeNpcs();
  262. temp.removePlayers();
  263. temp.removeDoors();
  264. temp.cancelTimer();
  265. _instanceList.remove(instanceid);
  266. if (_instanceWorlds.containsKey(instanceid))
  267. {
  268. _instanceWorlds.remove(instanceid);
  269. }
  270. }
  271. }
  272. /**
  273. * @param instanceid
  274. * @return
  275. */
  276. public Instance getInstance(int instanceid)
  277. {
  278. return _instanceList.get(instanceid);
  279. }
  280. /**
  281. * @return
  282. */
  283. public FastMap<Integer, Instance> getInstances()
  284. {
  285. return _instanceList;
  286. }
  287. /**
  288. * @param objectId
  289. * @return
  290. */
  291. public int getPlayerInstance(int objectId)
  292. {
  293. for (Instance temp : _instanceList.values())
  294. {
  295. if (temp == null)
  296. {
  297. continue;
  298. }
  299. // check if the player is in any active instance
  300. if (temp.containsPlayer(objectId))
  301. {
  302. return temp.getId();
  303. }
  304. }
  305. // 0 is default instance aka the world
  306. return 0;
  307. }
  308. /**
  309. * @param id
  310. * @return
  311. */
  312. public boolean createInstance(int id)
  313. {
  314. if (getInstance(id) != null)
  315. {
  316. return false;
  317. }
  318. Instance instance = new Instance(id);
  319. _instanceList.put(id, instance);
  320. return true;
  321. }
  322. /**
  323. * @param id
  324. * @param template
  325. * @return
  326. */
  327. public boolean createInstanceFromTemplate(int id, String template)
  328. {
  329. if (getInstance(id) != null)
  330. {
  331. return false;
  332. }
  333. Instance instance = new Instance(id);
  334. _instanceList.put(id, instance);
  335. instance.loadInstanceTemplate(template);
  336. return true;
  337. }
  338. /**
  339. * Create a new instance with a dynamic instance id based on a template (or null)
  340. * @param template xml file
  341. * @return
  342. */
  343. public int createDynamicInstance(String template)
  344. {
  345. while (getInstance(_dynamic) != null)
  346. {
  347. _dynamic++;
  348. if (_dynamic == Integer.MAX_VALUE)
  349. {
  350. _log.warning(getClass().getSimpleName() + ": More then " + (Integer.MAX_VALUE - 300000) + " instances created");
  351. _dynamic = 300000;
  352. }
  353. }
  354. Instance instance = new Instance(_dynamic);
  355. _instanceList.put(_dynamic, instance);
  356. if (template != null)
  357. {
  358. instance.loadInstanceTemplate(template);
  359. }
  360. return _dynamic;
  361. }
  362. public static final InstanceManager getInstance()
  363. {
  364. return SingletonHolder._instance;
  365. }
  366. private static class SingletonHolder
  367. {
  368. protected static final InstanceManager _instance = new InstanceManager();
  369. }
  370. }