StaticObjects.java 3.5 KB

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