InstanceManager.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 FastList<Integer> allowed = new FastList<Integer>();
  185. public int status;
  186. }
  187. public void addWorld(InstanceWorld world)
  188. {
  189. _instanceWorlds.put(world.instanceId, world);
  190. }
  191. public InstanceWorld getWorld(int instanceId)
  192. {
  193. return _instanceWorlds.get(instanceId);
  194. }
  195. public InstanceWorld getPlayerWorld(L2PcInstance player)
  196. {
  197. for (InstanceWorld temp : _instanceWorlds.values())
  198. {
  199. if (temp == null)
  200. continue;
  201. // check if the player have a World Instance where he/she is allowed to enter
  202. if (temp.allowed.contains(player.getObjectId()))
  203. return temp;
  204. }
  205. return null;
  206. }
  207. private InstanceManager()
  208. {
  209. _log.info("Initializing InstanceManager");
  210. loadInstanceNames();
  211. _log.info("Loaded " + _instanceIdNames.size() + " instance names");
  212. createWorld();
  213. }
  214. public static final InstanceManager getInstance()
  215. {
  216. return SingletonHolder._instance;
  217. }
  218. private void createWorld()
  219. {
  220. Instance themultiverse = new Instance(-1);
  221. themultiverse.setName("multiverse");
  222. _instanceList.put(-1, themultiverse);
  223. _log.info("Multiverse Instance created");
  224. Instance universe = new Instance(0);
  225. universe.setName("universe");
  226. _instanceList.put(0, universe);
  227. _log.info("Universe Instance created");
  228. }
  229. public void destroyInstance(int instanceid)
  230. {
  231. if (instanceid <= 0)
  232. return;
  233. Instance temp = _instanceList.get(instanceid);
  234. if (temp != null)
  235. {
  236. temp.removeNpcs();
  237. temp.removePlayers();
  238. temp.removeDoors();
  239. temp.cancelTimer();
  240. _instanceList.remove(instanceid);
  241. if (_instanceWorlds.containsKey(instanceid))
  242. _instanceWorlds.remove(instanceid);
  243. }
  244. }
  245. public Instance getInstance(int instanceid)
  246. {
  247. return _instanceList.get(instanceid);
  248. }
  249. public FastMap<Integer, Instance> getInstances()
  250. {
  251. return _instanceList;
  252. }
  253. public int getPlayerInstance(int objectId)
  254. {
  255. for (Instance temp : _instanceList.values())
  256. {
  257. if (temp == null)
  258. continue;
  259. // check if the player is in any active instance
  260. if (temp.containsPlayer(objectId))
  261. return temp.getId();
  262. }
  263. // 0 is default instance aka the world
  264. return 0;
  265. }
  266. public boolean createInstance(int id)
  267. {
  268. if (getInstance(id) != null)
  269. return false;
  270. Instance instance = new Instance(id);
  271. _instanceList.put(id, instance);
  272. return true;
  273. }
  274. public boolean createInstanceFromTemplate(int id, String template) throws FileNotFoundException
  275. {
  276. if (getInstance(id) != null)
  277. return false;
  278. Instance instance = new Instance(id);
  279. _instanceList.put(id, instance);
  280. instance.loadInstanceTemplate(template);
  281. return true;
  282. }
  283. /**
  284. * Create a new instance with a dynamic instance id based on a template (or null)
  285. * @param template xml file
  286. * @return
  287. */
  288. public int createDynamicInstance(String template)
  289. {
  290. while (getInstance(_dynamic) != null)
  291. {
  292. _dynamic++;
  293. if (_dynamic == Integer.MAX_VALUE)
  294. {
  295. _log.warning("InstanceManager: More then " + (Integer.MAX_VALUE - 300000) + " instances created");
  296. _dynamic = 300000;
  297. }
  298. }
  299. Instance instance = new Instance(_dynamic);
  300. _instanceList.put(_dynamic, instance);
  301. if (template != null)
  302. {
  303. try
  304. {
  305. instance.loadInstanceTemplate(template);
  306. }
  307. catch (FileNotFoundException e)
  308. {
  309. _log.warning("InstanceManager: Failed creating instance from template " + template + ", " + e.getMessage());
  310. e.printStackTrace();
  311. }
  312. }
  313. return _dynamic;
  314. }
  315. @SuppressWarnings("synthetic-access")
  316. private static class SingletonHolder
  317. {
  318. protected static final InstanceManager _instance = new InstanceManager();
  319. }
  320. }