StaticObjects.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.datatables;
  16. import java.util.Collection;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import org.w3c.dom.NamedNodeMap;
  20. import org.w3c.dom.Node;
  21. import com.l2jserver.gameserver.engines.DocumentParser;
  22. import com.l2jserver.gameserver.idfactory.IdFactory;
  23. import com.l2jserver.gameserver.model.StatsSet;
  24. import com.l2jserver.gameserver.model.actor.instance.L2StaticObjectInstance;
  25. import com.l2jserver.gameserver.model.actor.templates.L2CharTemplate;
  26. /**
  27. * This class loads and holds all static object data.
  28. * @author UnAfraid
  29. */
  30. public final class StaticObjects extends DocumentParser
  31. {
  32. private static final Map<Integer, L2StaticObjectInstance> _staticObjects = new HashMap<>();
  33. /**
  34. * Instantiates a new static objects.
  35. */
  36. protected StaticObjects()
  37. {
  38. load();
  39. }
  40. @Override
  41. public void load()
  42. {
  43. _staticObjects.clear();
  44. parseDatapackFile("data/staticObjects.xml");
  45. _log.info("StaticObject: Loaded " + _staticObjects.size() + " StaticObject Templates.");
  46. }
  47. @Override
  48. protected void parseDocument()
  49. {
  50. NamedNodeMap attrs;
  51. Node att;
  52. StatsSet set;
  53. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  54. {
  55. if ("list".equalsIgnoreCase(n.getNodeName()))
  56. {
  57. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  58. {
  59. if ("object".equalsIgnoreCase(d.getNodeName()))
  60. {
  61. attrs = d.getAttributes();
  62. set = new StatsSet();
  63. for (int i = 0; i < attrs.getLength(); i++)
  64. {
  65. att = attrs.item(i);
  66. set.set(att.getNodeName(), att.getNodeValue());
  67. }
  68. addObject(set);
  69. }
  70. }
  71. }
  72. }
  73. }
  74. /**
  75. * Initialize an static object based on the stats set and add it to the map.
  76. * @param set the stats set to add.
  77. */
  78. private void addObject(StatsSet set)
  79. {
  80. L2StaticObjectInstance obj = new L2StaticObjectInstance(IdFactory.getInstance().getNextId(), new L2CharTemplate(new StatsSet()), set.getInteger("id"));
  81. obj.setType(set.getInteger("type", 0));
  82. obj.setName(set.getString("name"));
  83. obj.setMap(set.getString("texture", "none"), set.getInteger("map_x", 0), set.getInteger("map_y", 0));
  84. obj.spawnMe(set.getInteger("x"), set.getInteger("y"), set.getInteger("z"));
  85. _staticObjects.put(obj.getObjectId(), obj);
  86. }
  87. /**
  88. * Gets the static objects.
  89. * @return a collection of static objects.
  90. */
  91. public Collection<L2StaticObjectInstance> getStaticObjects()
  92. {
  93. return _staticObjects.values();
  94. }
  95. /**
  96. * Gets the single instance of StaticObjects.
  97. * @return single instance of StaticObjects
  98. */
  99. public static StaticObjects getInstance()
  100. {
  101. return SingletonHolder._instance;
  102. }
  103. private static class SingletonHolder
  104. {
  105. protected static final StaticObjects _instance = new StaticObjects();
  106. }
  107. }