2
0

MerchantPriceConfigTable.java 7.1 KB

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