MerchantPriceConfigTable.java 7.1 KB

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