RecipeData.java 8.6 KB

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