InstanceManager.java 10 KB

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