MerchantPriceConfigTable.java 7.0 KB

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