StaticObjects.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 net.sf.l2j.gameserver.datatables;
  16. import java.io.BufferedReader;
  17. import java.io.File;
  18. import java.io.FileNotFoundException;
  19. import java.io.FileReader;
  20. import java.io.LineNumberReader;
  21. import java.util.Map;
  22. import java.util.StringTokenizer;
  23. import java.util.logging.Logger;
  24. import javolution.util.FastMap;
  25. import net.sf.l2j.Config;
  26. import net.sf.l2j.gameserver.idfactory.IdFactory;
  27. import net.sf.l2j.gameserver.model.actor.instance.L2StaticObjectInstance;
  28. public class StaticObjects
  29. {
  30. private static Logger _log = Logger.getLogger(StaticObjects.class.getName());
  31. private static StaticObjects _instance;
  32. private Map<Integer, L2StaticObjectInstance> _staticObjects;
  33. public static StaticObjects getInstance()
  34. {
  35. if (_instance == null)
  36. _instance = new StaticObjects();
  37. return _instance;
  38. }
  39. public StaticObjects()
  40. {
  41. _staticObjects = new FastMap<Integer, L2StaticObjectInstance>();
  42. parseData();
  43. _log.config("StaticObject: Loaded " + _staticObjects.size() + " StaticObject Templates.");
  44. }
  45. private void parseData()
  46. {
  47. LineNumberReader lnr = null;
  48. try
  49. {
  50. File doorData = new File(Config.DATAPACK_ROOT, "data/staticobjects.csv");
  51. lnr = new LineNumberReader(new BufferedReader(new FileReader(doorData)));
  52. String line = null;
  53. while ((line = lnr.readLine()) != null)
  54. {
  55. if (line.trim().length() == 0 || line.startsWith("#"))
  56. continue;
  57. L2StaticObjectInstance obj = parse(line);
  58. _staticObjects.put(obj.getStaticObjectId(), obj);
  59. }
  60. }
  61. catch (FileNotFoundException e)
  62. {
  63. _log.warning("staticobjects.csv is missing in data folder");
  64. }
  65. catch (Exception e)
  66. {
  67. _log.warning("error while creating StaticObjects table " + e);
  68. }
  69. finally
  70. {
  71. try
  72. {
  73. lnr.close();
  74. }
  75. catch (Exception e)
  76. {
  77. }
  78. }
  79. }
  80. public static L2StaticObjectInstance parse(String line)
  81. {
  82. StringTokenizer st = new StringTokenizer(line, ";");
  83. st.nextToken(); //Pass over static object name (not used in server)
  84. int id = Integer.parseInt(st.nextToken());
  85. int x = Integer.parseInt(st.nextToken());
  86. int y = Integer.parseInt(st.nextToken());
  87. int z = Integer.parseInt(st.nextToken());
  88. int type = Integer.parseInt(st.nextToken());
  89. String texture = st.nextToken();
  90. int map_x = Integer.parseInt(st.nextToken());
  91. int map_y = Integer.parseInt(st.nextToken());
  92. L2StaticObjectInstance obj = new L2StaticObjectInstance(IdFactory.getInstance().getNextId());
  93. obj.setType(type);
  94. obj.setStaticObjectId(id);
  95. obj.setXYZ(x, y, z);
  96. obj.setMap(texture, map_x, map_y);
  97. obj.spawnMe();
  98. return obj;
  99. }
  100. }