DocumentItem.java 6.2 KB

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