MerchantPriceConfigTable.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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.datatables;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.Map;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import javax.xml.parsers.DocumentBuilderFactory;
  22. import javax.xml.parsers.ParserConfigurationException;
  23. import javolution.util.FastMap;
  24. import net.sf.l2j.Config;
  25. import net.sf.l2j.gameserver.instancemanager.CastleManager;
  26. import net.sf.l2j.gameserver.model.actor.instance.L2MerchantInstance;
  27. import net.sf.l2j.gameserver.model.entity.Castle;
  28. import org.w3c.dom.Document;
  29. import org.w3c.dom.Node;
  30. import org.xml.sax.SAXException;
  31. /**
  32. *
  33. * @author KenM
  34. */
  35. public class MerchantPriceConfigTable
  36. {
  37. private static Logger _log = Logger.getLogger(MerchantPriceConfigTable.class.getName());
  38. private static MerchantPriceConfigTable _instance;
  39. public static MerchantPriceConfigTable getInstance()
  40. {
  41. if (_instance == null)
  42. {
  43. _instance = new MerchantPriceConfigTable();
  44. }
  45. return _instance;
  46. }
  47. private static final String MPCS_FILE = "MerchantPriceConfig.xml";
  48. private Map<Integer, MerchantPriceConfig> _mpcs = new FastMap<Integer, MerchantPriceConfig>();
  49. private MerchantPriceConfig _defaultMpc;
  50. private MerchantPriceConfigTable()
  51. {
  52. try
  53. {
  54. this.loadXML();
  55. _log.info("MerchantPriceConfigTable: Loaded " + _mpcs.size() + " merchant price configs.");
  56. }
  57. catch (Exception e)
  58. {
  59. _log.log(Level.SEVERE, "Failed loading MerchantPriceConfigTable. Reason: " + e.getMessage(), e);
  60. }
  61. }
  62. public MerchantPriceConfig getMerchantPriceConfig(L2MerchantInstance npc)
  63. {
  64. for (MerchantPriceConfig mpc : _mpcs.values())
  65. {
  66. if (npc.getWorldRegion().containsZone(mpc.getZoneId()))
  67. {
  68. return mpc;
  69. }
  70. }
  71. return _defaultMpc;
  72. }
  73. public MerchantPriceConfig getMerchantPriceConfig(int id)
  74. {
  75. return _mpcs.get(id);
  76. }
  77. public void loadXML() throws SAXException, IOException, ParserConfigurationException
  78. {
  79. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  80. factory.setValidating(false);
  81. factory.setIgnoringComments(true);
  82. File file = new File(Config.DATAPACK_ROOT + "/data/" + MPCS_FILE);
  83. if (file.exists())
  84. {
  85. int defaultPriceConfigId;
  86. Document doc = factory.newDocumentBuilder().parse(file);
  87. Node n = doc.getDocumentElement();
  88. Node dpcNode = n.getAttributes().getNamedItem("defaultPriceConfig");
  89. if (dpcNode == null)
  90. {
  91. throw new IllegalStateException("merchantPriceConfig must define an 'defaultPriceConfig'");
  92. }
  93. else
  94. {
  95. defaultPriceConfigId = Integer.parseInt(dpcNode.getNodeValue());
  96. }
  97. MerchantPriceConfig mpc;
  98. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  99. {
  100. mpc = this.parseMerchantPriceConfig(n);
  101. if (mpc != null)
  102. {
  103. _mpcs.put(mpc.getId(), mpc);
  104. }
  105. }
  106. MerchantPriceConfig defaultMpc = this.getMerchantPriceConfig(defaultPriceConfigId);
  107. if (defaultMpc == null)
  108. {
  109. throw new IllegalStateException("'defaultPriceConfig' points to an non-loaded priceConfig");
  110. }
  111. _defaultMpc = defaultMpc;
  112. }
  113. }
  114. private MerchantPriceConfig parseMerchantPriceConfig(Node n)
  115. {
  116. if (n.getNodeName().equals("priceConfig"))
  117. {
  118. int id, baseTax, castleId, zoneId = -1;
  119. String name;
  120. Castle castle = null;
  121. Node node = n.getAttributes().getNamedItem("id");
  122. if (node == null)
  123. {
  124. throw new IllegalStateException("Must define the priceConfig 'id'");
  125. }
  126. else
  127. {
  128. id = Integer.parseInt(node.getNodeValue());
  129. }
  130. node = n.getAttributes().getNamedItem("name");
  131. if (node == null)
  132. {
  133. throw new IllegalStateException("Must define the priceConfig 'name'");
  134. }
  135. else
  136. {
  137. name = node.getNodeValue();
  138. }
  139. node = n.getAttributes().getNamedItem("baseTax");
  140. if (node == null)
  141. {
  142. throw new IllegalStateException("Must define the priceConfig 'baseTax'");
  143. }
  144. else
  145. {
  146. baseTax = Integer.parseInt(node.getNodeValue());
  147. }
  148. node = n.getAttributes().getNamedItem("castleId");
  149. if (node != null)
  150. {
  151. castleId = Integer.parseInt(node.getNodeValue());
  152. castle = CastleManager.getInstance().getCastleById(castleId);
  153. }
  154. node = n.getAttributes().getNamedItem("zoneId");
  155. if (node != null)
  156. {
  157. zoneId = Integer.parseInt(node.getNodeValue());
  158. }
  159. return new MerchantPriceConfig(id, name, baseTax, castle, zoneId);
  160. }
  161. return null;
  162. }
  163. /**
  164. *
  165. *
  166. * @author KenM
  167. */
  168. public static final class MerchantPriceConfig
  169. {
  170. private final int _id;
  171. private final String _name;
  172. private final int _baseTax;
  173. private final Castle _castle;
  174. private final int _zoneId;
  175. public MerchantPriceConfig(int id, String name, int baseTax, Castle castle, int zoneId)
  176. {
  177. _id = id;
  178. _name = name;
  179. _baseTax = baseTax;
  180. _castle = castle;
  181. _zoneId = zoneId;
  182. }
  183. /**
  184. * @return Returns the id.
  185. */
  186. public int getId()
  187. {
  188. return _id;
  189. }
  190. /**
  191. * @return Returns the name.
  192. */
  193. public String getName()
  194. {
  195. return _name;
  196. }
  197. /**
  198. * @return Returns the baseTax.
  199. */
  200. public int getBaseTax()
  201. {
  202. return _baseTax;
  203. }
  204. /**
  205. * @return Returns the baseTax / 100.0.
  206. */
  207. public double getBaseTaxRate()
  208. {
  209. return _baseTax / 100.0;
  210. }
  211. /**
  212. * @return Returns the castle.
  213. */
  214. public Castle getCastle()
  215. {
  216. return _castle;
  217. }
  218. /**
  219. * @return Returns the zoneId.
  220. */
  221. public int getZoneId()
  222. {
  223. return _zoneId;
  224. }
  225. public boolean hasCastle()
  226. {
  227. return getCastle() != null;
  228. }
  229. public double getCastleTaxRate()
  230. {
  231. return this.hasCastle() ? this.getCastle().getTaxRate() : 0.0;
  232. }
  233. public int getTotalTax()
  234. {
  235. return this.hasCastle() ? (getCastle().getTaxPercent() + getBaseTax()) : getBaseTax();
  236. }
  237. public double getTotalTaxRate()
  238. {
  239. return this.getTotalTax() / 100.0;
  240. }
  241. }
  242. }