2
0

ExtractableItemsData.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /**
  16. *
  17. * @author FBIagent
  18. *
  19. */
  20. package net.sf.l2j.gameserver.datatables;
  21. import java.io.File;
  22. import java.util.Scanner;
  23. import java.util.logging.Logger;
  24. import javolution.util.FastList;
  25. import javolution.util.FastMap;
  26. import net.sf.l2j.Config;
  27. import net.sf.l2j.gameserver.model.L2ExtractableItem;
  28. import net.sf.l2j.gameserver.model.L2ExtractableProductItem;
  29. public class ExtractableItemsData
  30. {
  31. protected static final Logger _log = Logger.getLogger(ExtractableItemsData.class.getName());
  32. // Map<itemid, L2ExtractableItem>
  33. private FastMap<Integer, L2ExtractableItem> _items;
  34. private static ExtractableItemsData _instance = null;
  35. public static ExtractableItemsData getInstance()
  36. {
  37. if (_instance == null)
  38. _instance = new ExtractableItemsData();
  39. return _instance;
  40. }
  41. public ExtractableItemsData()
  42. {
  43. _items = new FastMap<Integer, L2ExtractableItem>();
  44. Scanner s;
  45. try
  46. {
  47. s = new Scanner(new File(Config.DATAPACK_ROOT + "/data/extractable_items.csv"));
  48. }
  49. catch (Exception e)
  50. {
  51. _log.warning("Extractable items data: Can not find '" + Config.DATAPACK_ROOT + "/data/extractable_items.csv'");
  52. return;
  53. }
  54. int lineCount = 0;
  55. while (s.hasNextLine())
  56. {
  57. lineCount++;
  58. String line = s.nextLine();
  59. if (line.startsWith("#"))
  60. continue;
  61. else if (line.equals(""))
  62. continue;
  63. String[] lineSplit = line.split(";");
  64. boolean ok = true;
  65. int itemID = 0;
  66. try
  67. {
  68. itemID = Integer.parseInt(lineSplit[0]);
  69. }
  70. catch (Exception e)
  71. {
  72. _log.warning("Extractable items data: Error in line " + lineCount + " -> invalid item id or wrong seperator after item id!");
  73. _log.warning(" " + line);
  74. ok = false;
  75. }
  76. if (!ok)
  77. continue;
  78. FastList<L2ExtractableProductItem> product_temp = new FastList<L2ExtractableProductItem>();
  79. for (int i = 0; i < lineSplit.length - 1; i++)
  80. {
  81. ok = true;
  82. String[] lineSplit2 = lineSplit[i + 1].split(",");
  83. if (lineSplit2.length != 3)
  84. {
  85. _log.warning("Extractable items data: Error in line " + lineCount + " -> wrong seperator!");
  86. _log.warning(" " + line);
  87. ok = false;
  88. }
  89. if (!ok)
  90. continue;
  91. int production = 0, amount = 0, chance = 0;
  92. try
  93. {
  94. production = Integer.parseInt(lineSplit2[0]);
  95. amount = Integer.parseInt(lineSplit2[1]);
  96. chance = Integer.parseInt(lineSplit2[2]);
  97. }
  98. catch (Exception e)
  99. {
  100. _log.warning("Extractable items data: Error in line " + lineCount + " -> incomplete/invalid production data or wrong seperator!");
  101. _log.warning(" " + line);
  102. ok = false;
  103. }
  104. if (!ok)
  105. continue;
  106. L2ExtractableProductItem product = new L2ExtractableProductItem(production, amount, chance);
  107. product_temp.add(product);
  108. }
  109. int fullChances = 0;
  110. for (L2ExtractableProductItem Pi : product_temp)
  111. fullChances += Pi.getChance();
  112. if (fullChances > 100)
  113. {
  114. _log.warning("Extractable items data: Error in line " + lineCount + " -> all chances together are more then 100!");
  115. _log.warning(" " + line);
  116. continue;
  117. }
  118. L2ExtractableItem product = new L2ExtractableItem(itemID, product_temp);
  119. _items.put(itemID, product);
  120. }
  121. s.close();
  122. _log.info("Extractable items data: Loaded " + _items.size() + " extractable items!");
  123. }
  124. public L2ExtractableItem getExtractableItem(int itemID)
  125. {
  126. return _items.get(itemID);
  127. }
  128. public int[] itemIDs()
  129. {
  130. int size = _items.size();
  131. int[] result = new int[size];
  132. int i = 0;
  133. for (L2ExtractableItem ei : _items.values())
  134. {
  135. result[i] = ei.getItemId();
  136. i++;
  137. }
  138. return result;
  139. }
  140. }