InstanceManager.java 9.6 KB

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