L2Multisell.java 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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.model;
  16. import gnu.trove.TIntObjectHashMap;
  17. import java.io.File;
  18. import java.util.List;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import javax.xml.parsers.DocumentBuilderFactory;
  22. import javolution.util.FastList;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.Node;
  25. import com.l2jserver.Config;
  26. import com.l2jserver.gameserver.datatables.ItemTable;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.itemcontainer.PcInventory;
  29. import com.l2jserver.gameserver.network.serverpackets.MultiSellList;
  30. import com.l2jserver.gameserver.templates.item.L2Armor;
  31. import com.l2jserver.gameserver.templates.item.L2Item;
  32. import com.l2jserver.gameserver.templates.item.L2Weapon;
  33. /**
  34. * Multisell list manager.
  35. */
  36. public class L2Multisell
  37. {
  38. private static final Logger _log = Logger.getLogger(L2Multisell.class.getName());
  39. private final TIntObjectHashMap<MultiSellListContainer> _entries = new TIntObjectHashMap<MultiSellListContainer>();
  40. /**
  41. * Instantiates a new l2 multisell.
  42. */
  43. private L2Multisell()
  44. {
  45. reload();
  46. }
  47. /**
  48. * Gets the single instance of L2Multisell.
  49. *
  50. * @return single instance of L2Multisell
  51. */
  52. public static L2Multisell getInstance()
  53. {
  54. return SingletonHolder._instance;
  55. }
  56. /**
  57. * Reload.
  58. */
  59. public void reload()
  60. {
  61. _entries.clear();
  62. parse();
  63. _log.info("L2Multisell: Loaded " + _entries.size() + " lists.");
  64. }
  65. /**
  66. * Gets the list.
  67. *
  68. * @param id the id
  69. * @return the list
  70. */
  71. public MultiSellListContainer getList(int id)
  72. {
  73. final MultiSellListContainer list = _entries.get(id);
  74. if (list != null)
  75. return list;
  76. _log.warning("[L2Multisell] can't find list with id: " + id);
  77. return null;
  78. }
  79. /**
  80. * This will generate the multisell list for the items. There exist various
  81. * parameters in multisells that affect the way they will appear:
  82. * 1) inventory only:
  83. * * if true, only show items of the multisell for which the
  84. * "primary" ingredients are already in the player's inventory. By "primary"
  85. * ingredients we mean weapon and armor.
  86. * * if false, show the entire list.
  87. * 2) maintain enchantment: presumably, only lists with "inventory only" set to true
  88. * should sometimes have this as true. This makes no sense otherwise...
  89. * * If true, then the product will match the enchantment level of the ingredient.
  90. * if the player has multiple items that match the ingredient list but the enchantment
  91. * levels differ, then the entries need to be duplicated to show the products and
  92. * ingredients for each enchantment level.
  93. * For example: If the player has a crystal staff +1 and a crystal staff +3 and goes
  94. * to exchange it at the mammon, the list should have all exchange possibilities for
  95. * the +1 staff, followed by all possibilities for the +3 staff.
  96. * * If false, then any level ingredient will be considered equal and product will always
  97. * be at +0
  98. * 3) apply taxes: Uses the "taxIngredient" entry in order to add a certain amount of adena to the ingredients
  99. *
  100. * @param listId the list id
  101. * @param inventoryOnly the inventory only
  102. * @param player the player
  103. * @param npcId the npc id
  104. * @param taxRate the tax rate
  105. * @return the multisell list container
  106. * @see com.l2jserver.util.network.BaseSendablePacket.ServerBasePacket#runImpl()
  107. */
  108. private MultiSellListContainer generateMultiSell(int listId, boolean inventoryOnly, L2PcInstance player, int npcId, double taxRate)
  109. {
  110. MultiSellListContainer listTemplate = getList(listId);
  111. MultiSellListContainer list = new MultiSellListContainer();
  112. if (listTemplate == null)
  113. return list;
  114. list.setListId(listId);
  115. if (npcId != 0 && !listTemplate.checkNpcId(npcId))
  116. listTemplate.addNpcId(npcId);
  117. if (inventoryOnly)
  118. {
  119. if (player == null)
  120. return list;
  121. L2ItemInstance[] items;
  122. if (listTemplate.getMaintainEnchantment())
  123. items = player.getInventory().getUniqueItemsByEnchantLevel(false, false, false);
  124. else
  125. items = player.getInventory().getUniqueItems(false, false, false);
  126. int enchantLevel, elementId, elementValue, augmentId, fireVal, waterVal, windVal, earthVal, holyVal, darkVal;
  127. for (L2ItemInstance item : items)
  128. {
  129. // only do the matchup on equipable items that are not currently equipped
  130. // so for each appropriate item, produce a set of entries for the multisell list.
  131. if (!item.isEquipped() && ((item.getItem() instanceof L2Armor) || (item.getItem() instanceof L2Weapon)))
  132. {
  133. enchantLevel = (listTemplate.getMaintainEnchantment() ? item.getEnchantLevel() : 0);
  134. augmentId = (listTemplate.getMaintainEnchantment() ? (item.getAugmentation() != null ? item.getAugmentation().getAugmentationId() : 0) : 0);
  135. elementId = (listTemplate.getMaintainEnchantment() ? item.getAttackElementType() : -2);
  136. elementValue = (listTemplate.getMaintainEnchantment() ? item.getAttackElementPower() : 0);
  137. fireVal = (listTemplate.getMaintainEnchantment() ? item.getElementDefAttr(Elementals.FIRE) : 0);
  138. waterVal = (listTemplate.getMaintainEnchantment() ? item.getElementDefAttr(Elementals.WATER) : 0);
  139. windVal = (listTemplate.getMaintainEnchantment() ? item.getElementDefAttr(Elementals.WIND) : 0);
  140. earthVal = (listTemplate.getMaintainEnchantment() ? item.getElementDefAttr(Elementals.EARTH) : 0);
  141. holyVal = (listTemplate.getMaintainEnchantment() ? item.getElementDefAttr(Elementals.HOLY) : 0);
  142. darkVal = (listTemplate.getMaintainEnchantment() ? item.getElementDefAttr(Elementals.DARK) : 0);
  143. // loop through the entries to see which ones we wish to include
  144. for (MultiSellEntry ent : listTemplate.getEntries())
  145. {
  146. boolean doInclude = false;
  147. // check ingredients of this entry to see if it's an entry we'd like to include.
  148. for (MultiSellIngredient ing : ent.getIngredients())
  149. {
  150. if (item.getItemId() == ing.getItemId())
  151. {
  152. doInclude = true;
  153. break;
  154. }
  155. }
  156. // manipulate the ingredients of the template entry for this particular instance shown
  157. // i.e: Assign enchant levels and/or apply taxes as needed.
  158. if (doInclude)
  159. list.addEntry(prepareEntry(ent, listTemplate.getApplyTaxes(), listTemplate.getMaintainEnchantment(), enchantLevel, augmentId, elementId, elementValue, fireVal, waterVal, windVal, earthVal, holyVal, darkVal, taxRate));
  160. }
  161. }
  162. } // end for each inventory item.
  163. } // end if "inventory-only"
  164. else
  165. // this is a list-all type
  166. {
  167. // if no taxes are applied, no modifications are needed
  168. for (MultiSellEntry ent : listTemplate.getEntries())
  169. list.addEntry(prepareEntry(ent, listTemplate.getApplyTaxes(), false, 0, 0, -2, 0, 0, 0, 0, 0, 0, 0, taxRate));
  170. }
  171. return list;
  172. }
  173. // Regarding taxation, the following is the case:
  174. // a) The taxes come out purely from the adena TaxIngredient
  175. // b) If the entry has no adena ingredients other than the taxIngredient, the resulting
  176. // amount of adena is appended to the entry
  177. // c) If the entry already has adena as an entry, the taxIngredient is used in order to increase
  178. // the count for the existing adena ingredient
  179. /**
  180. * Prepare entry.
  181. *
  182. * @param templateEntry the template entry
  183. * @param applyTaxes the apply taxes
  184. * @param maintainEnchantment the maintain enchantment
  185. * @param enchantLevel the enchant level
  186. * @param augmentId the augment id
  187. * @param elementId the element id
  188. * @param elementValue the element value
  189. * @param fireValue the fire value
  190. * @param waterValue the water value
  191. * @param windValue the wind value
  192. * @param earthValue the earth value
  193. * @param holyValue the holy value
  194. * @param darkValue the dark value
  195. * @param taxRate the tax rate
  196. * @return the multisell entry
  197. */
  198. private MultiSellEntry prepareEntry(MultiSellEntry templateEntry, boolean applyTaxes, boolean maintainEnchantment, int enchantLevel, int augmentId, int elementId, int elementValue, int fireValue, int waterValue, int windValue, int earthValue, int holyValue, int darkValue, double taxRate)
  199. {
  200. MultiSellEntry newEntry = new MultiSellEntry();
  201. newEntry.setEntryId(templateEntry.getEntryId() * 100000 + enchantLevel);
  202. long adenaAmount = 0;
  203. for (MultiSellIngredient ing : templateEntry.getIngredients())
  204. {
  205. // load the ingredient from the template
  206. MultiSellIngredient newIngredient = new MultiSellIngredient(ing);
  207. // if taxes are to be applied, modify/add the adena count based on the template adena/ancient adena count
  208. if (ing.getItemId() == PcInventory.ADENA_ID && ing.isTaxIngredient())
  209. {
  210. if (applyTaxes)
  211. adenaAmount += Math.round(ing.getItemCount() * taxRate);
  212. continue; // do not adena yet, as non-taxIngredient adena entries might occur next (order not guaranteed)
  213. }
  214. else if (ing.getItemId() == PcInventory.ADENA_ID) // && !ing.isTaxIngredient()
  215. {
  216. adenaAmount += ing.getItemCount();
  217. continue; // do not adena yet, as taxIngredient adena entries might occur next (order not guaranteed)
  218. }
  219. // if it is an armor/weapon, modify the enchantment level appropriately, if necessary
  220. // not used for clan reputation and fame
  221. else if (maintainEnchantment && newIngredient.getItemId() > 0)
  222. {
  223. L2Item tempItem = ItemTable.getInstance().createDummyItem(ing.getItemId()).getItem();
  224. if ((tempItem instanceof L2Armor) || (tempItem instanceof L2Weapon))
  225. {
  226. newIngredient.setEnchantmentLevel(enchantLevel);
  227. newIngredient.setAugmentId(augmentId);
  228. newIngredient.setElementId(elementId);
  229. newIngredient.setElementValue(elementValue);
  230. newIngredient.setFireValue(fireValue);
  231. newIngredient.setWaterValue(waterValue);
  232. newIngredient.setWindValue(windValue);
  233. newIngredient.setEarthValue(earthValue);
  234. newIngredient.setHolyValue(holyValue);
  235. newIngredient.setDarkValue(darkValue);
  236. }
  237. }
  238. // finally, add this ingredient to the entry
  239. newEntry.addIngredient(newIngredient);
  240. }
  241. // now add the adena, if any.
  242. if (adenaAmount > 0)
  243. {
  244. newEntry.addIngredient(new MultiSellIngredient(PcInventory.ADENA_ID, adenaAmount, 0, 0, -2, 0, 0, 0, 0, 0, 0, 0, false, false));
  245. }
  246. // Now modify the enchantment level of products, if necessary
  247. for (MultiSellIngredient ing : templateEntry.getProducts())
  248. {
  249. // load the ingredient from the template
  250. MultiSellIngredient newIngredient = new MultiSellIngredient(ing);
  251. if (maintainEnchantment)
  252. {
  253. // if it is an armor/weapon, modify the enchantment level appropriately
  254. // (note, if maintain enchantment is "false" this modification will result to a +0)
  255. L2Item tempItem = ItemTable.getInstance().createDummyItem(ing.getItemId()).getItem();
  256. if ((tempItem instanceof L2Armor) || (tempItem instanceof L2Weapon))
  257. {
  258. newIngredient.setEnchantmentLevel(enchantLevel);
  259. newIngredient.setAugmentId(augmentId);
  260. newIngredient.setElementId(elementId);
  261. newIngredient.setElementValue(elementValue);
  262. newIngredient.setFireValue(fireValue);
  263. newIngredient.setWaterValue(waterValue);
  264. newIngredient.setWindValue(windValue);
  265. newIngredient.setEarthValue(earthValue);
  266. newIngredient.setHolyValue(holyValue);
  267. newIngredient.setDarkValue(darkValue);
  268. }
  269. }
  270. newEntry.addProduct(newIngredient);
  271. }
  272. return newEntry;
  273. }
  274. /**
  275. * Separate and send.
  276. *
  277. * @param listId the list id
  278. * @param player the player
  279. * @param npcId the npc id
  280. * @param inventoryOnly the inventory only
  281. * @param taxRate the tax rate
  282. */
  283. public void separateAndSend(int listId, L2PcInstance player, int npcId, boolean inventoryOnly, double taxRate)
  284. {
  285. MultiSellListContainer list = generateMultiSell(listId, inventoryOnly, player, npcId, taxRate);
  286. MultiSellListContainer temp = new MultiSellListContainer();
  287. int page = 1;
  288. temp.setListId(list.getListId());
  289. for (MultiSellEntry e : list.getEntries())
  290. {
  291. if (temp.getEntries().size() == 40)
  292. {
  293. player.sendPacket(new MultiSellList(temp, page++, 0));
  294. temp = new MultiSellListContainer();
  295. temp.setListId(list.getListId());
  296. }
  297. temp.addEntry(e);
  298. }
  299. player.sendPacket(new MultiSellList(temp, page, 1));
  300. }
  301. /**
  302. * The Class MultiSellEntry.
  303. */
  304. public class MultiSellEntry
  305. {
  306. private int _entryId;
  307. private List<MultiSellIngredient> _products = new FastList<MultiSellIngredient>();
  308. private List<MultiSellIngredient> _ingredients = new FastList<MultiSellIngredient>();
  309. /**
  310. * Sets the entry id.
  311. *
  312. * @param entryId The entryId to set.
  313. */
  314. public void setEntryId(int entryId)
  315. {
  316. _entryId = entryId;
  317. }
  318. /**
  319. * Gets the entry id.
  320. *
  321. * @return Returns the entryId.
  322. */
  323. public int getEntryId()
  324. {
  325. return _entryId;
  326. }
  327. /**
  328. * Adds the product.
  329. *
  330. * @param product The product to add.
  331. */
  332. public void addProduct(MultiSellIngredient product)
  333. {
  334. _products.add(product);
  335. }
  336. /**
  337. * Gets the products.
  338. *
  339. * @return Returns the products.
  340. */
  341. public List<MultiSellIngredient> getProducts()
  342. {
  343. return _products;
  344. }
  345. /**
  346. * Adds the ingredient.
  347. *
  348. * @param ingredient The ingredient to add to ingredients.
  349. */
  350. public void addIngredient(MultiSellIngredient ingredient)
  351. {
  352. _ingredients.add(ingredient);
  353. }
  354. /**
  355. * Gets the ingredients.
  356. *
  357. * @return Returns the ingredients.
  358. */
  359. public List<MultiSellIngredient> getIngredients()
  360. {
  361. return _ingredients;
  362. }
  363. /**
  364. * Stackable.
  365. *
  366. * @return the int
  367. */
  368. public int stackable()
  369. {
  370. for (MultiSellIngredient p : _products)
  371. {
  372. if (p.getItemId() > 0)
  373. {
  374. L2Item template = ItemTable.getInstance().getTemplate(p.getItemId());
  375. if (template != null && !template.isStackable())
  376. return 0;
  377. }
  378. }
  379. return 1;
  380. }
  381. }
  382. /**
  383. * The Class MultiSellIngredient.
  384. */
  385. public class MultiSellIngredient
  386. {
  387. private int _itemId, _enchantmentLevel, _element, _elementVal, _augment, _fireVal, _waterVal, _windVal, _earthVal, _holyVal, _darkVal;
  388. private long _itemCount;
  389. private boolean _isTaxIngredient, _maintainIngredient;
  390. /**
  391. * Instantiates a new multisell ingredient.
  392. *
  393. * @param itemId the item id
  394. * @param itemCount the item count
  395. * @param isTaxIngredient the is tax ingredient
  396. * @param maintainIngredient the maintain ingredient
  397. */
  398. public MultiSellIngredient(int itemId, long itemCount, boolean isTaxIngredient, boolean maintainIngredient)
  399. {
  400. this(itemId, itemCount, 0, 0, -2, 0, 0, 0, 0, 0, 0, 0, isTaxIngredient, maintainIngredient);
  401. }
  402. /**
  403. * Instantiates a new multisell ingredient.
  404. *
  405. * @param itemId the item id
  406. * @param itemCount the item count
  407. * @param enchantmentLevel the enchantment level
  408. * @param augmentId the augment id
  409. * @param elementId the element id
  410. * @param elementVal the element val
  411. * @param fireVal the fire val
  412. * @param waterVal the water val
  413. * @param windVal the wind val
  414. * @param earthVal the earth val
  415. * @param holyVal the holy val
  416. * @param darkVal the dark val
  417. * @param isTaxIngredient the is tax ingredient
  418. * @param maintainIngredient the maintain ingredient
  419. */
  420. public MultiSellIngredient(int itemId, long itemCount, int enchantmentLevel, int augmentId, int elementId, int elementVal, int fireVal, int waterVal, int windVal, int earthVal, int holyVal, int darkVal, boolean isTaxIngredient, boolean maintainIngredient)
  421. {
  422. setItemId(itemId);
  423. setItemCount(itemCount);
  424. setEnchantmentLevel(enchantmentLevel);
  425. setAugmentId(augmentId);
  426. setElementId(elementId);
  427. setElementValue(elementVal);
  428. setFireValue(fireVal);
  429. setWaterValue(waterVal);
  430. setWindValue(windVal);
  431. setEarthValue(earthVal);
  432. setHolyValue(holyVal);
  433. setDarkValue(darkVal);
  434. setIsTaxIngredient(isTaxIngredient);
  435. setMaintainIngredient(maintainIngredient);
  436. }
  437. /**
  438. * Instantiates a new multisell ingredient.
  439. *
  440. * @param e the e
  441. */
  442. public MultiSellIngredient(MultiSellIngredient e)
  443. {
  444. _itemId = e.getItemId();
  445. _itemCount = e.getItemCount();
  446. _enchantmentLevel = e.getEnchantmentLevel();
  447. _isTaxIngredient = e.isTaxIngredient();
  448. _maintainIngredient = e.getMaintainIngredient();
  449. _augment = e.getAugmentId();
  450. _element = e.getElementId();
  451. _elementVal = e.getElementVal();
  452. _fireVal = e.getFireVal();
  453. _waterVal = e.getWaterVal();
  454. _windVal = e.getWindVal();
  455. _earthVal = e.getEarthVal();
  456. _holyVal = e.getHolyVal();
  457. _darkVal = e.getDarkVal();
  458. }
  459. /**
  460. * Sets the augment id.
  461. *
  462. * @param augment the new augment id
  463. */
  464. public void setAugmentId(int augment)
  465. {
  466. _augment = augment;
  467. }
  468. /**
  469. * Sets the element id.
  470. *
  471. * @param element the new element id
  472. */
  473. public void setElementId(int element)
  474. {
  475. _element = element;
  476. }
  477. /**
  478. * Sets the element value.
  479. *
  480. * @param elementVal the new element value
  481. */
  482. public void setElementValue(int elementVal)
  483. {
  484. _elementVal = elementVal;
  485. }
  486. /**
  487. * Sets the fire value.
  488. *
  489. * @param val the new fire value
  490. */
  491. public void setFireValue(int val)
  492. {
  493. _fireVal = val;
  494. }
  495. /**
  496. * Sets the water value.
  497. *
  498. * @param val the new water value
  499. */
  500. public void setWaterValue(int val)
  501. {
  502. _waterVal = val;
  503. }
  504. /**
  505. * Sets the wind value.
  506. *
  507. * @param val the new wind value
  508. */
  509. public void setWindValue(int val)
  510. {
  511. _windVal = val;
  512. }
  513. /**
  514. * Sets the earth value.
  515. *
  516. * @param val the new earth value
  517. */
  518. public void setEarthValue(int val)
  519. {
  520. _earthVal = val;
  521. }
  522. /**
  523. * Sets the holy value.
  524. *
  525. * @param val the new holy value
  526. */
  527. public void setHolyValue(int val)
  528. {
  529. _holyVal = val;
  530. }
  531. /**
  532. * Sets the dark value.
  533. *
  534. * @param val the new dark value
  535. */
  536. public void setDarkValue(int val)
  537. {
  538. _darkVal = val;
  539. }
  540. /**
  541. * Gets the augment id.
  542. *
  543. * @return the augment id
  544. */
  545. public int getAugmentId()
  546. {
  547. return _augment;
  548. }
  549. /**
  550. * Gets the element id.
  551. *
  552. * @return the element id
  553. */
  554. public int getElementId()
  555. {
  556. return _element;
  557. }
  558. /**
  559. * Gets the element val.
  560. *
  561. * @return the element val
  562. */
  563. public int getElementVal()
  564. {
  565. return _elementVal;
  566. }
  567. /**
  568. * Gets the fire val.
  569. *
  570. * @return the fire val
  571. */
  572. public int getFireVal()
  573. {
  574. return _fireVal;
  575. }
  576. /**
  577. * Gets the water val.
  578. *
  579. * @return the water val
  580. */
  581. public int getWaterVal()
  582. {
  583. return _waterVal;
  584. }
  585. /**
  586. * Gets the wind val.
  587. *
  588. * @return the wind val
  589. */
  590. public int getWindVal()
  591. {
  592. return _windVal;
  593. }
  594. /**
  595. * Gets the earth val.
  596. *
  597. * @return the earth val
  598. */
  599. public int getEarthVal()
  600. {
  601. return _earthVal;
  602. }
  603. /**
  604. * Gets the holy val.
  605. *
  606. * @return the holy val
  607. */
  608. public int getHolyVal()
  609. {
  610. return _holyVal;
  611. }
  612. /**
  613. * Gets the dark val.
  614. *
  615. * @return the dark val
  616. */
  617. public int getDarkVal()
  618. {
  619. return _darkVal;
  620. }
  621. /**
  622. * Sets the item id.
  623. *
  624. * @param itemId The itemId to set.
  625. */
  626. public void setItemId(int itemId)
  627. {
  628. _itemId = itemId;
  629. }
  630. /**
  631. * Gets the item id.
  632. *
  633. * @return Returns the itemId.
  634. */
  635. public int getItemId()
  636. {
  637. return _itemId;
  638. }
  639. /**
  640. * Sets the item count.
  641. *
  642. * @param itemCount The itemCount to set.
  643. */
  644. public void setItemCount(long itemCount)
  645. {
  646. _itemCount = itemCount;
  647. }
  648. /**
  649. * Gets the item count.
  650. *
  651. * @return Returns the itemCount.
  652. */
  653. public long getItemCount()
  654. {
  655. return _itemCount;
  656. }
  657. /**
  658. * Sets the enchantment level.
  659. *
  660. * @param enchantmentLevel The enchantmentLevel to set.
  661. */
  662. public void setEnchantmentLevel(int enchantmentLevel)
  663. {
  664. _enchantmentLevel = enchantmentLevel;
  665. }
  666. /**
  667. * Gets the enchantment level.
  668. *
  669. * @return Returns the enchantmentLevel.
  670. */
  671. public int getEnchantmentLevel()
  672. {
  673. return _enchantmentLevel;
  674. }
  675. /**
  676. * Sets the checks if is tax ingredient.
  677. *
  678. * @param isTaxIngredient the new checks if is tax ingredient
  679. */
  680. public void setIsTaxIngredient(boolean isTaxIngredient)
  681. {
  682. _isTaxIngredient = isTaxIngredient;
  683. }
  684. /**
  685. * Checks if is tax ingredient.
  686. *
  687. * @return true, if is tax ingredient
  688. */
  689. public boolean isTaxIngredient()
  690. {
  691. return _isTaxIngredient;
  692. }
  693. /**
  694. * Sets the maintain ingredient.
  695. *
  696. * @param maintainIngredient the new maintain ingredient
  697. */
  698. public void setMaintainIngredient(boolean maintainIngredient)
  699. {
  700. _maintainIngredient = maintainIngredient;
  701. }
  702. /**
  703. * Gets the maintain ingredient.
  704. *
  705. * @return the maintain ingredient
  706. */
  707. public boolean getMaintainIngredient()
  708. {
  709. return _maintainIngredient;
  710. }
  711. }
  712. /**
  713. * The Class MultiSellListContainer.
  714. */
  715. public class MultiSellListContainer
  716. {
  717. private int _listId;
  718. private boolean _applyTaxes = false;
  719. private boolean _maintainEnchantment = false;
  720. private List<Integer> _npcIds;
  721. List<MultiSellEntry> _entriesC;
  722. /**
  723. * Instantiates a new multisell list container.
  724. */
  725. public MultiSellListContainer()
  726. {
  727. _entriesC = new FastList<MultiSellEntry>();
  728. }
  729. /**
  730. * Sets the list id.
  731. *
  732. * @param listId The listId to set.
  733. */
  734. public void setListId(int listId)
  735. {
  736. _listId = listId;
  737. }
  738. /**
  739. * Sets the apply taxes.
  740. *
  741. * @param applyTaxes the new apply taxes
  742. */
  743. public void setApplyTaxes(boolean applyTaxes)
  744. {
  745. _applyTaxes = applyTaxes;
  746. }
  747. /**
  748. * Sets the maintain enchantment.
  749. *
  750. * @param maintainEnchantment the new maintain enchantment
  751. */
  752. public void setMaintainEnchantment(boolean maintainEnchantment)
  753. {
  754. _maintainEnchantment = maintainEnchantment;
  755. }
  756. /**
  757. * Adds the npc id.
  758. *
  759. * @param objId the obj id
  760. */
  761. public void addNpcId(int objId)
  762. {
  763. _npcIds.add(objId);
  764. }
  765. /**
  766. * Gets the list id.
  767. *
  768. * @return Returns the listId.
  769. */
  770. public int getListId()
  771. {
  772. return _listId;
  773. }
  774. /**
  775. * Gets the apply taxes.
  776. *
  777. * @return the apply taxes
  778. */
  779. public boolean getApplyTaxes()
  780. {
  781. return _applyTaxes;
  782. }
  783. /**
  784. * Gets the maintain enchantment.
  785. *
  786. * @return the maintain enchantment
  787. */
  788. public boolean getMaintainEnchantment()
  789. {
  790. return _maintainEnchantment;
  791. }
  792. /**
  793. * Check npc id.
  794. *
  795. * @param npcId the npc id
  796. * @return true, if successful
  797. */
  798. public boolean checkNpcId(int npcId)
  799. {
  800. if (_npcIds == null)
  801. {
  802. synchronized (this)
  803. {
  804. if (_npcIds == null)
  805. _npcIds = new FastList<Integer>();
  806. }
  807. return false;
  808. }
  809. return _npcIds.contains(npcId);
  810. }
  811. /**
  812. * Adds the entry.
  813. *
  814. * @param e the e
  815. */
  816. public void addEntry(MultiSellEntry e)
  817. {
  818. _entriesC.add(e);
  819. }
  820. /**
  821. * Gets the entries.
  822. *
  823. * @return the entries
  824. */
  825. public List<MultiSellEntry> getEntries()
  826. {
  827. return _entriesC;
  828. }
  829. }
  830. /**
  831. * Hash files.
  832. *
  833. * @param dirname the dirname
  834. * @param hash the hash
  835. */
  836. private void hashFiles(String dirname, List<File> hash)
  837. {
  838. File dir = new File(Config.DATAPACK_ROOT, "data/" + dirname);
  839. if (!dir.exists())
  840. {
  841. _log.warning("Dir " + dir.getAbsolutePath() + " not exists");
  842. return;
  843. }
  844. File[] files = dir.listFiles();
  845. for (File f : files)
  846. {
  847. if (f.getName().endsWith(".xml"))
  848. hash.add(f);
  849. }
  850. }
  851. /**
  852. * Parses the.
  853. */
  854. private void parse()
  855. {
  856. Document doc = null;
  857. int id = 0;
  858. List<File> files = new FastList<File>();
  859. hashFiles("multisell", files);
  860. for (File f : files)
  861. {
  862. try
  863. {
  864. id = Integer.parseInt(f.getName().replaceAll(".xml", ""));
  865. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  866. factory.setValidating(false);
  867. factory.setIgnoringComments(true);
  868. doc = factory.newDocumentBuilder().parse(f);
  869. }
  870. catch (Exception e)
  871. {
  872. _log.log(Level.SEVERE, "Error loading file " + f, e);
  873. continue;
  874. }
  875. try
  876. {
  877. MultiSellListContainer list = parseDocument(doc);
  878. list.setListId(id);
  879. _entries.put(id, list);
  880. }
  881. catch (Exception e)
  882. {
  883. _log.log(Level.SEVERE, "Error in file " + f, e);
  884. }
  885. }
  886. }
  887. /**
  888. * Parses the document.
  889. *
  890. * @param doc the doc
  891. * @return the multisell list container
  892. */
  893. protected MultiSellListContainer parseDocument(Document doc)
  894. {
  895. MultiSellListContainer list = new MultiSellListContainer();
  896. int entryId = 1;
  897. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  898. {
  899. if ("list".equalsIgnoreCase(n.getNodeName()))
  900. {
  901. if (n.getAttributes() != null)
  902. {
  903. Node attribute = n.getAttributes().getNamedItem("applyTaxes");
  904. if (attribute == null)
  905. list.setApplyTaxes(false);
  906. else
  907. list.setApplyTaxes(Boolean.parseBoolean(attribute.getNodeValue()));
  908. attribute = n.getAttributes().getNamedItem("maintainEnchantment");
  909. if (attribute == null)
  910. list.setMaintainEnchantment(false);
  911. else
  912. list.setMaintainEnchantment(Boolean.parseBoolean(attribute.getNodeValue()));
  913. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  914. {
  915. if ("item".equalsIgnoreCase(d.getNodeName()))
  916. {
  917. MultiSellEntry e = parseEntry(d, entryId++);
  918. list.addEntry(e);
  919. }
  920. }
  921. }
  922. }
  923. else if ("item".equalsIgnoreCase(n.getNodeName()))
  924. {
  925. MultiSellEntry e = parseEntry(n, entryId++);
  926. list.addEntry(e);
  927. }
  928. }
  929. return list;
  930. }
  931. /**
  932. * Parses the entry.
  933. *
  934. * @param n the n
  935. * @param entryId the entry id
  936. * @return the multisell entry
  937. */
  938. protected MultiSellEntry parseEntry(Node n, int entryId)
  939. {
  940. Node first = n.getFirstChild();
  941. MultiSellEntry entry = new MultiSellEntry();
  942. for (n = first; n != null; n = n.getNextSibling())
  943. {
  944. if ("ingredient".equalsIgnoreCase(n.getNodeName()))
  945. {
  946. Node attribute;
  947. int id = Integer.parseInt(n.getAttributes().getNamedItem("id").getNodeValue());
  948. long count = Long.parseLong(n.getAttributes().getNamedItem("count").getNodeValue());
  949. boolean isTaxIngredient = false, maintainIngredient = false;
  950. attribute = n.getAttributes().getNamedItem("isTaxIngredient");
  951. if (attribute != null)
  952. isTaxIngredient = Boolean.parseBoolean(attribute.getNodeValue());
  953. attribute = n.getAttributes().getNamedItem("maintainIngredient");
  954. if (attribute != null)
  955. maintainIngredient = Boolean.parseBoolean(attribute.getNodeValue());
  956. MultiSellIngredient e = new MultiSellIngredient(id, count, isTaxIngredient, maintainIngredient);
  957. entry.addIngredient(e);
  958. validateItemId(id);
  959. }
  960. else if ("production".equalsIgnoreCase(n.getNodeName()))
  961. {
  962. int id = Integer.parseInt(n.getAttributes().getNamedItem("id").getNodeValue());
  963. long count = Long.parseLong(n.getAttributes().getNamedItem("count").getNodeValue());
  964. MultiSellIngredient e = new MultiSellIngredient(id, count, false, false);
  965. entry.addProduct(e);
  966. validateItemId(id);
  967. }
  968. }
  969. entry.setEntryId(entryId);
  970. return entry;
  971. }
  972. /**
  973. * Validate item id.
  974. *
  975. * @param itemId the item id
  976. */
  977. private void validateItemId(int itemId)
  978. {
  979. switch (itemId)
  980. {
  981. case -200: // Clan Reputation Score
  982. case -300: // Player Fame
  983. {
  984. break;
  985. }
  986. default:
  987. {
  988. L2Item template = ItemTable.getInstance().getTemplate(itemId);
  989. if (template == null)
  990. _log.warning("[L2Multisell] can't find item with itemId: " + itemId);
  991. }
  992. }
  993. }
  994. /**
  995. * The Class SingletonHolder.
  996. */
  997. @SuppressWarnings("synthetic-access")
  998. private static class SingletonHolder
  999. {
  1000. protected static final L2Multisell _instance = new L2Multisell();
  1001. }
  1002. }