L2Manor.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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.File;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import javax.xml.parsers.DocumentBuilderFactory;
  20. import javolution.util.FastList;
  21. import org.w3c.dom.Document;
  22. import org.w3c.dom.Node;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.datatables.ItemTable;
  25. import com.l2jserver.gameserver.templates.item.L2Item;
  26. import com.l2jserver.gameserver.util.L2TIntObjectHashMap;
  27. /**
  28. * Service class for manor
  29. * @author l3x
  30. */
  31. public class L2Manor
  32. {
  33. private static Logger _log = Logger.getLogger(L2Manor.class.getName());
  34. private static L2TIntObjectHashMap<SeedData> _seeds;
  35. private L2Manor()
  36. {
  37. _seeds = new L2TIntObjectHashMap<SeedData>();
  38. parseData();
  39. }
  40. public static L2Manor getInstance()
  41. {
  42. return SingletonHolder._instance;
  43. }
  44. public SeedData[] getSeedsDataArray()
  45. {
  46. return _seeds.values(new SeedData[_seeds.size()]);
  47. }
  48. public FastList<Integer> getAllCrops()
  49. {
  50. FastList<Integer> crops = new FastList<Integer>();
  51. for (SeedData seed : getSeedsDataArray())
  52. {
  53. if (!crops.contains(seed.getCrop()) && seed.getCrop() != 0 && !crops.contains(seed.getCrop()))
  54. {
  55. crops.add(seed.getCrop());
  56. }
  57. }
  58. return crops;
  59. }
  60. public int getSeedBasicPrice(int seedId)
  61. {
  62. final L2Item seedItem = ItemTable.getInstance().getTemplate(seedId);
  63. if (seedItem != null)
  64. {
  65. return seedItem.getReferencePrice();
  66. }
  67. return 0;
  68. }
  69. public int getSeedBasicPriceByCrop(int cropId)
  70. {
  71. for (SeedData seed : getSeedsDataArray())
  72. {
  73. if (seed.getCrop() == cropId)
  74. return getSeedBasicPrice(seed.getId());
  75. }
  76. return 0;
  77. }
  78. public int getCropBasicPrice(int cropId)
  79. {
  80. final L2Item cropItem = ItemTable.getInstance().getTemplate(cropId);
  81. if (cropItem != null)
  82. return cropItem.getReferencePrice();
  83. return 0;
  84. }
  85. public int getMatureCrop(int cropId)
  86. {
  87. for (SeedData seed : getSeedsDataArray())
  88. {
  89. if (seed.getCrop() == cropId)
  90. return seed.getMature();
  91. }
  92. return 0;
  93. }
  94. /**
  95. * Returns price which lord pays to buy one seed
  96. * @param seedId
  97. * @return seed price
  98. */
  99. public long getSeedBuyPrice(int seedId)
  100. {
  101. long buyPrice = getSeedBasicPrice(seedId);
  102. return (buyPrice > 0 ? buyPrice : 1);
  103. }
  104. public int getSeedMinLevel(int seedId)
  105. {
  106. SeedData seed = _seeds.get(seedId);
  107. if (seed != null)
  108. return seed.getLevel() - 5;
  109. return -1;
  110. }
  111. public int getSeedMaxLevel(int seedId)
  112. {
  113. SeedData seed = _seeds.get(seedId);
  114. if (seed != null)
  115. return seed.getLevel() + 5;
  116. return -1;
  117. }
  118. public int getSeedLevelByCrop(int cropId)
  119. {
  120. for (SeedData seed : getSeedsDataArray())
  121. {
  122. if (seed.getCrop() == cropId)
  123. {
  124. return seed.getLevel();
  125. }
  126. }
  127. return 0;
  128. }
  129. public int getSeedLevel(int seedId)
  130. {
  131. SeedData seed = _seeds.get(seedId);
  132. if (seed != null)
  133. {
  134. return seed.getLevel();
  135. }
  136. return -1;
  137. }
  138. public boolean isAlternative(int seedId)
  139. {
  140. for (SeedData seed : getSeedsDataArray())
  141. {
  142. if (seed.getId() == seedId)
  143. {
  144. return seed.isAlternative();
  145. }
  146. }
  147. return false;
  148. }
  149. public int getCropType(int seedId)
  150. {
  151. SeedData seed = _seeds.get(seedId);
  152. if (seed != null)
  153. return seed.getCrop();
  154. return -1;
  155. }
  156. public int getRewardItem(int cropId, int type)
  157. {
  158. for (SeedData seed : getSeedsDataArray())
  159. {
  160. if (seed.getCrop() == cropId)
  161. {
  162. return seed.getReward(type); // there can be several
  163. // seeds with same crop, but
  164. // reward should be the same for
  165. // all
  166. }
  167. }
  168. return -1;
  169. }
  170. public int getRewardItemBySeed(int seedId, int type)
  171. {
  172. SeedData seed = _seeds.get(seedId);
  173. if (seed != null)
  174. {
  175. return seed.getReward(type);
  176. }
  177. return 0;
  178. }
  179. /**
  180. * Return all crops which can be purchased by given castle
  181. *
  182. * @param castleId
  183. * @return
  184. */
  185. public FastList<Integer> getCropsForCastle(int castleId)
  186. {
  187. FastList<Integer> crops = new FastList<Integer>();
  188. for (SeedData seed : getSeedsDataArray())
  189. {
  190. if (seed.getManorId() == castleId && !crops.contains(seed.getCrop()))
  191. {
  192. crops.add(seed.getCrop());
  193. }
  194. }
  195. return crops;
  196. }
  197. /**
  198. * Return list of seed ids, which belongs to castle with given id
  199. * @param castleId - id of the castle
  200. * @return seedIds - list of seed ids
  201. */
  202. public FastList<Integer> getSeedsForCastle(int castleId)
  203. {
  204. FastList<Integer> seedsID = new FastList<Integer>();
  205. for (SeedData seed : getSeedsDataArray())
  206. {
  207. if (seed.getManorId() == castleId && !seedsID.contains(seed.getId()))
  208. {
  209. seedsID.add(seed.getId());
  210. }
  211. }
  212. return seedsID;
  213. }
  214. /**
  215. * Returns castle id where seed can be sowned<br>
  216. * @param seedId
  217. * @return castleId
  218. */
  219. public int getCastleIdForSeed(int seedId)
  220. {
  221. SeedData seed = _seeds.get(seedId);
  222. if (seed != null)
  223. {
  224. return seed.getManorId();
  225. }
  226. return 0;
  227. }
  228. public int getSeedSaleLimit(int seedId)
  229. {
  230. SeedData seed = _seeds.get(seedId);
  231. if (seed != null)
  232. {
  233. return seed.getSeedLimit();
  234. }
  235. return 0;
  236. }
  237. public int getCropPuchaseLimit(int cropId)
  238. {
  239. for (SeedData seed : getSeedsDataArray())
  240. {
  241. if (seed.getCrop() == cropId)
  242. {
  243. return seed.getCropLimit();
  244. }
  245. }
  246. return 0;
  247. }
  248. private static class SeedData
  249. {
  250. private int _id;
  251. private int _level; // seed level
  252. private int _crop; // crop type
  253. private int _mature; // mature crop type
  254. private int _type1;
  255. private int _type2;
  256. private int _manorId; // id of manor (castle id) where seed can be farmed
  257. private boolean _isAlternative;
  258. private int _limitSeeds;
  259. private int _limitCrops;
  260. public SeedData(int level, int crop, int mature)
  261. {
  262. this._level = level;
  263. this._crop = crop;
  264. this._mature = mature;
  265. }
  266. public void setData(int id, int t1, int t2, int manorId, boolean isAlt, int lim1, int lim2)
  267. {
  268. this._id = id;
  269. _type1 = t1;
  270. _type2 = t2;
  271. _manorId = manorId;
  272. _isAlternative = isAlt;
  273. _limitSeeds = lim1;
  274. _limitCrops = lim2;
  275. }
  276. public int getManorId()
  277. {
  278. return _manorId;
  279. }
  280. public int getId()
  281. {
  282. return _id;
  283. }
  284. public int getCrop()
  285. {
  286. return _crop;
  287. }
  288. public int getMature()
  289. {
  290. return _mature;
  291. }
  292. public int getReward(int type)
  293. {
  294. return (type == 1 ? _type1 : _type2);
  295. }
  296. public int getLevel()
  297. {
  298. return _level;
  299. }
  300. public boolean isAlternative()
  301. {
  302. return _isAlternative;
  303. }
  304. public int getSeedLimit()
  305. {
  306. return _limitSeeds * Config.RATE_DROP_MANOR;
  307. }
  308. public int getCropLimit()
  309. {
  310. return _limitCrops * Config.RATE_DROP_MANOR;
  311. }
  312. @Override
  313. public String toString()
  314. {
  315. return "SeedData [_id=" + _id + ", _level=" + _level + ", _crop=" + _crop + ", _mature=" + _mature + ", _type1=" + _type1 + ", _type2=" + _type2 + ", _manorId=" + _manorId + ", _isAlternative=" + _isAlternative + ", _limitSeeds=" + _limitSeeds + ", _limitCrops=" + _limitCrops + "]";
  316. }
  317. }
  318. private void parseData()
  319. {
  320. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  321. factory.setValidating(false);
  322. factory.setIgnoringComments(true);
  323. File file = new File(Config.DATAPACK_ROOT, "/data/seeds.xml");
  324. Document doc = null;
  325. try
  326. {
  327. doc = factory.newDocumentBuilder().parse(file);
  328. }
  329. catch (Exception e)
  330. {
  331. _log.log(Level.WARNING, "Could not parse seeds.xml file: " + e.getMessage(), e);
  332. }
  333. doc.getDocumentElement().normalize();
  334. //list
  335. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  336. {
  337. if ("list".equalsIgnoreCase(n.getNodeName()))
  338. {
  339. //castle
  340. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  341. {
  342. if ("castle".equalsIgnoreCase(d.getNodeName()))
  343. {
  344. int castleId = Integer.parseInt(d.getAttributes().getNamedItem("id").getNodeValue());
  345. //crop
  346. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  347. {
  348. if ("crop".equalsIgnoreCase(c.getNodeName()))
  349. {
  350. int cropId = Integer.parseInt(c.getAttributes().getNamedItem("id").getNodeValue());
  351. int seedId = 0;
  352. int matureId = 0;
  353. int type1R = 0;
  354. int type2R = 0;
  355. boolean isAlt = false;
  356. int level = 0;
  357. int limitSeeds = 0;
  358. int limitCrops = 0;
  359. //attrib
  360. for (Node a = c.getFirstChild(); a != null; a = a.getNextSibling())
  361. {
  362. if (a.getNodeName().equalsIgnoreCase("seed_id"))
  363. seedId = Integer.parseInt(a.getAttributes().getNamedItem("val").getNodeValue());
  364. else if (a.getNodeName().equalsIgnoreCase("mature_id"))
  365. matureId = Integer.parseInt(a.getAttributes().getNamedItem("val").getNodeValue());
  366. else if (a.getNodeName().equalsIgnoreCase("reward1"))
  367. type1R = Integer.parseInt(a.getAttributes().getNamedItem("val").getNodeValue());
  368. else if (a.getNodeName().equalsIgnoreCase("reward2"))
  369. type2R = Integer.parseInt(a.getAttributes().getNamedItem("val").getNodeValue());
  370. else if (a.getNodeName().equalsIgnoreCase("alternative"))
  371. isAlt = Integer.parseInt(a.getAttributes().getNamedItem("val").getNodeValue()) == 1;
  372. else if (a.getNodeName().equalsIgnoreCase("level"))
  373. level = Integer.parseInt(a.getAttributes().getNamedItem("val").getNodeValue());
  374. else if (a.getNodeName().equalsIgnoreCase("limit_seed"))
  375. limitSeeds = Integer.parseInt(a.getAttributes().getNamedItem("val").getNodeValue());
  376. else if (a.getNodeName().equalsIgnoreCase("limit_crops"))
  377. limitCrops = Integer.parseInt(a.getAttributes().getNamedItem("val").getNodeValue());
  378. }
  379. SeedData seed = new SeedData(level, cropId, matureId);
  380. seed.setData(seedId, type1R, type2R, castleId, isAlt, limitSeeds, limitCrops);
  381. _seeds.put(seed.getId(), seed);
  382. }
  383. }
  384. }
  385. }
  386. }
  387. _log.info(getClass().getSimpleName()+": Loaded "+_seeds.size()+ " Seeds.");
  388. }
  389. }
  390. @SuppressWarnings("synthetic-access")
  391. private static class SingletonHolder
  392. {
  393. protected static final L2Manor _instance = new L2Manor();
  394. }
  395. public static void main(String[] arg)
  396. {
  397. L2Manor.getInstance();
  398. }
  399. }