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