MultiSell.java 11 KB

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