L2Manor.java 9.2 KB

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