Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. package org.rsbot.script;
  2.  
  3. import org.rsbot.event.listeners.PaintListener;
  4. import org.rsbot.script.methods.MethodContext;
  5. import org.rsbot.script.methods.Methods;
  6.  
  7. import java.awt.*;
  8.  
  9. public abstract class Random extends Methods implements PaintListener {
  10.  
  11.     protected String name;
  12.  
  13.     private volatile boolean enabled = true;
  14.  
  15.     private Script script;
  16.  
  17.     private int timeout = random(240, 300);
  18.  
  19.     /**
  20.      * Detects whether or not this anti-random should
  21.      * activate.
  22.      *
  23.      * @return <tt>true</tt> if the current script
  24.      * should be paused and control passed to this
  25.      * anti-random's loop.
  26.      */
  27.     public abstract boolean activateCondition();
  28.  
  29.     public abstract int loop();
  30.    
  31.    
  32.     /**
  33.      * Called after the method providers for this Random
  34.      * become available for use in initialization.
  35.      */
  36.     public void onStart() {
  37.        
  38.     }
  39.  
  40.     public void onFinish() {
  41.  
  42.     }
  43.  
  44.     /**
  45.      * Override to provide a time limit in seconds for
  46.      * this anti-random to complete.
  47.      *
  48.      * @return The number of seconds after activateCondition
  49.      *         returns <tt>true</tt> before the anti-random should be
  50.      *         detected as having failed. If this time is reached
  51.      *         the random and running script will be stopped.
  52.      */
  53.     public long getTimeout() { /*int before fix*/
  54.         return timeout;
  55.     }
  56.    
  57.     @Override
  58.     public final void init(MethodContext ctx) {
  59.         super.init(ctx);
  60.         onStart();
  61.     }
  62.  
  63.     public final boolean isActive() {
  64.         return script != null;
  65.     }
  66.    
  67.     public final boolean isEnabled() {
  68.         return enabled;
  69.     }
  70.  
  71.     public final void setEnabled(boolean enabled) {
  72.         this.enabled = enabled;
  73.     }
  74.  
  75.     /**
  76.      * Stops the current script; player can be logged out before
  77.      * the script is stopped.
  78.      *
  79.      * @param logout <tt>true</tt> if the player should be logged
  80.      * out before the script is stopped.
  81.      */
  82.     protected void stopScript(boolean logout) {
  83.         script.stopScript(logout);
  84.     }
  85.  
  86.     public final boolean run(Script ctx) {
  87.         if (!activateCondition()) {
  88.             return false;
  89.         }
  90.         script = ctx;
  91.         name = getClass().getAnnotation(ScriptManifest.class).name();
  92.         ctx.ctx.bot.getEventManager().removeListener(ctx);
  93.         for (Script s : ctx.delegates) {
  94.             ctx.ctx.bot.getEventManager().removeListener(s);
  95.         }
  96.         ctx.ctx.bot.getEventManager().addListener(this);
  97.         log("Random event started: " + name);
  98.         long timeout = getTimeout(); /*int before fix*/
  99.         if (timeout > 0) {
  100.             timeout *= 1000;
  101.             timeout += System.currentTimeMillis();
  102.         }
  103.         while (ctx.isActive()) {
  104.             try {
  105.                 int wait = loop();
  106.                 if (wait == -1) {
  107.                     break;
  108.                 } else if (timeout > 0 && System.currentTimeMillis() >= timeout) {
  109.                     log.warning("Time limit reached for " + name + ".");
  110.                     ctx.stopScript();
  111.                 } else {
  112.                     sleep(wait);
  113.                 }
  114.             } catch (Exception ex) {
  115.                 log.severe(ex.toString());
  116.                 break;
  117.             }
  118.         }
  119.         script = null;
  120.         onFinish();
  121.         log("Random event finished: " + name);
  122.         ctx.ctx.bot.getEventManager().removeListener(this);
  123.         sleep(1000);
  124.         ctx.ctx.bot.getEventManager().addListener(ctx);
  125.         for (Script s : ctx.delegates) {
  126.             ctx.ctx.bot.getEventManager().addListener(s);
  127.         }
  128.         return true;
  129.     }
  130.  
  131.     public final void onRepaint(Graphics g) {
  132.         Point p = mouse.getLocation();
  133.         int w = game.getWidth(), h = game.getHeight();
  134.         g.setColor(new Color(0, 0, 0, 100));
  135.         g.fillRect(0, 0, p.x - 1, p.y - 1);
  136.         g.fillRect(p.x + 1, 0, w - (p.x + 1), p.y - 1);
  137.         g.fillRect(0, p.y + 1, p.x - 1, h - (p.y - 1));
  138.         g.fillRect(p.x + 1, p.y + 1, w - (p.x + 1), h - (p.y - 1));
  139.     }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement