MultisellData.java 12 KB

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