InstanceManager.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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.io.FileInputStream;
  17. import java.io.FileNotFoundException;
  18. import java.io.InputStream;
  19. import java.sql.Connection;
  20. import java.sql.PreparedStatement;
  21. import java.sql.ResultSet;
  22. import java.util.Map;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import javolution.io.UTF8StreamReader;
  26. import javolution.util.FastList;
  27. import javolution.util.FastMap;
  28. import javolution.xml.stream.XMLStreamConstants;
  29. import javolution.xml.stream.XMLStreamException;
  30. import javolution.xml.stream.XMLStreamReaderImpl;
  31. import com.l2jserver.Config;
  32. import com.l2jserver.L2DatabaseFactory;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.gameserver.model.entity.Instance;
  35. /**
  36. * @author evill33t, GodKratos
  37. *
  38. */
  39. public class InstanceManager
  40. {
  41. private final static Logger _log = Logger.getLogger(InstanceManager.class.getName());
  42. private FastMap<Integer, Instance> _instanceList = new FastMap<Integer, Instance>();
  43. private FastMap<Integer, InstanceWorld> _instanceWorlds = new FastMap<Integer, InstanceWorld>();
  44. private int _dynamic = 300000;
  45. // InstanceId Names
  46. private final static Map<Integer, String> _instanceIdNames = new FastMap<Integer, String>();
  47. private Map<Integer,Map<Integer,Long>> _playerInstanceTimes = new FastMap<Integer, Map<Integer,Long>>();
  48. private static final String ADD_INSTANCE_TIME = "INSERT INTO character_instance_time (charId,instanceId,time) values (?,?,?) ON DUPLICATE KEY UPDATE time=?";
  49. private static final String RESTORE_INSTANCE_TIMES = "SELECT instanceId,time FROM character_instance_time WHERE charId=?";
  50. private static final String DELETE_INSTANCE_TIME = "DELETE FROM character_instance_time WHERE charId=? AND instanceId=?";
  51. public long getInstanceTime(int playerObjId, int id)
  52. {
  53. if (!_playerInstanceTimes.containsKey(playerObjId))
  54. restoreInstanceTimes(playerObjId);
  55. if (_playerInstanceTimes.get(playerObjId).containsKey(id))
  56. return _playerInstanceTimes.get(playerObjId).get(id);
  57. return -1;
  58. }
  59. public Map<Integer,Long> getAllInstanceTimes(int playerObjId)
  60. {
  61. if (!_playerInstanceTimes.containsKey(playerObjId))
  62. restoreInstanceTimes(playerObjId);
  63. return _playerInstanceTimes.get(playerObjId);
  64. }
  65. public void setInstanceTime(int playerObjId, int id, long time)
  66. {
  67. if (!_playerInstanceTimes.containsKey(playerObjId))
  68. restoreInstanceTimes(playerObjId);
  69. Connection con = null;
  70. try
  71. {
  72. con = L2DatabaseFactory.getInstance().getConnection();
  73. PreparedStatement statement = con.prepareStatement(ADD_INSTANCE_TIME);
  74. statement.setInt(1, playerObjId);
  75. statement.setInt(2, id);
  76. statement.setLong(3, time);
  77. statement.setLong(4, time);
  78. statement.execute();
  79. statement.close();
  80. _playerInstanceTimes.get(playerObjId).put(id, time);
  81. }
  82. catch (Exception e)
  83. {
  84. _log.log(Level.WARNING, "Could not insert character instance time data: "+ e.getMessage(), e);
  85. }
  86. finally
  87. {
  88. L2DatabaseFactory.close(con);
  89. }
  90. }
  91. public void deleteInstanceTime(int playerObjId, int id)
  92. {
  93. Connection con = null;
  94. try
  95. {
  96. con = L2DatabaseFactory.getInstance().getConnection();
  97. PreparedStatement statement = con.prepareStatement(DELETE_INSTANCE_TIME);
  98. statement.setInt(1, playerObjId);
  99. statement.setInt(2, id);
  100. statement.execute();
  101. statement.close();
  102. _playerInstanceTimes.get(playerObjId).remove(id);
  103. }
  104. catch (Exception e)
  105. {
  106. _log.log(Level.WARNING, "Could not delete character instance time data: "+ e.getMessage(), e);
  107. }
  108. finally
  109. {
  110. L2DatabaseFactory.close(con);
  111. }
  112. }
  113. public void restoreInstanceTimes(int playerObjId)
  114. {
  115. if (_playerInstanceTimes.containsKey(playerObjId))
  116. return; // already restored
  117. _playerInstanceTimes.put(playerObjId, new FastMap<Integer, Long>());
  118. Connection con = null;
  119. try
  120. {
  121. con = L2DatabaseFactory.getInstance().getConnection();
  122. PreparedStatement statement = con.prepareStatement(RESTORE_INSTANCE_TIMES);
  123. statement.setInt(1, playerObjId);
  124. ResultSet rset = statement.executeQuery();
  125. while (rset.next())
  126. {
  127. int id = rset.getInt("instanceId");
  128. long time = rset.getLong("time");
  129. if (time < System.currentTimeMillis())
  130. deleteInstanceTime(playerObjId, id);
  131. else
  132. _playerInstanceTimes.get(playerObjId).put(id, time);
  133. }
  134. rset.close();
  135. statement.close();
  136. }
  137. catch (Exception e)
  138. {
  139. _log.log(Level.WARNING, "Could not delete character instance time data: "+ e.getMessage(), e);
  140. }
  141. finally
  142. {
  143. L2DatabaseFactory.close(con);
  144. }
  145. }
  146. public String getInstanceIdName(int id)
  147. {
  148. if (_instanceIdNames.containsKey(id))
  149. return _instanceIdNames.get(id);
  150. return ("UnknownInstance");
  151. }
  152. private void loadInstanceNames()
  153. {
  154. InputStream in = null;
  155. try
  156. {
  157. in = new FileInputStream(Config.DATAPACK_ROOT + "/data/instancenames.xml");
  158. XMLStreamReaderImpl xpp = new XMLStreamReaderImpl();
  159. xpp.setInput(new UTF8StreamReader().setInput(in));
  160. for (int e = xpp.getEventType(); e != XMLStreamConstants.END_DOCUMENT; e = xpp.next())
  161. {
  162. if (e == XMLStreamConstants.START_ELEMENT)
  163. {
  164. if (xpp.getLocalName().toString().equals("instance"))
  165. {
  166. Integer id = Integer.valueOf(xpp.getAttributeValue(null, "id").toString());
  167. String name = xpp.getAttributeValue(null, "name").toString();
  168. _instanceIdNames.put(id, name);
  169. }
  170. }
  171. }
  172. }
  173. catch (FileNotFoundException e)
  174. {
  175. _log.warning("instancenames.xml could not be loaded: file not found");
  176. }
  177. catch (XMLStreamException xppe)
  178. {
  179. _log.log(Level.WARNING, "Error while loading instance names: " + xppe.getMessage(), xppe);
  180. }
  181. finally
  182. {
  183. try
  184. {
  185. in.close();
  186. }
  187. catch (Exception e)
  188. {
  189. }
  190. }
  191. }
  192. public static class InstanceWorld
  193. {
  194. public int instanceId;
  195. public int templateId = -1;
  196. public FastList<Integer> allowed = new FastList<Integer>();
  197. public volatile int status;
  198. }
  199. public void addWorld(InstanceWorld world)
  200. {
  201. _instanceWorlds.put(world.instanceId, world);
  202. }
  203. public InstanceWorld getWorld(int instanceId)
  204. {
  205. return _instanceWorlds.get(instanceId);
  206. }
  207. public InstanceWorld getPlayerWorld(L2PcInstance player)
  208. {
  209. for (InstanceWorld temp : _instanceWorlds.values())
  210. {
  211. if (temp == null)
  212. continue;
  213. // check if the player have a World Instance where he/she is allowed to enter
  214. if (temp.allowed.contains(player.getObjectId()))
  215. return temp;
  216. }
  217. return null;
  218. }
  219. private InstanceManager()
  220. {
  221. _log.info("Initializing InstanceManager");
  222. loadInstanceNames();
  223. _log.info("Loaded " + _instanceIdNames.size() + " instance names");
  224. createWorld();
  225. }
  226. public static final InstanceManager getInstance()
  227. {
  228. return SingletonHolder._instance;
  229. }
  230. private void createWorld()
  231. {
  232. Instance themultiverse = new Instance(-1);
  233. themultiverse.setName("multiverse");
  234. _instanceList.put(-1, themultiverse);
  235. _log.info("Multiverse Instance created");
  236. Instance universe = new Instance(0);
  237. universe.setName("universe");
  238. _instanceList.put(0, universe);
  239. _log.info("Universe Instance created");
  240. }
  241. public void destroyInstance(int instanceid)
  242. {
  243. if (instanceid <= 0)
  244. return;
  245. Instance temp = _instanceList.get(instanceid);
  246. if (temp != null)
  247. {
  248. temp.removeNpcs();
  249. temp.removePlayers();
  250. temp.removeDoors();
  251. temp.cancelTimer();
  252. _instanceList.remove(instanceid);
  253. if (_instanceWorlds.containsKey(instanceid))
  254. _instanceWorlds.remove(instanceid);
  255. }
  256. }
  257. public Instance getInstance(int instanceid)
  258. {
  259. return _instanceList.get(instanceid);
  260. }
  261. public FastMap<Integer, Instance> getInstances()
  262. {
  263. return _instanceList;
  264. }
  265. public int getPlayerInstance(int objectId)
  266. {
  267. for (Instance temp : _instanceList.values())
  268. {
  269. if (temp == null)
  270. continue;
  271. // check if the player is in any active instance
  272. if (temp.containsPlayer(objectId))
  273. return temp.getId();
  274. }
  275. // 0 is default instance aka the world
  276. return 0;
  277. }
  278. public boolean createInstance(int id)
  279. {
  280. if (getInstance(id) != null)
  281. return false;
  282. Instance instance = new Instance(id);
  283. _instanceList.put(id, instance);
  284. return true;
  285. }
  286. public boolean createInstanceFromTemplate(int id, String template)
  287. {
  288. if (getInstance(id) != null)
  289. return false;
  290. Instance instance = new Instance(id);
  291. _instanceList.put(id, instance);
  292. instance.loadInstanceTemplate(template);
  293. return true;
  294. }
  295. /**
  296. * Create a new instance with a dynamic instance id based on a template (or null)
  297. * @param template xml file
  298. * @return
  299. */
  300. public int createDynamicInstance(String template)
  301. {
  302. while (getInstance(_dynamic) != null)
  303. {
  304. _dynamic++;
  305. if (_dynamic == Integer.MAX_VALUE)
  306. {
  307. _log.warning("InstanceManager: More then " + (Integer.MAX_VALUE - 300000) + " instances created");
  308. _dynamic = 300000;
  309. }
  310. }
  311. Instance instance = new Instance(_dynamic);
  312. _instanceList.put(_dynamic, instance);
  313. if (template != null)
  314. {
  315. instance.loadInstanceTemplate(template);
  316. }
  317. return _dynamic;
  318. }
  319. @SuppressWarnings("synthetic-access")
  320. private static class SingletonHolder
  321. {
  322. protected static final InstanceManager _instance = new InstanceManager();
  323. }
  324. }