ManorData.java 7.2 KB

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