forked from TheHolyWaffle/TeamSpeak-3-Java-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrollExample.java
190 lines (162 loc) · 6.29 KB
/
TrollExample.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
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* #%L
* TeamSpeak 3 Java API
* %%
* Copyright (C) 2014 Bert De Geyter
* %%
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* #L%
*/
import com.github.theholywaffle.teamspeak3.TS3Api;
import com.github.theholywaffle.teamspeak3.TS3ApiAsync;
import com.github.theholywaffle.teamspeak3.TS3Config;
import com.github.theholywaffle.teamspeak3.TS3Query;
import com.github.theholywaffle.teamspeak3.api.CommandFuture.SuccessListener;
import com.github.theholywaffle.teamspeak3.api.event.ClientJoinEvent;
import com.github.theholywaffle.teamspeak3.api.event.ClientLeaveEvent;
import com.github.theholywaffle.teamspeak3.api.event.TS3EventAdapter;
import com.github.theholywaffle.teamspeak3.api.event.TS3EventType;
import com.github.theholywaffle.teamspeak3.api.wrapper.Channel;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* A more advanced example of a query which trolls clients on a server
* by moving them into other channels and sending them creepy messages.
* <p>
* Unlike the other examples, this class uses the asynchronous API
* in combination with a {@code ScheduledExecutorService}.
* </p>
*/
public class TrollExample {
private static final Random RANDOM = new Random();
// Troll-y messages because reason. They're not especially good, I'm sorry :(
private static final String[] MESSAGES = {
"Get off my server, damn kids!",
"Tell me a joke!",
"Hey Apple! Hey Apple! Apple! Hey Apple!",
"A virus has been detected",
"Move out of this channel or you will be banned in 15 seconds.",
"Say something :3",
"Boo!"
};
public static void main(String[] args) {
new TrollExample();
}
private final TS3Query query;
private final ScheduledExecutorService executor;
private final HashMap<Integer, ClientTrollWorker> workers;
public TrollExample() {
// Start the query
final TS3Config config = new TS3Config();
config.setHost("77.77.77.77");
config.setEnableCommunicationsLogging(true);
query = new TS3Query(config);
query.connect();
// And let's use one separate thread to troll everyone
executor = new ScheduledThreadPoolExecutor(1);
workers = new HashMap<>();
// Log in, select the virtual server and set a nickname
TS3Api api = query.getApi();
api.login("serveradmin", "serveradminpassword");
api.selectVirtualServerById(1);
api.setNickname("Global Server Admin");
api.registerEvent(TS3EventType.SERVER);
api.addTS3Listeners(new TS3EventAdapter() {
@Override
public void onClientJoin(ClientJoinEvent e) {
final int clientId = e.getClientId();
final ClientTrollWorker worker = new ClientTrollWorker(clientId);
workers.put(clientId, worker);
// Let's wait a short moment until we begin trolling - 10 to 20 seconds
long timeout = 10 + RANDOM.nextInt(11);
executor.schedule(worker, timeout, TimeUnit.SECONDS);
}
@Override
public void onClientLeave(ClientLeaveEvent e) {
final ClientTrollWorker worker = workers.remove(e.getClientId());
if (worker == null) return;
worker.shutdown();
}
});
}
private class ClientTrollWorker implements Runnable {
private final int clientId;
private boolean cancelled;
private ClientTrollWorker(int clientId) {
this.clientId = clientId;
cancelled = false;
}
@Override
public void run() {
// We were cancelled, abandon ship!
if (cancelled) return;
// Important!
// We're using the asynchronous API so we don't block
// our executor which has only 1 thread
final TS3ApiAsync asyncApi = query.getAsyncApi();
// Let's decide what to do
int choice = RANDOM.nextInt(4); // 0 .. 3
switch (choice) {
case 0:
// Poke the client with a random message
String pokeMessage = MESSAGES[RANDOM.nextInt(MESSAGES.length)];
asyncApi.pokeClient(clientId, pokeMessage);
break;
case 1:
// Send the client a private message
String privateMessage = MESSAGES[RANDOM.nextInt(MESSAGES.length)];
asyncApi.sendPrivateMessage(clientId, privateMessage);
break;
case 2:
// Kick the client to the default channel
asyncApi.kickClientFromChannel(clientId);
break;
case 3:
// Move client into a different channel
// First, get all channels
// (Using .getChannels every time works on small servers, but might
// not be a good idea on servers with 50+ clients! )
asyncApi.getChannels().onSuccess(new SuccessListener<List<Channel>>() {
@Override
public void handleSuccess(List<Channel> result) {
// Once we retrieved the channels (some time in the future), choose one channel
int randomChannelIndex = RANDOM.nextInt(result.size());
Channel randomChannel = result.get(randomChannelIndex);
// Then move the client into that channel
asyncApi.moveClient(clientId, randomChannel.getId());
}
});
break;
default:
// java.util.Random is broken
throw new IllegalStateException("java.util.Random returned an illegal value.");
}
// And finally, re-schedule the runnable
long timeout = RANDOM.nextInt(30) + 30; // Wait 30 to 60 seconds
executor.schedule(this, timeout, TimeUnit.SECONDS);
}
private void shutdown() {
cancelled = true;
}
}
}