SubClassSkills.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package custom.Validators;
  16. import java.util.Arrays;
  17. import javolution.util.FastList;
  18. import com.l2jserver.Config;
  19. import com.l2jserver.gameserver.model.L2ItemInstance;
  20. import com.l2jserver.gameserver.model.L2Skill;
  21. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  22. import com.l2jserver.gameserver.model.quest.Quest;
  23. import com.l2jserver.gameserver.model.quest.QuestState;
  24. import com.l2jserver.gameserver.util.Util;
  25. /**
  26. * @author DS
  27. */
  28. public final class SubClassSkills extends Quest
  29. {
  30. // arrays must be sorted
  31. private static final int[] _allCertSkillIds = {631,632,633,634,637,638,639,640,641,642,643,644,645,646,647,648,650,651,652,653,654,655,656,657,658,659,660,661,662,799,800,801,802,803,804,1489,1490,1491};
  32. private static final int[][] _certSkillsByLevel =
  33. {
  34. { 631,632,633,634 },
  35. { 631,632,633,634 },
  36. { 637,638,639,640,641,642,643,644,645,646,647,648,650,651,652,653,654,655,799,800,801,802,803,804,1489,1490,1491 },
  37. { 656,657,658,659,660,661,662 }
  38. };
  39. private static final int[] _allCertItemIds = {10280,10281,10282,10283,10284,10285,10286,10287,10288,10289,10290,10291,10292,10293,10294,10612};
  40. private static final int[][] _certItemsByLevel =
  41. {
  42. { 10280 },
  43. { 10280 },
  44. { 10612,10281,10282,10283,10284,10285,10286,10287 },
  45. { 10288,10289,10290,10291,10292,10293,10294 }
  46. };
  47. private static final String[] VARS =
  48. {
  49. "EmergentAbility65-",
  50. "EmergentAbility70-",
  51. "ClassAbility75-",
  52. "ClassAbility80-"
  53. };
  54. public SubClassSkills(int id, String name, String descr)
  55. {
  56. super(id, name, descr);
  57. setOnEnterWorld(true);
  58. }
  59. @Override
  60. public String onEnterWorld(L2PcInstance player)
  61. {
  62. if (!Config.SKILL_CHECK_ENABLE)
  63. return null;
  64. if (player.isGM() && !Config.SKILL_CHECK_GM)
  65. return null;
  66. final L2Skill[] certSkills = getCertSkills(player);
  67. final boolean hasCertSkills = certSkills != null;
  68. if (player.isSubClassActive())
  69. {
  70. if (hasCertSkills)
  71. {
  72. for (L2Skill s : certSkills)
  73. {
  74. Util.handleIllegalPlayerAction(player, "Player "+player.getName() + " has cert skill on subclass :" + s.getName() + "("+s.getId()+"/"+s.getLevel()+"), class:" + player.getTemplate().className, 0);
  75. if (Config.SKILL_CHECK_REMOVE)
  76. player.removeSkill(s);
  77. }
  78. }
  79. return null;
  80. }
  81. L2Skill skill;
  82. int[][] cSkills = null; // skillId/skillLvl
  83. if (hasCertSkills)
  84. {
  85. cSkills = new int[certSkills.length][2];
  86. for (int i = certSkills.length; --i >= 0;)
  87. {
  88. skill = certSkills[i];
  89. cSkills[i][0] = skill.getId();
  90. cSkills[i][1] = skill.getLevel();
  91. }
  92. }
  93. L2ItemInstance item;
  94. int[][] cItems = null; // objectId/number
  95. final L2ItemInstance[] certItems = getCertItems(player);
  96. final boolean hasCertItems = certItems != null;
  97. if (hasCertItems)
  98. {
  99. cItems = new int[certItems.length][2];
  100. for (int i = certItems.length; --i >= 0;)
  101. {
  102. item = certItems[i];
  103. cItems[i][0] = item.getObjectId();
  104. cItems[i][1] = (int)Math.min(item.getCount(), Integer.MAX_VALUE);
  105. }
  106. }
  107. QuestState st = player.getQuestState("SubClassSkills");
  108. if (st == null)
  109. st = newQuestState(player);
  110. String qName,qValue;
  111. int id, index;
  112. for (int i = VARS.length; --i >= 0;)
  113. {
  114. for (int j = Config.MAX_SUBCLASS; j > 0; j--)
  115. {
  116. qName = VARS[i]+String.valueOf(j);
  117. qValue = st.getGlobalQuestVar(qName);
  118. if (qValue == null || qValue.isEmpty())
  119. continue;
  120. if (qValue.endsWith(";")) // found skill
  121. {
  122. try
  123. {
  124. id = Integer.parseInt(qValue.replace(";", ""));
  125. skill = null;
  126. if (hasCertSkills)
  127. {
  128. // searching skill in test array
  129. for (index = certSkills.length; --index >= 0;)
  130. {
  131. if (cSkills[index][0] == id)
  132. {
  133. skill = certSkills[index];
  134. cSkills[index][1]--;
  135. break;
  136. }
  137. }
  138. if (skill != null)
  139. {
  140. if (!Util.contains(_certSkillsByLevel[i], id))
  141. {
  142. // should remove this skill ?
  143. Util.handleIllegalPlayerAction(player, "Invalid cert variable WITH skill:" + qName + "=" + qValue + " - skill does not match certificate level", 0);
  144. }
  145. }
  146. else
  147. {
  148. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" + qName + "=" + qValue + " - skill not found", 0);
  149. }
  150. }
  151. else
  152. {
  153. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" + qName + "=" + qValue + " - no certified skills found", 0);
  154. }
  155. }
  156. catch(NumberFormatException e)
  157. {
  158. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" + qName + "=" + qValue + " - not a number", 0);
  159. }
  160. }
  161. else // found item
  162. {
  163. try
  164. {
  165. id = Integer.parseInt(qValue);
  166. if (id == 0) // canceled skill, no item
  167. continue;
  168. item = null;
  169. if (hasCertItems)
  170. {
  171. // searching item in test array
  172. for (index = certItems.length; --index >= 0;)
  173. {
  174. if (cItems[index][0] == id)
  175. {
  176. item = certItems[index];
  177. cItems[index][1]--;
  178. break;
  179. }
  180. }
  181. if (item != null)
  182. {
  183. if (!Util.contains(_certItemsByLevel[i], item.getItemId()))
  184. {
  185. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" +
  186. qName + "=" + qValue + " - item found but does not match certificate level", 0);
  187. }
  188. }
  189. else
  190. {
  191. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" +
  192. qName + "=" + qValue + " - item not found", 0);
  193. }
  194. }
  195. else
  196. {
  197. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" +
  198. qName + "=" + qValue + " - no cert item found in inventory", 0);
  199. }
  200. }
  201. catch(NumberFormatException e)
  202. {
  203. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" +
  204. qName + "=" + qValue + " - not a number", 0);
  205. }
  206. }
  207. }
  208. }
  209. if (hasCertSkills)
  210. {
  211. for (int i = cSkills.length; --i >= 0;)
  212. {
  213. if (cSkills[i][1] == 0)
  214. continue;
  215. skill = certSkills[i];
  216. if (cSkills[i][1] > 0)
  217. {
  218. if (cSkills[i][1] == skill.getLevel())
  219. Util.handleIllegalPlayerAction(player, "Player " + player.getName() +
  220. " has invalid cert skill :" + skill.getName() +
  221. "(" + skill.getId() + "/" + skill.getLevel() + ")", 0);
  222. else
  223. Util.handleIllegalPlayerAction(player, "Player " + player.getName() +
  224. " has invalid cert skill :" + skill.getName() +
  225. "(" + skill.getId() + "/" + skill.getLevel() + "), level too high", 0);
  226. if (Config.SKILL_CHECK_REMOVE)
  227. player.removeSkill(skill);
  228. }
  229. else
  230. {
  231. Util.handleIllegalPlayerAction(player, "Invalid cert skill :" + skill.getName() +
  232. "(" + skill.getId() + "/" + skill.getLevel() + "), level too low", 0);
  233. }
  234. }
  235. }
  236. if (hasCertItems)
  237. {
  238. for (int i = cItems.length; --i >= 0;)
  239. {
  240. if (cItems[i][1] == 0)
  241. continue;
  242. item = certItems[i];
  243. Util.handleIllegalPlayerAction(player, "Invalid cert item without variable or with wrong count:" + item.getObjectId(), 0);
  244. }
  245. }
  246. return null;
  247. }
  248. private L2Skill[] getCertSkills(L2PcInstance player)
  249. {
  250. FastList<L2Skill> tmp = null;
  251. for(L2Skill s : player.getAllSkills())
  252. {
  253. if (s != null && Arrays.binarySearch(_allCertSkillIds, s.getId()) >= 0)
  254. {
  255. if (tmp == null)
  256. tmp = FastList.newInstance();
  257. tmp.add(s);
  258. }
  259. }
  260. if (tmp == null)
  261. return null;
  262. final L2Skill[] result = tmp.toArray(new L2Skill[tmp.size()]);
  263. FastList.recycle(tmp);
  264. return result;
  265. }
  266. private L2ItemInstance[] getCertItems(L2PcInstance player)
  267. {
  268. FastList<L2ItemInstance> tmp = null;
  269. for (L2ItemInstance i : player.getInventory().getItems())
  270. {
  271. if (i != null && Arrays.binarySearch(_allCertItemIds, i.getItemId()) >= 0)
  272. {
  273. if (tmp == null)
  274. tmp = FastList.newInstance();
  275. tmp.add(i);
  276. }
  277. }
  278. if (tmp == null)
  279. return null;
  280. final L2ItemInstance[] result = tmp.toArray(new L2ItemInstance[tmp.size()]);
  281. FastList.recycle(tmp);
  282. return result;
  283. }
  284. public static void main(String[] args)
  285. {
  286. new SubClassSkills(-1, "SubClassSkills", "custom");
  287. }
  288. }