Validator.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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.SubClassCertification;
  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. *
  27. * @author DS
  28. *
  29. */
  30. public class Validator extends Quest
  31. {
  32. // arrays must be sorted
  33. 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};
  34. private static final int[][] _certSkillsByLevel =
  35. {
  36. { 631,632,633,634 },
  37. { 631,632,633,634 },
  38. { 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 },
  39. { 656,657,658,659,660,661,662 }
  40. };
  41. private static final int[] _allCertItemIds = {10280,10281,10282,10283,10284,10285,10286,10287,10288,10289,10290,10291,10292,10293,10294,10612};
  42. private static final int[][] _certItemsByLevel =
  43. {
  44. { 10280 },
  45. { 10280 },
  46. { 10612,10281,10282,10283,10284,10285,10286,10287 },
  47. { 10288,10289,10290,10291,10292,10293,10294 }
  48. };
  49. private static final String[] VARS =
  50. {
  51. "EmergentAbility65-",
  52. "EmergentAbility70-",
  53. "ClassAbility75-",
  54. "ClassAbility80-"
  55. };
  56. public Validator(int id, String name, String descr)
  57. {
  58. super(id, name, descr);
  59. setOnEnterWorld(true);
  60. }
  61. @Override
  62. public final String onEnterWorld(L2PcInstance player)
  63. {
  64. if (!Config.SKILL_CHECK_ENABLE)
  65. return null;
  66. if (player.isGM() && !Config.SKILL_CHECK_GM)
  67. return null;
  68. final L2Skill[] certSkills = getCertSkills(player);
  69. final boolean hasCertSkills = certSkills != null;
  70. if (player.isSubClassActive())
  71. {
  72. if (hasCertSkills)
  73. {
  74. for (L2Skill s : certSkills)
  75. {
  76. Util.handleIllegalPlayerAction(player, "Player "+player.getName() +
  77. " has cert skill on subclass :" + s.getName() +
  78. "("+s.getId()+"/"+s.getLevel()+"), class:" +
  79. player.getTemplate().className, 0);
  80. if (Config.SKILL_CHECK_REMOVE)
  81. player.removeSkill(s);
  82. }
  83. }
  84. return null;
  85. }
  86. L2Skill skill;
  87. int[][] cSkills = null; // skillId/skillLvl
  88. if (hasCertSkills)
  89. {
  90. cSkills = new int[certSkills.length][2];
  91. for (int i = certSkills.length; --i >= 0;)
  92. {
  93. skill = certSkills[i];
  94. cSkills[i][0] = skill.getId();
  95. cSkills[i][1] = skill.getLevel();
  96. }
  97. }
  98. L2ItemInstance item;
  99. int[][] cItems = null; // objectId/number
  100. final L2ItemInstance[] certItems = getCertItems(player);
  101. final boolean hasCertItems = certItems != null;
  102. if (hasCertItems)
  103. {
  104. cItems = new int[certItems.length][2];
  105. for (int i = certItems.length; --i >= 0;)
  106. {
  107. item = certItems[i];
  108. cItems[i][0] = item.getObjectId();
  109. cItems[i][1] = (int)Math.min(item.getCount(), Integer.MAX_VALUE);
  110. }
  111. }
  112. QuestState st = player.getQuestState(Validator.class.getSimpleName());
  113. if (st == null)
  114. st = newQuestState(player);
  115. String qName,qValue;
  116. int id, index;
  117. for (int i = VARS.length; --i >= 0;)
  118. {
  119. for (int j = Config.MAX_SUBCLASS; j > 0; j--)
  120. {
  121. qName = VARS[i]+String.valueOf(j);
  122. qValue = st.getGlobalQuestVar(qName);
  123. if (qValue == null || qValue.isEmpty())
  124. continue;
  125. if (qValue.endsWith(";")) // found skill
  126. {
  127. try
  128. {
  129. id = Integer.parseInt(qValue.replace(";", ""));
  130. skill = null;
  131. if (hasCertSkills)
  132. {
  133. // searching skill in test array
  134. for (index = certSkills.length; --index >= 0;)
  135. {
  136. if (cSkills[index][0] == id)
  137. {
  138. skill = certSkills[index];
  139. cSkills[index][1]--;
  140. break;
  141. }
  142. }
  143. if (skill != null)
  144. {
  145. if (!Util.contains(_certSkillsByLevel[i], id))
  146. {
  147. // should remove this skill ?
  148. Util.handleIllegalPlayerAction(player, "Invalid cert variable WITH skill:" +
  149. qName + "=" + qValue + " - skill does not match certificate level", 0);
  150. }
  151. }
  152. else
  153. {
  154. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" +
  155. qName + "=" + qValue + " - skill not found", 0);
  156. }
  157. }
  158. else
  159. {
  160. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" +
  161. qName + "=" + qValue + " - no certified skills found", 0);
  162. }
  163. }
  164. catch(NumberFormatException e)
  165. {
  166. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" +
  167. qName + "=" + qValue + " - not a number", 0);
  168. }
  169. }
  170. else // found item
  171. {
  172. try
  173. {
  174. id = Integer.parseInt(qValue);
  175. if (id == 0) // canceled skill, no item
  176. continue;
  177. item = null;
  178. if (hasCertItems)
  179. {
  180. // searching item in test array
  181. for (index = certItems.length; --index >= 0;)
  182. {
  183. if (cItems[index][0] == id)
  184. {
  185. item = certItems[index];
  186. cItems[index][1]--;
  187. break;
  188. }
  189. }
  190. if (item != null)
  191. {
  192. if (!Util.contains(_certItemsByLevel[i], item.getItemId()))
  193. {
  194. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" +
  195. qName + "=" + qValue + " - item found but does not match certificate level", 0);
  196. }
  197. }
  198. else
  199. {
  200. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" +
  201. qName + "=" + qValue + " - item not found", 0);
  202. }
  203. }
  204. else
  205. {
  206. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" +
  207. qName + "=" + qValue + " - no cert item found in inventory", 0);
  208. }
  209. }
  210. catch(NumberFormatException e)
  211. {
  212. Util.handleIllegalPlayerAction(player, "Invalid cert variable:" +
  213. qName + "=" + qValue + " - not a number", 0);
  214. }
  215. }
  216. }
  217. }
  218. if (hasCertSkills)
  219. {
  220. for (int i = cSkills.length; --i >= 0;)
  221. {
  222. if (cSkills[i][1] == 0)
  223. continue;
  224. skill = certSkills[i];
  225. if (cSkills[i][1] > 0)
  226. {
  227. if (cSkills[i][1] == skill.getLevel())
  228. Util.handleIllegalPlayerAction(player, "Player " + player.getName() +
  229. " has invalid cert skill :" + skill.getName() +
  230. "(" + skill.getId() + "/" + skill.getLevel() + ")", 0);
  231. else
  232. Util.handleIllegalPlayerAction(player, "Player " + player.getName() +
  233. " has invalid cert skill :" + skill.getName() +
  234. "(" + skill.getId() + "/" + skill.getLevel() + "), level too high", 0);
  235. if (Config.SKILL_CHECK_REMOVE)
  236. player.removeSkill(skill);
  237. }
  238. else
  239. {
  240. Util.handleIllegalPlayerAction(player, "Invalid cert skill :" + skill.getName() +
  241. "(" + skill.getId() + "/" + skill.getLevel() + "), level too low", 0);
  242. }
  243. }
  244. }
  245. if (hasCertItems)
  246. {
  247. for (int i = cItems.length; --i >= 0;)
  248. {
  249. if (cItems[i][1] == 0)
  250. continue;
  251. item = certItems[i];
  252. Util.handleIllegalPlayerAction(player, "Invalid cert item without variable or with wrong count:" + item.getObjectId(), 0);
  253. }
  254. }
  255. return null;
  256. }
  257. private final L2Skill[] getCertSkills(L2PcInstance player)
  258. {
  259. FastList<L2Skill> tmp = null;
  260. for(L2Skill s : player.getAllSkills())
  261. {
  262. if (s != null && Arrays.binarySearch(_allCertSkillIds, s.getId()) >= 0)
  263. {
  264. if (tmp == null)
  265. tmp = FastList.newInstance();
  266. tmp.add(s);
  267. }
  268. }
  269. if (tmp == null)
  270. return null;
  271. final L2Skill[] result = tmp.toArray(new L2Skill[tmp.size()]);
  272. FastList.recycle(tmp);
  273. return result;
  274. }
  275. private final L2ItemInstance[] getCertItems(L2PcInstance player)
  276. {
  277. FastList<L2ItemInstance> tmp = null;
  278. for (L2ItemInstance i : player.getInventory().getItems())
  279. {
  280. if (i != null && Arrays.binarySearch(_allCertItemIds, i.getItemId()) >= 0)
  281. {
  282. if (tmp == null)
  283. tmp = FastList.newInstance();
  284. tmp.add(i);
  285. }
  286. }
  287. if (tmp == null)
  288. return null;
  289. final L2ItemInstance[] result = tmp.toArray(new L2ItemInstance[tmp.size()]);
  290. FastList.recycle(tmp);
  291. return result;
  292. }
  293. public static final void main(String[] args)
  294. {
  295. new Validator(-1, Validator.class.getSimpleName(), "custom");
  296. }
  297. }