2
0

MultiSell.java 13 KB

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