2
0

ManorData.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.datatables;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import org.w3c.dom.NamedNodeMap;
  27. import org.w3c.dom.Node;
  28. import com.l2jserver.gameserver.engines.DocumentParser;
  29. import com.l2jserver.gameserver.model.L2Seed;
  30. import com.l2jserver.gameserver.model.StatsSet;
  31. import com.l2jserver.gameserver.model.items.L2Item;
  32. /**
  33. * Service class for manor
  34. * @author l3x, UnAfraid
  35. */
  36. public class ManorData extends DocumentParser
  37. {
  38. private static Logger _log = Logger.getLogger(ManorData.class.getName());
  39. private static Map<Integer, L2Seed> _seeds = new HashMap<>();
  40. protected ManorData()
  41. {
  42. load();
  43. }
  44. @Override
  45. public void load()
  46. {
  47. _seeds.clear();
  48. parseDatapackFile("data/seeds.xml");
  49. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _seeds.size() + " Seeds");
  50. }
  51. @Override
  52. protected void parseDocument()
  53. {
  54. StatsSet set;
  55. NamedNodeMap attrs;
  56. Node att;
  57. int castleId;
  58. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  59. {
  60. if ("list".equalsIgnoreCase(n.getNodeName()))
  61. {
  62. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  63. {
  64. if ("castle".equalsIgnoreCase(d.getNodeName()))
  65. {
  66. castleId = parseInt(d.getAttributes(), "id");
  67. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  68. {
  69. if ("crop".equalsIgnoreCase(c.getNodeName()))
  70. {
  71. set = new StatsSet();
  72. set.set("castleId", castleId);
  73. attrs = c.getAttributes();
  74. for (int i = 0; i < attrs.getLength(); i++)
  75. {
  76. att = attrs.item(i);
  77. set.set(att.getNodeName(), att.getNodeValue());
  78. }
  79. L2Seed seed = new L2Seed(set);
  80. _seeds.put(seed.getSeedId(), seed);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. public List<Integer> getAllCrops()
  89. {
  90. List<Integer> crops = new ArrayList<>();
  91. for (L2Seed seed : _seeds.values())
  92. {
  93. if (!crops.contains(seed.getCropId()) && (seed.getCropId() != 0) && !crops.contains(seed.getCropId()))
  94. {
  95. crops.add(seed.getCropId());
  96. }
  97. }
  98. return crops;
  99. }
  100. public int getSeedBasicPrice(int seedId)
  101. {
  102. final L2Item seedItem = ItemTable.getInstance().getTemplate(seedId);
  103. if (seedItem != null)
  104. {
  105. return seedItem.getReferencePrice();
  106. }
  107. return 0;
  108. }
  109. public int getSeedBasicPriceByCrop(int cropId)
  110. {
  111. for (L2Seed seed : _seeds.values())
  112. {
  113. if (seed.getCropId() == cropId)
  114. {
  115. return getSeedBasicPrice(seed.getSeedId());
  116. }
  117. }
  118. return 0;
  119. }
  120. public int getCropBasicPrice(int cropId)
  121. {
  122. final L2Item cropItem = ItemTable.getInstance().getTemplate(cropId);
  123. if (cropItem != null)
  124. {
  125. return cropItem.getReferencePrice();
  126. }
  127. return 0;
  128. }
  129. public int getMatureCrop(int cropId)
  130. {
  131. for (L2Seed seed : _seeds.values())
  132. {
  133. if (seed.getCropId() == cropId)
  134. {
  135. return seed.getMatureId();
  136. }
  137. }
  138. return 0;
  139. }
  140. /**
  141. * Returns price which lord pays to buy one seed
  142. * @param seedId
  143. * @return seed price
  144. */
  145. public long getSeedBuyPrice(int seedId)
  146. {
  147. long buyPrice = getSeedBasicPrice(seedId);
  148. return (buyPrice > 0 ? buyPrice : 1);
  149. }
  150. public int getSeedMinLevel(int seedId)
  151. {
  152. L2Seed seed = _seeds.get(seedId);
  153. if (seed != null)
  154. {
  155. return seed.getLevel() - 5;
  156. }
  157. return -1;
  158. }
  159. public int getSeedMaxLevel(int seedId)
  160. {
  161. L2Seed seed = _seeds.get(seedId);
  162. if (seed != null)
  163. {
  164. return seed.getLevel() + 5;
  165. }
  166. return -1;
  167. }
  168. public int getSeedLevelByCrop(int cropId)
  169. {
  170. for (L2Seed seed : _seeds.values())
  171. {
  172. if (seed.getCropId() == cropId)
  173. {
  174. return seed.getLevel();
  175. }
  176. }
  177. return 0;
  178. }
  179. public int getSeedLevel(int seedId)
  180. {
  181. L2Seed seed = _seeds.get(seedId);
  182. if (seed != null)
  183. {
  184. return seed.getLevel();
  185. }
  186. return -1;
  187. }
  188. public boolean isAlternative(int seedId)
  189. {
  190. L2Seed seed = _seeds.get(seedId);
  191. if (seed != null)
  192. {
  193. return seed.isAlternative();
  194. }
  195. return false;
  196. }
  197. public int getCropType(int seedId)
  198. {
  199. L2Seed seed = _seeds.get(seedId);
  200. if (seed != null)
  201. {
  202. return seed.getCropId();
  203. }
  204. return -1;
  205. }
  206. public int getRewardItem(int cropId, int type)
  207. {
  208. for (L2Seed seed : _seeds.values())
  209. {
  210. if (seed.getCropId() == cropId)
  211. {
  212. return seed.getReward(type); // there can be several seeds with same crop, but reward should be the same for all.
  213. }
  214. }
  215. return -1;
  216. }
  217. public int getRewardItemBySeed(int seedId, int type)
  218. {
  219. L2Seed seed = _seeds.get(seedId);
  220. if (seed != null)
  221. {
  222. return seed.getReward(type);
  223. }
  224. return 0;
  225. }
  226. /**
  227. * Return all crops which can be purchased by given castle
  228. * @param castleId
  229. * @return
  230. */
  231. public List<Integer> getCropsForCastle(int castleId)
  232. {
  233. List<Integer> crops = new ArrayList<>();
  234. for (L2Seed seed : _seeds.values())
  235. {
  236. if ((seed.getCastleId() == castleId) && !crops.contains(seed.getCropId()))
  237. {
  238. crops.add(seed.getCropId());
  239. }
  240. }
  241. return crops;
  242. }
  243. /**
  244. * Return list of seed ids, which belongs to castle with given id
  245. * @param castleId - id of the castle
  246. * @return seedIds - list of seed ids
  247. */
  248. public List<Integer> getSeedsForCastle(int castleId)
  249. {
  250. List<Integer> seedsID = new ArrayList<>();
  251. for (L2Seed seed : _seeds.values())
  252. {
  253. if ((seed.getCastleId() == castleId) && !seedsID.contains(seed.getSeedId()))
  254. {
  255. seedsID.add(seed.getSeedId());
  256. }
  257. }
  258. return seedsID;
  259. }
  260. /**
  261. * Returns castle id where seed can be sowned<br>
  262. * @param seedId
  263. * @return castleId
  264. */
  265. public int getCastleIdForSeed(int seedId)
  266. {
  267. L2Seed seed = _seeds.get(seedId);
  268. if (seed != null)
  269. {
  270. return seed.getCastleId();
  271. }
  272. return 0;
  273. }
  274. public int getSeedSaleLimit(int seedId)
  275. {
  276. L2Seed seed = _seeds.get(seedId);
  277. if (seed != null)
  278. {
  279. return seed.getSeedLimit();
  280. }
  281. return 0;
  282. }
  283. public int getCropPuchaseLimit(int cropId)
  284. {
  285. for (L2Seed seed : _seeds.values())
  286. {
  287. if (seed.getCropId() == cropId)
  288. {
  289. return seed.getCropLimit();
  290. }
  291. }
  292. return 0;
  293. }
  294. public static ManorData getInstance()
  295. {
  296. return SingletonHolder._instance;
  297. }
  298. private static class SingletonHolder
  299. {
  300. protected static final ManorData _instance = new ManorData();
  301. }
  302. }