123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- /*
- * Copyright (C) 2004-2013 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.datatables;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.w3c.dom.NamedNodeMap;
- import org.w3c.dom.Node;
- import com.l2jserver.gameserver.engines.DocumentParser;
- import com.l2jserver.gameserver.model.L2Seed;
- import com.l2jserver.gameserver.model.StatsSet;
- import com.l2jserver.gameserver.model.items.L2Item;
- /**
- * Service class for manor
- * @author l3x, UnAfraid
- */
- public class ManorData extends DocumentParser
- {
- private static Logger _log = Logger.getLogger(ManorData.class.getName());
-
- private static Map<Integer, L2Seed> _seeds = new HashMap<>();
-
- protected ManorData()
- {
- load();
- }
-
- @Override
- public void load()
- {
- _seeds.clear();
- parseDatapackFile("data/seeds.xml");
- _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _seeds.size() + " seeds.");
- }
-
- @Override
- protected void parseDocument()
- {
- StatsSet set;
- NamedNodeMap attrs;
- Node att;
- int castleId;
- for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
- {
- if ("list".equalsIgnoreCase(n.getNodeName()))
- {
- for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
- {
- if ("castle".equalsIgnoreCase(d.getNodeName()))
- {
- castleId = parseInt(d.getAttributes(), "id");
- for (Node c = d.getFirstChild(); c != null; c = c.getNextSibling())
- {
- if ("crop".equalsIgnoreCase(c.getNodeName()))
- {
- set = new StatsSet();
- set.set("castleId", castleId);
-
- attrs = c.getAttributes();
- for (int i = 0; i < attrs.getLength(); i++)
- {
- att = attrs.item(i);
- set.set(att.getNodeName(), att.getNodeValue());
- }
-
- L2Seed seed = new L2Seed(set);
- _seeds.put(seed.getSeedId(), seed);
- }
- }
- }
- }
- }
- }
- }
-
- public List<Integer> getAllCrops()
- {
- List<Integer> crops = new ArrayList<>();
-
- for (L2Seed seed : _seeds.values())
- {
- if (!crops.contains(seed.getCropId()) && (seed.getCropId() != 0) && !crops.contains(seed.getCropId()))
- {
- crops.add(seed.getCropId());
- }
- }
-
- return crops;
- }
-
- public int getSeedBasicPrice(int seedId)
- {
- final L2Item seedItem = ItemTable.getInstance().getTemplate(seedId);
- if (seedItem != null)
- {
- return seedItem.getReferencePrice();
- }
- return 0;
- }
-
- public int getSeedBasicPriceByCrop(int cropId)
- {
- for (L2Seed seed : _seeds.values())
- {
- if (seed.getCropId() == cropId)
- {
- return getSeedBasicPrice(seed.getSeedId());
- }
- }
- return 0;
- }
-
- public int getCropBasicPrice(int cropId)
- {
- final L2Item cropItem = ItemTable.getInstance().getTemplate(cropId);
- if (cropItem != null)
- {
- return cropItem.getReferencePrice();
- }
- return 0;
- }
-
- public int getMatureCrop(int cropId)
- {
- for (L2Seed seed : _seeds.values())
- {
- if (seed.getCropId() == cropId)
- {
- return seed.getMatureId();
- }
- }
- return 0;
- }
-
- /**
- * Returns price which lord pays to buy one seed
- * @param seedId
- * @return seed price
- */
- public long getSeedBuyPrice(int seedId)
- {
- long buyPrice = getSeedBasicPrice(seedId);
- return (buyPrice > 0 ? buyPrice : 1);
- }
-
- public int getSeedMinLevel(int seedId)
- {
- L2Seed seed = _seeds.get(seedId);
-
- if (seed != null)
- {
- return seed.getLevel() - 5;
- }
- return -1;
- }
-
- public int getSeedMaxLevel(int seedId)
- {
- L2Seed seed = _seeds.get(seedId);
-
- if (seed != null)
- {
- return seed.getLevel() + 5;
- }
- return -1;
- }
-
- public int getSeedLevelByCrop(int cropId)
- {
- for (L2Seed seed : _seeds.values())
- {
- if (seed.getCropId() == cropId)
- {
- return seed.getLevel();
- }
- }
- return 0;
- }
-
- public int getSeedLevel(int seedId)
- {
- L2Seed seed = _seeds.get(seedId);
-
- if (seed != null)
- {
- return seed.getLevel();
- }
- return -1;
- }
-
- public boolean isAlternative(int seedId)
- {
- L2Seed seed = _seeds.get(seedId);
-
- if (seed != null)
- {
- return seed.isAlternative();
- }
- return false;
- }
-
- public int getCropType(int seedId)
- {
- L2Seed seed = _seeds.get(seedId);
-
- if (seed != null)
- {
- return seed.getCropId();
- }
- return -1;
- }
-
- public int getRewardItem(int cropId, int type)
- {
- for (L2Seed seed : _seeds.values())
- {
- if (seed.getCropId() == cropId)
- {
- return seed.getReward(type); // there can be several seeds with same crop, but reward should be the same for all.
- }
- }
- return -1;
- }
-
- public int getRewardItemBySeed(int seedId, int type)
- {
- L2Seed seed = _seeds.get(seedId);
-
- if (seed != null)
- {
- return seed.getReward(type);
- }
- return 0;
- }
-
- /**
- * Return all crops which can be purchased by given castle
- * @param castleId
- * @return
- */
- public List<Integer> getCropsForCastle(int castleId)
- {
- List<Integer> crops = new ArrayList<>();
-
- for (L2Seed seed : _seeds.values())
- {
- if ((seed.getCastleId() == castleId) && !crops.contains(seed.getCropId()))
- {
- crops.add(seed.getCropId());
- }
- }
-
- return crops;
- }
-
- /**
- * Return list of seed ids, which belongs to castle with given id
- * @param castleId - id of the castle
- * @return seedIds - list of seed ids
- */
- public List<Integer> getSeedsForCastle(int castleId)
- {
- List<Integer> seedsID = new ArrayList<>();
-
- for (L2Seed seed : _seeds.values())
- {
- if ((seed.getCastleId() == castleId) && !seedsID.contains(seed.getSeedId()))
- {
- seedsID.add(seed.getSeedId());
- }
- }
-
- return seedsID;
- }
-
- /**
- * Returns castle id where seed can be sowned<br>
- * @param seedId
- * @return castleId
- */
- public int getCastleIdForSeed(int seedId)
- {
- L2Seed seed = _seeds.get(seedId);
-
- if (seed != null)
- {
- return seed.getCastleId();
- }
- return 0;
- }
-
- public int getSeedSaleLimit(int seedId)
- {
- L2Seed seed = _seeds.get(seedId);
-
- if (seed != null)
- {
- return seed.getSeedLimit();
- }
- return 0;
- }
-
- public int getCropPuchaseLimit(int cropId)
- {
- for (L2Seed seed : _seeds.values())
- {
- if (seed.getCropId() == cropId)
- {
- return seed.getCropLimit();
- }
- }
- return 0;
- }
-
- public static ManorData getInstance()
- {
- return SingletonHolder._instance;
- }
-
- private static class SingletonHolder
- {
- protected static final ManorData _instance = new ManorData();
- }
- }
|