L2Manor.java 11 KB

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