InstanceManager.java 9.7 KB

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