MultisellData.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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.io.File;
  21. import java.io.FileFilter;
  22. import java.util.HashMap;
  23. import java.util.Iterator;
  24. import java.util.Map;
  25. import org.w3c.dom.DOMException;
  26. import org.w3c.dom.Document;
  27. import org.w3c.dom.NamedNodeMap;
  28. import org.w3c.dom.Node;
  29. import com.l2jserver.Config;
  30. import com.l2jserver.gameserver.model.StatsSet;
  31. import com.l2jserver.gameserver.model.actor.L2Npc;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. import com.l2jserver.gameserver.model.multisell.Entry;
  34. import com.l2jserver.gameserver.model.multisell.Ingredient;
  35. import com.l2jserver.gameserver.model.multisell.ListContainer;
  36. import com.l2jserver.gameserver.model.multisell.PreparedListContainer;
  37. import com.l2jserver.gameserver.network.SystemMessageId;
  38. import com.l2jserver.gameserver.network.serverpackets.ExBrExtraUserInfo;
  39. import com.l2jserver.gameserver.network.serverpackets.MultiSellList;
  40. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  41. import com.l2jserver.gameserver.network.serverpackets.UserInfo;
  42. import com.l2jserver.gameserver.util.Util;
  43. import com.l2jserver.util.data.xml.IXmlReader;
  44. import com.l2jserver.util.file.filter.NumericNameFilter;
  45. public final class MultisellData implements IXmlReader
  46. {
  47. private final Map<Integer, ListContainer> _entries = new HashMap<>();
  48. public static final int PAGE_SIZE = 40;
  49. // Special IDs.
  50. public static final int PC_BANG_POINTS = -100;
  51. public static final int CLAN_REPUTATION = -200;
  52. public static final int FAME = -300;
  53. // Misc
  54. private static final FileFilter NUMERIC_FILTER = new NumericNameFilter();
  55. protected MultisellData()
  56. {
  57. load();
  58. }
  59. @Override
  60. public void load()
  61. {
  62. _entries.clear();
  63. parseDatapackDirectory("data/multisell", false);
  64. if (Config.CUSTOM_MULTISELL_LOAD)
  65. {
  66. parseDatapackDirectory("data/multisell/custom", false);
  67. }
  68. verify();
  69. LOGGER.info("{}: Loaded {} multisell lists.", getClass().getSimpleName(), _entries.size());
  70. }
  71. @Override
  72. public void parseDocument(Document doc, File f)
  73. {
  74. try
  75. {
  76. int id = Integer.parseInt(f.getName().replaceAll(".xml", ""));
  77. int entryId = 1;
  78. Node att;
  79. final ListContainer list = new ListContainer(id);
  80. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  81. {
  82. if ("list".equalsIgnoreCase(n.getNodeName()))
  83. {
  84. att = n.getAttributes().getNamedItem("applyTaxes");
  85. list.setApplyTaxes((att != null) && Boolean.parseBoolean(att.getNodeValue()));
  86. att = n.getAttributes().getNamedItem("useRate");
  87. if (att != null)
  88. {
  89. try
  90. {
  91. list.setUseRate(Double.valueOf(att.getNodeValue()));
  92. if (list.getUseRate() <= 1e-6)
  93. {
  94. throw new NumberFormatException("The value cannot be 0"); // threat 0 as invalid value
  95. }
  96. }
  97. catch (NumberFormatException e)
  98. {
  99. try
  100. {
  101. list.setUseRate(Config.class.getField(att.getNodeValue()).getDouble(Config.class));
  102. }
  103. catch (Exception e1)
  104. {
  105. LOGGER.warn("{}: Unable to parse {}", getClass().getSimpleName(), doc.getLocalName(), e1);
  106. list.setUseRate(1.0);
  107. }
  108. }
  109. catch (DOMException e)
  110. {
  111. LOGGER.warn("{}: Unable to parse {}", getClass().getSimpleName(), doc.getLocalName(), e);
  112. }
  113. }
  114. att = n.getAttributes().getNamedItem("maintainEnchantment");
  115. list.setMaintainEnchantment((att != null) && Boolean.parseBoolean(att.getNodeValue()));
  116. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  117. {
  118. if ("item".equalsIgnoreCase(d.getNodeName()))
  119. {
  120. Entry e = parseEntry(d, entryId++, list);
  121. list.getEntries().add(e);
  122. }
  123. else if ("npcs".equalsIgnoreCase(d.getNodeName()))
  124. {
  125. for (Node b = d.getFirstChild(); b != null; b = b.getNextSibling())
  126. {
  127. if ("npc".equalsIgnoreCase(b.getNodeName()))
  128. {
  129. if (Util.isDigit(b.getTextContent()))
  130. {
  131. list.allowNpc(Integer.parseInt(b.getTextContent()));
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. _entries.put(id, list);
  140. }
  141. catch (Exception e)
  142. {
  143. LOGGER.error("{}: Error in file {}", getClass().getSimpleName(), f, e);
  144. }
  145. }
  146. @Override
  147. public FileFilter getCurrentFileFilter()
  148. {
  149. return NUMERIC_FILTER;
  150. }
  151. private final Entry parseEntry(Node n, int entryId, ListContainer list)
  152. {
  153. Node first = n.getFirstChild();
  154. final Entry entry = new Entry(entryId);
  155. NamedNodeMap attrs;
  156. Node att;
  157. StatsSet set;
  158. for (n = first; n != null; n = n.getNextSibling())
  159. {
  160. if ("ingredient".equalsIgnoreCase(n.getNodeName()))
  161. {
  162. attrs = n.getAttributes();
  163. set = new StatsSet();
  164. for (int i = 0; i < attrs.getLength(); i++)
  165. {
  166. att = attrs.item(i);
  167. set.set(att.getNodeName(), att.getNodeValue());
  168. }
  169. entry.addIngredient(new Ingredient(set));
  170. }
  171. else if ("production".equalsIgnoreCase(n.getNodeName()))
  172. {
  173. attrs = n.getAttributes();
  174. set = new StatsSet();
  175. for (int i = 0; i < attrs.getLength(); i++)
  176. {
  177. att = attrs.item(i);
  178. set.set(att.getNodeName(), att.getNodeValue());
  179. }
  180. entry.addProduct(new Ingredient(set));
  181. }
  182. }
  183. return entry;
  184. }
  185. /**
  186. * This will generate the multisell list for the items.<br>
  187. * There exist various parameters in multisells that affect the way they will appear:
  188. * <ol>
  189. * <li>Inventory only:
  190. * <ul>
  191. * <li>If true, only show items of the multisell for which the "primary" ingredients are already in the player's inventory. By "primary" ingredients we mean weapon and armor.</li>
  192. * <li>If false, show the entire list.</li>
  193. * </ul>
  194. * </li>
  195. * <li>Maintain enchantment: presumably, only lists with "inventory only" set to true should sometimes have this as true. This makes no sense otherwise...
  196. * <ul>
  197. * <li>If true, then the product will match the enchantment level of the ingredient.<br>
  198. * If the player has multiple items that match the ingredient list but the enchantment levels differ, then the entries need to be duplicated to show the products and ingredients for each enchantment level.<br>
  199. * For example: If the player has a crystal staff +1 and a crystal staff +3 and goes to exchange it at the mammon, the list should have all exchange possibilities for the +1 staff, followed by all possibilities for the +3 staff.</li>
  200. * <li>If false, then any level ingredient will be considered equal and product will always be at +0</li>
  201. * </ul>
  202. * </li>
  203. * <li>Apply taxes: Uses the "taxIngredient" entry in order to add a certain amount of adena to the ingredients.
  204. * <li>
  205. * <li>Additional product and ingredient multipliers.</li>
  206. * </ol>
  207. * @param listId
  208. * @param player
  209. * @param npc
  210. * @param inventoryOnly
  211. * @param productMultiplier
  212. * @param ingredientMultiplier
  213. */
  214. public final void separateAndSend(int listId, L2PcInstance player, L2Npc npc, boolean inventoryOnly, double productMultiplier, double ingredientMultiplier)
  215. {
  216. ListContainer template = _entries.get(listId);
  217. if (template == null)
  218. {
  219. LOGGER.warn("{}: Cannot find list ID: {} requested by player: {}, NPC ID: {}!", getClass().getSimpleName(), listId, player, (npc != null ? npc.getId() : 0));
  220. return;
  221. }
  222. if (((npc != null) && !template.isNpcAllowed(npc.getId())) || ((npc == null) && template.isNpcOnly()))
  223. {
  224. LOGGER.warn("{}: Player {} attempted to open multisell {} from npc {} which is not allowed!", getClass().getSimpleName(), player, listId, npc);
  225. return;
  226. }
  227. final PreparedListContainer list = new PreparedListContainer(template, inventoryOnly, player, npc);
  228. // Pass through this only when multipliers are different from 1
  229. if ((productMultiplier != 1) || (ingredientMultiplier != 1))
  230. {
  231. list.getEntries().forEach(entry ->
  232. {
  233. // Math.max used here to avoid dropping count to 0
  234. entry.getProducts().forEach(product -> product.setItemCount((long) Math.max(product.getItemCount() * productMultiplier, 1)));
  235. // Math.max used here to avoid dropping count to 0
  236. entry.getIngredients().forEach(ingredient -> ingredient.setItemCount((long) Math.max(ingredient.getItemCount() * ingredientMultiplier, 1)));
  237. });
  238. }
  239. int index = 0;
  240. do
  241. {
  242. // send list at least once even if size = 0
  243. player.sendPacket(new MultiSellList(list, index));
  244. index += PAGE_SIZE;
  245. }
  246. while (index < list.getEntries().size());
  247. player.setMultiSell(list);
  248. }
  249. public final void separateAndSend(int listId, L2PcInstance player, L2Npc npc, boolean inventoryOnly)
  250. {
  251. separateAndSend(listId, player, npc, inventoryOnly, 1, 1);
  252. }
  253. public static final boolean hasSpecialIngredient(int id, long amount, L2PcInstance player)
  254. {
  255. switch (id)
  256. {
  257. case CLAN_REPUTATION:
  258. if (player.getClan() == null)
  259. {
  260. player.sendPacket(SystemMessageId.YOU_ARE_NOT_A_CLAN_MEMBER);
  261. break;
  262. }
  263. if (!player.isClanLeader())
  264. {
  265. player.sendPacket(SystemMessageId.ONLY_THE_CLAN_LEADER_IS_ENABLED);
  266. break;
  267. }
  268. if (player.getClan().getReputationScore() < amount)
  269. {
  270. player.sendPacket(SystemMessageId.THE_CLAN_REPUTATION_SCORE_IS_TOO_LOW);
  271. break;
  272. }
  273. return true;
  274. case FAME:
  275. if (player.getFame() < amount)
  276. {
  277. player.sendPacket(SystemMessageId.NOT_ENOUGH_FAME_POINTS);
  278. break;
  279. }
  280. return true;
  281. }
  282. return false;
  283. }
  284. public static final boolean takeSpecialIngredient(int id, long amount, L2PcInstance player)
  285. {
  286. switch (id)
  287. {
  288. case CLAN_REPUTATION:
  289. player.getClan().takeReputationScore((int) amount, true);
  290. SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.S1_DEDUCTED_FROM_CLAN_REP);
  291. smsg.addLong(amount);
  292. player.sendPacket(smsg);
  293. return true;
  294. case FAME:
  295. player.setFame(player.getFame() - (int) amount);
  296. player.sendPacket(new UserInfo(player));
  297. player.sendPacket(new ExBrExtraUserInfo(player));
  298. return true;
  299. }
  300. return false;
  301. }
  302. public static final void giveSpecialProduct(int id, long amount, L2PcInstance player)
  303. {
  304. switch (id)
  305. {
  306. case CLAN_REPUTATION:
  307. player.getClan().addReputationScore((int) amount, true);
  308. break;
  309. case FAME:
  310. player.setFame((int) (player.getFame() + amount));
  311. player.sendPacket(new UserInfo(player));
  312. player.sendPacket(new ExBrExtraUserInfo(player));
  313. break;
  314. }
  315. }
  316. private final void verify()
  317. {
  318. ListContainer list;
  319. final Iterator<ListContainer> iter = _entries.values().iterator();
  320. while (iter.hasNext())
  321. {
  322. list = iter.next();
  323. for (Entry ent : list.getEntries())
  324. {
  325. for (Ingredient ing : ent.getIngredients())
  326. {
  327. if (!verifyIngredient(ing))
  328. {
  329. LOGGER.warn("{}: Cannot find ingredient with item ID: {} in list: {}!", getClass().getSimpleName(), ing.getItemId(), list.getListId());
  330. }
  331. }
  332. for (Ingredient ing : ent.getProducts())
  333. {
  334. if (!verifyIngredient(ing))
  335. {
  336. LOGGER.warn("{}: Cannot find product with item ID: {} in list: {}!", getClass().getSimpleName(), ing.getItemId(), list.getListId());
  337. }
  338. }
  339. }
  340. }
  341. }
  342. private final boolean verifyIngredient(Ingredient ing)
  343. {
  344. switch (ing.getItemId())
  345. {
  346. case CLAN_REPUTATION:
  347. case FAME:
  348. return true;
  349. default:
  350. return ing.getTemplate() != null;
  351. }
  352. }
  353. public static MultisellData getInstance()
  354. {
  355. return SingletonHolder._instance;
  356. }
  357. private static class SingletonHolder
  358. {
  359. protected static final MultisellData _instance = new MultisellData();
  360. }
  361. }