ExtractableItemsData.java 4.5 KB

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