ManorData.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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.datatables;
  16. import java.util.ArrayList;
  17. import java.util.Collection;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import org.w3c.dom.Document;
  23. import org.w3c.dom.Node;
  24. import com.l2jserver.gameserver.engines.DocumentParser;
  25. import com.l2jserver.gameserver.model.L2Seed;
  26. import com.l2jserver.gameserver.model.StatsSet;
  27. import com.l2jserver.gameserver.model.items.L2Item;
  28. import com.l2jserver.util.L2FastMap;
  29. /**
  30. * Service class for manor
  31. * @author l3x, UnAfraid
  32. */
  33. public class ManorData extends DocumentParser
  34. {
  35. private static Logger _log = Logger.getLogger(ManorData.class.getName());
  36. private static Map<Integer, L2Seed> _seeds;
  37. protected ManorData()
  38. {
  39. _seeds = new L2FastMap<>(true);
  40. load();
  41. }
  42. @Override
  43. public void load()
  44. {
  45. _seeds.clear();
  46. parseDatapackFile("data/seeds.xml");
  47. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _seeds.size() + " Seeds");
  48. }
  49. @Override
  50. protected void parseDocument()
  51. {
  52. Document doc = getCurrentDocument();
  53. doc.getDocumentElement().normalize();
  54. StatsSet set;
  55. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  56. {
  57. if ("list".equalsIgnoreCase(n.getNodeName()))
  58. {
  59. // castle
  60. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  61. {
  62. if ("castle".equalsIgnoreCase(d.getNodeName()))
  63. {
  64. int castleId = parseInt(d.getAttributes(), "id");
  65. // crop
  66. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  67. {
  68. if ("crop".equalsIgnoreCase(c.getNodeName()))
  69. {
  70. set = new StatsSet();
  71. set.set("cropId", parseString(c.getAttributes(), "id"));
  72. set.set("castleId", castleId);
  73. for (Node a = c.getFirstChild(); a != null; a = a.getNextSibling())
  74. {
  75. switch (a.getNodeName())
  76. {
  77. case "seed_id":
  78. case "mature_id":
  79. case "reward1":
  80. case "reward2":
  81. case "alternative":
  82. case "level":
  83. case "limit_seed":
  84. case "limit_crops":
  85. {
  86. set.set(a.getNodeName(), parseString(a.getAttributes(), "val"));
  87. break;
  88. }
  89. }
  90. }
  91. L2Seed seed = new L2Seed(set);
  92. _seeds.put(seed.getSeedId(), seed);
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. public Collection<L2Seed> getSeedsData()
  101. {
  102. return _seeds.values();
  103. }
  104. public List<Integer> getAllCrops()
  105. {
  106. List<Integer> crops = new ArrayList<>();
  107. for (L2Seed seed : getSeedsData())
  108. {
  109. if (!crops.contains(seed.getCropId()) && (seed.getCropId() != 0) && !crops.contains(seed.getCropId()))
  110. {
  111. crops.add(seed.getCropId());
  112. }
  113. }
  114. return crops;
  115. }
  116. public int getSeedBasicPrice(int seedId)
  117. {
  118. final L2Item seedItem = ItemTable.getInstance().getTemplate(seedId);
  119. if (seedItem != null)
  120. {
  121. return seedItem.getReferencePrice();
  122. }
  123. return 0;
  124. }
  125. public int getSeedBasicPriceByCrop(int cropId)
  126. {
  127. for (L2Seed seed : getSeedsData())
  128. {
  129. if (seed.getCropId() == cropId)
  130. {
  131. return getSeedBasicPrice(seed.getSeedId());
  132. }
  133. }
  134. return 0;
  135. }
  136. public int getCropBasicPrice(int cropId)
  137. {
  138. final L2Item cropItem = ItemTable.getInstance().getTemplate(cropId);
  139. if (cropItem != null)
  140. {
  141. return cropItem.getReferencePrice();
  142. }
  143. return 0;
  144. }
  145. public int getMatureCrop(int cropId)
  146. {
  147. for (L2Seed seed : getSeedsData())
  148. {
  149. if (seed.getCropId() == cropId)
  150. {
  151. return seed.getMatureId();
  152. }
  153. }
  154. return 0;
  155. }
  156. /**
  157. * Returns price which lord pays to buy one seed
  158. * @param seedId
  159. * @return seed price
  160. */
  161. public long getSeedBuyPrice(int seedId)
  162. {
  163. long buyPrice = getSeedBasicPrice(seedId);
  164. return (buyPrice > 0 ? buyPrice : 1);
  165. }
  166. public int getSeedMinLevel(int seedId)
  167. {
  168. L2Seed seed = _seeds.get(seedId);
  169. if (seed != null)
  170. {
  171. return seed.getLevel() - 5;
  172. }
  173. return -1;
  174. }
  175. public int getSeedMaxLevel(int seedId)
  176. {
  177. L2Seed seed = _seeds.get(seedId);
  178. if (seed != null)
  179. {
  180. return seed.getLevel() + 5;
  181. }
  182. return -1;
  183. }
  184. public int getSeedLevelByCrop(int cropId)
  185. {
  186. for (L2Seed seed : getSeedsData())
  187. {
  188. if (seed.getCropId() == cropId)
  189. {
  190. return seed.getLevel();
  191. }
  192. }
  193. return 0;
  194. }
  195. public int getSeedLevel(int seedId)
  196. {
  197. L2Seed seed = _seeds.get(seedId);
  198. if (seed != null)
  199. {
  200. return seed.getLevel();
  201. }
  202. return -1;
  203. }
  204. public boolean isAlternative(int seedId)
  205. {
  206. L2Seed seed = _seeds.get(seedId);
  207. if (seed != null)
  208. {
  209. return seed.isAlternative();
  210. }
  211. return false;
  212. }
  213. public int getCropType(int seedId)
  214. {
  215. L2Seed seed = _seeds.get(seedId);
  216. if (seed != null)
  217. {
  218. return seed.getCropId();
  219. }
  220. return -1;
  221. }
  222. public int getRewardItem(int cropId, int type)
  223. {
  224. for (L2Seed seed : getSeedsData())
  225. {
  226. if (seed.getCropId() == cropId)
  227. {
  228. return seed.getReward(type); // there can be several seeds with same crop, but reward should be the same for all.
  229. }
  230. }
  231. return -1;
  232. }
  233. public int getRewardItemBySeed(int seedId, int type)
  234. {
  235. L2Seed seed = _seeds.get(seedId);
  236. if (seed != null)
  237. {
  238. return seed.getReward(type);
  239. }
  240. return 0;
  241. }
  242. /**
  243. * Return all crops which can be purchased by given castle
  244. * @param castleId
  245. * @return
  246. */
  247. public List<Integer> getCropsForCastle(int castleId)
  248. {
  249. List<Integer> crops = new ArrayList<>();
  250. for (L2Seed seed : getSeedsData())
  251. {
  252. if ((seed.getCastleId() == castleId) && !crops.contains(seed.getCropId()))
  253. {
  254. crops.add(seed.getCropId());
  255. }
  256. }
  257. return crops;
  258. }
  259. /**
  260. * Return list of seed ids, which belongs to castle with given id
  261. * @param castleId - id of the castle
  262. * @return seedIds - list of seed ids
  263. */
  264. public List<Integer> getSeedsForCastle(int castleId)
  265. {
  266. List<Integer> seedsID = new ArrayList<>();
  267. for (L2Seed seed : getSeedsData())
  268. {
  269. if ((seed.getCastleId() == castleId) && !seedsID.contains(seed.getSeedId()))
  270. {
  271. seedsID.add(seed.getSeedId());
  272. }
  273. }
  274. return seedsID;
  275. }
  276. /**
  277. * Returns castle id where seed can be sowned<br>
  278. * @param seedId
  279. * @return castleId
  280. */
  281. public int getCastleIdForSeed(int seedId)
  282. {
  283. L2Seed seed = _seeds.get(seedId);
  284. if (seed != null)
  285. {
  286. return seed.getCastleId();
  287. }
  288. return 0;
  289. }
  290. public int getSeedSaleLimit(int seedId)
  291. {
  292. L2Seed seed = _seeds.get(seedId);
  293. if (seed != null)
  294. {
  295. return seed.getSeedLimit();
  296. }
  297. return 0;
  298. }
  299. public int getCropPuchaseLimit(int cropId)
  300. {
  301. for (L2Seed seed : getSeedsData())
  302. {
  303. if (seed.getCropId() == cropId)
  304. {
  305. return seed.getCropLimit();
  306. }
  307. }
  308. return 0;
  309. }
  310. public static ManorData getInstance()
  311. {
  312. return SingletonHolder._instance;
  313. }
  314. private static class SingletonHolder
  315. {
  316. protected static final ManorData _instance = new ManorData();
  317. }
  318. }