DocumentItem.java 6.3 KB

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