InstanceManager.java 3.9 KB

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