Advertisement
Nik

Untitled

Nik
Apr 10th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1. Index: java/com/l2jserver/gameserver/datatables/TeleportDataTable.java
  2. ===================================================================
  3. --- java/com/l2jserver/gameserver/datatables/TeleportDataTable.java (revision 0)
  4. +++ java/com/l2jserver/gameserver/datatables/TeleportDataTable.java (revision 0)
  5. @@ -0,0 +1,334 @@
  6. +/*
  7. + * This program is free software: you can redistribute it and/or modify it under
  8. + * the terms of the GNU General Public License as published by the Free Software
  9. + * Foundation, either version 3 of the License, or (at your option) any later
  10. + * version.
  11. + *
  12. + * This program is distributed in the hope that it will be useful, but WITHOUT
  13. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  15. + * details.
  16. + *
  17. + * You should have received a copy of the GNU General Public License along with
  18. + * this program. If not, see <http://www.gnu.org/licenses/>.
  19. + */
  20. +package com.l2jserver.gameserver.datatables;
  21. +
  22. +import java.io.File;
  23. +import java.util.Map;
  24. +import java.util.logging.Level;
  25. +import java.util.logging.Logger;
  26. +
  27. +import javax.xml.parsers.DocumentBuilderFactory;
  28. +
  29. +import org.w3c.dom.Document;
  30. +import org.w3c.dom.NamedNodeMap;
  31. +import org.w3c.dom.Node;
  32. +
  33. +import javolution.util.FastMap;
  34. +
  35. +import com.l2jserver.Config;
  36. +import com.l2jserver.gameserver.model.Location;
  37. +
  38. +/**
  39. + *
  40. + * @author Nik
  41. + *
  42. + */
  43. +public class TeleportDataTable
  44. +{
  45. + private static Logger _log = Logger.getLogger(TeleportDataTable.class.getName());
  46. +
  47. + private Map<Integer, FastMap<Integer, TeleportData>> _npcTeleports;
  48. + private int _lastDynamicId = 300000;
  49. +
  50. + enum TeleportType
  51. + {
  52. + NORMAL,
  53. + NOBLESSE,
  54. + }
  55. +
  56. + public static TeleportDataTable getInstance()
  57. + {
  58. + return SingletonHolder._instance;
  59. + }
  60. +
  61. + private TeleportDataTable()
  62. + {
  63. + reloadAll();
  64. + }
  65. +
  66. + public void reloadAll()
  67. + {
  68. + _npcTeleports = new FastMap<Integer, FastMap<Integer, TeleportData>>();
  69. +
  70. + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  71. + factory.setIgnoringComments(true);
  72. + factory.setIgnoringElementContentWhitespace(true);
  73. + File file = new File(Config.DATAPACK_ROOT, "data/teleportData.xml");
  74. + Document doc = null;
  75. +
  76. + if (file.exists())
  77. + {
  78. + try
  79. + {
  80. + doc = factory.newDocumentBuilder().parse(file);
  81. + }
  82. + catch (Exception e)
  83. + {
  84. + _log.log(Level.WARNING, "Could not parse teleportData.xml file: " + e.getMessage(), e);
  85. + }
  86. +
  87. + for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  88. + {
  89. + if ("list".equalsIgnoreCase(n.getNodeName()))
  90. + {
  91. + for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  92. + {
  93. + if ("npc".equalsIgnoreCase(d.getNodeName()))
  94. + {
  95. + NamedNodeMap attrs = d.getAttributes();
  96. + Node att;
  97. + int[] npcIds;
  98. +
  99. + att = attrs.getNamedItem("ids");
  100. + if (att == null)
  101. + {
  102. + _log.severe("[TeleportDataTable] Missing npc Ids... skipping");
  103. + continue;
  104. + }
  105. + String[] split = att.getNodeValue().trim().split(",");
  106. + npcIds = new int[split.length];
  107. +
  108. + for (int i = 0; i < split.length; i++)
  109. + {
  110. + try
  111. + {
  112. + npcIds[i] = Integer.parseInt(split[i]);
  113. + }
  114. + catch (NumberFormatException e)
  115. + {
  116. + _log.warning("[TeleportDataTable] cannot parse to int "+e.getMessage());
  117. + }
  118. + }
  119. +
  120. + // For any <teleport... /> fields inside <npc... >
  121. + for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
  122. + {
  123. + TeleportData tmp = parseTeleportData(cd);
  124. +
  125. + if (tmp == null)
  126. + continue;
  127. +
  128. + for (int npcId : npcIds)
  129. + {
  130. + // If the NPC where we want to put the TeleportData is not in the Map, lets put it, because we will need it.
  131. + if (_npcTeleports.get(npcId) == null)
  132. + _npcTeleports.put(npcId, new FastMap<Integer, TeleportData>());
  133. +
  134. + _npcTeleports.get(npcId).put(tmp.getId(), tmp);
  135. + }
  136. + }
  137. + }
  138. + }
  139. + }
  140. + }
  141. + }
  142. + }
  143. +
  144. + private TeleportData parseTeleportData(Node cd)
  145. + {
  146. + TeleportData tmp = null;
  147. +
  148. + if ("teleport".equalsIgnoreCase(cd.getNodeName()))
  149. + {
  150. + Node val = null;
  151. + int id = -1;
  152. + String name = "";
  153. + int x = 0;
  154. + int y = 0;
  155. + int z = 0;
  156. + int heading = 0;
  157. + String price = "";
  158. + TeleportType type = TeleportType.NOBLESSE;
  159. +
  160. + try
  161. + {
  162. + val = cd.getAttributes().getNamedItem("id");
  163. + if (val != null)
  164. + id = Integer.parseInt(val.getNodeValue().trim());
  165. +
  166. + val = cd.getAttributes().getNamedItem("name");
  167. + if (val != null)
  168. + name = val.getNodeValue();
  169. +
  170. + val = cd.getAttributes().getNamedItem("X");
  171. + if (val != null)
  172. + x = Integer.parseInt(val.getNodeValue());
  173. +
  174. + val = cd.getAttributes().getNamedItem("Y");
  175. + if (val != null)
  176. + y = Integer.parseInt(val.getNodeValue());
  177. +
  178. + val = cd.getAttributes().getNamedItem("Z");
  179. + if (val != null)
  180. + z = Integer.parseInt(val.getNodeValue());
  181. +
  182. + val = cd.getAttributes().getNamedItem("heading");
  183. + if (val != null)
  184. + heading = Integer.parseInt(val.getNodeValue());
  185. +
  186. + val = cd.getAttributes().getNamedItem("price");
  187. + if (val != null)
  188. + price = val.getNodeValue();
  189. +
  190. + val = cd.getAttributes().getNamedItem("type");
  191. + if (val != null)
  192. + {
  193. + type = TeleportType.valueOf(val.getNodeValue());
  194. + }
  195. + }
  196. + catch (Exception e)
  197. + {
  198. + _log.log(Level.WARNING, "[TeleportDataTable] Error while parsing TeleportData " + e.getMessage(), e);
  199. + }
  200. + finally
  201. + {
  202. + // We use -1 id, when the teleport id should be dynamic. Also, if a teleport id is greater than 300,000(dynamic id reserved)
  203. + // change the static id to dynamic, to prevent problems.
  204. + if (id == -1 || id > 300000)
  205. + id = _lastDynamicId++;
  206. + tmp = new TeleportData(id, name, new Location(x, y, z, heading), price, type);
  207. + }
  208. + }
  209. +
  210. + return tmp;
  211. + }
  212. +
  213. + /**
  214. + * @param npc id
  215. + * @return Map with all teleports this NPC has
  216. + */
  217. + public FastMap<Integer, TeleportData> getNpcTeleportDatas(int npcId)
  218. + {
  219. + return _npcTeleports.get(npcId);
  220. + }
  221. +
  222. + public TeleportData getNpcTeleportData(int npcId, int teleportId)
  223. + {
  224. + if (_npcTeleports.get(npcId) != null)
  225. + return _npcTeleports.get(npcId).get(teleportId);
  226. + else
  227. + return null;
  228. + }
  229. + /**
  230. + * @param teleportId
  231. + * @return the TeleportData assigned to this teleportId. Static teleport Ids use -1 as NPC Id
  232. + */
  233. + public TeleportData getTeleportData(int teleportId)
  234. + {
  235. + if (_npcTeleports.get(-1) != null)
  236. + return _npcTeleports.get(-1).get(teleportId);
  237. + else
  238. + return null;
  239. + }
  240. +
  241. + @SuppressWarnings("synthetic-access")
  242. + private static class SingletonHolder
  243. + {
  244. + protected static final TeleportDataTable _instance = new TeleportDataTable();
  245. + }
  246. +
  247. + public class TeleportData
  248. + {
  249. + int _id;
  250. + String _name;
  251. + Location _loc;
  252. + int[][] _price;
  253. + TeleportType _type;
  254. +
  255. + private TeleportData(int id, String name, Location loc, int[][] price, TeleportType type)
  256. + {
  257. + insertTeleportData(id, name, loc, price, type);
  258. + }
  259. +
  260. + private TeleportData(int id, String name, Location loc, String price, TeleportType type)
  261. + {
  262. + insertTeleportData(id, name, loc, parsePrice(price), type);
  263. + }
  264. +
  265. + private void insertTeleportData(int id, String name, Location loc, int[][] price, TeleportType type)
  266. + {
  267. + _id = id;
  268. + _name = name;
  269. + _loc = loc;
  270. + _price = price;
  271. + _type = type;
  272. +
  273. + }
  274. +
  275. + private int[][] parsePrice(String price)
  276. + {
  277. + String[] items = price.trim().split(";");
  278. + int[][] tmp = new int[items.length][2];
  279. +
  280. + for (int i = 0; i < items.length; i++)
  281. + {
  282. + int itemId = 0;
  283. + int itemCount = 0;
  284. + try
  285. + {
  286. + String[] split = items[i].trim().split(",");
  287. + itemId = Integer.parseInt(split[0]);
  288. + itemCount = Integer.parseInt(split[1]);
  289. +
  290. + tmp[i][0] = itemId;
  291. + tmp[i][1] = itemCount;
  292. + }
  293. + catch(NumberFormatException e)
  294. + {
  295. + _log.warning("[TeleportDataTable] Error while parsing teleport price, cannot turn to integer values:"+items[i]+" NFE Error:"+e.getMessage());
  296. + }
  297. + }
  298. +
  299. + return tmp;
  300. + }
  301. +
  302. + /**
  303. + * int[][0] = itemId;<BR>
  304. + * int[][1] = itemCount;<BR>
  305. + */
  306. + public int[][] getPrice() {return _price;}
  307. + public int getId() {return _id;}
  308. + public String getName() {return _name;}
  309. + public TeleportType getType() {return _type;}
  310. + public Location getLocation() {return _loc;}
  311. +
  312. + /**
  313. + *
  314. + * @return name - itemId itemCount
  315. + * <br>
  316. + * Example: Rune Township - 53000 Adena
  317. + * <br>
  318. + * If the item is not found or the ID/Count is 0, it will return the teleport name
  319. + */
  320. + /*public String generateFullName()
  321. + {
  322. + // Free Teleport
  323. + if (_price[0][0] == 0 || _price[0][1] == 0)
  324. + {
  325. + return _name;
  326. + }
  327. + else
  328. + {
  329. + L2ItemInstance item = ItemTable.getInstance().createDummyItem(_price[0][0]);
  330. +
  331. + if (item == null)
  332. + return _name;
  333. + else
  334. + return _name + " - " + _price[0][1] + " " + item.getName();
  335. + }
  336. + }*/
  337. +
  338. + }
  339. +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement