Advertisement
Guest User

Anti-Bot.java

a guest
Jun 27th, 2013
3,470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.99 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.l2revive.antibot;
  6.  
  7. import com.l2revive.CustomConfig;
  8. import com.l2revive.util.Util;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.concurrent.ScheduledFuture;
  12. import java.util.logging.Logger;
  13. import net.sf.l2j.Config;
  14. import net.sf.l2j.gameserver.ThreadPoolManager;
  15. import net.sf.l2j.gameserver.model.L2Object;
  16. import net.sf.l2j.gameserver.model.actor.L2Character;
  17. import net.sf.l2j.gameserver.model.actor.L2Summon;
  18. import net.sf.l2j.gameserver.model.actor.instance.L2GrandBossInstance;
  19. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  20. import net.sf.l2j.gameserver.model.actor.instance.L2RaidBossInstance;
  21. import net.sf.l2j.gameserver.network.SystemMessageId;
  22. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  23. import net.sf.l2j.gameserver.skills.AbnormalEffect;
  24. import net.sf.l2j.gameserver.util.IllegalPlayerAction;
  25.  
  26. public class AntiBot {
  27.  
  28. private static final Logger _log = Logger.getLogger(AntiBot.class.getName());
  29. private CustomConfig _config;
  30. private Map<L2PcInstance, BotResults> _players;// player=>BotResults(success/fail)
  31. private Map<L2PcInstance, String> _codes;
  32. protected Map<L2PcInstance, BotEye> _activeEyes;
  33.  
  34. public static AntiBot getInstance() {
  35. return SingletonHolder._instance;
  36. }
  37.  
  38. public AntiBot() {
  39. _log.info("AntiBot: Loading...");
  40. _players = new HashMap<>();
  41. _codes = new HashMap<>();
  42. _activeEyes = new HashMap<>();
  43. }
  44.  
  45. public void setConfig(CustomConfig config) {
  46. _config = config;
  47. String triggerChance = getConfig().getString("AntiBotTriggerChance");
  48. if (triggerChance == null) {
  49. _log.info("AntiBot: #setConfig invalid value for AntiBotTriggerChance; defaulting to 3.");
  50. getConfig().set("TriggerChance", "3");
  51. }
  52. String checkInterval = getConfig().getString("AntiBotReCheckInterval");
  53. if (checkInterval == null) {
  54. _log.info("AntiBot: #setConfig invalid value for AntiBotReCheckInterval; defaulting to 60.");
  55. getConfig().set("AntiBotReCheckInterval", "60");
  56. }
  57. }
  58.  
  59. private CustomConfig getConfig() {
  60. return _config;
  61. }
  62.  
  63. public void add(L2PcInstance player) {
  64. _players.put(player, new BotResults(0, 0));
  65. player.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.S1_SYSMSG_RED).addString("AntiBot: Initialized Successfully."));
  66. }
  67.  
  68. public void remove(L2PcInstance player) {
  69. _players.remove(player);
  70. }
  71.  
  72. public void onKill(L2Character character) {
  73. L2PcInstance player = null;
  74. L2Object target = (character != null) ? character.getTarget() : null;
  75. if (character instanceof L2PcInstance) {
  76. player = (L2PcInstance) character;
  77. } else if (character instanceof L2Summon) {
  78. player = ((L2Summon) character).getActingPlayer();
  79. }
  80. if (player == null) {
  81. _log.info("AntiBot: #onKill for null.");
  82. return;
  83. }
  84. /*
  85. * note: below disabled anti-bot on raids to prevent item stealing.
  86. */
  87. if (target instanceof L2RaidBossInstance || target instanceof L2GrandBossInstance) {
  88. return;
  89. }
  90. /*
  91. * note: below disabled anti-bot on players and summons to prevent pvp interruption.
  92. */
  93. if (target instanceof L2PcInstance || target instanceof L2Summon) {
  94. return;
  95. }
  96. if (!_players.containsKey(player)) {
  97. _players.put(player, new BotResults(0, 0));
  98. triggerCaptcha(player, true);
  99. }
  100. int triggerChance = getConfig().getInteger("AntiBotTriggerChance");
  101. if ((Math.random() * 100) < triggerChance && !_activeEyes.containsKey(player)) {
  102. triggerCaptcha(player, true);
  103. }
  104. }
  105.  
  106. private void triggerCaptcha(L2PcInstance player, boolean setEffect) {
  107. int id = (int) ((Math.random() * 1000) + 1);
  108. if (id < 100) {
  109. id = id + 100;
  110. }
  111. String code = String.valueOf(id);
  112. //_log.info("code=" + code);
  113. _codes.put(player, code);
  114. DDSConverter.sendImage(player, id, DDSConverter.generateImage(code));
  115. //DDSConverter.sendImage(player, id, "data/images/image.png");
  116. int seconds = getConfig().getInteger("AntiBotReCheckInterval");
  117. String html = "<html><title>AntiBot</title><body><center><br><br>"
  118. + "<font color=\"AA00AA\">L2Makai Anti Bot System</font><br><br>"
  119. + "<img src=\"Crest.crest_" + Config.SERVER_ID + "_" + id + "\" width=128 height=32><br>"
  120. + "<edit width=120 var=\"code\"><br>"
  121. + "<button action=\"bypass -h antibot $code\" value=\"Submit Code\" width=75 height=21 back=\"L2UI_ch3.Btn1_normalOn\" fore=\"L2UI_ch3.Btn1_normal\">"
  122. + "<br><br>If you close this window, use <font color=\"LEVEL\">/code</font><br>"
  123. + "You have <font color=\"LEVEL\">" + seconds + " seconds</font> to complete the captcha!<br>"
  124. + "<a action=\"bypass -h antibot refresh\"><font color=\"990099\">Refresh Image Above</font></a><br>"
  125. + "</center></body></html>";
  126. Util.sendRawHtml(player, html);
  127. if (setEffect) {
  128. player.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.S1_SYSMSG_RED).addString("AntiBot: /code"));
  129. ((L2Character) player).startAbnormalEffect(AbnormalEffect.FIREROOT_STUN);
  130. player.setIsImmobilized(true);
  131. player.setIsInvul(true);
  132. _activeEyes.put(player, new BotEye(player, seconds));
  133. }
  134. }
  135.  
  136. public void show(L2PcInstance player) {
  137. if (_activeEyes.containsKey(player)) {
  138. triggerCaptcha(player, false);
  139. }
  140. }
  141.  
  142. public void validate(L2PcInstance player, String code) {
  143. //_log.log(Level.INFO, "validate={0}", code);
  144. code = code.trim();
  145. if (code == null || code.equalsIgnoreCase("refresh")) {
  146. triggerCaptcha(player, false);
  147. return;
  148. }
  149. String valid = _codes.get(player);
  150. boolean status = false;
  151. if (valid != null && valid.equalsIgnoreCase(code)) {
  152. player.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.S1_SYSMSG_RED).addString("AntiBot: Success! You have also been healed."));
  153. ((L2Character) player).stopAbnormalEffect(AbnormalEffect.FIREROOT_STUN);
  154. player.setIsImmobilized(false);
  155. player.setIsInvul(false);
  156. player.setCurrentHp(player.getMaxHp());
  157. if (_activeEyes.containsKey(player)) {
  158. _activeEyes.remove(player).cancel();
  159. }
  160. status = true;
  161. } else {
  162. player.sendPacket(SystemMessage.getSystemMessage(SystemMessageId.S1_SYSMSG_RED).addString("AntiBot: Incorrect! Please try again."));
  163. triggerCaptcha(player, false);
  164. }
  165. BotResults res = _players.get(player);
  166. _players.put(player, new BotResults(res._success + (status ? 1 : 0), res._fails + (!status ? 1 : 0)));
  167. }
  168.  
  169. private static class SingletonHolder {
  170.  
  171. protected static final AntiBot _instance = new AntiBot();
  172. }
  173.  
  174. public static final class BotResults {
  175.  
  176. public final int _success;
  177. public final int _fails;
  178.  
  179. public BotResults(int success, int fails) {
  180. _success = success;
  181. _fails = fails;
  182. }
  183. }
  184.  
  185. private final class BotEye implements Runnable {
  186.  
  187. private ScheduledFuture _task;
  188. private int _delay;
  189. private L2PcInstance _player;
  190.  
  191. public BotEye(L2PcInstance player, int delay) {
  192. _player = player;
  193. _delay = delay;
  194. run();
  195. }
  196.  
  197. /*
  198. * if we reach here, then the player did not enter the code intime.
  199. */
  200. @Override
  201. public void run() {
  202. if (_delay < 1) {
  203. net.sf.l2j.gameserver.util.Util.handleIllegalPlayerAction(_player, "Player " + _player.getName() + " did not enter the antibot captcha intime.", IllegalPlayerAction.PUNISH_KICK);
  204. return;
  205. }
  206. handleTick();
  207. _task = ThreadPoolManager.getInstance().scheduleGeneral(this, 1000);
  208. _delay--;
  209. }
  210.  
  211. public void cancel() {
  212. if (_task != null) {
  213. _task.cancel(true);
  214. }
  215. }
  216.  
  217. private void handleTick() {
  218. switch (_delay) {
  219. case 30:
  220. case 10:
  221. case 5:
  222. case 4:
  223. case 3:
  224. case 2:
  225. case 1:
  226. Util.sendMessageInBlue(_player, "AntiBot: You have " + _delay + " seconds to complete the captcha.");
  227. break;
  228. }
  229. }
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement