StaticObjectData.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.data.xml.impl;
  20. import java.util.Collection;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  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. import com.l2jserver.util.data.xml.IXmlReader;
  30. /**
  31. * This class loads and holds all static object data.
  32. * @author UnAfraid
  33. */
  34. public final class StaticObjectData implements IXmlReader
  35. {
  36. private final Map<Integer, L2StaticObjectInstance> _staticObjects = new HashMap<>();
  37. /**
  38. * Instantiates a new static objects.
  39. */
  40. protected StaticObjectData()
  41. {
  42. load();
  43. }
  44. @Override
  45. public void load()
  46. {
  47. _staticObjects.clear();
  48. parseDatapackFile("data/staticObjects.xml");
  49. LOGGER.info("{}: Loaded {} static object templates.", getClass().getSimpleName(), _staticObjects.size());
  50. }
  51. @Override
  52. public void parseDocument(Document doc)
  53. {
  54. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  55. {
  56. if ("list".equalsIgnoreCase(n.getNodeName()))
  57. {
  58. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  59. {
  60. if ("object".equalsIgnoreCase(d.getNodeName()))
  61. {
  62. final NamedNodeMap attrs = d.getAttributes();
  63. final StatsSet set = new StatsSet();
  64. for (int i = 0; i < attrs.getLength(); i++)
  65. {
  66. final Node att = attrs.item(i);
  67. set.set(att.getNodeName(), att.getNodeValue());
  68. }
  69. addObject(set);
  70. }
  71. }
  72. }
  73. }
  74. }
  75. /**
  76. * Initialize an static object based on the stats set and add it to the map.
  77. * @param set the stats set to add.
  78. */
  79. private void addObject(StatsSet set)
  80. {
  81. L2StaticObjectInstance obj = new L2StaticObjectInstance(new L2CharTemplate(new StatsSet()), set.getInt("id"));
  82. obj.setType(set.getInt("type", 0));
  83. obj.setName(set.getString("name"));
  84. obj.setMap(set.getString("texture", "none"), set.getInt("map_x", 0), set.getInt("map_y", 0));
  85. obj.spawnMe(set.getInt("x"), set.getInt("y"), set.getInt("z"));
  86. _staticObjects.put(obj.getObjectId(), obj);
  87. }
  88. /**
  89. * Gets the static objects.
  90. * @return a collection of static objects.
  91. */
  92. public Collection<L2StaticObjectInstance> getStaticObjects()
  93. {
  94. return _staticObjects.values();
  95. }
  96. /**
  97. * Gets the single instance of StaticObjects.
  98. * @return single instance of StaticObjects
  99. */
  100. public static StaticObjectData getInstance()
  101. {
  102. return SingletonHolder._instance;
  103. }
  104. private static class SingletonHolder
  105. {
  106. protected static final StaticObjectData _instance = new StaticObjectData();
  107. }
  108. }