/
dPlayer.java
548 lines (419 loc) · 20.6 KB
/
dPlayer.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
package net.aufdemrand.denizen.objects;
import net.aufdemrand.denizen.flags.FlagManager;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.tags.core.PlayerTags;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
public class dPlayer implements dObject {
/////////////////////
// STATIC METHODS
/////////////////
static Map<String, dPlayer> players = new HashMap<String, dPlayer>();
public static dPlayer mirrorBukkitPlayer(Player player) {
if (player == null) return null;
if (players.containsKey(player.getName())) return players.get(player.getName());
else return new dPlayer(player);
}
/////////////////////
// OBJECT FETCHER
/////////////////
@ObjectFetcher("p")
public static dPlayer valueOf(String string) {
if (string == null) return null;
string = string.replace("p@", "");
////////
// Match player name
OfflinePlayer returnable = null;
for (OfflinePlayer player : Bukkit.getOfflinePlayers())
if (player.getName().equalsIgnoreCase(string)) {
returnable = player;
break;
}
if (returnable != null) {
if (players.containsKey(returnable.getName())) return players.get(returnable.getName());
else return new dPlayer(returnable);
}
else dB.echoError("Invalid Player! '" + string
+ "' could not be found. Has the player logged off?");
return null;
}
public static boolean matches(String arg) {
arg = arg.replace("p@", "");
OfflinePlayer returnable = null;
for (OfflinePlayer player : Bukkit.getOfflinePlayers())
if (player.getName().equalsIgnoreCase(arg)) {
returnable = player;
break;
}
if (returnable != null) return true;
return false;
}
/////////////////////
// STATIC CONSTRUCTORS
/////////////////
public dPlayer(OfflinePlayer player) {
if (player == null) return;
this.player_name = player.getName();
// Keep in a map to avoid multiple instances of a dPlayer per player.
players.put(this.player_name, this);
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
String player_name = null;
public boolean isValid() {
if (player_name == null) return false;
if (getPlayerEntity() == null && getOfflinePlayer() == null) return false;
return true;
}
public Player getPlayerEntity() {
if (player_name == null) return null;
return Bukkit.getPlayer(player_name);
}
public OfflinePlayer getOfflinePlayer() {
if (player_name == null) return null;
return Bukkit.getOfflinePlayer(player_name);
}
public String getName() {
return player_name;
}
public dLocation getLocation() {
if (isOnline()) return new dLocation(getPlayerEntity().getLocation());
else return null;
}
public dLocation getEyeLocation() {
if (isOnline()) return new dLocation(getPlayerEntity().getEyeLocation());
else return null;
}
public World getWorld() {
if (isOnline()) return getPlayerEntity().getWorld();
else return null;
}
public boolean isOnline() {
if (player_name == null) return false;
if (Bukkit.getPlayer(player_name) != null) return true;
return false;
}
/////////////////////
// dObject Methods
/////////////////
private String prefix = "Player";
@Override
public String getPrefix() {
return prefix;
}
@Override
public dPlayer setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String debug() {
return (prefix + "='<A>" + identify() + "<G>' ");
}
@Override
public boolean isUnique() {
return true;
}
@Override
public String getType() {
return "Player";
}
@Override
public String identify() {
return "p@" + player_name;
}
@Override
public String toString() {
return identify();
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return "null";
if (player_name == null) return "null";
if (attribute.startsWith("entity"))
return new dEntity(getPlayerEntity())
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("has_played_before"))
return new Element(String.valueOf(getOfflinePlayer().hasPlayedBefore()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("is_op"))
return new Element(String.valueOf(getOfflinePlayer().isOp()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("first_played"))
return new Element(String.valueOf(getOfflinePlayer().getFirstPlayed()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("last_played"))
return new Element(String.valueOf(getOfflinePlayer().getLastPlayed()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("is_banned"))
return new Element(String.valueOf(getOfflinePlayer().isBanned()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("is_whitelisted"))
return new Element(String.valueOf(getOfflinePlayer().isWhitelisted()))
.getAttribute(attribute.fulfill(1));
// This can be parsed later with more detail if the player is online, so only check for offline.
if (attribute.startsWith("name") && !isOnline())
return new Element(player_name).getAttribute(attribute.fulfill(1));
if (attribute.startsWith("is_online"))
return new Element(String.valueOf(isOnline())).getAttribute(attribute.fulfill(1));
if (attribute.startsWith("list")) {
List<String> players = new ArrayList<String>();
if (attribute.startsWith("list.online")) {
for(Player player : Bukkit.getOnlinePlayers())
players.add(player.getName());
return new dList(players).getAttribute(attribute.fulfill(2));
}
else if (attribute.startsWith("list.offline")) {
for(OfflinePlayer player : Bukkit.getOfflinePlayers()) {
if (!Bukkit.getOnlinePlayers().toString().contains(player.getName()))
players.add(player.getName());
}
return new dList(players).getAttribute(attribute.fulfill(2));
}
else {
for(OfflinePlayer player : Bukkit.getOfflinePlayers())
players.add(player.getName());
return new dList(players).getAttribute(attribute.fulfill(1));
}
}
if (attribute.startsWith("chat_history_list"))
return new dList(PlayerTags.playerChatHistory.get(player_name))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("chat_history")) {
int x = 1;
if (attribute.hasContext(1) && aH.matchesInteger(attribute.getContext(1)))
x = attribute.getIntContext(1);
return new Element(PlayerTags.playerChatHistory.get(player_name).get(x - 1))
.getAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("bed_spawn"))
return new dLocation(getOfflinePlayer().getBedSpawnLocation())
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("money")) {
if(Depends.economy != null) {
if (attribute.startsWith("money.currency_singular"))
return new Element(Depends.economy.currencyNameSingular())
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("money.currency"))
return new Element(Depends.economy.currencyNamePlural())
.getAttribute(attribute.fulfill(2));
return new Element(String.valueOf(Depends.economy.getBalance(player_name)))
.getAttribute(attribute.fulfill(1));
} else {
dB.echoError("No economy loaded! Have you installed Vault and a compatible economy plugin?");
return null;
}
}
if (!isOnline()) return new Element(identify()).getAttribute(attribute);
// Player is required to be online after this point...
if (attribute.startsWith("xp.to_next_level"))
return new Element(String.valueOf(getPlayerEntity().getExpToLevel()))
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("xp.total"))
return new Element(String.valueOf(getPlayerEntity().getTotalExperience()))
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("xp.level"))
return new Element(getPlayerEntity().getLevel())
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("xp"))
return new Element(String.valueOf(getPlayerEntity().getExp() * 100))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("equipment.boots"))
if (getPlayerEntity().getInventory().getBoots() != null)
return new dItem(getPlayerEntity().getInventory().getBoots())
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("equipment.chestplate"))
if (getPlayerEntity().getInventory().getChestplate() != null)
return new dItem(getPlayerEntity().getInventory().getChestplate())
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("equipment.helmet"))
if (getPlayerEntity().getInventory().getHelmet() != null)
return new dItem(getPlayerEntity().getInventory().getHelmet())
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("equipment.leggings"))
if (getPlayerEntity().getInventory().getLeggings() != null)
return new dItem(getPlayerEntity().getInventory().getLeggings())
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("equipment"))
// The only way to return correct size for dInventory
// created from equipment is to use a CRAFTING type
// that has the expected 4 slots
return new dInventory(InventoryType.CRAFTING).add(getPlayerEntity().getInventory().getArmorContents())
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("inventory"))
return new dInventory(getPlayerEntity().getInventory())
.getAttribute(attribute.fulfill(1));
// Estimate the location of the item the player is holding
if (attribute.startsWith("item_in_hand.location")) {
dLocation location = new dLocation(getPlayerEntity().getLocation());
//location.setYaw((float) Rotation.normalizeYaw(Rotation.getYaw(location.getDirection())));
location.setYaw(location.getYaw() + 30);
//location.add(location.getDirection().multiply(1.2));
getPlayerEntity().teleport(location);
return location.getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("item_in_hand"))
return new dItem(getPlayerEntity().getItemInHand())
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("name.display"))
return new Element(getPlayerEntity().getDisplayName())
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("name.list"))
return new Element(getPlayerEntity().getPlayerListName())
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("name"))
return new Element(player_name).getAttribute(attribute.fulfill(1));
if (attribute.startsWith("eyes"))
return new dLocation(getEyeLocation())
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("compass_target"))
return new dLocation(getPlayerEntity().getCompassTarget())
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("food_level.formatted")) {
double maxHunger = getPlayerEntity().getMaxHealth();
if (attribute.hasContext(2))
maxHunger = attribute.getIntContext(2);
if ((float) getPlayerEntity().getFoodLevel() / maxHunger < .10)
return new Element("starving").getAttribute(attribute.fulfill(2));
else if ((float) getPlayerEntity().getFoodLevel() / maxHunger < .40)
return new Element("famished").getAttribute(attribute.fulfill(2));
else if ((float) getPlayerEntity().getFoodLevel() / maxHunger < .75)
return new Element("parched").getAttribute(attribute.fulfill(2));
else if ((float) getPlayerEntity().getFoodLevel() / maxHunger < 1)
return new Element("hungry").getAttribute(attribute.fulfill(2));
else return new Element("healthy").getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("food_level"))
return new Element(String.valueOf(getPlayerEntity().getFoodLevel()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("permission")
|| attribute.startsWith("has_permission")) {
if (Depends.permissions == null) {
dB.echoError("No permission system loaded! Have you installed Vault and a compatible permissions plugin?");
return null;
}
String permission = attribute.getContext(1);
// Non-world specific permission
if (attribute.getAttribute(2).startsWith("global"))
return new Element(String.valueOf(Depends.permissions.has((World) null, player_name, permission)))
.getAttribute(attribute.fulfill(2));
// Permission in certain world
else if (attribute.getAttribute(2).startsWith("world"))
return new Element(String.valueOf(Depends.permissions.has(attribute.getContext(2), player_name, permission)))
.getAttribute(attribute.fulfill(2));
// Permission in current world
return new Element(String.valueOf(Depends.permissions.has(getPlayerEntity(), permission)))
.getAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("flag")) {
String flag_name;
if (attribute.hasContext(1)) flag_name = attribute.getContext(1);
else return "null";
attribute.fulfill(1);
if (attribute.startsWith("is_expired")
|| attribute.startsWith("isexpired"))
return new Element(!FlagManager.playerHasFlag(this, flag_name))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("size") && !FlagManager.playerHasFlag(this, flag_name))
return new Element(0).getAttribute(attribute.fulfill(1));
if (FlagManager.playerHasFlag(this, flag_name))
return new dList(DenizenAPI.getCurrentInstance().flagManager()
.getPlayerFlag(getName(), flag_name))
.getAttribute(attribute);
else return "null";
}
if (attribute.startsWith("group")
|| attribute.startsWith("in_group")) {
if (Depends.permissions == null) {
dB.echoError("No permission system loaded! Have you installed Vault and a compatible permissions plugin?");
return "null";
}
String group = attribute.getContext(1);
// Non-world specific permission
if (attribute.getAttribute(2).startsWith("global"))
return new Element(String.valueOf(Depends.permissions.playerInGroup((World) null, player_name, group)))
.getAttribute(attribute.fulfill(2));
// Permission in certain world
else if (attribute.getAttribute(2).startsWith("world"))
return new Element(String.valueOf(Depends.permissions.playerInGroup(attribute.getContext(2), player_name, group)))
.getAttribute(attribute.fulfill(2));
// Permission in current world
return new Element(String.valueOf(Depends.permissions.playerInGroup(getPlayerEntity(), group)))
.getAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("is_flying"))
return new Element(String.valueOf(getPlayerEntity().isFlying()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("is_sneaking"))
return new Element(String.valueOf(getPlayerEntity().isSneaking()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("is_blocking"))
return new Element(String.valueOf(getPlayerEntity().isBlocking()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("is_sleeping"))
return new Element(String.valueOf(getPlayerEntity().isSleeping()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("is_sprinting"))
return new Element(String.valueOf(getPlayerEntity().isSprinting()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("gamemode.id"))
return new Element(String.valueOf(getPlayerEntity().getGameMode().getValue()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("gamemode"))
return new Element(String.valueOf(getPlayerEntity().getGameMode().toString()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("item_on_cursor"))
return new dItem(getPlayerEntity().getItemOnCursor())
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("selected_npc")) {
if (getPlayerEntity().hasMetadata("selected"))
return dNPC.valueOf(getPlayerEntity().getMetadata("selected").get(0).asString())
.getAttribute(attribute.fulfill(1));
else return "null";
}
if (attribute.startsWith("allowed_flight"))
return new Element(String.valueOf(getPlayerEntity().getAllowFlight()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("host_name"))
return new Element(String.valueOf(getPlayerEntity().getAddress().getHostName()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("time_asleep"))
return new Duration(getPlayerEntity().getSleepTicks() / 20)
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("player_time"))
return new Element(String.valueOf(getPlayerEntity().getPlayerTime()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("player_time_offset"))
return new Element(String.valueOf(getPlayerEntity().getPlayerTimeOffset()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("prefix"))
return new Element(prefix)
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("debug.log")) {
dB.log(debug());
return new Element(Boolean.TRUE.toString())
.getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("debug.no_color")) {
return new Element(ChatColor.stripColor(debug()))
.getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("debug")) {
return new Element(debug())
.getAttribute(attribute.fulfill(1));
}
return new dEntity(getPlayerEntity()).getAttribute(attribute.fulfill(0));
}
}