Advertisement
Guest User

Antibot.java Antibot v2 Handler

a guest
Jun 15th, 2011
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.73 KB | None | 0 0
  1. package handlers.voicedcommandhandlers;
  2.  
  3. import gov.nasa.worldwind.formats.dds.DDSConverter;
  4.  
  5. import java.awt.Color;
  6. import java.awt.Font;
  7. import java.awt.FontMetrics;
  8. import java.awt.Graphics2D;
  9. import java.awt.geom.AffineTransform;
  10. import java.awt.image.BufferedImage;
  11. import java.io.File;
  12. import java.util.StringTokenizer;
  13. import java.util.logging.Level;
  14.  
  15. import javax.imageio.ImageIO;
  16.  
  17. import com.l2jserver.Config;
  18. import com.l2jserver.gameserver.handler.IVoicedCommandHandler;
  19. import com.l2jserver.gameserver.idfactory.IdFactory;
  20. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  21. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  22. import com.l2jserver.gameserver.network.serverpackets.PledgeCrest;
  23. import com.l2jserver.gameserver.skills.AbnormalEffect;
  24. /**
  25. *
  26. * @author Pipiou211
  27. *
  28. */
  29. public class Antibot implements IVoicedCommandHandler
  30. {
  31. private static final String[] _voicedCommands = { "antibot" };
  32.  
  33.  
  34. public static StringBuilder finalString = new StringBuilder();
  35. NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
  36. private static BufferedImage generateCaptcha()
  37. {
  38. Color textColor = new Color(98, 213, 43);
  39. Color circleColor = new Color(98, 213, 43);
  40. Font textFont = new Font("comic sans ms", Font.BOLD, 24);
  41. int charsToPrint = 5;
  42. int width = 256;
  43. int height = 64;
  44. int circlesToDraw = 8;
  45. float horizMargin = 20.0f;
  46. double rotationRange = 0.7; // this is radians
  47. BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  48.  
  49. Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
  50.  
  51. //Draw an oval
  52. g.setColor(new Color(30,31,31));
  53. g.fillRect(0, 0, width, height);
  54.  
  55. // lets make some noisey circles
  56. g.setColor(circleColor);
  57. for ( int i = 0; i < circlesToDraw; i++ ) {
  58. int circleRadius = (int) (Math.random() * height / 2.0);
  59. int circleX = (int) (Math.random() * width - circleRadius);
  60. int circleY = (int) (Math.random() * height - circleRadius);
  61. g.drawOval(circleX, circleY, circleRadius * 2, circleRadius * 2);
  62. }
  63.  
  64. g.setColor(textColor);
  65. g.setFont(textFont);
  66.  
  67. FontMetrics fontMetrics = g.getFontMetrics();
  68. int maxAdvance = fontMetrics.getMaxAdvance();
  69. int fontHeight = fontMetrics.getHeight();
  70.  
  71. // Suggestions ----------------------------------------------------------------------
  72. // i removed 1 and l and i because there are confusing to users...
  73. // Z, z, and N also get confusing when rotated
  74. // 0, O, and o are also confusing...
  75. // lowercase G looks a lot like a 9 so i killed it
  76. // this should ideally be done for every language...
  77. // i like controlling the characters though because it helps prevent confusion
  78. // So recommended chars are:
  79. // String elegibleChars = "ABCDEFGHJKLMPQRSTUVWXYabcdefhjkmnpqrstuvwxy23456789";
  80. // Suggestions ----------------------------------------------------------------------
  81. String elegibleChars = "ABCDEFGHJKLMPQRSTUVWXYZ";
  82. char[] chars = elegibleChars.toCharArray();
  83.  
  84. float spaceForLetters = -horizMargin * 2 + width;
  85. float spacePerChar = spaceForLetters / (charsToPrint - 1.0f);
  86.  
  87. for ( int i = 0; i < charsToPrint; i++ ) {
  88. double randomValue = Math.random();
  89. int randomIndex = (int) Math.round(randomValue * (chars.length - 1));
  90. char characterToShow = chars[randomIndex];
  91. finalString.append(characterToShow);
  92.  
  93. // this is a separate canvas used for the character so that
  94. // we can rotate it independently
  95. int charWidth = fontMetrics.charWidth(characterToShow);
  96. int charDim = Math.max(maxAdvance, fontHeight);
  97. int halfCharDim = (charDim / 2);
  98.  
  99. BufferedImage charImage = new BufferedImage(charDim, charDim, BufferedImage.TYPE_INT_ARGB);
  100. Graphics2D charGraphics = charImage.createGraphics();
  101. charGraphics.translate(halfCharDim, halfCharDim);
  102. double angle = (Math.random() - 0.5) * rotationRange;
  103. charGraphics.transform(AffineTransform.getRotateInstance(angle));
  104. charGraphics.translate(-halfCharDim,-halfCharDim);
  105. charGraphics.setColor(textColor);
  106. charGraphics.setFont(textFont);
  107.  
  108. int charX = (int) (0.5 * charDim - 0.5 * charWidth);
  109. charGraphics.drawString("" + characterToShow, charX,
  110. ((charDim - fontMetrics.getAscent())
  111. / 2 + fontMetrics.getAscent()));
  112.  
  113. float x = horizMargin + spacePerChar * (i) - charDim / 2.0f;
  114. int y = ((height - charDim) / 2);
  115. g.drawImage(charImage, (int) x, y, charDim, charDim, null, null);
  116.  
  117. charGraphics.dispose();
  118. }
  119.  
  120. g.dispose();
  121.  
  122. return bufferedImage;
  123. }
  124.  
  125. public boolean useVoicedCommand(String command, L2PcInstance activeChar, String target)
  126. {
  127. NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  128. if (command.equalsIgnoreCase("antibot") && target != null)
  129. {
  130. StringTokenizer st = new StringTokenizer(target);
  131. try
  132. {
  133. String newpass = null, repeatnewpass = null;
  134. if (st.hasMoreTokens())
  135. newpass = st.nextToken();
  136. repeatnewpass = activeChar.getCode();
  137.  
  138. if (!(newpass == null || repeatnewpass == null))
  139. {
  140. if (newpass.equals(repeatnewpass))//Right:)
  141. {
  142. npcHtmlMessage.setHtml("<html><title>Captcha Antibot System</title><body><center><font color=\"00FF00\">Correct Captcha.<br><br></font><center><br><button value=\"Exit\" action=\"bypass -h npc_%objectId%_Quest\" width=45 height=25 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></center></body></html>");
  143. activeChar.sendPacket(npcHtmlMessage);
  144. activeChar.stopAbnormalEffect(AbnormalEffect.REAL_TARGET);
  145. activeChar.setIsInvul(false);
  146. activeChar.setIsParalyzed(false);
  147. activeChar.setKills(0);
  148. activeChar.setCodeRight(true);
  149. return false;
  150. }
  151.  
  152. }
  153. if (!newpass.equals(repeatnewpass))//Wrong
  154. {
  155. if (activeChar.getTries() > 1)
  156. {
  157. activeChar.setTries(activeChar.getTries() -1);
  158. //Random image file name
  159. int imgId = IdFactory.getInstance().getNextId();
  160. //Convertion from .png to .dds, and crest packed send
  161. try
  162. {
  163. File captcha = new File("data/captcha/captcha.png");
  164. ImageIO.write(generateCaptcha(), "png", captcha);
  165. PledgeCrest packet = new PledgeCrest(imgId, DDSConverter.convertToDDS(captcha).array()); //Convertion to DDS where is antybot
  166. activeChar.sendPacket(packet);
  167. }
  168. catch (Exception e)
  169. {
  170. _log.warning(e.getMessage());
  171. }
  172. //Paralyze, abnormal effect, invul, html with captcha output and start of the 1 min counter
  173. adminReply.setHtml("<html><title>Captcha Antibot System</title><body><center>Enter the 5-digits code below and click Confirm.<br><img src=\"Crest.crest_" + Config.SERVER_ID + "_" + imgId + "\" width=256 height=64><br><font color=\"888888\">(There are only english uppercase letters.)</font><br1><font color=\"FF0000\">Tries Left: " + activeChar.getTries() +"</font><br><edit var=\"antibot\" width=110><br><button value=\"Confirm\" action=\"bypass -h voice .antibot $antibot\" width=80 height=26 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"><br>If you close by mistake this window,<br1>you can re-open it by typing \".captcha\" on Chat.<br1>You have 3 minutes to answer or you<br1>will get jailed.<br1>You have 3 tries, if you will<br1>answer wrong to all of them you<br1>will get punished.</center></body></html>");
  174. activeChar.sendPacket(adminReply);
  175. activeChar.setCode(finalString);
  176. finalString.replace(0, 5, "");
  177. return false;
  178. }
  179. activeChar.setPunishLevel(L2PcInstance.PunishLevel.JAIL, 1);
  180. return false;
  181. }
  182. else
  183. {
  184. if (activeChar.getTries() > 1)
  185. {
  186. activeChar.setTries(activeChar.getTries() -1);
  187. //Random image file name
  188. int imgId = IdFactory.getInstance().getNextId();
  189. //Convertion from .png to .dds, and crest packed send
  190. try
  191. {
  192. File captcha = new File("data/captcha/captcha.png");
  193. ImageIO.write(generateCaptcha(), "png", captcha);
  194. PledgeCrest packet = new PledgeCrest(imgId, DDSConverter.convertToDDS(captcha).array()); //Convertion to DDS where is antybot
  195. activeChar.sendPacket(packet);
  196. }
  197. catch (Exception e)
  198. {
  199. _log.warning(e.getMessage());
  200. }
  201. //Paralyze, abnormal effect, invul, html with captcha output and start of the 1 min counter
  202. adminReply.setHtml("<html><title>Captcha Antibot System</title><body><center>Enter the 5-digits code below and click Confirm.<br><img src=\"Crest.crest_" + Config.SERVER_ID + "_" + imgId + "\" width=256 height=64><br><font color=\"888888\">(There are only english uppercase letters.)</font><br1><font color=\"FF0000\">Tries Left: " + activeChar.getTries() +"</font><br><edit var=\"antibot\" width=110><br><button value=\"Confirm\" action=\"bypass -h voice .antibot $antibot\" width=80 height=26 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"><br>If you close by mistake this window,<br1>you can re-open it by typing \".captcha\" on Chat.<br1>You have 3 minutes to answer or you<br1>will get jailed.<br1>You have 3 tries, if you will<br1>answer wrong to all of them you<br1>will get punished.</center></body></html>");
  203. activeChar.sendPacket(adminReply);
  204. activeChar.setCode(finalString);
  205. finalString.replace(0, 5, "");
  206. return false;
  207. }
  208. //here will run method with jailing player
  209. activeChar.stopAbnormalEffect(AbnormalEffect.REAL_TARGET);
  210. npcHtmlMessage.setHtml("<html><title>Captcha Antibot System</title><body><center><font color=\"FF0000\">You have wasted your Tries.<br><br></font><font color=\"66FF00\"><center></font><font color=\"FF0000\">You will be jailed.</font><br><button value=\"Exit\" action=\"bypass -h npc_%objectId%_Quest\" width=45 height=25 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></center></body></html>");
  211. if (activeChar.isFlyingMounted())
  212. activeChar.untransform();
  213. activeChar.setPunishLevel(L2PcInstance.PunishLevel.JAIL, 1);
  214. activeChar.setIsInvul(false);
  215. activeChar.setIsParalyzed(false);
  216. activeChar.sendPacket(npcHtmlMessage);
  217. return false;
  218. }
  219. }
  220. catch (Exception e)
  221. {
  222. activeChar.sendMessage("A problem occured while adding captcha!");
  223. _log.log(Level.WARNING, "", e);
  224. }
  225. }
  226. else
  227. {
  228. if (activeChar.getTries() > 1)
  229. {
  230. activeChar.setTries(activeChar.getTries() -1);
  231. //Random image file name
  232. int imgId = IdFactory.getInstance().getNextId();
  233. //Convertion from .png to .dds, and crest packed send
  234. try
  235. {
  236. File captcha = new File("data/captcha/captcha.png");
  237. ImageIO.write(generateCaptcha(), "png", captcha);
  238. PledgeCrest packet = new PledgeCrest(imgId, DDSConverter.convertToDDS(captcha).array()); //Convertion to DDS where is antybot
  239. activeChar.sendPacket(packet);
  240. }
  241. catch (Exception e)
  242. {
  243. _log.warning(e.getMessage());
  244. }
  245. //Paralyze, abnormal effect, invul, html with captcha output and start of the 1 min counter
  246. adminReply.setHtml("<html><title></title><body><center>Enter the 5-digits code below and click Confirm.<br><img src=\"Crest.crest_" + Config.SERVER_ID + "_" + imgId + "\" width=256 height=64><br><font color=\"888888\">(There are only english uppercase letters.)</font><br1><font color=\"FF0000\">Tries Left: " + activeChar.getTries() +"</font><br><edit var=\"antibot\" width=110><br><button value=\"Confirm\" action=\"bypass -h voice .antibot $antibot\" width=80 height=26 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"><br>If you close by mistake this window,<br1>you can re-open it by typing \".captcha\" on Chat.<br1>You have 3 minutes to answer or you<br1>will get jailed.<br1>You have 3 tries, if you will<br1>answer wrong to all of them you<br1>will get punished.</center></body></html>");
  247. activeChar.sendPacket(adminReply);
  248. activeChar.setCode(finalString);
  249. finalString.replace(0, 5, "");
  250. return false;
  251. }
  252. //here will run method with jailing player
  253. activeChar.stopAbnormalEffect(AbnormalEffect.REAL_TARGET);
  254. npcHtmlMessage.setHtml("<html><title>Captcha Antibot System</title><body><center><font color=\"FF0000\">You have wasted your Tries.<br><br></font><font color=\"66FF00\"><center></font><font color=\"FF0000\">You will be jailed.</font><br><button value=\"Exit\" action=\"bypass -h npc_%objectId%_Quest\" width=45 height=25 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></center></body></html>");
  255. if (activeChar.isFlyingMounted())
  256. activeChar.untransform();
  257. activeChar.setPunishLevel(L2PcInstance.PunishLevel.JAIL, 1);
  258. activeChar.setIsInvul(false);
  259. activeChar.setIsParalyzed(false);
  260. activeChar.sendPacket(npcHtmlMessage);
  261. return false;
  262. }
  263. return true;
  264. }
  265.  
  266. public String[] getVoicedCommandList()
  267. {
  268. return _voicedCommands;
  269. }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement