StaticObjects.java 3.3 KB

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