StaticObjects.java 3.4 KB

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