-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDreamDeathswap.java
405 lines (343 loc) · 17.4 KB
/
DreamDeathswap.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package me.prostedeni.goodcraft.dreamdeathswap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerPortalEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.StringUtil;
import static org.bukkit.ChatColor.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
public final class DreamDeathswap extends JavaPlugin implements Listener {
HashMap<Player, Player> playerMap = new HashMap<>();
public static int Delay;
public static int WarnTime;
public static int SwapChance;
public static boolean AlternativeMode;
public static boolean Nether;
public static boolean Debug;
@Override
public void onEnable() {
Bukkit.getPluginManager().registerEvents(this,this);
getConfig().options().copyDefaults();
saveDefaultConfig();
fetchConfig();
}
@Override
public void onDisable() {
reloadConfig();
saveConfig();
}
@EventHandler (priority = EventPriority.LOW)
public void onNether(PlayerPortalEvent p){
if (playerMap.containsKey(p.getPlayer()) || playerMap.containsValue(p.getPlayer())) {
if (!Nether) {
p.getPlayer().sendMessage(DARK_RED + "Nether has been disabled in the config");
p.setCancelled(true);
}
}
}
//this forbids players from entering Nether if it's disallowed
@EventHandler (priority = EventPriority.HIGH)
public void onDeath(PlayerDeathEvent d){
Player died = d.getEntity();
if (playerMap.containsKey(died)){
playerMap.get(died).sendMessage(DARK_GREEN + "You have won the deathswap!");
died.sendMessage(DARK_RED + "You have lost the deathswap!");
playerMap.remove(died);
}
if (playerMap.containsValue(died)){
Player key = getKey(died);
key.sendMessage(DARK_GREEN + "You have won the deathswap!");
died.sendMessage(DARK_RED + "You have lost the deathswap!");
playerMap.remove(key);
}
}
//declares winner and ends deathswap
private static int getRandomNumberInRange(int min, int max) {
if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
//just a method i found on the internet for getting random numbers
public Player getKey(Player player){
try {
for (Player p : Bukkit.getOnlinePlayers()) {
if (playerMap.get(p).equals(player)) {
return p;
}
}
} catch (NullPointerException e) {
//just don't display it
if (Debug) {
System.out.println("NullPointerException in getKey method");
}
}
return player;
}
/*
this method gets key from value of HashMap, however, this is not very optimized
because it loops through every online player and asks if he is the key
Optimally, there would be two HashMaps, Key to Value and Value to Key
but that also uses up twice the RAM
additionally, this will break if there are more keys holding the same value
*/
public void fetchConfig(){
Delay = getConfig().getInt("Delay");
WarnTime = getConfig().getInt("WarnTime");
SwapChance = getConfig().getInt("SwapChance");
AlternativeMode = getConfig().getBoolean("AlternativeMode");
Nether = getConfig().getBoolean("Nether");
Debug = getConfig().getBoolean("Debug");
}
//this just gets config on startup or on reload, because accessing static variables
//is less resource taxing than getting the value from physical file every time
public void startRunnable(Player p1, Player p2){
new BukkitRunnable(){
Player player1 = p1;
Player player2 = p2;
int Time = 0;
@Override
public void run() {
Time++;
if (!(playerMap.containsKey(player1)) || !(playerMap.containsValue(player2)) || !(player1.isOnline()) || !(player2.isOnline())) {
if (!player1.isOnline()){
player2.sendMessage(DARK_AQUA + "Other player in deathswap left the game");
if (playerMap.containsKey(player1)){
playerMap.remove(player1);
cancel();
} else if (playerMap.containsValue(player1)){
Player key = getKey(player1);
playerMap.remove(key);
cancel();
} else {
cancel();
}
} else if (!player2.isOnline()){
player1.sendMessage(DARK_AQUA + "Other player in deathswap left the game");
if (playerMap.containsKey(player2)){
playerMap.remove(player2);
cancel();
} else if (playerMap.containsValue(player2)){
Player key = getKey(player2);
playerMap.remove(key);
cancel();
} else {
cancel();
}
} else {
cancel();
}
}
//cancels the runnable if one specified players isn't in HashMap or disconnected
if (Debug) {
System.out.println("Time: " + Time);
}
if (!isCancelled()) {
if ((Delay - Time) <= WarnTime) {
player1.sendMessage(translateAlternateColorCodes('&', "&cSwapping in " + (Delay - Time) + "&c seconds"));
player2.sendMessage(translateAlternateColorCodes('&', "&cSwapping in " + (Delay - Time) + "&c seconds"));
if (Delay - Time < 1) {
player1.playSound(player1.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 4F, 4F);
player2.playSound(player1.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 4F, 4F);
} else {
player1.playSound(player1.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 2F, 1F);
player2.playSound(player1.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 2F, 1F);
}
}
if (Delay - Time <= 0) {
Location loc1 = player1.getLocation();
Location loc2 = player2.getLocation();
//saves locations before swapping
int rando = getRandomNumberInRange(0, 100);
//calls to method of generating pseudorandom numbers above
if (AlternativeMode) {
//if AlternativeMode == true, it calculates with chances
if (rando <= SwapChance) {
teleport(player1, player2, loc1, loc2);
player1.sendMessage(GREEN + "You have been swapped");
player2.sendMessage(GREEN + "You have been swapped");
if (Debug) {
System.out.print("loc1: " + loc1 + " loc2: " + loc2 + " rando: " + rando + " SwapChance: " + SwapChance);
}
} else {
player1.sendMessage(GREEN + "Swapping did not occur");
player2.sendMessage(GREEN + "Swapping did not occur");
if (Debug) {
System.out.print("loc1: " + loc1 + " loc2: " + loc2 + " rando: " + rando + " SwapChance: " + SwapChance);
}
}
} else {
teleport(player1, player2, loc1, loc2);
player1.sendMessage(GREEN + "You have been swapped");
player2.sendMessage(GREEN + "You have been swapped");
if (Debug) {
System.out.print("loc1: " + loc1 + " loc2: " + loc2);
}
}
Time = 0;
}
}
}
}.runTaskTimerAsynchronously(this, 0, 20);
}
//This whole runnable runs asynchronously, minimizing workload of main thread
//but teleporting can't be done asynchronously, so i used next method to achieve both
public void teleport(Player player1, Player player2, Location loc1, Location loc2){
new BukkitRunnable(){
@Override
public void run(){
player1.teleport(loc2);
player2.teleport(loc1);
}
}.runTaskLater(this, 1);
}
//piece of code i couldn't put into previous method,
//and had to make sure it runs synchronously
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("deathswap")) {
if ((Delay > 0) && (WarnTime > 0) && (SwapChance > 0) && (SwapChance <= 100)) {
if (args.length == 0) {
sender.sendMessage(DARK_RED + "Invalid arguments");
return false;
} else if (args.length == 1){
switch (args[0]) {
//using switch statements instead of ifs and else ifs is faster
case "reload" : {
if (sender instanceof Player) {
if (sender.hasPermission("deathswap.use")) {
reloadConfig();
getConfig();
saveConfig();
fetchConfig();
sender.sendMessage(DARK_AQUA + "Config reloaded");
break;
} else {
sender.sendMessage(ChatColor.DARK_RED + "You do not have permission to execute this command");
break;
}
} else {
reloadConfig();
getConfig();
saveConfig();
fetchConfig();
sender.sendMessage(DARK_AQUA + "Config reloaded");
break;
}
}
case "stop" : {
if (sender instanceof Player){
if (sender.hasPermission("deathswap.use")) {
if (playerMap.containsValue(sender)){
getKey(((Player) sender).getPlayer()).sendMessage(DARK_PURPLE + "Deathswap has been stopped");
sender.sendMessage(DARK_PURPLE + "Deathswap has been stopped");
playerMap.remove(getKey(((Player) sender).getPlayer()));
break;
} else if (playerMap.containsKey(sender)){
playerMap.get(sender).sendMessage(DARK_PURPLE + "Deathswap has been stopped");
sender.sendMessage(DARK_PURPLE + "Deathswap has been stopped");
playerMap.remove(sender);
break;
} else {
sender.sendMessage(DARK_RED + "You are not in deathswap");
break;
}
} else {
sender.sendMessage(ChatColor.DARK_RED + "You do not have permission to execute this command");
break;
}
} else {
sender.sendMessage(DARK_RED + "Only players can send this command");
break;
}
}
default: {
if (sender instanceof Player) {
if (sender.hasPermission("deathswap.use")) {
if (Bukkit.getOnlinePlayers().contains(Bukkit.getPlayer(args[0]))) {
if (!(Bukkit.getPlayer(args[0]).equals(sender))) {
if (!(playerMap.containsKey(sender)) && !(playerMap.containsValue(sender))) {
if (!(playerMap.containsKey(Bukkit.getPlayer(args[0]))) && !(playerMap.containsValue(Bukkit.getPlayer(args[0])))) {
playerMap.put(((Player) sender).getPlayer(), Bukkit.getPlayer(args[0]));
startRunnable(((Player) sender).getPlayer(), Bukkit.getPlayer(args[0]));
sender.sendMessage(DARK_GREEN + "You have been put in deathswap");
Bukkit.getPlayer(args[0]).sendMessage(DARK_GREEN + "You have been put in deathswap");
break;
} else {
sender.sendMessage(DARK_RED + "Specified player already is in deathswap");
break;
}
} else {
sender.sendMessage(DARK_RED + "You already are in deathswap");
break;
}
} else {
sender.sendMessage(DARK_RED + "You can't swap with yourself");
break;
}
} else {
sender.sendMessage(DARK_RED + "Player " + args[0] + " isn't online");
break;
}
} else {
sender.sendMessage(ChatColor.DARK_RED + "You do not have permission to execute this command");
break;
}
} else {
sender.sendMessage(DARK_RED + "Only players can send this command");
break;
}
}
}
} else {
sender.sendMessage(DARK_RED + "Too many arguments");
}
} else {
sender.sendMessage(DARK_RED + "Config values must be above 0 and SwapChance below or equal to 100");
}
}
return false;
}
//this is just the command, nothing special here
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String commandLabel, String[] args) {
if (command.getName().equalsIgnoreCase("deathswap")) {
if (args.length == 1) {
if (sender.hasPermission("deathswap.use")) {
final ArrayList<String> l = new ArrayList<>();
final ArrayList<String> commands = new ArrayList<>();
commands.add("reload");
commands.add("stop");
for (Player p : Bukkit.getOnlinePlayers()){
if (!(playerMap.containsKey(p)) && !(playerMap.containsValue(p))) {
if (!(p.equals(sender))) {
if (!(playerMap.containsKey((Player) sender)) && !(playerMap.containsValue((Player) sender))) {
commands.add(p.getName());
}
}
}
}
StringUtil.copyPartialMatches(args[0], commands, l);
return l;
}
}
}
return null;
}
//TabCompleter, nothing special
}