InstanceManager.java 10 KB

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