RecipeData.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (C) 2004-2015 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.data.xml.impl;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.w3c.dom.Document;
  25. import org.w3c.dom.NamedNodeMap;
  26. import org.w3c.dom.Node;
  27. import com.l2jserver.gameserver.model.L2RecipeInstance;
  28. import com.l2jserver.gameserver.model.L2RecipeList;
  29. import com.l2jserver.gameserver.model.L2RecipeStatInstance;
  30. import com.l2jserver.gameserver.model.StatsSet;
  31. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  32. import com.l2jserver.util.data.xml.IXmlReader;
  33. /**
  34. * The Class RecipeData.
  35. * @author Zoey76
  36. */
  37. public class RecipeData implements IXmlReader
  38. {
  39. private final Map<Integer, L2RecipeList> _recipes = new HashMap<>();
  40. /**
  41. * Instantiates a new recipe data.
  42. */
  43. protected RecipeData()
  44. {
  45. load();
  46. }
  47. @Override
  48. public void load()
  49. {
  50. _recipes.clear();
  51. parseDatapackFile("data/recipes.xml");
  52. LOGGER.info("{}: Loaded {} recipes.", getClass().getSimpleName(), _recipes.size());
  53. }
  54. @Override
  55. public void parseDocument(Document doc)
  56. {
  57. // TODO: Cleanup checks enforced by XSD.
  58. final List<L2RecipeInstance> recipePartList = new ArrayList<>();
  59. final List<L2RecipeStatInstance> recipeStatUseList = new ArrayList<>();
  60. final List<L2RecipeStatInstance> recipeAltStatChangeList = new ArrayList<>();
  61. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  62. {
  63. if ("list".equalsIgnoreCase(n.getNodeName()))
  64. {
  65. RECIPES_FILE: for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  66. {
  67. if ("item".equalsIgnoreCase(d.getNodeName()))
  68. {
  69. recipePartList.clear();
  70. recipeStatUseList.clear();
  71. recipeAltStatChangeList.clear();
  72. NamedNodeMap attrs = d.getAttributes();
  73. Node att;
  74. int id = -1;
  75. boolean haveRare = false;
  76. StatsSet set = new StatsSet();
  77. att = attrs.getNamedItem("id");
  78. if (att == null)
  79. {
  80. LOGGER.error("{}: Missing id for recipe item, skipping!", getClass().getSimpleName());
  81. continue;
  82. }
  83. id = Integer.parseInt(att.getNodeValue());
  84. set.set("id", id);
  85. att = attrs.getNamedItem("recipeId");
  86. if (att == null)
  87. {
  88. LOGGER.error("{}: Missing recipeId for recipe item ID: {}, skipping!", getClass().getSimpleName(), id);
  89. continue;
  90. }
  91. set.set("recipeId", Integer.parseInt(att.getNodeValue()));
  92. att = attrs.getNamedItem("name");
  93. if (att == null)
  94. {
  95. LOGGER.error("{}: Missing name for recipe item ID: {}, skipping!", getClass().getSimpleName(), id);
  96. continue;
  97. }
  98. set.set("recipeName", att.getNodeValue());
  99. att = attrs.getNamedItem("craftLevel");
  100. if (att == null)
  101. {
  102. LOGGER.error("{}: Missing level for recipe item ID: {}, skipping!", getClass().getSimpleName(), id);
  103. continue;
  104. }
  105. set.set("craftLevel", Integer.parseInt(att.getNodeValue()));
  106. att = attrs.getNamedItem("type");
  107. if (att == null)
  108. {
  109. LOGGER.error("{}: Missing type for recipe item ID: {}, skipping!", getClass().getSimpleName(), id);
  110. continue;
  111. }
  112. set.set("isDwarvenRecipe", att.getNodeValue().equalsIgnoreCase("dwarven"));
  113. att = attrs.getNamedItem("successRate");
  114. if (att == null)
  115. {
  116. LOGGER.error("{}: Missing successRate for recipe item ID: {}, skipping!", getClass().getSimpleName(), id);
  117. continue;
  118. }
  119. set.set("successRate", Integer.parseInt(att.getNodeValue()));
  120. for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
  121. {
  122. if ("statUse".equalsIgnoreCase(c.getNodeName()))
  123. {
  124. String statName = c.getAttributes().getNamedItem("name").getNodeValue();
  125. int value = Integer.parseInt(c.getAttributes().getNamedItem("value").getNodeValue());
  126. try
  127. {
  128. recipeStatUseList.add(new L2RecipeStatInstance(statName, value));
  129. }
  130. catch (Exception e)
  131. {
  132. LOGGER.error("{}: Error in StatUse parameter for recipe item ID: {}, skipping!", getClass().getSimpleName(), id);
  133. continue RECIPES_FILE;
  134. }
  135. }
  136. else if ("altStatChange".equalsIgnoreCase(c.getNodeName()))
  137. {
  138. String statName = c.getAttributes().getNamedItem("name").getNodeValue();
  139. int value = Integer.parseInt(c.getAttributes().getNamedItem("value").getNodeValue());
  140. try
  141. {
  142. recipeAltStatChangeList.add(new L2RecipeStatInstance(statName, value));
  143. }
  144. catch (Exception e)
  145. {
  146. LOGGER.error("{}: Error in AltStatChange parameter for recipe item ID: {}, skipping!", getClass().getSimpleName(), id);
  147. continue RECIPES_FILE;
  148. }
  149. }
  150. else if ("ingredient".equalsIgnoreCase(c.getNodeName()))
  151. {
  152. int ingId = Integer.parseInt(c.getAttributes().getNamedItem("id").getNodeValue());
  153. int ingCount = Integer.parseInt(c.getAttributes().getNamedItem("count").getNodeValue());
  154. recipePartList.add(new L2RecipeInstance(ingId, ingCount));
  155. }
  156. else if ("production".equalsIgnoreCase(c.getNodeName()))
  157. {
  158. set.set("itemId", Integer.parseInt(c.getAttributes().getNamedItem("id").getNodeValue()));
  159. set.set("count", Integer.parseInt(c.getAttributes().getNamedItem("count").getNodeValue()));
  160. }
  161. else if ("productionRare".equalsIgnoreCase(c.getNodeName()))
  162. {
  163. set.set("rareItemId", Integer.parseInt(c.getAttributes().getNamedItem("id").getNodeValue()));
  164. set.set("rareCount", Integer.parseInt(c.getAttributes().getNamedItem("count").getNodeValue()));
  165. set.set("rarity", Integer.parseInt(c.getAttributes().getNamedItem("rarity").getNodeValue()));
  166. haveRare = true;
  167. }
  168. }
  169. L2RecipeList recipeList = new L2RecipeList(set, haveRare);
  170. for (L2RecipeInstance recipePart : recipePartList)
  171. {
  172. recipeList.addRecipe(recipePart);
  173. }
  174. for (L2RecipeStatInstance recipeStatUse : recipeStatUseList)
  175. {
  176. recipeList.addStatUse(recipeStatUse);
  177. }
  178. for (L2RecipeStatInstance recipeAltStatChange : recipeAltStatChangeList)
  179. {
  180. recipeList.addAltStatChange(recipeAltStatChange);
  181. }
  182. _recipes.put(id, recipeList);
  183. }
  184. }
  185. }
  186. }
  187. }
  188. /**
  189. * Gets the recipe list.
  190. * @param listId the list id
  191. * @return the recipe list
  192. */
  193. public L2RecipeList getRecipeList(int listId)
  194. {
  195. return _recipes.get(listId);
  196. }
  197. /**
  198. * Gets the recipe by item id.
  199. * @param itemId the item id
  200. * @return the recipe by item id
  201. */
  202. public L2RecipeList getRecipeByItemId(int itemId)
  203. {
  204. for (L2RecipeList find : _recipes.values())
  205. {
  206. if (find.getRecipeId() == itemId)
  207. {
  208. return find;
  209. }
  210. }
  211. return null;
  212. }
  213. /**
  214. * Gets the all item ids.
  215. * @return the all item ids
  216. */
  217. public int[] getAllItemIds()
  218. {
  219. int[] idList = new int[_recipes.size()];
  220. int i = 0;
  221. for (L2RecipeList rec : _recipes.values())
  222. {
  223. idList[i++] = rec.getRecipeId();
  224. }
  225. return idList;
  226. }
  227. /**
  228. * Gets the valid recipe list.
  229. * @param player the player
  230. * @param id the recipe list id
  231. * @return the valid recipe list
  232. */
  233. public L2RecipeList getValidRecipeList(L2PcInstance player, int id)
  234. {
  235. L2RecipeList recipeList = _recipes.get(id);
  236. if ((recipeList == null) || (recipeList.getRecipes().length == 0))
  237. {
  238. player.sendMessage(getClass().getSimpleName() + ": No recipe for: " + id);
  239. player.isInCraftMode(false);
  240. return null;
  241. }
  242. return recipeList;
  243. }
  244. /**
  245. * Gets the single instance of RecipeData.
  246. * @return single instance of RecipeData
  247. */
  248. public static RecipeData getInstance()
  249. {
  250. return SingletonHolder._instance;
  251. }
  252. /**
  253. * The Class SingletonHolder.
  254. */
  255. private static class SingletonHolder
  256. {
  257. protected static final RecipeData _instance = new RecipeData();
  258. }
  259. }