NpcData.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*
  2. * Copyright © 2004-2019 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.data.xml.impl;
  20. import static com.l2jserver.gameserver.config.Configuration.general;
  21. import java.io.File;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.EnumMap;
  25. import java.util.HashMap;
  26. import java.util.HashSet;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Set;
  30. import java.util.concurrent.ConcurrentHashMap;
  31. import java.util.function.Predicate;
  32. import java.util.stream.Collectors;
  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;
  35. import org.w3c.dom.Document;
  36. import org.w3c.dom.NamedNodeMap;
  37. import org.w3c.dom.Node;
  38. import com.l2jserver.gameserver.datatables.SkillData;
  39. import com.l2jserver.gameserver.enums.AISkillScope;
  40. import com.l2jserver.gameserver.model.StatsSet;
  41. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  42. import com.l2jserver.gameserver.model.base.ClassId;
  43. import com.l2jserver.gameserver.model.drops.DropListScope;
  44. import com.l2jserver.gameserver.model.drops.GeneralDropItem;
  45. import com.l2jserver.gameserver.model.drops.GroupedGeneralDropItem;
  46. import com.l2jserver.gameserver.model.drops.IDropItem;
  47. import com.l2jserver.gameserver.model.effects.L2EffectType;
  48. import com.l2jserver.gameserver.model.holders.MinionHolder;
  49. import com.l2jserver.gameserver.model.holders.SkillHolder;
  50. import com.l2jserver.gameserver.model.skills.Skill;
  51. import com.l2jserver.gameserver.util.IXmlReader;
  52. import com.l2jserver.gameserver.util.Util;
  53. /**
  54. * NPC data parser.
  55. * @author NosBit
  56. */
  57. public class NpcData implements IXmlReader {
  58. private static final Logger LOG = LoggerFactory.getLogger(NpcData.class);
  59. private final Map<Integer, L2NpcTemplate> _npcs = new ConcurrentHashMap<>();
  60. private final Map<String, Integer> _clans = new ConcurrentHashMap<>();
  61. private MinionData _minionData;
  62. protected NpcData() {
  63. load();
  64. }
  65. @Override
  66. public synchronized void load() {
  67. _minionData = new MinionData();
  68. parseDatapackDirectory("data/stats/npcs", false);
  69. LOG.info("Loaded {} NPCs.", _npcs.size());
  70. if (general().customNpcData()) {
  71. final int npcCount = _npcs.size();
  72. parseDatapackDirectory("data/stats/npcs/custom", true);
  73. LOG.info("Loaded {} custom NPCs.", (_npcs.size() - npcCount));
  74. }
  75. _minionData = null;
  76. loadNpcsSkillLearn();
  77. }
  78. @Override
  79. public void parseDocument(Document doc, File f) {
  80. for (Node node = doc.getFirstChild(); node != null; node = node.getNextSibling()) {
  81. if ("list".equalsIgnoreCase(node.getNodeName())) {
  82. for (Node listNode = node.getFirstChild(); listNode != null; listNode = listNode.getNextSibling()) {
  83. if ("npc".equalsIgnoreCase(listNode.getNodeName())) {
  84. NamedNodeMap attrs = listNode.getAttributes();
  85. final StatsSet set = new StatsSet();
  86. final int npcId = parseInteger(attrs, "id");
  87. Map<String, Object> parameters = null;
  88. Map<Integer, Skill> skills = null;
  89. Set<Integer> clans = null;
  90. Set<Integer> ignoreClanNpcIds = null;
  91. Map<DropListScope, List<IDropItem>> dropLists = null;
  92. set.set("id", npcId);
  93. set.set("displayId", parseInteger(attrs, "displayId"));
  94. set.set("level", parseByte(attrs, "level"));
  95. set.set("type", parseString(attrs, "type"));
  96. set.set("name", parseString(attrs, "name"));
  97. set.set("usingServerSideName", parseBoolean(attrs, "usingServerSideName"));
  98. set.set("title", parseString(attrs, "title"));
  99. set.set("usingServerSideTitle", parseBoolean(attrs, "usingServerSideTitle"));
  100. for (Node npcNode = listNode.getFirstChild(); npcNode != null; npcNode = npcNode.getNextSibling()) {
  101. attrs = npcNode.getAttributes();
  102. switch (npcNode.getNodeName().toLowerCase()) {
  103. case "parameters": {
  104. if (parameters == null) {
  105. parameters = new HashMap<>();
  106. }
  107. for (Node parametersNode = npcNode.getFirstChild(); parametersNode != null; parametersNode = parametersNode.getNextSibling()) {
  108. attrs = parametersNode.getAttributes();
  109. switch (parametersNode.getNodeName().toLowerCase()) {
  110. case "param": {
  111. parameters.put(parseString(attrs, "name"), parseString(attrs, "value"));
  112. break;
  113. }
  114. case "skill": {
  115. parameters.put(parseString(attrs, "name"), new SkillHolder(parseInteger(attrs, "id"), parseInteger(attrs, "level")));
  116. break;
  117. }
  118. case "minions": {
  119. final List<MinionHolder> minions = new ArrayList<>(1);
  120. for (Node minionsNode = parametersNode.getFirstChild(); minionsNode != null; minionsNode = minionsNode.getNextSibling()) {
  121. if (minionsNode.getNodeName().equalsIgnoreCase("npc")) {
  122. attrs = minionsNode.getAttributes();
  123. minions.add(new MinionHolder(parseInteger(attrs, "id"), parseInteger(attrs, "count"), parseInteger(attrs, "respawnTime"), parseInteger(attrs, "weightPoint")));
  124. }
  125. }
  126. if (!minions.isEmpty()) {
  127. parameters.put(parseString(parametersNode.getAttributes(), "name"), minions);
  128. }
  129. break;
  130. }
  131. }
  132. }
  133. break;
  134. }
  135. case "race":
  136. case "sex":
  137. set.set(npcNode.getNodeName(), npcNode.getTextContent().toUpperCase());
  138. break;
  139. case "equipment": {
  140. set.set("chestId", parseInteger(attrs, "chest"));
  141. set.set("rhandId", parseInteger(attrs, "rhand"));
  142. set.set("lhandId", parseInteger(attrs, "lhand"));
  143. set.set("weaponEnchant", parseInteger(attrs, "weaponEnchant"));
  144. break;
  145. }
  146. case "acquire": {
  147. set.set("expRate", parseDouble(attrs, "expRate"));
  148. set.set("sp", parseDouble(attrs, "sp"));
  149. set.set("raidPoints", parseDouble(attrs, "raidPoints"));
  150. break;
  151. }
  152. case "stats": {
  153. set.set("baseSTR", parseInteger(attrs, "str"));
  154. set.set("baseINT", parseInteger(attrs, "int"));
  155. set.set("baseDEX", parseInteger(attrs, "dex"));
  156. set.set("baseWIT", parseInteger(attrs, "wit"));
  157. set.set("baseCON", parseInteger(attrs, "con"));
  158. set.set("baseMEN", parseInteger(attrs, "men"));
  159. for (Node statsNode = npcNode.getFirstChild(); statsNode != null; statsNode = statsNode.getNextSibling()) {
  160. attrs = statsNode.getAttributes();
  161. switch (statsNode.getNodeName().toLowerCase()) {
  162. case "vitals": {
  163. set.set("baseHpMax", parseDouble(attrs, "hp"));
  164. set.set("baseHpReg", parseDouble(attrs, "hpRegen"));
  165. set.set("baseMpMax", parseDouble(attrs, "mp"));
  166. set.set("baseMpReg", parseDouble(attrs, "mpRegen"));
  167. break;
  168. }
  169. case "attack": {
  170. set.set("basePAtk", parseDouble(attrs, "physical"));
  171. set.set("baseMAtk", parseDouble(attrs, "magical"));
  172. set.set("baseRndDam", parseInteger(attrs, "random"));
  173. set.set("baseCritRate", parseInteger(attrs, "critical"));
  174. set.set("accuracy", parseDouble(attrs, "accuracy"));// TODO: Implement me
  175. set.set("basePAtkSpd", parseInteger(attrs, "attackSpeed"));
  176. set.set("reuseDelay", parseInteger(attrs, "reuseDelay"));// TODO: Implement me
  177. set.set("baseAtkType", parseString(attrs, "type"));
  178. set.set("baseAtkRange", parseInteger(attrs, "range"));
  179. set.set("distance", parseInteger(attrs, "distance"));// TODO: Implement me
  180. set.set("width", parseInteger(attrs, "width"));// TODO: Implement me
  181. break;
  182. }
  183. case "defence": {
  184. set.set("basePDef", parseDouble(attrs, "physical"));
  185. set.set("baseMDef", parseDouble(attrs, "magical"));
  186. set.set("evasion", parseInteger(attrs, "evasion"));// TODO: Implement me
  187. set.set("baseShldDef", parseInteger(attrs, "shield"));
  188. set.set("baseShldRate", parseInteger(attrs, "shieldRate"));
  189. break;
  190. }
  191. case "attribute": {
  192. for (Node attributeNode = statsNode.getFirstChild(); attributeNode != null; attributeNode = attributeNode.getNextSibling()) {
  193. attrs = attributeNode.getAttributes();
  194. switch (attributeNode.getNodeName().toLowerCase()) {
  195. case "attack": {
  196. String attackAttributeType = parseString(attrs, "type");
  197. switch (attackAttributeType.toUpperCase()) {
  198. case "FIRE":
  199. set.set("baseFire", parseInteger(attrs, "value"));
  200. break;
  201. case "WATER":
  202. set.set("baseWater", parseInteger(attrs, "value"));
  203. break;
  204. case "WIND":
  205. set.set("baseWind", parseInteger(attrs, "value"));
  206. break;
  207. case "EARTH":
  208. set.set("baseEarth", parseInteger(attrs, "value"));
  209. break;
  210. case "DARK":
  211. set.set("baseDark", parseInteger(attrs, "value"));
  212. break;
  213. case "HOLY":
  214. set.set("baseHoly", parseInteger(attrs, "value"));
  215. break;
  216. }
  217. break;
  218. }
  219. case "defence": {
  220. set.set("baseFireRes", parseInteger(attrs, "fire"));
  221. set.set("baseWaterRes", parseInteger(attrs, "water"));
  222. set.set("baseWindRes", parseInteger(attrs, "wind"));
  223. set.set("baseEarthRes", parseInteger(attrs, "earth"));
  224. set.set("baseHolyRes", parseInteger(attrs, "holy"));
  225. set.set("baseDarkRes", parseInteger(attrs, "dark"));
  226. set.set("baseElementRes", parseInteger(attrs, "default"));
  227. break;
  228. }
  229. }
  230. }
  231. break;
  232. }
  233. case "speed": {
  234. for (Node speedNode = statsNode.getFirstChild(); speedNode != null; speedNode = speedNode.getNextSibling()) {
  235. attrs = speedNode.getAttributes();
  236. switch (speedNode.getNodeName().toLowerCase()) {
  237. case "walk": {
  238. set.set("baseWalkSpd", parseDouble(attrs, "ground"));
  239. set.set("baseSwimWalkSpd", parseDouble(attrs, "swim"));
  240. set.set("baseFlyWalkSpd", parseDouble(attrs, "fly"));
  241. break;
  242. }
  243. case "run": {
  244. set.set("baseRunSpd", parseDouble(attrs, "ground"));
  245. set.set("baseSwimRunSpd", parseDouble(attrs, "swim"));
  246. set.set("baseFlyRunSpd", parseDouble(attrs, "fly"));
  247. break;
  248. }
  249. }
  250. }
  251. break;
  252. }
  253. case "hittime":
  254. set.set("hitTime", npcNode.getTextContent());// TODO: Implement me default 600 (value in ms)
  255. break;
  256. }
  257. }
  258. break;
  259. }
  260. case "status": {
  261. set.set("unique", parseBoolean(attrs, "unique"));
  262. set.set("attackable", parseBoolean(attrs, "attackable"));
  263. set.set("targetable", parseBoolean(attrs, "targetable"));
  264. set.set("undying", parseBoolean(attrs, "undying"));
  265. set.set("showName", parseBoolean(attrs, "showName"));
  266. set.set("flying", parseBoolean(attrs, "flying"));
  267. set.set("canMove", parseBoolean(attrs, "canMove"));
  268. set.set("noSleepMode", parseBoolean(attrs, "noSleepMode"));
  269. set.set("passableDoor", parseBoolean(attrs, "passableDoor"));
  270. set.set("hasSummoner", parseBoolean(attrs, "hasSummoner"));
  271. set.set("canBeSown", parseBoolean(attrs, "canBeSown"));
  272. break;
  273. }
  274. case "skilllist": {
  275. skills = new HashMap<>();
  276. for (Node skillListNode = npcNode.getFirstChild(); skillListNode != null; skillListNode = skillListNode.getNextSibling()) {
  277. if ("skill".equalsIgnoreCase(skillListNode.getNodeName())) {
  278. attrs = skillListNode.getAttributes();
  279. final int skillId = parseInteger(attrs, "id");
  280. final int skillLevel = parseInteger(attrs, "level");
  281. final Skill skill = SkillData.getInstance().getSkill(skillId, skillLevel);
  282. if (skill != null) {
  283. skills.put(skill.getId(), skill);
  284. } else {
  285. LOG.warn("{} skill not found. NPC Id {}, skill Id {}, skill level: {}!", f, npcId, skillId, skillLevel);
  286. }
  287. }
  288. }
  289. break;
  290. }
  291. case "shots": {
  292. set.set("soulShot", parseInteger(attrs, "soul"));
  293. set.set("spiritShot", parseInteger(attrs, "spirit"));
  294. set.set("shotShotChance", parseInteger(attrs, "shotChance"));
  295. set.set("spiritShotChance", parseInteger(attrs, "spiritChance"));
  296. break;
  297. }
  298. case "corpsetime":
  299. set.set("corpseTime", npcNode.getTextContent());
  300. break;
  301. case "excrteffect":
  302. set.set("exCrtEffect", npcNode.getTextContent()); // TODO: Implement me default ? type boolean
  303. break;
  304. case "snpcprophprate":
  305. set.set("sNpcPropHpRate", npcNode.getTextContent()); // TODO: Implement me default 1 type double
  306. break;
  307. case "ai": {
  308. set.set("aiType", parseString(attrs, "type"));
  309. set.set("aggroRange", parseInteger(attrs, "aggroRange"));
  310. set.set("clanHelpRange", parseInteger(attrs, "clanHelpRange"));
  311. set.set("dodge", parseInteger(attrs, "dodge"));
  312. set.set("isChaos", parseBoolean(attrs, "isChaos"));
  313. set.set("isAggressive", parseBoolean(attrs, "isAggressive"));
  314. for (Node aiNode = npcNode.getFirstChild(); aiNode != null; aiNode = aiNode.getNextSibling()) {
  315. attrs = aiNode.getAttributes();
  316. switch (aiNode.getNodeName().toLowerCase()) {
  317. case "skill": {
  318. set.set("minSkillChance", parseInteger(attrs, "minChance"));
  319. set.set("maxSkillChance", parseInteger(attrs, "maxChance"));
  320. set.set("primarySkillId", parseInteger(attrs, "primaryId"));
  321. set.set("shortRangeSkillId", parseInteger(attrs, "shortRangeId"));
  322. set.set("shortRangeSkillChance", parseInteger(attrs, "shortRangeChance"));
  323. set.set("longRangeSkillId", parseInteger(attrs, "longRangeId"));
  324. set.set("longRangeSkillChance", parseInteger(attrs, "longRangeChance"));
  325. break;
  326. }
  327. case "clanlist": {
  328. for (Node clanListNode = aiNode.getFirstChild(); clanListNode != null; clanListNode = clanListNode.getNextSibling()) {
  329. attrs = clanListNode.getAttributes();
  330. switch (clanListNode.getNodeName().toLowerCase()) {
  331. case "clan": {
  332. if (clans == null) {
  333. clans = new HashSet<>(1);
  334. }
  335. clans.add(getOrCreateClanId(clanListNode.getTextContent()));
  336. break;
  337. }
  338. case "ignorenpcid": {
  339. if (ignoreClanNpcIds == null) {
  340. ignoreClanNpcIds = new HashSet<>(1);
  341. }
  342. ignoreClanNpcIds.add(Integer.parseInt(clanListNode.getTextContent()));
  343. break;
  344. }
  345. }
  346. }
  347. break;
  348. }
  349. }
  350. }
  351. break;
  352. }
  353. case "droplists": {
  354. for (Node dropListsNode = npcNode.getFirstChild(); dropListsNode != null; dropListsNode = dropListsNode.getNextSibling()) {
  355. DropListScope dropListScope = null;
  356. try {
  357. dropListScope = Enum.valueOf(DropListScope.class, dropListsNode.getNodeName().toUpperCase());
  358. } catch (Exception e) {
  359. }
  360. if (dropListScope != null) {
  361. if (dropLists == null) {
  362. dropLists = new EnumMap<>(DropListScope.class);
  363. }
  364. List<IDropItem> dropList = new ArrayList<>();
  365. parseDropList(f, dropListsNode, dropListScope, dropList);
  366. dropLists.put(dropListScope, Collections.unmodifiableList(dropList));
  367. }
  368. }
  369. break;
  370. }
  371. case "collision": {
  372. for (Node collisionNode = npcNode.getFirstChild(); collisionNode != null; collisionNode = collisionNode.getNextSibling()) {
  373. attrs = collisionNode.getAttributes();
  374. switch (collisionNode.getNodeName().toLowerCase()) {
  375. case "radius": {
  376. set.set("collisionRadius", parseDouble(attrs, "normal"));
  377. set.set("collisionRadiusGrown", parseDouble(attrs, "grown"));
  378. break;
  379. }
  380. case "height": {
  381. set.set("collisionHeight", parseDouble(attrs, "normal"));
  382. set.set("collisionHeightGrown", parseDouble(attrs, "grown"));
  383. break;
  384. }
  385. }
  386. }
  387. break;
  388. }
  389. }
  390. }
  391. L2NpcTemplate template = _npcs.get(npcId);
  392. if (template == null) {
  393. template = new L2NpcTemplate(set);
  394. _npcs.put(template.getId(), template);
  395. } else {
  396. template.set(set);
  397. }
  398. if (_minionData._tempMinions.containsKey(npcId)) {
  399. if (parameters == null) {
  400. parameters = new HashMap<>();
  401. }
  402. parameters.putIfAbsent("Privates", _minionData._tempMinions.get(npcId));
  403. }
  404. if (parameters != null) {
  405. // Using unmodifiable map parameters of template are not meant to be changed at runtime.
  406. template.setParameters(new StatsSet(Collections.unmodifiableMap(parameters)));
  407. } else {
  408. template.setParameters(StatsSet.EMPTY_STATSET);
  409. }
  410. if (skills != null) {
  411. Map<AISkillScope, List<Skill>> aiSkillLists = null;
  412. for (Skill skill : skills.values()) {
  413. if (skill.isPassive()) {
  414. continue;
  415. }
  416. if (aiSkillLists == null) {
  417. aiSkillLists = new EnumMap<>(AISkillScope.class);
  418. }
  419. final List<AISkillScope> aiSkillScopes = new ArrayList<>();
  420. final AISkillScope shortOrLongRangeScope = skill.getCastRange() <= 150 ? AISkillScope.SHORT_RANGE : AISkillScope.LONG_RANGE;
  421. if (skill.isSuicideAttack()) {
  422. aiSkillScopes.add(AISkillScope.SUICIDE);
  423. } else {
  424. aiSkillScopes.add(AISkillScope.GENERAL);
  425. if (skill.isContinuous()) {
  426. if (!skill.isDebuff()) {
  427. aiSkillScopes.add(AISkillScope.BUFF);
  428. } else {
  429. aiSkillScopes.add(AISkillScope.DEBUFF);
  430. aiSkillScopes.add(AISkillScope.COT);
  431. aiSkillScopes.add(shortOrLongRangeScope);
  432. }
  433. } else {
  434. if (skill.hasEffectType(L2EffectType.DISPEL)) {
  435. aiSkillScopes.add(AISkillScope.NEGATIVE);
  436. aiSkillScopes.add(shortOrLongRangeScope);
  437. } else if (skill.hasEffectType(L2EffectType.HP)) {
  438. aiSkillScopes.add(AISkillScope.HEAL);
  439. } else if (skill.hasEffectType(L2EffectType.PHYSICAL_ATTACK, L2EffectType.MAGICAL_ATTACK, L2EffectType.HP_DRAIN)) {
  440. aiSkillScopes.add(AISkillScope.ATTACK);
  441. aiSkillScopes.add(AISkillScope.UNIVERSAL);
  442. aiSkillScopes.add(shortOrLongRangeScope);
  443. } else if (skill.hasEffectType(L2EffectType.SLEEP)) {
  444. aiSkillScopes.add(AISkillScope.IMMOBILIZE);
  445. } else if (skill.hasEffectType(L2EffectType.STUN, L2EffectType.ROOT)) {
  446. aiSkillScopes.add(AISkillScope.IMMOBILIZE);
  447. aiSkillScopes.add(shortOrLongRangeScope);
  448. } else if (skill.hasEffectType(L2EffectType.MUTE, L2EffectType.FEAR)) {
  449. aiSkillScopes.add(AISkillScope.COT);
  450. aiSkillScopes.add(shortOrLongRangeScope);
  451. } else if (skill.hasEffectType(L2EffectType.PARALYZE)) {
  452. aiSkillScopes.add(AISkillScope.IMMOBILIZE);
  453. aiSkillScopes.add(shortOrLongRangeScope);
  454. } else if (skill.hasEffectType(L2EffectType.DMG_OVER_TIME)) {
  455. aiSkillScopes.add(shortOrLongRangeScope);
  456. } else if (skill.hasEffectType(L2EffectType.RESURRECTION)) {
  457. aiSkillScopes.add(AISkillScope.RES);
  458. } else {
  459. aiSkillScopes.add(AISkillScope.UNIVERSAL);
  460. }
  461. }
  462. }
  463. for (AISkillScope aiSkillScope : aiSkillScopes) {
  464. List<Skill> aiSkills = aiSkillLists.get(aiSkillScope);
  465. if (aiSkills == null) {
  466. aiSkills = new ArrayList<>();
  467. aiSkillLists.put(aiSkillScope, aiSkills);
  468. }
  469. aiSkills.add(skill);
  470. }
  471. }
  472. template.setSkills(skills);
  473. template.setAISkillLists(aiSkillLists);
  474. } else {
  475. template.setSkills(null);
  476. template.setAISkillLists(null);
  477. }
  478. template.setClans(clans);
  479. template.setIgnoreClanNpcIds(ignoreClanNpcIds);
  480. template.setDropLists(dropLists);
  481. }
  482. }
  483. }
  484. }
  485. }
  486. private void parseDropList(File f, Node dropListNode, DropListScope dropListScope, List<IDropItem> drops) {
  487. for (Node dropNode = dropListNode.getFirstChild(); dropNode != null; dropNode = dropNode.getNextSibling()) {
  488. NamedNodeMap attrs = dropNode.getAttributes();
  489. switch (dropNode.getNodeName().toLowerCase()) {
  490. case "group": {
  491. GroupedGeneralDropItem dropItem = dropListScope.newGroupedDropItem(parseDouble(attrs, "chance"));
  492. List<IDropItem> groupedDropList = new ArrayList<>(2);
  493. for (Node groupNode = dropNode.getFirstChild(); groupNode != null; groupNode = groupNode.getNextSibling()) {
  494. parseDropListItem(groupNode, dropListScope, groupedDropList);
  495. }
  496. List<GeneralDropItem> items = new ArrayList<>(groupedDropList.size());
  497. for (IDropItem item : groupedDropList) {
  498. if (item instanceof GeneralDropItem) {
  499. items.add((GeneralDropItem) item);
  500. } else {
  501. LOG.warn("{} grouped general drop item supports only general drop item.", f);
  502. }
  503. }
  504. dropItem.setItems(items);
  505. drops.add(dropItem);
  506. break;
  507. }
  508. default: {
  509. parseDropListItem(dropNode, dropListScope, drops);
  510. break;
  511. }
  512. }
  513. }
  514. }
  515. private void parseDropListItem(Node dropListItem, DropListScope dropListScope, List<IDropItem> drops) {
  516. NamedNodeMap attrs = dropListItem.getAttributes();
  517. switch (dropListItem.getNodeName().toLowerCase()) {
  518. case "item": {
  519. final IDropItem dropItem = dropListScope.newDropItem(parseInteger(attrs, "id"), parseLong(attrs, "min"), parseLong(attrs, "max"), parseDouble(attrs, "chance"));
  520. if (dropItem != null) {
  521. drops.add(dropItem);
  522. }
  523. break;
  524. }
  525. }
  526. }
  527. /**
  528. * Gets or creates a clan id if it doesnt exists.
  529. * @param clanName the clan name to get or create its id
  530. * @return the clan id for the given clan name
  531. */
  532. private int getOrCreateClanId(String clanName) {
  533. Integer id = _clans.get(clanName.toUpperCase());
  534. if (id == null) {
  535. id = _clans.size();
  536. _clans.put(clanName.toUpperCase(), id);
  537. }
  538. return id;
  539. }
  540. /**
  541. * Gets the clan id
  542. * @param clanName the clan name to get its id
  543. * @return the clan id for the given clan name if it exists, -1 otherwise
  544. */
  545. public int getClanId(String clanName) {
  546. Integer id = _clans.get(clanName.toUpperCase());
  547. return id != null ? id : -1;
  548. }
  549. /**
  550. * Gets the template.
  551. * @param id the template Id to get.
  552. * @return the template for the given id.
  553. */
  554. public L2NpcTemplate getTemplate(int id) {
  555. return _npcs.get(id);
  556. }
  557. /**
  558. * Gets the template by name.
  559. * @param name of the template to get.
  560. * @return the template for the given name.
  561. */
  562. public L2NpcTemplate getTemplateByName(String name) {
  563. for (L2NpcTemplate npcTemplate : _npcs.values()) {
  564. if (npcTemplate.getName().equalsIgnoreCase(name)) {
  565. return npcTemplate;
  566. }
  567. }
  568. return null;
  569. }
  570. /**
  571. * Gets all templates matching the filter.
  572. * @param filter
  573. * @return the template list for the given filter
  574. */
  575. public List<L2NpcTemplate> getTemplates(Predicate<L2NpcTemplate> filter) {
  576. //@formatter:off
  577. return _npcs.values().stream()
  578. .filter(filter)
  579. .collect(Collectors.toList());
  580. //@formatter:on
  581. }
  582. /**
  583. * Gets the all of level.
  584. * @param lvls of all the templates to get.
  585. * @return the template list for the given level.
  586. */
  587. public List<L2NpcTemplate> getAllOfLevel(int... lvls) {
  588. return getTemplates(template -> Util.contains(lvls, template.getLevel()));
  589. }
  590. /**
  591. * Gets the all monsters of level.
  592. * @param lvls of all the monster templates to get.
  593. * @return the template list for the given level.
  594. */
  595. public List<L2NpcTemplate> getAllMonstersOfLevel(int... lvls) {
  596. return getTemplates(template -> Util.contains(lvls, template.getLevel()) && template.isType("L2Monster"));
  597. }
  598. /**
  599. * Gets the all npc starting with.
  600. * @param text of all the NPC templates which its name start with.
  601. * @return the template list for the given letter.
  602. */
  603. public List<L2NpcTemplate> getAllNpcStartingWith(String text) {
  604. return getTemplates(template -> template.isType("L2Npc") && template.getName().startsWith(text));
  605. }
  606. /**
  607. * Gets the all npc of class type.
  608. * @param classTypes of all the templates to get.
  609. * @return the template list for the given class type.
  610. */
  611. public List<L2NpcTemplate> getAllNpcOfClassType(String... classTypes) {
  612. return getTemplates(template -> Util.contains(classTypes, template.getType(), true));
  613. }
  614. public void loadNpcsSkillLearn() {
  615. _npcs.values().forEach(template -> {
  616. final List<ClassId> teachInfo = SkillLearnData.getInstance().getSkillLearnData(template.getId());
  617. if (teachInfo != null) {
  618. template.addTeachInfo(teachInfo);
  619. }
  620. });
  621. }
  622. /**
  623. * This class handles minions from Spawn System<br>
  624. * Once Spawn System gets reworked delete this class<br>
  625. * @author Zealar
  626. */
  627. private static class MinionData implements IXmlReader {
  628. private static final Logger LOG = LoggerFactory.getLogger(MinionData.class);
  629. public final Map<Integer, List<MinionHolder>> _tempMinions = new HashMap<>();
  630. protected MinionData() {
  631. load();
  632. }
  633. @Override
  634. public void load() {
  635. _tempMinions.clear();
  636. parseDatapackFile("data/minionData.xml");
  637. LOG.info("Loaded {} minions data.", _tempMinions.size());
  638. }
  639. @Override
  640. public void parseDocument(Document doc) {
  641. for (Node node = doc.getFirstChild(); node != null; node = node.getNextSibling()) {
  642. if ("list".equals(node.getNodeName())) {
  643. for (Node listNode = node.getFirstChild(); listNode != null; listNode = listNode.getNextSibling()) {
  644. if ("npc".equals(listNode.getNodeName())) {
  645. final List<MinionHolder> minions = new ArrayList<>(1);
  646. NamedNodeMap attrs = listNode.getAttributes();
  647. int id = parseInteger(attrs, "id");
  648. for (Node npcNode = listNode.getFirstChild(); npcNode != null; npcNode = npcNode.getNextSibling()) {
  649. if ("minion".equals(npcNode.getNodeName())) {
  650. attrs = npcNode.getAttributes();
  651. minions.add(new MinionHolder(parseInteger(attrs, "id"), parseInteger(attrs, "count"), parseInteger(attrs, "respawnTime"), 0));
  652. }
  653. }
  654. _tempMinions.put(id, minions);
  655. }
  656. }
  657. }
  658. }
  659. }
  660. }
  661. public static NpcData getInstance() {
  662. return SingletonHolder.INSTANCE;
  663. }
  664. private static class SingletonHolder {
  665. protected static final NpcData INSTANCE = new NpcData();
  666. }
  667. }