L2Manor.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 net.sf.l2j.gameserver.model;
  16. import java.io.BufferedReader;
  17. import java.io.File;
  18. import java.io.FileNotFoundException;
  19. import java.io.FileReader;
  20. import java.io.LineNumberReader;
  21. import java.util.StringTokenizer;
  22. import java.util.logging.Logger;
  23. import javolution.util.FastList;
  24. import javolution.util.FastMap;
  25. import net.sf.l2j.Config;
  26. import net.sf.l2j.gameserver.datatables.ItemTable;
  27. import net.sf.l2j.gameserver.templates.item.L2Item;
  28. /**
  29. * Service class for manor
  30. * @author l3x
  31. */
  32. public class L2Manor
  33. {
  34. private static Logger _log = Logger.getLogger(L2Manor.class.getName());
  35. private static L2Manor _instance;
  36. private static FastMap<Integer,SeedData> _seeds;
  37. public L2Manor() {
  38. _seeds = new FastMap<Integer, SeedData>().setShared(true);
  39. parseData();
  40. }
  41. public static L2Manor getInstance() {
  42. if (_instance == null) {
  43. _instance = new L2Manor();
  44. }
  45. return _instance;
  46. }
  47. public FastList<Integer> getAllCrops() {
  48. FastList<Integer> crops = new FastList<Integer>();
  49. for (SeedData seed: _seeds.values()) {
  50. if (!crops.contains(seed.getCrop()) && seed.getCrop() != 0 && !crops.contains(seed.getCrop())) {
  51. crops.add(seed.getCrop());
  52. }
  53. }
  54. return crops;
  55. }
  56. public int getSeedBasicPrice (int seedId) {
  57. L2Item seedItem = ItemTable.getInstance().getTemplate(seedId);
  58. if (seedItem != null) {
  59. return seedItem.getReferencePrice();
  60. } else {
  61. return 0;
  62. }
  63. }
  64. public int getSeedBasicPriceByCrop (int cropId) {
  65. for (SeedData seed: _seeds.values()) {
  66. if (seed.getCrop() == cropId)
  67. return getSeedBasicPrice(seed.getId());
  68. }
  69. return 0;
  70. }
  71. public int getCropBasicPrice (int cropId) {
  72. L2Item cropItem = ItemTable.getInstance().getTemplate(cropId);
  73. if (cropItem != null)
  74. return cropItem.getReferencePrice();
  75. else
  76. return 0;
  77. }
  78. public int getMatureCrop (int cropId) {
  79. for (SeedData seed: _seeds.values()) {
  80. if (seed.getCrop() == cropId)
  81. return seed.getMature();
  82. }
  83. return 0;
  84. }
  85. /**
  86. * Returns price which lord pays to buy one seed
  87. * @param seedId
  88. * @return seed price
  89. */
  90. public long getSeedBuyPrice (int seedId) {
  91. long buyPrice = getSeedBasicPrice(seedId) / 10;
  92. return (buyPrice > 0?buyPrice:1);
  93. }
  94. public int getSeedMinLevel(int seedId)
  95. {
  96. SeedData seed = _seeds.get(seedId);
  97. if (seed != null)
  98. return seed.getLevel() - 5;
  99. return -1;
  100. }
  101. public int getSeedMaxLevel(int seedId) {
  102. SeedData seed = _seeds.get(seedId);
  103. if (seed != null)
  104. return seed.getLevel() + 5;
  105. return -1;
  106. }
  107. public int getSeedLevelByCrop (int cropId) {
  108. for (SeedData seed: _seeds.values()) {
  109. if (seed.getCrop() == cropId) {
  110. return seed.getLevel();
  111. }
  112. }
  113. return 0;
  114. }
  115. public int getSeedLevel(int seedId) {
  116. SeedData seed = _seeds.get(seedId);
  117. if (seed != null) {
  118. return seed.getLevel();
  119. }
  120. return -1;
  121. }
  122. public boolean isAlternative(int seedId) {
  123. for (SeedData seed: _seeds.values()) {
  124. if (seed.getId() == seedId) {
  125. return seed.isAlternative();
  126. }
  127. }
  128. return false;
  129. }
  130. public int getCropType(int seedId) {
  131. SeedData seed = _seeds.get(seedId);
  132. if (seed != null)
  133. return seed.getCrop();
  134. return -1;
  135. }
  136. public synchronized int getRewardItem(int cropId, int type) {
  137. for (SeedData seed : _seeds.values()) {
  138. if (seed.getCrop() == cropId) {
  139. return seed.getReward(type); // there can be several
  140. // seeds with same crop, but
  141. // reward should be the same for
  142. // all
  143. }
  144. }
  145. return -1;
  146. }
  147. public synchronized int getRewardItemBySeed (int seedId, int type) {
  148. SeedData seed = _seeds.get(seedId);
  149. if (seed != null) {
  150. return seed.getReward(type);
  151. }
  152. return 0;
  153. }
  154. /**
  155. * Return all crops which can be purchased by given castle
  156. *
  157. * @param castleId
  158. * @return
  159. */
  160. public FastList<Integer> getCropsForCastle(int castleId) {
  161. FastList<Integer> crops = new FastList<Integer>();
  162. for (SeedData seed : _seeds.values()) {
  163. if (seed.getManorId() == castleId && !crops.contains(seed.getCrop())) {
  164. crops.add(seed.getCrop());
  165. }
  166. }
  167. return crops;
  168. }
  169. /**
  170. * Return list of seed ids, which belongs to castle with given id
  171. * @param castleId - id of the castle
  172. * @return seedIds - list of seed ids
  173. */
  174. public FastList<Integer> getSeedsForCastle(int castleId) {
  175. FastList<Integer> seedsID = new FastList<Integer>();
  176. for (SeedData seed : _seeds.values()) {
  177. if (seed.getManorId() == castleId && !seedsID.contains(seed.getId())) {
  178. seedsID.add(seed.getId());
  179. }
  180. }
  181. return seedsID;
  182. }
  183. /**
  184. * Returns castle id where seed can be sowned<br>
  185. * @param seedId
  186. * @return castleId
  187. */
  188. public int getCastleIdForSeed(int seedId) {
  189. SeedData seed = _seeds.get(seedId);
  190. if (seed != null) {
  191. return seed.getManorId();
  192. }
  193. return 0;
  194. }
  195. public int getSeedSaleLimit(int seedId) {
  196. SeedData seed = _seeds.get(seedId);
  197. if (seed != null) {
  198. return seed.getSeedLimit();
  199. }
  200. return 0;
  201. }
  202. public int getCropPuchaseLimit(int cropId) {
  203. for (SeedData seed : _seeds.values()) {
  204. if (seed.getCrop() == cropId){
  205. return seed.getCropLimit();
  206. }
  207. }
  208. return 0;
  209. }
  210. private class SeedData {
  211. private int _id;
  212. private int _level; // seed level
  213. private int _crop; // crop type
  214. private int _mature; // mature crop type
  215. private int _type1;
  216. private int _type2;
  217. private int _manorId; // id of manor (castle id) where seed can be farmed
  218. private int _isAlternative;
  219. private int _limitSeeds;
  220. private int _limitCrops;
  221. public SeedData(int level ,int crop, int mature) {
  222. this._level = level;
  223. this._crop = crop;
  224. this._mature = mature;
  225. }
  226. public void setData(int id, int t1, int t2, int manorId, int isAlt, int lim1, int lim2) {
  227. this._id = id;
  228. _type1 = t1;
  229. _type2 = t2;
  230. _manorId = manorId;
  231. _isAlternative = isAlt;
  232. _limitSeeds = lim1;
  233. _limitCrops = lim2;
  234. }
  235. public int getManorId () {
  236. return _manorId;
  237. }
  238. public int getId() {
  239. return _id;
  240. }
  241. public int getCrop() {
  242. return _crop;
  243. }
  244. public int getMature() {
  245. return _mature;
  246. }
  247. public int getReward(int type) {
  248. return (type == 1?_type1:_type2);
  249. }
  250. public int getLevel() {
  251. return _level;
  252. }
  253. public boolean isAlternative() {
  254. return (_isAlternative == 1);
  255. }
  256. public int getSeedLimit() {
  257. return _limitSeeds*Config.RATE_DROP_MANOR;
  258. }
  259. public int getCropLimit() {
  260. return _limitCrops*Config.RATE_DROP_MANOR;
  261. }
  262. }
  263. private void parseData() {
  264. LineNumberReader lnr = null;
  265. try {
  266. File seedData = new File(Config.DATAPACK_ROOT, "data/seeds.csv");
  267. lnr = new LineNumberReader(new BufferedReader(new FileReader(
  268. seedData)));
  269. String line = null;
  270. while ((line = lnr.readLine()) != null) {
  271. if (line.trim().length() == 0 || line.startsWith("#")) {
  272. continue;
  273. }
  274. SeedData seed = parseList(line);
  275. _seeds.put(seed.getId(), seed);
  276. }
  277. _log.info("ManorManager: Loaded " + _seeds.size() + " seeds");
  278. } catch (FileNotFoundException e) {
  279. _log.info("seeds.csv is missing in data folder");
  280. } catch (Exception e) {
  281. _log.info("error while loading seeds: " + e.getMessage());
  282. } finally {
  283. try {
  284. lnr.close();
  285. } catch (Exception e1) {
  286. }
  287. }
  288. }
  289. private SeedData parseList(String line) {
  290. StringTokenizer st = new StringTokenizer(line, ";");
  291. int seedId = Integer.parseInt(st.nextToken()); // seed id
  292. int level = Integer.parseInt(st.nextToken()); // seed level
  293. int cropId = Integer.parseInt(st.nextToken()); // crop id
  294. int matureId = Integer.parseInt(st.nextToken()); // mature crop id
  295. int type1R = Integer.parseInt(st.nextToken()); // type I reward
  296. int type2R = Integer.parseInt(st.nextToken()); // type II reward
  297. int manorId = Integer.parseInt(st.nextToken()); // id of manor, where seed can be farmed
  298. int isAlt = Integer.parseInt(st.nextToken()); // alternative seed
  299. int limitSeeds = Integer.parseInt(st.nextToken()); // limit for seeds
  300. int limitCrops = Integer.parseInt(st.nextToken()); // limit for crops
  301. SeedData seed = new SeedData(level, cropId, matureId);
  302. seed.setData(seedId, type1R, type2R, manorId, isAlt, limitSeeds, limitCrops);
  303. return seed;
  304. }
  305. }