MultiSell.java 11 KB

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