MerchantPriceConfigTable.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.datatables;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.util.Map;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import javax.xml.parsers.DocumentBuilderFactory;
  26. import javax.xml.parsers.ParserConfigurationException;
  27. import javolution.util.FastMap;
  28. import org.w3c.dom.Document;
  29. import org.w3c.dom.Node;
  30. import org.xml.sax.SAXException;
  31. import com.l2jserver.Config;
  32. import com.l2jserver.gameserver.InstanceListManager;
  33. import com.l2jserver.gameserver.instancemanager.CastleManager;
  34. import com.l2jserver.gameserver.model.actor.instance.L2MerchantInstance;
  35. import com.l2jserver.gameserver.model.entity.Castle;
  36. /**
  37. * @author KenM
  38. */
  39. public class MerchantPriceConfigTable implements InstanceListManager
  40. {
  41. private static Logger _log = Logger.getLogger(MerchantPriceConfigTable.class.getName());
  42. public static MerchantPriceConfigTable getInstance()
  43. {
  44. return SingletonHolder._instance;
  45. }
  46. private static final String MPCS_FILE = "MerchantPriceConfig.xml";
  47. private final Map<Integer, MerchantPriceConfig> _mpcs = new FastMap<>();
  48. private MerchantPriceConfig _defaultMpc;
  49. public MerchantPriceConfig getMerchantPriceConfig(L2MerchantInstance npc)
  50. {
  51. for (MerchantPriceConfig mpc : _mpcs.values())
  52. {
  53. if ((npc.getWorldRegion() != null) && 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. defaultPriceConfigId = Integer.parseInt(dpcNode.getNodeValue());
  81. MerchantPriceConfig mpc;
  82. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  83. {
  84. mpc = parseMerchantPriceConfig(n);
  85. if (mpc != null)
  86. {
  87. _mpcs.put(mpc.getId(), mpc);
  88. }
  89. }
  90. MerchantPriceConfig defaultMpc = this.getMerchantPriceConfig(defaultPriceConfigId);
  91. if (defaultMpc == null)
  92. {
  93. throw new IllegalStateException("'defaultPriceConfig' points to an non-loaded priceConfig");
  94. }
  95. _defaultMpc = defaultMpc;
  96. }
  97. }
  98. private MerchantPriceConfig parseMerchantPriceConfig(Node n)
  99. {
  100. if (n.getNodeName().equals("priceConfig"))
  101. {
  102. final int id;
  103. final int baseTax;
  104. int castleId = -1;
  105. int zoneId = -1;
  106. final String name;
  107. Node node = n.getAttributes().getNamedItem("id");
  108. if (node == null)
  109. {
  110. throw new IllegalStateException("Must define the priceConfig 'id'");
  111. }
  112. id = Integer.parseInt(node.getNodeValue());
  113. node = n.getAttributes().getNamedItem("name");
  114. if (node == null)
  115. {
  116. throw new IllegalStateException("Must define the priceConfig 'name'");
  117. }
  118. name = node.getNodeValue();
  119. node = n.getAttributes().getNamedItem("baseTax");
  120. if (node == null)
  121. {
  122. throw new IllegalStateException("Must define the priceConfig 'baseTax'");
  123. }
  124. baseTax = Integer.parseInt(node.getNodeValue());
  125. node = n.getAttributes().getNamedItem("castleId");
  126. if (node != null)
  127. {
  128. castleId = Integer.parseInt(node.getNodeValue());
  129. }
  130. node = n.getAttributes().getNamedItem("zoneId");
  131. if (node != null)
  132. {
  133. zoneId = Integer.parseInt(node.getNodeValue());
  134. }
  135. return new MerchantPriceConfig(id, name, baseTax, castleId, zoneId);
  136. }
  137. return null;
  138. }
  139. @Override
  140. public void loadInstances()
  141. {
  142. try
  143. {
  144. loadXML();
  145. _log.info(getClass().getSimpleName() + ": Loaded " + _mpcs.size() + " merchant price configs.");
  146. }
  147. catch (Exception e)
  148. {
  149. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Failed loading MerchantPriceConfigTable. Reason: " + e.getMessage(), e);
  150. }
  151. }
  152. @Override
  153. public void updateReferences()
  154. {
  155. for (final MerchantPriceConfig mpc : _mpcs.values())
  156. {
  157. mpc.updateReferences();
  158. }
  159. }
  160. @Override
  161. public void activateInstances()
  162. {
  163. }
  164. /**
  165. * @author KenM
  166. */
  167. public static final class MerchantPriceConfig
  168. {
  169. private final int _id;
  170. private final String _name;
  171. private final int _baseTax;
  172. private final int _castleId;
  173. private Castle _castle;
  174. private final int _zoneId;
  175. public MerchantPriceConfig(final int id, final String name, final int baseTax, final int castleId, final int zoneId)
  176. {
  177. _id = id;
  178. _name = name;
  179. _baseTax = baseTax;
  180. _castleId = castleId;
  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 hasCastle() ? getCastle().getTaxRate() : 0.0;
  232. }
  233. public int getTotalTax()
  234. {
  235. return hasCastle() ? (getCastle().getTaxPercent() + getBaseTax()) : getBaseTax();
  236. }
  237. public double getTotalTaxRate()
  238. {
  239. return getTotalTax() / 100.0;
  240. }
  241. public void updateReferences()
  242. {
  243. _castle = CastleManager.getInstance().getCastleById(_castleId);
  244. }
  245. }
  246. private static class SingletonHolder
  247. {
  248. protected static final MerchantPriceConfigTable _instance = new MerchantPriceConfigTable();
  249. }
  250. }