forked from konatakun/powerbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientContext.java
162 lines (148 loc) · 4.17 KB
/
ClientContext.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
package org.powerbot.script;
import java.util.Collection;
import java.util.EventListener;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
import org.powerbot.bot.AbstractBot;
import org.powerbot.bot.InputSimulator;
import org.powerbot.bot.ScriptController;
import org.powerbot.bot.ScriptEventDispatcher;
/**
* ClientContext
* A context class which interlinks all core classes for a {@link org.powerbot.script.Bot}.
*
* @param <C> the bot client
*/
public abstract class ClientContext<C extends Client> {
public static final RuntimePermission INTERNAL_API_ACCESS = new RuntimePermission("checkInternalApiAccess");
/**
* The script controller.
*/
public final Script.Controller controller;
/**
* A table of key/value pairs representing environmental properties.
*/
public final Properties properties;
/**
* A collection representing the event listeners attached to the {@link org.powerbot.script.Bot}.
*/
public final Collection<EventListener> dispatcher;
/**
* The input simulator for sending keyboard and mouse events.
*/
public final Input input;
private final AtomicReference<Bot<? extends ClientContext<C>>> bot;
private final AtomicReference<C> client;
/**
* Creates a new context with the given {@link org.powerbot.script.Bot}.
*
* @param bot the bot
*/
protected ClientContext(final Bot<? extends ClientContext<C>> bot) {
this.bot = new AtomicReference<Bot<? extends ClientContext<C>>>(bot);
client = new AtomicReference<C>(null);
@SuppressWarnings("unchecked")
final ScriptController c = new ScriptController(this);
controller = c;
properties = new Properties();
dispatcher = new ScriptEventDispatcher<C, EventListener>(this);
input = new InputSimulator((AbstractBot) bot);
}
/**
* Creates a chained context.
*
* @param ctx the parent context
*/
protected ClientContext(final ClientContext<C> ctx) {
bot = ctx.bot;
client = ctx.client;
controller = ctx.controller;
properties = ctx.properties;
dispatcher = ctx.dispatcher;
input = ctx.input;
}
/**
* Returns the client version.
*
* @return the client version, which is {@code 6} for {@code rt6} and {@code} 4 for {@code rt4}
*/
public final String rtv() {
final Class<?> c = getClass();
if (org.powerbot.script.rt6.ClientContext.class.isAssignableFrom(c)) {
return "6";
}
if (org.powerbot.script.rt4.ClientContext.class.isAssignableFrom(c)) {
return "4";
}
return "";
}
/**
* Returns the bot.
*
* @return the bot
*/
public final Bot<? extends ClientContext<C>> bot() {
return bot.get();
}
/**
* Returns the client.
*
* @return the client.
*/
public final C client() {
return client.get();
}
/**
* Sets the client.
*
* @param c the new client
* @return the previous value, which may be {@code null}
*/
public final C client(final C c) {
return client.getAndSet(c);
}
/**
* Returns the script controller.
*
* @return the script controller
* @deprecated use {@link #controller}
*/
@Deprecated
public final Script.Controller controller() {
return controller;
}
/**
* Returns the primary script.
*
* @param <T> the type of script
* @return the primary script, or {@code null} if one is not attached
* @deprecated use {@link org.powerbot.script.Script.Controller#script()}
*/
@SuppressWarnings("unchecked")
public final <T extends AbstractScript<? extends ClientContext<C>>> T script() {
return (T) controller.script();
}
/**
* Returns the property value for the specified key, or an empty string as the default value.
*
* @param k the key to lookup
* @return the value for the specified key, otherwise an empty string if the requested entry does not exist
* @see #properties
*/
@Deprecated
public final String property(final String k) {
return property(k, "");
}
/**
* Returns the property value for the specified key, or a default value.
*
* @param k the key to lookup
* @param d the default value
* @return the value for the specified key, otherwise the default value if the requested entry does not exist
* @see #properties
*/
@Deprecated
public final String property(final String k, final String d) {
return properties.getProperty(k, d);
}
}