InstanceManager.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.FileNotFoundException;
  17. import java.util.logging.Logger;
  18. import javolution.util.FastList;
  19. import javolution.util.FastMap;
  20. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  21. import net.sf.l2j.gameserver.model.entity.Instance;
  22. /**
  23. * @author evill33t, GodKratos
  24. *
  25. */
  26. public class InstanceManager
  27. {
  28. private final static Logger _log = Logger.getLogger(InstanceManager.class.getName());
  29. private FastMap<Integer, Instance> _instanceList = new FastMap<Integer, Instance>();
  30. private FastMap<Integer, InstanceWorld> _instanceWorlds = new FastMap<Integer, InstanceWorld>();
  31. private int _dynamic = 300000;
  32. public class InstanceWorld
  33. {
  34. public int instanceId;
  35. public FastList<Integer> allowed = new FastList<Integer>();
  36. public int status;
  37. }
  38. public void addWorld(InstanceWorld world)
  39. {
  40. _instanceWorlds.put(world.instanceId, world);
  41. }
  42. public InstanceWorld getWorld(int instanceId)
  43. {
  44. return _instanceWorlds.get(instanceId);
  45. }
  46. public InstanceWorld getPlayerWorld(L2PcInstance player)
  47. {
  48. for (InstanceWorld temp : _instanceWorlds.values())
  49. {
  50. if (temp == null)
  51. continue;
  52. // check if the player have a World Instance where he/she is allowed to enter
  53. if (temp.allowed.contains(player.getObjectId()))
  54. return temp;
  55. }
  56. return null;
  57. }
  58. private InstanceManager()
  59. {
  60. _log.info("Initializing InstanceManager");
  61. createWorld();
  62. }
  63. public static final InstanceManager getInstance()
  64. {
  65. return SingletonHolder._instance;
  66. }
  67. private void createWorld()
  68. {
  69. Instance themultiverse = new Instance(-1);
  70. themultiverse.setName("multiverse");
  71. _instanceList.put(-1, themultiverse);
  72. _log.info("Multiverse Instance created");
  73. Instance universe = new Instance(0);
  74. universe.setName("universe");
  75. _instanceList.put(0, universe);
  76. _log.info("Universe Instance created");
  77. }
  78. public void destroyInstance(int instanceid)
  79. {
  80. if (instanceid <= 0)
  81. return;
  82. Instance temp = _instanceList.get(instanceid);
  83. if (temp != null)
  84. {
  85. temp.removeNpcs();
  86. temp.removePlayers();
  87. temp.removeDoors();
  88. temp.cancelTimer();
  89. _instanceList.remove(instanceid);
  90. if (_instanceWorlds.containsKey(instanceid))
  91. _instanceWorlds.remove(instanceid);
  92. }
  93. }
  94. public Instance getInstance(int instanceid)
  95. {
  96. return _instanceList.get(instanceid);
  97. }
  98. public FastMap<Integer, Instance> getInstances()
  99. {
  100. return _instanceList;
  101. }
  102. public int getPlayerInstance(int objectId)
  103. {
  104. for (Instance temp : _instanceList.values())
  105. {
  106. if (temp == null)
  107. continue;
  108. // check if the player is in any active instance
  109. if (temp.containsPlayer(objectId))
  110. return temp.getId();
  111. }
  112. // 0 is default instance aka the world
  113. return 0;
  114. }
  115. public boolean createInstance(int id)
  116. {
  117. if (getInstance(id) != null)
  118. return false;
  119. Instance instance = new Instance(id);
  120. _instanceList.put(id, instance);
  121. return true;
  122. }
  123. public boolean createInstanceFromTemplate(int id, String template) throws FileNotFoundException
  124. {
  125. if (getInstance(id) != null)
  126. return false;
  127. Instance instance = new Instance(id);
  128. _instanceList.put(id, instance);
  129. instance.loadInstanceTemplate(template);
  130. return true;
  131. }
  132. /**
  133. * Create a new instance with a dynamic instance id based on a template (or null)
  134. * @param template xml file
  135. * @return
  136. */
  137. public int createDynamicInstance(String template)
  138. {
  139. while (getInstance(_dynamic) != null)
  140. {
  141. _dynamic++;
  142. if (_dynamic == Integer.MAX_VALUE)
  143. {
  144. _log.warning("InstanceManager: More then " + (Integer.MAX_VALUE - 300000) + " instances created");
  145. _dynamic = 300000;
  146. }
  147. }
  148. Instance instance = new Instance(_dynamic);
  149. _instanceList.put(_dynamic, instance);
  150. if (template != null)
  151. {
  152. try
  153. {
  154. instance.loadInstanceTemplate(template);
  155. }
  156. catch (FileNotFoundException e)
  157. {
  158. _log.warning("InstanceManager: Failed creating instance from template " + template + ", " + e.getMessage());
  159. e.printStackTrace();
  160. }
  161. }
  162. return _dynamic;
  163. }
  164. @SuppressWarnings("synthetic-access")
  165. private static class SingletonHolder
  166. {
  167. protected static final InstanceManager _instance = new InstanceManager();
  168. }
  169. }