Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: java/com/l2jserver/gameserver/datatables/TeleportDataTable.java
- ===================================================================
- --- java/com/l2jserver/gameserver/datatables/TeleportDataTable.java (revision 0)
- +++ java/com/l2jserver/gameserver/datatables/TeleportDataTable.java (revision 0)
- @@ -0,0 +1,334 @@
- +/*
- + * This program is free software: you can redistribute it and/or modify it under
- + * the terms of the GNU General Public License as published by the Free Software
- + * Foundation, either version 3 of the License, or (at your option) any later
- + * version.
- + *
- + * This program is distributed in the hope that it will be useful, but WITHOUT
- + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- + * details.
- + *
- + * You should have received a copy of the GNU General Public License along with
- + * this program. If not, see <http://www.gnu.org/licenses/>.
- + */
- +package com.l2jserver.gameserver.datatables;
- +
- +import java.io.File;
- +import java.util.Map;
- +import java.util.logging.Level;
- +import java.util.logging.Logger;
- +
- +import javax.xml.parsers.DocumentBuilderFactory;
- +
- +import org.w3c.dom.Document;
- +import org.w3c.dom.NamedNodeMap;
- +import org.w3c.dom.Node;
- +
- +import javolution.util.FastMap;
- +
- +import com.l2jserver.Config;
- +import com.l2jserver.gameserver.model.Location;
- +
- +/**
- + *
- + * @author Nik
- + *
- + */
- +public class TeleportDataTable
- +{
- + private static Logger _log = Logger.getLogger(TeleportDataTable.class.getName());
- +
- + private Map<Integer, FastMap<Integer, TeleportData>> _npcTeleports;
- + private int _lastDynamicId = 300000;
- +
- + enum TeleportType
- + {
- + NORMAL,
- + NOBLESSE,
- + }
- +
- + public static TeleportDataTable getInstance()
- + {
- + return SingletonHolder._instance;
- + }
- +
- + private TeleportDataTable()
- + {
- + reloadAll();
- + }
- +
- + public void reloadAll()
- + {
- + _npcTeleports = new FastMap<Integer, FastMap<Integer, TeleportData>>();
- +
- + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- + factory.setIgnoringComments(true);
- + factory.setIgnoringElementContentWhitespace(true);
- + File file = new File(Config.DATAPACK_ROOT, "data/teleportData.xml");
- + Document doc = null;
- +
- + if (file.exists())
- + {
- + try
- + {
- + doc = factory.newDocumentBuilder().parse(file);
- + }
- + catch (Exception e)
- + {
- + _log.log(Level.WARNING, "Could not parse teleportData.xml file: " + e.getMessage(), e);
- + }
- +
- + for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
- + {
- + if ("list".equalsIgnoreCase(n.getNodeName()))
- + {
- + for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
- + {
- + if ("npc".equalsIgnoreCase(d.getNodeName()))
- + {
- + NamedNodeMap attrs = d.getAttributes();
- + Node att;
- + int[] npcIds;
- +
- + att = attrs.getNamedItem("ids");
- + if (att == null)
- + {
- + _log.severe("[TeleportDataTable] Missing npc Ids... skipping");
- + continue;
- + }
- + String[] split = att.getNodeValue().trim().split(",");
- + npcIds = new int[split.length];
- +
- + for (int i = 0; i < split.length; i++)
- + {
- + try
- + {
- + npcIds[i] = Integer.parseInt(split[i]);
- + }
- + catch (NumberFormatException e)
- + {
- + _log.warning("[TeleportDataTable] cannot parse to int "+e.getMessage());
- + }
- + }
- +
- + // For any <teleport... /> fields inside <npc... >
- + for (Node cd = d.getFirstChild(); cd != null; cd = cd.getNextSibling())
- + {
- + TeleportData tmp = parseTeleportData(cd);
- +
- + if (tmp == null)
- + continue;
- +
- + for (int npcId : npcIds)
- + {
- + // If the NPC where we want to put the TeleportData is not in the Map, lets put it, because we will need it.
- + if (_npcTeleports.get(npcId) == null)
- + _npcTeleports.put(npcId, new FastMap<Integer, TeleportData>());
- +
- + _npcTeleports.get(npcId).put(tmp.getId(), tmp);
- + }
- + }
- + }
- + }
- + }
- + }
- + }
- + }
- +
- + private TeleportData parseTeleportData(Node cd)
- + {
- + TeleportData tmp = null;
- +
- + if ("teleport".equalsIgnoreCase(cd.getNodeName()))
- + {
- + Node val = null;
- + int id = -1;
- + String name = "";
- + int x = 0;
- + int y = 0;
- + int z = 0;
- + int heading = 0;
- + String price = "";
- + TeleportType type = TeleportType.NOBLESSE;
- +
- + try
- + {
- + val = cd.getAttributes().getNamedItem("id");
- + if (val != null)
- + id = Integer.parseInt(val.getNodeValue().trim());
- +
- + val = cd.getAttributes().getNamedItem("name");
- + if (val != null)
- + name = val.getNodeValue();
- +
- + val = cd.getAttributes().getNamedItem("X");
- + if (val != null)
- + x = Integer.parseInt(val.getNodeValue());
- +
- + val = cd.getAttributes().getNamedItem("Y");
- + if (val != null)
- + y = Integer.parseInt(val.getNodeValue());
- +
- + val = cd.getAttributes().getNamedItem("Z");
- + if (val != null)
- + z = Integer.parseInt(val.getNodeValue());
- +
- + val = cd.getAttributes().getNamedItem("heading");
- + if (val != null)
- + heading = Integer.parseInt(val.getNodeValue());
- +
- + val = cd.getAttributes().getNamedItem("price");
- + if (val != null)
- + price = val.getNodeValue();
- +
- + val = cd.getAttributes().getNamedItem("type");
- + if (val != null)
- + {
- + type = TeleportType.valueOf(val.getNodeValue());
- + }
- + }
- + catch (Exception e)
- + {
- + _log.log(Level.WARNING, "[TeleportDataTable] Error while parsing TeleportData " + e.getMessage(), e);
- + }
- + finally
- + {
- + // We use -1 id, when the teleport id should be dynamic. Also, if a teleport id is greater than 300,000(dynamic id reserved)
- + // change the static id to dynamic, to prevent problems.
- + if (id == -1 || id > 300000)
- + id = _lastDynamicId++;
- + tmp = new TeleportData(id, name, new Location(x, y, z, heading), price, type);
- + }
- + }
- +
- + return tmp;
- + }
- +
- + /**
- + * @param npc id
- + * @return Map with all teleports this NPC has
- + */
- + public FastMap<Integer, TeleportData> getNpcTeleportDatas(int npcId)
- + {
- + return _npcTeleports.get(npcId);
- + }
- +
- + public TeleportData getNpcTeleportData(int npcId, int teleportId)
- + {
- + if (_npcTeleports.get(npcId) != null)
- + return _npcTeleports.get(npcId).get(teleportId);
- + else
- + return null;
- + }
- + /**
- + * @param teleportId
- + * @return the TeleportData assigned to this teleportId. Static teleport Ids use -1 as NPC Id
- + */
- + public TeleportData getTeleportData(int teleportId)
- + {
- + if (_npcTeleports.get(-1) != null)
- + return _npcTeleports.get(-1).get(teleportId);
- + else
- + return null;
- + }
- +
- + @SuppressWarnings("synthetic-access")
- + private static class SingletonHolder
- + {
- + protected static final TeleportDataTable _instance = new TeleportDataTable();
- + }
- +
- + public class TeleportData
- + {
- + int _id;
- + String _name;
- + Location _loc;
- + int[][] _price;
- + TeleportType _type;
- +
- + private TeleportData(int id, String name, Location loc, int[][] price, TeleportType type)
- + {
- + insertTeleportData(id, name, loc, price, type);
- + }
- +
- + private TeleportData(int id, String name, Location loc, String price, TeleportType type)
- + {
- + insertTeleportData(id, name, loc, parsePrice(price), type);
- + }
- +
- + private void insertTeleportData(int id, String name, Location loc, int[][] price, TeleportType type)
- + {
- + _id = id;
- + _name = name;
- + _loc = loc;
- + _price = price;
- + _type = type;
- +
- + }
- +
- + private int[][] parsePrice(String price)
- + {
- + String[] items = price.trim().split(";");
- + int[][] tmp = new int[items.length][2];
- +
- + for (int i = 0; i < items.length; i++)
- + {
- + int itemId = 0;
- + int itemCount = 0;
- + try
- + {
- + String[] split = items[i].trim().split(",");
- + itemId = Integer.parseInt(split[0]);
- + itemCount = Integer.parseInt(split[1]);
- +
- + tmp[i][0] = itemId;
- + tmp[i][1] = itemCount;
- + }
- + catch(NumberFormatException e)
- + {
- + _log.warning("[TeleportDataTable] Error while parsing teleport price, cannot turn to integer values:"+items[i]+" NFE Error:"+e.getMessage());
- + }
- + }
- +
- + return tmp;
- + }
- +
- + /**
- + * int[][0] = itemId;<BR>
- + * int[][1] = itemCount;<BR>
- + */
- + public int[][] getPrice() {return _price;}
- + public int getId() {return _id;}
- + public String getName() {return _name;}
- + public TeleportType getType() {return _type;}
- + public Location getLocation() {return _loc;}
- +
- + /**
- + *
- + * @return name - itemId itemCount
- + * <br>
- + * Example: Rune Township - 53000 Adena
- + * <br>
- + * If the item is not found or the ID/Count is 0, it will return the teleport name
- + */
- + /*public String generateFullName()
- + {
- + // Free Teleport
- + if (_price[0][0] == 0 || _price[0][1] == 0)
- + {
- + return _name;
- + }
- + else
- + {
- + L2ItemInstance item = ItemTable.getInstance().createDummyItem(_price[0][0]);
- +
- + if (item == null)
- + return _name;
- + else
- + return _name + " - " + _price[0][1] + " " + item.getName();
- + }
- + }*/
- +
- + }
- +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement