forked from konatakun/powerbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInput.java
177 lines (142 loc) · 4.51 KB
/
Input.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package org.powerbot.script;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.powerbot.bot.MouseSpline;
/**
* Input
* A utility class for generating input to the canvas and retrieving information from the canvas.
*/
public abstract class Input {
protected final AtomicBoolean blocking;
private final MouseSpline spline;
private final AtomicInteger speed;
protected Input() {
blocking = new AtomicBoolean(false);
spline = new MouseSpline();
speed = new AtomicInteger(100);
}
/**
* Set the relative speed for mouse movements.
* This is a sensitive function and should be used in exceptional circumstances for a short period of time only.
*
* @param s the new speed as a percentage, i.e. {@code 10} is 10x faster, {@code 25} is 4x as fast
* and {@code 100} is the full speed. Specifying {@code 0} will not change the speed but return the
* current value instead.
* @return the speed, which can be different to the value requested
*/
public int speed(final int s) {
speed.set(Math.min(100, Math.max(10, s)));
return speed.get();
}
// TODO: remove boolean return values for input methods
public final boolean blocking() {
return blocking.get();
}
public void blocking(final boolean b) {
blocking.set(b);
}
public abstract void focus();
public abstract void defocus();
public abstract boolean send(final String s);
public final boolean sendln(final String s) {
return send(s + "\n");
}
public abstract Point getLocation();
public abstract Point getPressLocation();
public abstract long getPressWhen();
public abstract boolean press(final int button);
public abstract boolean release(final int button);
protected abstract boolean setLocation(final Point p);
public final boolean click(final int x, final int y, final int button) {
return click(new Point(x, y), button);
}
public final boolean click(final int x, final int y, final boolean left) {
return click(new Point(x, y), left);
}
public final boolean click(final Point point, final int button) {
return move(point) && click(button);
}
public final boolean click(final Point point, final boolean left) {
return move(point) && click(left);
}
public final boolean click(final boolean left) {
return click(left ? MouseEvent.BUTTON1 : MouseEvent.BUTTON3);
}
public final boolean click(final int button) {
press(button);
Condition.sleep(spline.getPressDuration());
release(button);
Condition.sleep(spline.getPressDuration());
return true;
}
public final boolean drag(final Point p, final boolean left) {
return drag(p, left ? MouseEvent.BUTTON1 : MouseEvent.BUTTON3);
}
public final boolean drag(final Point p, final int button) {
press(button);
final boolean b = move(p);
release(button);
return b;
}
public final boolean hop(final Point p) {
return setLocation(p);
}
public final boolean hop(final int x, final int y) {
return hop(new Point(x, y));
}
public final boolean move(final int x, final int y) {
return move(new Point(x, y));
}
public final boolean move(final Point p) {
return apply(
new Targetable() {
@Override
public Point nextPoint() {
return p;
}
@Override
public boolean contains(final Point point) {
return p.equals(point);
}
},
new Filter<Point>() {
@Override
public boolean accept(final Point point) {
return p.equals(point);
}
}
);
}
public final boolean apply(final Targetable targetable, final Filter<Point> filter) {
final Point target_point = new Point(-1, -1);
final int STANDARD_ATTEMPTS = 3;
for (int i = 0; i < STANDARD_ATTEMPTS; i++) {
final Point mp = getLocation();
final Vector3 start = new Vector3(mp.x, mp.y, 255);
final Point p = targetable.nextPoint();
if (p.x == -1 || p.y == -1) {
continue;
}
target_point.move(p.x, p.y);
final Vector3 end = new Vector3(p.x, p.y, 0);
final Iterable<Vector3> spline = this.spline.getPath(start, end);
for (final Vector3 v : spline) {
hop(v.x, v.y);
Condition.sleep((int) (this.spline.getAbsoluteDelay(v.z) * (speed.get() / 100d) / 1.33e6));
}
final Point p2 = getLocation(), ep = end.toPoint2D();
if (p2.equals(ep) && filter.accept(ep)) {
return true;
}
}
return false;
}
public final boolean scroll() {
return scroll(true);
}
public abstract boolean scroll(final boolean down);
public abstract Dimension getComponentSize();
}