123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- /*
- * Copyright (C) 2004-2015 L2J Server
- *
- * This file is part of L2J Server.
- *
- * L2J Server 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.
- *
- * L2J Server 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.data.xml.impl;
- import java.util.HashMap;
- import java.util.Map;
- import org.w3c.dom.Document;
- import org.w3c.dom.NamedNodeMap;
- import org.w3c.dom.Node;
- import com.l2jserver.gameserver.enums.MountType;
- import com.l2jserver.gameserver.model.L2PetData;
- import com.l2jserver.gameserver.model.L2PetLevelData;
- import com.l2jserver.gameserver.model.StatsSet;
- import com.l2jserver.util.data.xml.IXmlReader;
- /**
- * This class parse and hold all pet parameters.<br>
- * TODO: load and use all pet parameters.
- * @author Zoey76 (rework)
- */
- public final class PetDataTable implements IXmlReader
- {
- private final Map<Integer, L2PetData> _pets = new HashMap<>();
-
- /**
- * Instantiates a new pet data table.
- */
- protected PetDataTable()
- {
- load();
- }
-
- @Override
- public void load()
- {
- _pets.clear();
- parseDatapackDirectory("data/stats/pets", false);
- LOGGER.info("{}: Loaded {} Pets.", getClass().getSimpleName(), _pets.size());
- }
-
- @Override
- public void parseDocument(Document doc)
- {
- NamedNodeMap attrs;
- Node n = doc.getFirstChild();
- for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
- {
- if (d.getNodeName().equals("pet"))
- {
- int npcId = parseInteger(d.getAttributes(), "id");
- int itemId = parseInteger(d.getAttributes(), "itemId");
- // index ignored for now
- L2PetData data = new L2PetData(npcId, itemId);
- for (Node p = d.getFirstChild(); p != null; p = p.getNextSibling())
- {
- if (p.getNodeName().equals("set"))
- {
- attrs = p.getAttributes();
- String type = attrs.getNamedItem("name").getNodeValue();
- if ("food".equals(type))
- {
- for (String foodId : attrs.getNamedItem("val").getNodeValue().split(";"))
- {
- data.addFood(Integer.valueOf(foodId));
- }
- }
- else if ("load".equals(type))
- {
- data.setLoad(parseInteger(attrs, "val"));
- }
- else if ("hungry_limit".equals(type))
- {
- data.setHungryLimit(parseInteger(attrs, "val"));
- }
- else if ("sync_level".equals(type))
- {
- data.setSyncLevel(parseInteger(attrs, "val") == 1);
- }
- // evolve ignored
- }
- else if (p.getNodeName().equals("skills"))
- {
- for (Node s = p.getFirstChild(); s != null; s = s.getNextSibling())
- {
- if (s.getNodeName().equals("skill"))
- {
- attrs = s.getAttributes();
- data.addNewSkill(parseInteger(attrs, "skillId"), parseInteger(attrs, "skillLvl"), parseInteger(attrs, "minLvl"));
- }
- }
- }
- else if (p.getNodeName().equals("stats"))
- {
- for (Node s = p.getFirstChild(); s != null; s = s.getNextSibling())
- {
- if (s.getNodeName().equals("stat"))
- {
- final int level = Integer.parseInt(s.getAttributes().getNamedItem("level").getNodeValue());
- final StatsSet set = new StatsSet();
- for (Node bean = s.getFirstChild(); bean != null; bean = bean.getNextSibling())
- {
- if (bean.getNodeName().equals("set"))
- {
- attrs = bean.getAttributes();
- if (attrs.getNamedItem("name").getNodeValue().equals("speed_on_ride"))
- {
- set.set("walkSpeedOnRide", attrs.getNamedItem("walk").getNodeValue());
- set.set("runSpeedOnRide", attrs.getNamedItem("run").getNodeValue());
- set.set("slowSwimSpeedOnRide", attrs.getNamedItem("slowSwim").getNodeValue());
- set.set("fastSwimSpeedOnRide", attrs.getNamedItem("fastSwim").getNodeValue());
- if (attrs.getNamedItem("slowFly") != null)
- {
- set.set("slowFlySpeedOnRide", attrs.getNamedItem("slowFly").getNodeValue());
- }
- if (attrs.getNamedItem("fastFly") != null)
- {
- set.set("fastFlySpeedOnRide", attrs.getNamedItem("fastFly").getNodeValue());
- }
- }
- else
- {
- set.set(attrs.getNamedItem("name").getNodeValue(), attrs.getNamedItem("val").getNodeValue());
- }
- }
- }
- data.addNewStat(level, new L2PetLevelData(set));
- }
- }
- }
- }
- _pets.put(npcId, data);
- }
- }
- }
-
- /**
- * @param itemId
- * @return
- */
- public L2PetData getPetDataByItemId(int itemId)
- {
- for (L2PetData data : _pets.values())
- {
- if (data.getItemId() == itemId)
- {
- return data;
- }
- }
- return null;
- }
-
- /**
- * Gets the pet level data.
- * @param petId the pet Id.
- * @param petLevel the pet level.
- * @return the pet's parameters for the given Id and level.
- */
- public L2PetLevelData getPetLevelData(int petId, int petLevel)
- {
- final L2PetData pd = getPetData(petId);
- if (pd != null)
- {
- return pd.getPetLevelData(petLevel);
- }
- return null;
- }
-
- /**
- * Gets the pet data.
- * @param petId the pet Id.
- * @return the pet data
- */
- public L2PetData getPetData(int petId)
- {
- if (!_pets.containsKey(petId))
- {
- LOGGER.info("{}: Missing pet data for NPC ID {}!", getClass().getSimpleName(), petId);
- }
- return _pets.get(petId);
- }
-
- /**
- * Gets the pet min level.
- * @param petId the pet Id.
- * @return the pet min level
- */
- public int getPetMinLevel(int petId)
- {
- return _pets.get(petId).getMinLevel();
- }
-
- /**
- * Gets the pet items by npc.
- * @param npcId the NPC ID to get its summoning item
- * @return summoning item for the given NPC ID
- */
- public int getPetItemsByNpc(int npcId)
- {
- return _pets.get(npcId).getItemId();
- }
-
- /**
- * Checks if is mountable.
- * @param npcId the NPC Id to verify.
- * @return {@code true} if the given Id is from a mountable pet, {@code false} otherwise.
- */
- public static boolean isMountable(int npcId)
- {
- return MountType.findByNpcId(npcId) != MountType.NONE;
- }
-
- /**
- * Gets the single instance of PetDataTable.
- * @return this class unique instance.
- */
- public static PetDataTable getInstance()
- {
- return SingletonHolder._instance;
- }
-
- private static class SingletonHolder
- {
- protected static final PetDataTable _instance = new PetDataTable();
- }
- }
|