DocumentItem.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 net.sf.l2j.gameserver.skills;
  16. import java.io.File;
  17. import java.util.List;
  18. import java.util.Map;
  19. import javolution.util.FastList;
  20. import javolution.util.FastMap;
  21. import net.sf.l2j.gameserver.Item;
  22. import net.sf.l2j.gameserver.templates.L2Armor;
  23. import net.sf.l2j.gameserver.templates.L2ArmorType;
  24. import net.sf.l2j.gameserver.templates.L2EtcItem;
  25. import net.sf.l2j.gameserver.templates.L2EtcItemType;
  26. import net.sf.l2j.gameserver.templates.L2Item;
  27. import net.sf.l2j.gameserver.templates.L2Weapon;
  28. import net.sf.l2j.gameserver.templates.L2WeaponType;
  29. import net.sf.l2j.gameserver.templates.StatsSet;
  30. import org.w3c.dom.Document;
  31. import org.w3c.dom.Node;
  32. /**
  33. * @author mkizub
  34. *
  35. * TODO To change the template for this generated type comment go to
  36. * Window - Preferences - Java - Code Style - Code Templates
  37. */
  38. final class DocumentItem extends DocumentBase
  39. {
  40. private Item _currentItem = null;
  41. private List<L2Item> _itemsInFile = new FastList<L2Item>();
  42. private Map<Integer, Item> _itemData = new FastMap<Integer, Item>();
  43. /**
  44. * @param armorData
  45. * @param f
  46. */
  47. public DocumentItem(Map<Integer, Item> pItemData, File file)
  48. {
  49. super(file);
  50. _itemData = pItemData;
  51. }
  52. /**
  53. * @param item
  54. */
  55. private void setCurrentItem(Item item)
  56. {
  57. _currentItem = item;
  58. }
  59. @Override
  60. protected StatsSet getStatsSet()
  61. {
  62. return _currentItem.set;
  63. }
  64. @Override
  65. protected String getTableValue(String name)
  66. {
  67. return _tables.get(name)[_currentItem.currentLevel];
  68. }
  69. @Override
  70. protected String getTableValue(String name, int idx)
  71. {
  72. return _tables.get(name)[idx - 1];
  73. }
  74. @Override
  75. protected void parseDocument(Document doc)
  76. {
  77. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  78. {
  79. if ("list".equalsIgnoreCase(n.getNodeName()))
  80. {
  81. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  82. {
  83. if ("item".equalsIgnoreCase(d.getNodeName()))
  84. {
  85. setCurrentItem(new Item());
  86. parseItem(d);
  87. _itemsInFile.add(_currentItem.item);
  88. resetTable();
  89. }
  90. }
  91. }
  92. else if ("item".equalsIgnoreCase(n.getNodeName()))
  93. {
  94. setCurrentItem(new Item());
  95. parseItem(n);
  96. _itemsInFile.add(_currentItem.item);
  97. }
  98. }
  99. }
  100. protected void parseItem(Node n)
  101. {
  102. int itemId = Integer.parseInt(n.getAttributes().getNamedItem("id").getNodeValue());
  103. String itemName = n.getAttributes().getNamedItem("name").getNodeValue();
  104. _currentItem.id = itemId;
  105. _currentItem.name = itemName;
  106. Item item;
  107. if ((item = _itemData.get(_currentItem.id)) == null)
  108. {
  109. throw new IllegalStateException("No SQL data for Item ID: "+itemId+" - name: "+itemName);
  110. }
  111. _currentItem.set = item.set;
  112. _currentItem.type = item.type;
  113. Node first = n.getFirstChild();
  114. for (n = first; n != null; n = n.getNextSibling())
  115. {
  116. if ("table".equalsIgnoreCase(n.getNodeName())) parseTable(n);
  117. }
  118. for (n = first; n != null; n = n.getNextSibling())
  119. {
  120. if ("set".equalsIgnoreCase(n.getNodeName()))
  121. parseBeanSet(n, _itemData.get(_currentItem.id).set, 1);
  122. }
  123. for (n = first; n != null; n = n.getNextSibling())
  124. {
  125. if ("for".equalsIgnoreCase(n.getNodeName()))
  126. {
  127. makeItem();
  128. parseTemplate(n, _currentItem.item);
  129. }
  130. }
  131. }
  132. private void makeItem()
  133. {
  134. if (_currentItem.item != null) return;
  135. if (_currentItem.type instanceof L2ArmorType) _currentItem.item = new L2Armor(
  136. (L2ArmorType) _currentItem.type, _currentItem.set);
  137. else if (_currentItem.type instanceof L2WeaponType) _currentItem.item = new L2Weapon(
  138. (L2WeaponType) _currentItem.type, _currentItem.set);
  139. else if (_currentItem.type instanceof L2EtcItemType) _currentItem.item = new L2EtcItem(
  140. (L2EtcItemType) _currentItem.type, _currentItem.set);
  141. else throw new Error("Unknown item type " + _currentItem.type);
  142. }
  143. /**
  144. * @return
  145. */
  146. public List<L2Item> getItemList()
  147. {
  148. return _itemsInFile;
  149. }
  150. }