StaticObjects.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. _staticObjects.clear();
  37. parseDatapackFile("data/staticObjects.xml");
  38. _log.info("StaticObject: Loaded " + _staticObjects.size() + " StaticObject Templates.");
  39. }
  40. @Override
  41. protected void parseDocument(Document doc)
  42. {
  43. NamedNodeMap attrs;
  44. Node att;
  45. StatsSet set;
  46. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  47. {
  48. if ("list".equalsIgnoreCase(n.getNodeName()))
  49. {
  50. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  51. {
  52. if ("object".equalsIgnoreCase(d.getNodeName()))
  53. {
  54. attrs = d.getAttributes();
  55. set = new StatsSet();
  56. for (int i = 0; i < attrs.getLength(); i++)
  57. {
  58. att = attrs.item(i);
  59. set.set(att.getNodeName(), att.getNodeValue());
  60. }
  61. addObject(set);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. /**
  68. * Initialize an static object based on the stats set and add it to the map.
  69. * @param set the stats set to add.
  70. */
  71. private void addObject(StatsSet set)
  72. {
  73. L2StaticObjectInstance obj = new L2StaticObjectInstance(IdFactory.getInstance().getNextId(), new L2CharTemplate(new StatsSet()), set.getInteger("id"));
  74. obj.setType(set.getInteger("type", 0));
  75. obj.setName(set.getString("name"));
  76. obj.setMap(set.getString("texture", "none"), set.getInteger("map_x", 0), set.getInteger("map_y", 0));
  77. obj.spawnMe(set.getInteger("x"), set.getInteger("y"), set.getInteger("z"));
  78. _staticObjects.put(obj.getObjectId(), obj);
  79. }
  80. /**
  81. * @return a collection of static objects.
  82. */
  83. public Collection<L2StaticObjectInstance> getStaticObjects()
  84. {
  85. return _staticObjects.values();
  86. }
  87. public static StaticObjects getInstance()
  88. {
  89. return SingletonHolder._instance;
  90. }
  91. private static class SingletonHolder
  92. {
  93. protected static final StaticObjects _instance = new StaticObjects();
  94. }
  95. }