MultiSellChoose.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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.network.clientpackets;
  16. import java.util.logging.Logger;
  17. import com.l2jserver.Config;
  18. import com.l2jserver.gameserver.datatables.ItemTable;
  19. import com.l2jserver.gameserver.model.Elementals;
  20. import com.l2jserver.gameserver.model.L2Augmentation;
  21. import com.l2jserver.gameserver.model.L2ItemInstance;
  22. import com.l2jserver.gameserver.model.L2Multisell;
  23. import com.l2jserver.gameserver.model.L2Object;
  24. import com.l2jserver.gameserver.model.L2Multisell.MultiSellEntry;
  25. import com.l2jserver.gameserver.model.L2Multisell.MultiSellIngredient;
  26. import com.l2jserver.gameserver.model.L2Multisell.MultiSellListContainer;
  27. import com.l2jserver.gameserver.model.actor.L2Npc;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.gameserver.model.itemcontainer.PcInventory;
  30. import com.l2jserver.gameserver.network.SystemMessageId;
  31. import com.l2jserver.gameserver.network.serverpackets.ExBrExtraUserInfo;
  32. import com.l2jserver.gameserver.network.serverpackets.ItemList;
  33. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  34. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  35. import com.l2jserver.gameserver.network.serverpackets.UserInfo;
  36. import com.l2jserver.gameserver.templates.item.L2Armor;
  37. import com.l2jserver.gameserver.templates.item.L2Item;
  38. import com.l2jserver.gameserver.templates.item.L2Weapon;
  39. import javolution.util.FastList;
  40. public class MultiSellChoose extends L2GameClientPacket
  41. {
  42. private static final String _C__A7_MULTISELLCHOOSE = "[C] A7 MultiSellChoose";
  43. private static Logger _log = Logger.getLogger(MultiSellChoose.class.getName());
  44. private int _listId;
  45. private int _entryId;
  46. private long _amount;
  47. private int _enchantment;
  48. private long _transactionTax; // local handling of taxation
  49. @SuppressWarnings("unused")
  50. private int _unk1;
  51. @SuppressWarnings("unused")
  52. private int _unk2;
  53. @SuppressWarnings("unused")
  54. private int _unk3;
  55. @SuppressWarnings("unused")
  56. private int _unk7;
  57. @SuppressWarnings("unused")
  58. private int _unk4;
  59. @SuppressWarnings("unused")
  60. private int _unk5;
  61. @SuppressWarnings("unused")
  62. private int _unk6;
  63. @SuppressWarnings("unused")
  64. private int _unk8;
  65. @SuppressWarnings("unused")
  66. private int _unk9;
  67. @SuppressWarnings("unused")
  68. private int _unk10;
  69. @SuppressWarnings("unused")
  70. private int _unk11;
  71. @Override
  72. protected void readImpl()
  73. {
  74. _listId = readD();
  75. _entryId = readD();
  76. _amount = readQ();
  77. _unk1 = readH();
  78. _unk2 = readD();
  79. _unk3 = readD();
  80. _unk4 = readH(); // elemental attributes
  81. _unk5 = readH();// elemental attributes
  82. _unk6 = readH();// elemental attributes
  83. _unk7 = readH();// elemental attributes
  84. _unk8 = readH();// elemental attributes
  85. _unk9 = readH();// elemental attributes
  86. _unk10 = readH();// elemental attributes
  87. _unk11 = readH();// elemental attributes
  88. _enchantment = _entryId % 100000;
  89. _entryId = _entryId / 100000;
  90. _transactionTax = 0; // initialize tax amount to 0...
  91. }
  92. @Override
  93. public void runImpl()
  94. {
  95. L2PcInstance player = getClient().getActiveChar();
  96. if (player == null)
  97. return;
  98. if (!player.getFloodProtectors().getMultiSell().tryPerformAction("multisell choose"))
  99. return;
  100. if (_amount < 1 || _amount > 5000)
  101. return;
  102. MultiSellListContainer list = L2Multisell.getInstance().getList(_listId);
  103. if (list == null)
  104. return;
  105. L2Object target = player.getTarget();
  106. if (!player.isGM() && (target == null
  107. || !(target instanceof L2Npc)
  108. || !list.checkNpcId(((L2Npc)target).getNpcId())
  109. || !((L2Npc)target).canInteract(player)))
  110. return;
  111. for (MultiSellEntry entry : list.getEntries())
  112. {
  113. if (entry.getEntryId() == _entryId)
  114. {
  115. doExchange(player, entry, list.getApplyTaxes(), list.getMaintainEnchantment(), _enchantment);
  116. return;
  117. }
  118. }
  119. }
  120. private void doExchange(L2PcInstance player, MultiSellEntry templateEntry, boolean applyTaxes, boolean maintainEnchantment, int enchantment)
  121. {
  122. PcInventory inv = player.getInventory();
  123. // given the template entry and information about maintaining enchantment and applying taxes
  124. // re-create the instance of the entry that will be used for this exchange
  125. // i.e. change the enchantment level of select ingredient/products and adena amount appropriately.
  126. L2Npc merchant = (player.getTarget() instanceof L2Npc) ? (L2Npc) player.getTarget() : null;
  127. if (merchant == null)
  128. return;
  129. MultiSellEntry entry = prepareEntry(merchant, templateEntry, applyTaxes, maintainEnchantment, enchantment);
  130. int slots = 0;
  131. int weight = 0;
  132. for (MultiSellIngredient e : entry.getProducts())
  133. {
  134. if (e.getItemId() < 0)
  135. continue;
  136. L2Item template = ItemTable.getInstance().getTemplate(e.getItemId());
  137. if (template == null)
  138. continue;
  139. if (!template.isStackable())
  140. slots += e.getItemCount() * _amount;
  141. else if (player.getInventory().getItemByItemId(e.getItemId()) == null)
  142. slots++;
  143. weight += e.getItemCount() * _amount * template.getWeight();
  144. }
  145. if (!inv.validateWeight(weight))
  146. {
  147. player.sendPacket(new SystemMessage(SystemMessageId.WEIGHT_LIMIT_EXCEEDED));
  148. return;
  149. }
  150. if (!inv.validateCapacity(slots))
  151. {
  152. player.sendPacket(new SystemMessage(SystemMessageId.SLOTS_FULL));
  153. return;
  154. }
  155. // Generate a list of distinct ingredients and counts in order to check if the correct item-counts
  156. // are possessed by the player
  157. FastList<MultiSellIngredient> _ingredientsList = FastList.newInstance();
  158. boolean newIng = true;
  159. for (MultiSellIngredient e : entry.getIngredients())
  160. {
  161. newIng = true;
  162. // at this point, the template has already been modified so that enchantments are properly included
  163. // whenever they need to be applied. Uniqueness of items is thus judged by item id AND enchantment level
  164. for (MultiSellIngredient ex : _ingredientsList)
  165. {
  166. // if the item was already added in the list, merely increment the count
  167. // this happens if 1 list entry has the same ingredient twice (example 2 swords = 1 dual)
  168. if ((ex.getItemId() == e.getItemId()) && (ex.getEnchantmentLevel() == e.getEnchantmentLevel()))
  169. {
  170. if ((double) ex.getItemCount() + e.getItemCount() > Integer.MAX_VALUE)
  171. {
  172. player.sendPacket(new SystemMessage(SystemMessageId.YOU_HAVE_EXCEEDED_QUANTITY_THAT_CAN_BE_INPUTTED));
  173. _ingredientsList.clear();
  174. _ingredientsList = null;
  175. return;
  176. }
  177. ex.setItemCount(ex.getItemCount() + e.getItemCount());
  178. newIng = false;
  179. }
  180. }
  181. if (newIng)
  182. {
  183. // if it's a new ingredient, just store its info directly (item id, count, enchantment)
  184. _ingredientsList.add(L2Multisell.getInstance().new MultiSellIngredient(e));
  185. }
  186. }
  187. // now check if the player has sufficient items in the inventory to cover the ingredients' expences
  188. for (MultiSellIngredient e : _ingredientsList)
  189. {
  190. if ((double) e.getItemCount() * _amount > Integer.MAX_VALUE)
  191. {
  192. player.sendPacket(new SystemMessage(SystemMessageId.YOU_HAVE_EXCEEDED_QUANTITY_THAT_CAN_BE_INPUTTED));
  193. _ingredientsList.clear();
  194. _ingredientsList = null;
  195. return;
  196. }
  197. switch (e.getItemId())
  198. {
  199. case -200: // Clan Reputation Score
  200. {
  201. if (player.getClan() == null)
  202. {
  203. player.sendPacket(new SystemMessage(SystemMessageId.YOU_ARE_NOT_A_CLAN_MEMBER));
  204. return;
  205. }
  206. if (!player.isClanLeader())
  207. {
  208. player.sendPacket(new SystemMessage(SystemMessageId.ONLY_THE_CLAN_LEADER_IS_ENABLED));
  209. return;
  210. }
  211. if (player.getClan().getReputationScore() < e.getItemCount() * _amount)
  212. {
  213. player.sendPacket(new SystemMessage(SystemMessageId.THE_CLAN_REPUTATION_SCORE_IS_TOO_LOW));
  214. return;
  215. }
  216. break;
  217. }
  218. case -300: // Player Fame
  219. {
  220. if (player.getFame() < e.getItemCount() * _amount)
  221. {
  222. player.sendPacket(new SystemMessage(SystemMessageId.NOT_ENOUGH_FAME_POINTS));
  223. return;
  224. }
  225. break;
  226. }
  227. default:
  228. {
  229. // if this is not a list that maintains enchantment, check the count of all items that have the given id.
  230. // otherwise, check only the count of items with exactly the needed enchantment level
  231. if (inv.getInventoryItemCount(e.getItemId(), maintainEnchantment ? e.getEnchantmentLevel() : -1, false) < ((Config.ALT_BLACKSMITH_USE_RECIPES || !e.getMantainIngredient()) ? (e.getItemCount() * _amount) : e.getItemCount()))
  232. {
  233. player.sendPacket(new SystemMessage(SystemMessageId.NOT_ENOUGH_REQUIRED_ITEMS));//Update by rocknow
  234. _ingredientsList.clear();
  235. _ingredientsList = null;
  236. return;
  237. }
  238. break;
  239. }
  240. }
  241. }
  242. FastList.recycle(_ingredientsList);
  243. FastList<L2Augmentation> augmentation = FastList.newInstance();
  244. Elementals elemental = null;
  245. /** All ok, remove items and add final product */
  246. for (MultiSellIngredient e : entry.getIngredients())
  247. {
  248. switch (e.getItemId())
  249. {
  250. case -200: // Clan Reputation Score
  251. {
  252. int repCost = (int) (e.getItemCount() * _amount);
  253. player.getClan().takeReputationScore(repCost, true);
  254. SystemMessage smsg = new SystemMessage(SystemMessageId.S1_DEDUCTED_FROM_CLAN_REP);
  255. smsg.addItemNumber(e.getItemCount() * _amount);
  256. player.sendPacket(smsg);
  257. break;
  258. }
  259. case -300: // Player Fame
  260. {
  261. int fameCost = (int) (player.getFame() - (e.getItemCount() * _amount));
  262. player.setFame(fameCost);
  263. player.sendPacket(new UserInfo(player));
  264. player.sendPacket(new ExBrExtraUserInfo(player));
  265. break;
  266. }
  267. default:
  268. {
  269. L2ItemInstance itemToTake = inv.getItemByItemId(e.getItemId()); // initialize and initial guess for the item to take.
  270. if (itemToTake == null)
  271. { //this is a cheat, transaction will be aborted and if any items already taken will not be returned back to inventory!
  272. _log.severe("Character: " + player.getName() + " is trying to cheat in multisell, merchatnt id:" + merchant.getNpcId());
  273. return;
  274. }
  275. /*if (itemToTake.isEquipped())
  276. { //this is a cheat, transaction will be aborted and if any items already taken will not be returned back to inventory!
  277. _log.severe("Character: " + player.getName() + " is trying to cheat in multisell, exchanging equipped item, merchatnt id:" + merchant.getNpcId());
  278. return;
  279. }*/
  280. if (itemToTake.isWear())
  281. {//Player trying to buy something from the Multisell store with an item that's just being used from the Wear option from merchants.
  282. _log.severe("Character: " + player.getName() + " is trying to cheat in multisell, merchatnt id:" + merchant.getNpcId());
  283. return;
  284. }
  285. if (Config.ALT_BLACKSMITH_USE_RECIPES || !e.getMantainIngredient())
  286. {
  287. // if it's a stackable item, just reduce the amount from the first (only) instance that is found in the inventory
  288. if (itemToTake.isStackable())
  289. {
  290. if (!player.destroyItem("Multisell", itemToTake.getObjectId(), (e.getItemCount() * _amount), player.getTarget(), true))
  291. return;
  292. }
  293. else
  294. {
  295. // for non-stackable items, one of two scenaria are possible:
  296. // a) list maintains enchantment: get the instances that exactly match the requested enchantment level
  297. // b) list does not maintain enchantment: get the instances with the LOWEST enchantment level
  298. // a) if enchantment is maintained, then get a list of items that exactly match this enchantment
  299. if (maintainEnchantment)
  300. {
  301. // loop through this list and remove (one by one) each item until the required amount is taken.
  302. L2ItemInstance[] inventoryContents = inv.getAllItemsByItemId(e.getItemId(), e.getEnchantmentLevel(), false);
  303. for (int i = 0; i < (e.getItemCount() * _amount); i++)
  304. {
  305. if (inventoryContents[i].isAugmented())
  306. augmentation.add(inventoryContents[i].getAugmentation());
  307. if(inventoryContents[i].getElementals() != null)
  308. elemental = inventoryContents[i].getElementals();
  309. if (!player.destroyItem("Multisell", inventoryContents[i].getObjectId(), 1, player.getTarget(), true))
  310. return;
  311. }
  312. }
  313. else
  314. // b) enchantment is not maintained. Get the instances with the LOWEST enchantment level
  315. {
  316. /* NOTE: There are 2 ways to achieve the above goal.
  317. * 1) Get all items that have the correct itemId, loop through them until the lowest enchantment
  318. * level is found. Repeat all this for the next item until proper count of items is reached.
  319. * 2) Get all items that have the correct itemId, sort them once based on enchantment level,
  320. * and get the range of items that is necessary.
  321. * Method 1 is faster for a small number of items to be exchanged.
  322. * Method 2 is faster for large amounts.
  323. *
  324. * EXPLANATION:
  325. * Worst case scenario for algorithm 1 will make it run in a number of cycles given by:
  326. * m*(2n-m+1)/2 where m is the number of items to be exchanged and n is the total
  327. * number of inventory items that have a matching id.
  328. * With algorithm 2 (sort), sorting takes n*log(n) time and the choice is done in a single cycle
  329. * for case b (just grab the m first items) or in linear time for case a (find the beginning of items
  330. * with correct enchantment, index x, and take all items from x to x+m).
  331. * Basically, whenever m > log(n) we have: m*(2n-m+1)/2 = (2nm-m*m+m)/2 >
  332. * (2nlogn-logn*logn+logn)/2 = nlog(n) - log(n*n) + log(n) = nlog(n) + log(n/n*n) =
  333. * nlog(n) + log(1/n) = nlog(n) - log(n) = (n-1)log(n)
  334. * So for m < log(n) then m*(2n-m+1)/2 > (n-1)log(n) and m*(2n-m+1)/2 > nlog(n)
  335. *
  336. * IDEALLY:
  337. * In order to best optimize the performance, choose which algorithm to run, based on whether 2^m > n
  338. * if ( (2<<(e.getItemCount() * _amount)) < inventoryContents.length )
  339. * // do Algorithm 1, no sorting
  340. * else
  341. * // do Algorithm 2, sorting
  342. *
  343. * CURRENT IMPLEMENTATION:
  344. * In general, it is going to be very rare for a person to do a massive exchange of non-stackable items
  345. * For this reason, we assume that algorithm 1 will always suffice and we keep things simple.
  346. * If, in the future, it becomes necessary that we optimize, the above discussion should make it clear
  347. * what optimization exactly is necessary (based on the comments under "IDEALLY").
  348. */
  349. // choice 1. Small number of items exchanged. No sorting.
  350. for (int i = 1; i <= (e.getItemCount() * _amount); i++)
  351. {
  352. L2ItemInstance[] inventoryContents = inv.getAllItemsByItemId(e.getItemId(), false);
  353. itemToTake = inventoryContents[0];
  354. // get item with the LOWEST enchantment level from the inventory...
  355. // +0 is lowest by default...
  356. if (itemToTake.getEnchantLevel() > 0)
  357. {
  358. for (L2ItemInstance item : inventoryContents)
  359. {
  360. if (item.getEnchantLevel() < itemToTake.getEnchantLevel())
  361. {
  362. itemToTake = item;
  363. // nothing will have enchantment less than 0. If a zero-enchanted
  364. // item is found, just take it
  365. if (itemToTake.getEnchantLevel() == 0)
  366. break;
  367. }
  368. }
  369. }
  370. if (!player.destroyItem("Multisell", itemToTake.getObjectId(), 1, player.getTarget(), true))
  371. return;
  372. }
  373. }
  374. }
  375. }
  376. break;
  377. }
  378. }
  379. }
  380. // Generate the appropriate items
  381. for (MultiSellIngredient e : entry.getProducts())
  382. {
  383. switch (e.getItemId())
  384. {
  385. case -200: // Clan Reputation Score - now not supported
  386. {
  387. //player.getClan().setReputationScore((int)(player.getClan().getReputationScore() + e.getItemCount() * _amount), true);
  388. break;
  389. }
  390. case -300: // Player Fame
  391. {
  392. player.setFame((int)(player.getFame() + e.getItemCount() * _amount));
  393. player.sendPacket(new UserInfo(player));
  394. player.sendPacket(new ExBrExtraUserInfo(player));
  395. break;
  396. }
  397. default:
  398. {
  399. L2ItemInstance tempItem = ItemTable.getInstance().createDummyItem(e.getItemId());
  400. if (tempItem == null)
  401. {
  402. _log.severe("Problem with multisell ID:"+_listId+" entry ID:"+_entryId+" - Product ID:"+e.getItemId()+" not exist.");
  403. return;
  404. }
  405. if (tempItem.isStackable())
  406. {
  407. inv.addItem("Multisell", e.getItemId(), (e.getItemCount() * _amount), player, player.getTarget());
  408. }
  409. else
  410. {
  411. L2ItemInstance product = null;
  412. for (int i = 0; i < (e.getItemCount() * _amount); i++)
  413. {
  414. product = inv.addItem("Multisell", e.getItemId(), 1, player, player.getTarget());
  415. if (maintainEnchantment)
  416. {
  417. if (i < augmentation.size())
  418. product.setAugmentation(new L2Augmentation(augmentation.get(i).getAugmentationId(), augmentation.get(i).getSkill()));
  419. if (elemental != null)
  420. product.setElementAttr(elemental.getElement(), elemental.getValue());
  421. product.setEnchantLevel(e.getEnchantmentLevel());
  422. product.updateDatabase();
  423. }
  424. }
  425. }
  426. // msg part
  427. SystemMessage sm;
  428. if (e.getItemCount() * _amount > 1)
  429. {
  430. sm = new SystemMessage(SystemMessageId.EARNED_S2_S1_S);
  431. sm.addItemName(e.getItemId());
  432. sm.addItemNumber(e.getItemCount() * _amount);
  433. player.sendPacket(sm);
  434. sm = null;
  435. }
  436. else
  437. {
  438. if (maintainEnchantment && e.getEnchantmentLevel() > 0)
  439. {
  440. sm = new SystemMessage(SystemMessageId.ACQUIRED_S1_S2);
  441. sm.addItemNumber(e.getEnchantmentLevel());
  442. sm.addItemName(e.getItemId());
  443. }
  444. else
  445. {
  446. sm = new SystemMessage(SystemMessageId.EARNED_ITEM);
  447. sm.addItemName(e.getItemId());
  448. }
  449. player.sendPacket(sm);
  450. sm = null;
  451. }
  452. }
  453. }
  454. }
  455. player.sendPacket(new ItemList(player, false));
  456. StatusUpdate su = new StatusUpdate(player.getObjectId());
  457. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  458. player.sendPacket(su);
  459. su = null;
  460. // finally, give the tax to the castle...
  461. if (merchant.getIsInTown() && merchant.getCastle().getOwnerId() > 0)
  462. merchant.getCastle().addToTreasury(_transactionTax * _amount);
  463. FastList.recycle(augmentation);
  464. }
  465. // Regarding taxation, the following appears to be the case:
  466. // a) The count of aa remains unchanged (taxes do not affect aa directly).
  467. // b) 5/6 of the amount of aa is taxed by the normal tax rate.
  468. // c) the resulting taxes are added as normal adena value.
  469. // d) normal adena are taxed fully.
  470. // e) Items other than adena and ancient adena are not taxed even when the list is taxable.
  471. // example: If the template has an item worth 120aa, and the tax is 10%,
  472. // then from 120aa, take 5/6 so that is 100aa, apply the 10% tax in adena (10a)
  473. // so the final price will be 120aa and 10a!
  474. private MultiSellEntry prepareEntry(L2Npc merchant, MultiSellEntry templateEntry, boolean applyTaxes, boolean maintainEnchantment, int enchantLevel)
  475. {
  476. MultiSellEntry newEntry = L2Multisell.getInstance().new MultiSellEntry();
  477. newEntry.setEntryId(templateEntry.getEntryId());
  478. long totalAdenaCount = 0;
  479. boolean hasIngredient = false;
  480. for (MultiSellIngredient ing : templateEntry.getIngredients())
  481. {
  482. // load the ingredient from the template
  483. MultiSellIngredient newIngredient = L2Multisell.getInstance().new MultiSellIngredient(ing);
  484. if (newIngredient.getItemId() == 57 && newIngredient.isTaxIngredient())
  485. {
  486. double taxRate = 0.0;
  487. if (applyTaxes)
  488. {
  489. if (merchant != null && merchant.getIsInTown())
  490. taxRate = merchant.getCastle().getTaxRate();
  491. }
  492. _transactionTax = Math.round(newIngredient.getItemCount() * taxRate);
  493. totalAdenaCount += _transactionTax;
  494. continue; // do not yet add this adena amount to the list as non-taxIngredient adena might be entered later (order not guaranteed)
  495. }
  496. else if (ing.getItemId() == 57) // && !ing.isTaxIngredient()
  497. {
  498. totalAdenaCount += newIngredient.getItemCount();
  499. continue; // do not yet add this adena amount to the list as taxIngredient adena might be entered later (order not guaranteed)
  500. }
  501. // if it is an armor/weapon, modify the enchantment level appropriately, if necessary
  502. // not used for clan reputation and fame
  503. else if (maintainEnchantment && newIngredient.getItemId() > 0)
  504. {
  505. L2Item tempItem = ItemTable.getInstance().createDummyItem(newIngredient.getItemId()).getItem();
  506. if ((tempItem instanceof L2Armor) || (tempItem instanceof L2Weapon))
  507. {
  508. newIngredient.setEnchantmentLevel(enchantLevel);
  509. hasIngredient = true;
  510. }
  511. }
  512. // finally, add this ingredient to the entry
  513. newEntry.addIngredient(newIngredient);
  514. }
  515. // Next add the adena amount, if any
  516. if (totalAdenaCount > 0)
  517. newEntry.addIngredient(L2Multisell.getInstance().new MultiSellIngredient(57, totalAdenaCount, false, false));
  518. // Now modify the enchantment level of products, if necessary
  519. for (MultiSellIngredient ing : templateEntry.getProducts())
  520. {
  521. // load the ingredient from the template
  522. MultiSellIngredient newIngredient = L2Multisell.getInstance().new MultiSellIngredient(ing);
  523. if (maintainEnchantment && hasIngredient)
  524. {
  525. // if it is an armor/weapon, modify the enchantment level appropriately
  526. // (note, if maintain enchantment is "false" this modification will result to a +0)
  527. L2Item tempItem = ItemTable.getInstance().createDummyItem(newIngredient.getItemId()).getItem();
  528. if ((tempItem instanceof L2Armor) || (tempItem instanceof L2Weapon))
  529. newIngredient.setEnchantmentLevel(enchantLevel);
  530. }
  531. newEntry.addProduct(newIngredient);
  532. }
  533. return newEntry;
  534. }
  535. @Override
  536. public String getType()
  537. {
  538. return _C__A7_MULTISELLCHOOSE;
  539. }
  540. }