SubClassSkills.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (C) 2004-2015 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack 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 DataPack 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 custom.Validators;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.data.xml.impl.ClassListData;
  25. import com.l2jserver.gameserver.enums.IllegalActionPunishmentType;
  26. import com.l2jserver.gameserver.model.PcCondOverride;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  29. import com.l2jserver.gameserver.model.quest.Quest;
  30. import com.l2jserver.gameserver.model.quest.QuestState;
  31. import com.l2jserver.gameserver.model.skills.Skill;
  32. import com.l2jserver.gameserver.util.Util;
  33. /**
  34. * Sub-class skills validator.<br>
  35. * TODO: Rewrite.
  36. * @author DS
  37. */
  38. public final class SubClassSkills extends Quest
  39. {
  40. // arrays must be sorted
  41. // @formatter:off
  42. private static final int[] _allCertSkillIds =
  43. {
  44. 631, 632, 633, 634, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646,
  45. 647, 648, 650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661,
  46. 662, 799, 800, 801, 802, 803, 804, 1489, 1490, 1491
  47. };
  48. private static final int[][] _certSkillsByLevel =
  49. {
  50. {
  51. 631, 632, 633, 634
  52. },
  53. {
  54. 631, 632, 633, 634
  55. },
  56. {
  57. 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 650,
  58. 651, 652, 653, 654, 655, 799, 800, 801, 802, 803, 804, 1489, 1490,
  59. 1491
  60. },
  61. {
  62. 656, 657, 658, 659, 660, 661, 662
  63. }
  64. };
  65. private static final int[] _allCertItemIds =
  66. {
  67. 10280, 10281, 10282, 10283, 10284, 10285, 10286, 10287, 10288, 10289,
  68. 10290, 10291, 10292, 10293, 10294, 10612
  69. };
  70. private static final int[][] _certItemsByLevel =
  71. {
  72. { 10280 },
  73. { 10280 },
  74. { 10612, 10281, 10282, 10283, 10284, 10285, 10286, 10287 },
  75. { 10288, 10289, 10290, 10291, 10292, 10293, 10294 }
  76. };
  77. // @formatter:on
  78. private static final String[] VARS =
  79. {
  80. "EmergentAbility65-",
  81. "EmergentAbility70-",
  82. "ClassAbility75-",
  83. "ClassAbility80-"
  84. };
  85. private SubClassSkills()
  86. {
  87. super(-1, SubClassSkills.class.getSimpleName(), "custom");
  88. setOnEnterWorld(true);
  89. }
  90. @Override
  91. public String onEnterWorld(L2PcInstance player)
  92. {
  93. if (!Config.SKILL_CHECK_ENABLE)
  94. {
  95. return null;
  96. }
  97. if (player.canOverrideCond(PcCondOverride.SKILL_CONDITIONS) && !Config.SKILL_CHECK_GM)
  98. {
  99. return null;
  100. }
  101. final List<Skill> certSkills = getCertSkills(player);
  102. if (player.isSubClassActive())
  103. {
  104. for (Skill s : certSkills)
  105. {
  106. Util.handleIllegalPlayerAction(player, "Player " + player.getName() + " has cert skill on subclass :" + s.getName() + "(" + s.getId() + "/" + s.getLevel() + "), class:" + ClassListData.getInstance().getClass(player.getClassId()).getClassName(), IllegalActionPunishmentType.NONE);
  107. if (Config.SKILL_CHECK_REMOVE)
  108. {
  109. player.removeSkill(s);
  110. }
  111. }
  112. return null;
  113. }
  114. int[][] cSkills = new int[certSkills.size()][2]; // skillId/skillLvl
  115. for (int i = certSkills.size(); --i >= 0;)
  116. {
  117. Skill skill = certSkills.get(i);
  118. cSkills[i][0] = skill.getId();
  119. cSkills[i][1] = skill.getLevel();
  120. }
  121. final List<L2ItemInstance> certItems = getCertItems(player);
  122. int[][] cItems = new int[certItems.size()][2]; // objectId/number
  123. for (int i = certItems.size(); --i >= 0;)
  124. {
  125. L2ItemInstance item = certItems.get(i);
  126. cItems[i][0] = item.getObjectId();
  127. cItems[i][1] = (int) Math.min(item.getCount(), Integer.MAX_VALUE);
  128. }
  129. QuestState st = player.getQuestState("SubClassSkills");
  130. if (st == null)
  131. {
  132. st = newQuestState(player);
  133. }
  134. String qName, qValue;
  135. int id, index;
  136. for (int i = VARS.length; --i >= 0;)
  137. {
  138. for (int j = Config.MAX_SUBCLASS; j > 0; j--)
  139. {
  140. qName = VARS[i] + String.valueOf(j);
  141. qValue = st.getGlobalQuestVar(qName);
  142. if ((qValue == null) || qValue.isEmpty())
  143. {
  144. continue;
  145. }
  146. if (qValue.endsWith(";")) // found skill
  147. {
  148. try
  149. {
  150. id = Integer.parseInt(qValue.replace(";", ""));
  151. Skill skill = null;
  152. if (certSkills != null)
  153. {
  154. // searching skill in test array
  155. if (cSkills != null)
  156. {
  157. for (index = certSkills.size(); --index >= 0;)
  158. {
  159. if (cSkills[index][0] == id)
  160. {
  161. skill = certSkills.get(index);
  162. cSkills[index][1]--;
  163. break;
  164. }
  165. }
  166. }
  167. if (skill != null)
  168. {
  169. if (!Util.contains(_certSkillsByLevel[i], id))
  170. {
  171. // should remove this skill ?
  172. Util.handleIllegalPlayerAction(player, "Invalid cert variable WITH skill:" + qName + "=" + qValue + " - skill does not match certificate level", IllegalActionPunishmentType.NONE);
  173. }
  174. }
  175. else
  176. {
  177. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" + qName + "=" + qValue + " - skill not found", IllegalActionPunishmentType.NONE);
  178. }
  179. }
  180. else
  181. {
  182. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" + qName + "=" + qValue + " - no certified skills found", IllegalActionPunishmentType.NONE);
  183. }
  184. }
  185. catch (NumberFormatException e)
  186. {
  187. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" + qName + "=" + qValue + " - not a number", IllegalActionPunishmentType.NONE);
  188. }
  189. }
  190. else
  191. // found item
  192. {
  193. try
  194. {
  195. id = Integer.parseInt(qValue);
  196. if (id == 0)
  197. {
  198. continue;
  199. }
  200. L2ItemInstance item = null;
  201. if (certItems != null)
  202. {
  203. // searching item in test array
  204. if (cItems != null)
  205. {
  206. for (index = certItems.size(); --index >= 0;)
  207. {
  208. if (cItems[index][0] == id)
  209. {
  210. item = certItems.get(index);
  211. cItems[index][1]--;
  212. break;
  213. }
  214. }
  215. }
  216. if (item != null)
  217. {
  218. if (!Util.contains(_certItemsByLevel[i], item.getId()))
  219. {
  220. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" + qName + "=" + qValue + " - item found but does not match certificate level", IllegalActionPunishmentType.NONE);
  221. }
  222. }
  223. else
  224. {
  225. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" + qName + "=" + qValue + " - item not found", IllegalActionPunishmentType.NONE);
  226. }
  227. }
  228. else
  229. {
  230. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" + qName + "=" + qValue + " - no cert item found in inventory", IllegalActionPunishmentType.NONE);
  231. }
  232. }
  233. catch (NumberFormatException e)
  234. {
  235. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" + qName + "=" + qValue + " - not a number", IllegalActionPunishmentType.NONE);
  236. }
  237. }
  238. }
  239. }
  240. if ((certSkills != null) && (cSkills != null))
  241. {
  242. for (int i = cSkills.length; --i >= 0;)
  243. {
  244. if (cSkills[i][1] == 0)
  245. {
  246. continue;
  247. }
  248. Skill skill = certSkills.get(i);
  249. if (cSkills[i][1] > 0)
  250. {
  251. if (cSkills[i][1] == skill.getLevel())
  252. {
  253. Util.handleIllegalPlayerAction(player, "Player " + player.getName() + " has invalid cert skill :" + skill.getName() + "(" + skill.getId() + "/" + skill.getLevel() + ")", IllegalActionPunishmentType.NONE);
  254. }
  255. else
  256. {
  257. Util.handleIllegalPlayerAction(player, "Player " + player.getName() + " has invalid cert skill :" + skill.getName() + "(" + skill.getId() + "/" + skill.getLevel() + "), level too high", IllegalActionPunishmentType.NONE);
  258. }
  259. if (Config.SKILL_CHECK_REMOVE)
  260. {
  261. player.removeSkill(skill);
  262. }
  263. }
  264. else
  265. {
  266. Util.handleIllegalPlayerAction(player, "Invalid cert skill :" + skill.getName() + "(" + skill.getId() + "/" + skill.getLevel() + "), level too low", IllegalActionPunishmentType.NONE);
  267. }
  268. }
  269. }
  270. if ((certItems != null) && (cItems != null))
  271. {
  272. for (int i = cItems.length; --i >= 0;)
  273. {
  274. if (cItems[i][1] == 0)
  275. {
  276. continue;
  277. }
  278. L2ItemInstance item = certItems.get(i);
  279. Util.handleIllegalPlayerAction(player, "Invalid cert item without variable or with wrong count:" + item.getObjectId(), IllegalActionPunishmentType.NONE);
  280. }
  281. }
  282. return null;
  283. }
  284. private List<Skill> getCertSkills(L2PcInstance player)
  285. {
  286. final List<Skill> tmp = new ArrayList<>();
  287. for (Skill s : player.getAllSkills())
  288. {
  289. if ((s != null) && (Arrays.binarySearch(_allCertSkillIds, s.getId()) >= 0))
  290. {
  291. tmp.add(s);
  292. }
  293. }
  294. return tmp;
  295. }
  296. private List<L2ItemInstance> getCertItems(L2PcInstance player)
  297. {
  298. final List<L2ItemInstance> tmp = new ArrayList<>();
  299. for (L2ItemInstance i : player.getInventory().getItems())
  300. {
  301. if ((i != null) && (Arrays.binarySearch(_allCertItemIds, i.getId()) >= 0))
  302. {
  303. tmp.add(i);
  304. }
  305. }
  306. return tmp;
  307. }
  308. public static void main(String[] args)
  309. {
  310. new SubClassSkills();
  311. }
  312. }