MerchantPriceConfigTable.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (C) 2004-2015 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.HashMap;
  23. import java.util.Map;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javax.xml.parsers.DocumentBuilderFactory;
  27. import javax.xml.parsers.ParserConfigurationException;
  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. // Zoey76: TODO: Implement using IXmlReader.
  42. private static Logger LOGGER = Logger.getLogger(MerchantPriceConfigTable.class.getName());
  43. public static MerchantPriceConfigTable getInstance()
  44. {
  45. return SingletonHolder._instance;
  46. }
  47. private static final String MPCS_FILE = "MerchantPriceConfig.xml";
  48. private final Map<Integer, MerchantPriceConfig> _mpcs = new HashMap<>();
  49. private MerchantPriceConfig _defaultMpc;
  50. public MerchantPriceConfig getMerchantPriceConfig(L2MerchantInstance npc)
  51. {
  52. for (MerchantPriceConfig mpc : _mpcs.values())
  53. {
  54. if ((npc.getWorldRegion() != null) && npc.getWorldRegion().containsZone(mpc.getZoneId()))
  55. {
  56. return mpc;
  57. }
  58. }
  59. return _defaultMpc;
  60. }
  61. public MerchantPriceConfig getMerchantPriceConfig(int id)
  62. {
  63. return _mpcs.get(id);
  64. }
  65. public void loadXML() throws SAXException, IOException, ParserConfigurationException
  66. {
  67. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  68. factory.setValidating(false);
  69. factory.setIgnoringComments(true);
  70. File file = new File(Config.DATAPACK_ROOT + "/data/" + MPCS_FILE);
  71. if (file.exists())
  72. {
  73. int defaultPriceConfigId;
  74. Document doc = factory.newDocumentBuilder().parse(file);
  75. Node n = doc.getDocumentElement();
  76. Node dpcNode = n.getAttributes().getNamedItem("defaultPriceConfig");
  77. if (dpcNode == null)
  78. {
  79. throw new IllegalStateException("merchantPriceConfig must define an 'defaultPriceConfig'");
  80. }
  81. defaultPriceConfigId = Integer.parseInt(dpcNode.getNodeValue());
  82. MerchantPriceConfig mpc;
  83. for (n = n.getFirstChild(); n != null; n = n.getNextSibling())
  84. {
  85. mpc = parseMerchantPriceConfig(n);
  86. if (mpc != null)
  87. {
  88. _mpcs.put(mpc.getId(), mpc);
  89. }
  90. }
  91. MerchantPriceConfig defaultMpc = this.getMerchantPriceConfig(defaultPriceConfigId);
  92. if (defaultMpc == null)
  93. {
  94. throw new IllegalStateException("'defaultPriceConfig' points to an non-loaded priceConfig");
  95. }
  96. _defaultMpc = defaultMpc;
  97. }
  98. }
  99. private MerchantPriceConfig parseMerchantPriceConfig(Node n)
  100. {
  101. if (n.getNodeName().equals("priceConfig"))
  102. {
  103. final int id;
  104. final int baseTax;
  105. int castleId = -1;
  106. int zoneId = -1;
  107. final String name;
  108. Node node = n.getAttributes().getNamedItem("id");
  109. if (node == null)
  110. {
  111. throw new IllegalStateException("Must define the priceConfig 'id'");
  112. }
  113. id = Integer.parseInt(node.getNodeValue());
  114. node = n.getAttributes().getNamedItem("name");
  115. if (node == null)
  116. {
  117. throw new IllegalStateException("Must define the priceConfig 'name'");
  118. }
  119. name = node.getNodeValue();
  120. node = n.getAttributes().getNamedItem("baseTax");
  121. if (node == null)
  122. {
  123. throw new IllegalStateException("Must define the priceConfig 'baseTax'");
  124. }
  125. baseTax = Integer.parseInt(node.getNodeValue());
  126. node = n.getAttributes().getNamedItem("castleId");
  127. if (node != null)
  128. {
  129. castleId = Integer.parseInt(node.getNodeValue());
  130. }
  131. node = n.getAttributes().getNamedItem("zoneId");
  132. if (node != null)
  133. {
  134. zoneId = Integer.parseInt(node.getNodeValue());
  135. }
  136. return new MerchantPriceConfig(id, name, baseTax, castleId, zoneId);
  137. }
  138. return null;
  139. }
  140. @Override
  141. public void loadInstances()
  142. {
  143. try
  144. {
  145. loadXML();
  146. LOGGER.info(getClass().getSimpleName() + ": Loaded " + _mpcs.size() + " merchant price configs.");
  147. }
  148. catch (Exception e)
  149. {
  150. LOGGER.log(Level.SEVERE, getClass().getSimpleName() + ": Failed loading MerchantPriceConfigTable. Reason: " + e.getMessage(), e);
  151. }
  152. }
  153. @Override
  154. public void updateReferences()
  155. {
  156. for (final MerchantPriceConfig mpc : _mpcs.values())
  157. {
  158. mpc.updateReferences();
  159. }
  160. }
  161. @Override
  162. public void activateInstances()
  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 hasCastle() ? getCastle().getTaxRate() : 0.0;
  233. }
  234. public int getTotalTax()
  235. {
  236. return hasCastle() ? (getCastle().getTaxPercent() + getBaseTax()) : getBaseTax();
  237. }
  238. public double getTotalTaxRate()
  239. {
  240. return getTotalTax() / 100.0;
  241. }
  242. public void updateReferences()
  243. {
  244. if (_castleId > 0)
  245. {
  246. _castle = CastleManager.getInstance().getCastleById(_castleId);
  247. }
  248. }
  249. }
  250. private static class SingletonHolder
  251. {
  252. protected static final MerchantPriceConfigTable _instance = new MerchantPriceConfigTable();
  253. }
  254. }