ManorData.java 7.4 KB

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