SummonItemsData.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.HashMap;
  18. import java.util.Map;
  19. import java.util.Scanner;
  20. import java.util.logging.Logger;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.model.L2SummonItem;
  23. /**
  24. * @author FBIagent
  25. */
  26. public class SummonItemsData
  27. {
  28. protected static final Logger _log = Logger.getLogger(SummonItemsData.class.getName());
  29. private final Map<Integer, L2SummonItem> _summonitems = new HashMap<>();
  30. public static SummonItemsData getInstance()
  31. {
  32. return SingletonHolder._instance;
  33. }
  34. protected SummonItemsData()
  35. {
  36. try (Scanner s = new Scanner(new File(Config.DATAPACK_ROOT + "/data/summon_items.csv")))
  37. {
  38. int lineCount = 0;
  39. while (s.hasNextLine())
  40. {
  41. lineCount++;
  42. String line = s.nextLine();
  43. if (line.startsWith("#"))
  44. {
  45. continue;
  46. }
  47. else if (line.isEmpty())
  48. {
  49. continue;
  50. }
  51. String[] lineSplit = line.split(";");
  52. boolean ok = true;
  53. int itemID = 0, npcID = 0;
  54. byte summonType = 0;
  55. int despawn = -1;
  56. try
  57. {
  58. itemID = Integer.parseInt(lineSplit[0]);
  59. npcID = Integer.parseInt(lineSplit[1]);
  60. summonType = Byte.parseByte(lineSplit[2]);
  61. if (summonType == 0)
  62. {
  63. despawn = Integer.parseInt(lineSplit[3]);
  64. }
  65. }
  66. catch (Exception e)
  67. {
  68. _log.warning(getClass().getSimpleName() + ": Error in line " + lineCount + " -> incomplete/invalid data or wrong seperator!");
  69. _log.warning(" " + line);
  70. ok = false;
  71. }
  72. if (!ok)
  73. {
  74. continue;
  75. }
  76. L2SummonItem summonitem = new L2SummonItem(itemID, npcID, summonType, despawn);
  77. _summonitems.put(itemID, summonitem);
  78. }
  79. _log.info(getClass().getSimpleName() + ": Loaded " + _summonitems.size() + " summon items.");
  80. }
  81. catch (Exception e)
  82. {
  83. _log.warning(getClass().getSimpleName() + ": Can not find '" + Config.DATAPACK_ROOT + "/data/summon_items.csv'");
  84. return;
  85. }
  86. }
  87. public L2SummonItem getSummonItem(int itemId)
  88. {
  89. return _summonitems.get(itemId);
  90. }
  91. public int[] itemIDs()
  92. {
  93. int size = _summonitems.size();
  94. int[] result = new int[size];
  95. int i = 0;
  96. for (L2SummonItem si : _summonitems.values())
  97. {
  98. result[i++] = si.getItemId();
  99. }
  100. return result;
  101. }
  102. private static class SingletonHolder
  103. {
  104. protected static final SummonItemsData _instance = new SummonItemsData();
  105. }
  106. }