ExtractableItemsData.java 5.2 KB

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