SubClassSkills.java 10 KB

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