InstanceManager.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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<L2PcInstance> allowed = new FastList<L2PcInstance>();
  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. // check if the player have a World Instance where he/she is allowed to enter
  51. if (temp.allowed.contains(player))
  52. return temp;
  53. }
  54. return null;
  55. }
  56. private InstanceManager()
  57. {
  58. _log.info("Initializing InstanceManager");
  59. createWorld();
  60. }
  61. public static final InstanceManager getInstance()
  62. {
  63. return SingletonHolder._instance;
  64. }
  65. private void createWorld()
  66. {
  67. Instance themultiverse = new Instance(-1);
  68. themultiverse.setName("multiverse");
  69. _instanceList.put(-1, themultiverse);
  70. _log.info("Multiverse Instance created");
  71. Instance universe = new Instance(0);
  72. universe.setName("universe");
  73. _instanceList.put(0, universe);
  74. _log.info("Universe Instance created");
  75. }
  76. public void destroyInstance(int instanceid)
  77. {
  78. if (instanceid <= 0)
  79. return;
  80. Instance temp = _instanceList.get(instanceid);
  81. if (temp != null)
  82. {
  83. temp.removeNpcs();
  84. temp.removePlayers();
  85. temp.removeDoors();
  86. temp.cancelTimer();
  87. _instanceList.remove(instanceid);
  88. if (_instanceWorlds.containsKey(instanceid))
  89. _instanceWorlds.remove(instanceid);
  90. }
  91. }
  92. public Instance getInstance(int instanceid)
  93. {
  94. return _instanceList.get(instanceid);
  95. }
  96. public FastMap<Integer, Instance> getInstances()
  97. {
  98. return _instanceList;
  99. }
  100. public int getPlayerInstance(int objectId)
  101. {
  102. for (Instance temp : _instanceList.values())
  103. {
  104. // check if the player is in any active instance
  105. if (temp.containsPlayer(objectId))
  106. return temp.getId();
  107. }
  108. // 0 is default instance aka the world
  109. return 0;
  110. }
  111. public boolean createInstance(int id)
  112. {
  113. if (getInstance(id) != null)
  114. return false;
  115. Instance instance = new Instance(id);
  116. _instanceList.put(id, instance);
  117. return true;
  118. }
  119. public boolean createInstanceFromTemplate(int id, String template) throws FileNotFoundException
  120. {
  121. if (getInstance(id) != null)
  122. return false;
  123. Instance instance = new Instance(id);
  124. _instanceList.put(id, instance);
  125. instance.loadInstanceTemplate(template);
  126. return true;
  127. }
  128. /**
  129. * Create a new instance with a dynamic instance id based on a template (or null)
  130. * @param template xml file
  131. * @return
  132. */
  133. public int createDynamicInstance(String template)
  134. {
  135. while (getInstance(_dynamic) != null)
  136. {
  137. _dynamic++;
  138. if (_dynamic == Integer.MAX_VALUE)
  139. {
  140. _log.warning("InstanceManager: More then " + (Integer.MAX_VALUE - 300000) + " instances created");
  141. _dynamic = 300000;
  142. }
  143. }
  144. Instance instance = new Instance(_dynamic);
  145. if (template != null)
  146. {
  147. try
  148. {
  149. instance.loadInstanceTemplate(template);
  150. }
  151. catch (FileNotFoundException e)
  152. {
  153. _log.warning("InstanceManager: Failed creating instance from template " + template + ", " + e.getMessage());
  154. e.printStackTrace();
  155. }
  156. }
  157. _instanceList.put(_dynamic, instance);
  158. return _dynamic;
  159. }
  160. @SuppressWarnings("synthetic-access")
  161. private static class SingletonHolder
  162. {
  163. protected static final InstanceManager _instance = new InstanceManager();
  164. }
  165. }