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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
|
package com.earth2me.essentials;
import java.util.*;
import java.util.logging.*;
import java.io.*;
import org.bukkit.*;
import com.earth2me.essentials.commands.IEssentialsCommand;
import net.minecraft.server.EntityHuman;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.entity.*;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.reader.UnicodeReader;
public class User extends PlayerExtension implements Comparable<User>
{
private static final Logger logger = Logger.getLogger("Minecraft");
private final Yaml yaml = new Yaml(new SafeConstructor());
private boolean isLoaded = false;
private final File folder;
private Map<String, Object> data = new HashMap<String, Object>();
private static Map<String, User> users = new HashMap<String, User>();
private boolean teleEnabled = true;
private long lastTeleport = 0;
private long lastHeal = 0;
private boolean justPortaled = false;
//private TimerTask teleTimer = null;
private int teleTimer = -1;
public Location lastLocation = null;
private User replyTo = null;
private boolean isNew = false;
public String currentJail;
public CraftItemStack[] savedInventory;
private User(Player base)
{
super(base);
this.folder = new File((Essentials.getStatic() == null ? new File(".") : Essentials.getStatic().getDataFolder()), "userdata");
if (base instanceof EntityHuman)
{
this.lastLocation = getBase().getLocation();
}
load();
}
public static int size()
{
return users.size();
}
public static <T> User get(T base)
{
if (base instanceof Player)
return get((Player)base);
return null;
}
public static <T extends Player> User get(T base)
{
if (base == null)
return null;
if (base instanceof User)
return (User)base;
if (users.containsKey(base.getName()))
return users.get(base.getName()).update(base);
User u = new User(base);
users.put(u.getName(), u);
return u;
}
public static <T> void charge(T base, IEssentialsCommand cmd) throws Exception
{
if (base instanceof Player)
User.get(base).charge(cmd);
}
public boolean isNew()
{
return isNew;
}
public void respawn(Spawn spawn) throws Exception
{
respawn(spawn, null);
}
public void respawn(Spawn spawn, final String chargeFor) throws Exception
{
teleportTo(getSafeDestination(spawn.getSpawn(getGroup())), chargeFor);
}
private User update(Player base)
{
setBase(base);
return this;
}
public boolean isAuthorized(IEssentialsCommand cmd)
{
return isAuthorized("essentials." + (cmd.getName().equals("r") ? "msg" : cmd.getName()));
}
public boolean isAuthorized(String node)
{
if (isOp())
return true;
if (isJailed())
return false;
try
{
return com.nijikokun.bukkit.Permissions.Permissions.Security.permission(base, node);
}
catch (Throwable ex)
{
String[] cmds = node.split("\\.", 2);
return !Essentials.getSettings().isCommandRestricted(cmds[cmds.length - 1]);
}
}
public boolean isTeleEnabled()
{
return teleEnabled;
}
public boolean toggleTeleEnabled()
{
return teleEnabled = !teleEnabled;
}
public void teleportCooldown(boolean justCheck) throws Exception
{
long now = Calendar.getInstance().getTimeInMillis();
long cooldown = Essentials.getSettings().getTeleportCooldown();
long left = lastTeleport + cooldown - now;
if (left > 0 && !isOp() && !isAuthorized("essentials.teleport.cooldown.bypass"))
throw new Exception("Time before next teleport: " + Essentials.FormatTime(left));
// if justCheck is set, don't update lastTeleport; we're just checking
if (!justCheck) lastTeleport = now;
}
public void teleportCooldown() throws Exception
{
teleportCooldown(true);
}
public void healCooldown() throws Exception
{
long now = Calendar.getInstance().getTimeInMillis();
long cooldown = Essentials.getSettings().getHealCooldown();
long left = lastHeal + cooldown - now;
if (left > 0 && !isOp() && !isAuthorized("essentials.heal.cooldown.bypass"))
throw new Exception("Time before next heal: " + Essentials.FormatTime(left));
lastHeal = now;
}
private void load()
{
if (isLoaded) return;
isLoaded = true;
data = Essentials.getData(this);
try
{
if (!folder.exists()) folder.mkdirs();
File file = new File(folder, getName() + ".yml");
if (!file.exists())
{
isNew = true;
file.createNewFile();
logger.info(getName() + " has logged in for the first time.");
}
FileInputStream rx = new FileInputStream(file);
Map<String, Object> userData = (Map<String, Object>)yaml.load(new UnicodeReader(rx));
if (userData != null) data.putAll(userData);
rx.close();
}
catch (Throwable ex)
{
logger.log(Level.SEVERE, null, ex);
}
finally
{
if (data == null) data = new HashMap<String, Object>();
}
}
private void flush()
{
try
{
if (!folder.exists()) folder.mkdirs();
File file = new File(folder, getName() + ".yml");
if (!file.exists()) file.createNewFile();
FileWriter tx = new FileWriter(file);
tx.write(yaml.dump(data));
tx.flush();
tx.close();
}
catch (Throwable ex)
{
logger.log(Level.SEVERE, null, ex);
}
}
public boolean isGodModeEnabled()
{
load();
return data.containsKey("godmode") && (Boolean)data.get("godmode");
}
public boolean toggleGodMode()
{
boolean retval = !isGodModeEnabled();
data.put("godmode", retval);
flush();
return retval;
}
public boolean isMuted()
{
load();
return data.containsKey("muted") && (Boolean)data.get("muted");
}
public boolean toggleMuted()
{
boolean retval = !isMuted();
data.put("muted", retval);
flush();
return retval;
}
public boolean isJailed()
{
//load(); Do not load config everytime time!
return data.containsKey("jailed") && (Boolean)data.get("jailed");
}
public boolean toggleJailed()
{
boolean retval = !isJailed();
data.put("jailed", retval);
flush();
load();
return retval;
}
public double getMoney()
{
load();
if (data.containsKey("money"))
{
if (data.get("money") instanceof Integer)
return (double)((Integer)data.get("money"));
return (Double)data.get("money");
}
try
{
return com.nijiko.coelho.iConomy.iConomy.getBank().getAccount(getName()).getBalance();
}
catch (Throwable ex)
{
try
{
Map<String, Object> idata = Essentials.getData(this);
return (Integer)idata.get("money");
}
catch (Throwable ex2)
{
return Essentials.getSettings().getStartingBalance();
}
}
}
public void setMoney(double value)
{
try
{
com.nijiko.coelho.iConomy.iConomy.getBank().getAccount(getName()).setBalance(value);
}
catch (Throwable ex)
{
data.put("money", value);
flush();
}
}
public void giveMoney(double value)
{
if (value == 0) return;
setMoney(getMoney() + value);
sendMessage("§a$" + value + " has been added to your account.");
}
public void payUser(User reciever, int value) throws Exception
{
if (value == 0) return;
if (!canAfford(value))
{
throw new Exception("You do not have sufficient funds.");
}
else
{
setMoney(getMoney() - value);
reciever.setMoney(reciever.getMoney() + value);
sendMessage("§a$" + value + " has been sent to " + reciever.getDisplayName());
reciever.sendMessage("§a$" + value + " has been recieved from " + getDisplayName());
}
}
public void takeMoney(double value)
{
if (value == 0) return;
setMoney(getMoney() - value);
sendMessage("§c$" + value + " has been taken from your account.");
}
public void charge(String cmd) throws Exception
{
double mon = getMoney();
double cost = Essentials.getSettings().getCommandCost(cmd.startsWith("/") ? cmd.substring(1) : cmd);
if (mon < cost && !isOp())
throw new Exception("You do not have sufficient funds.");
takeMoney(cost);
}
public void canAfford(String cmd) throws Exception
{
double mon = getMoney();
double cost = Essentials.getSettings().getCommandCost(cmd.startsWith("/") ? cmd.substring(1) : cmd);
if (mon < cost && !isOp())
throw new Exception("You do not have sufficient funds.");
}
public boolean canAfford(double cost)
{
double mon = getMoney();
if (mon < cost && !isOp())
{
return false;
}
else
{
return true;
}
}
public void canAfford(IEssentialsCommand cmd) throws Exception
{
canAfford(cmd.getName());
}
public void cancelTeleport(boolean notifyUser)
{
if (teleTimer == -1) return;
try
{
getServer().getScheduler().cancelTask(teleTimer);
if (notifyUser) sendMessage("§cPending teleportation request cancelled.");
}
catch (Throwable ex)
{
}
finally
{
teleTimer = -1;
}
}
public void cancelTeleport()
{
cancelTeleport(false);
}
public void teleportTo(final Location loc, final String chargeFor)
{
final long delay = Essentials.getSettings().getTeleportDelay();
if (delay <= 0 || isOp() || isAuthorized("essentials.teleport.timer.bypass"))
{
try
{
if (chargeFor != null) charge(chargeFor);
teleportCooldown(false);
teleportToNow(loc);
}
catch (Throwable ex)
{
sendMessage("§cError: " + ex.getMessage());
}
return;
}
cancelTeleport();
sendMessage("§7Teleportation will commence in " + Essentials.FormatTime(delay) + ". Don't move.");
teleTimer = getServer().getScheduler().scheduleSyncRepeatingTask(Essentials.getStatic(), new TeleportTimer(this, delay)
{
public void DoTeleport()
{
try
{
if (chargeFor != null) charge(chargeFor);
teleportToNow(loc);
}
catch (Throwable ex)
{
sendMessage("§cError: " + ex.getMessage());
}
}
public void DoCancel()
{
cancelTeleport();
}
}, 10, 10);
}
@Override
public void teleportTo(final Location loc)
{
teleportTo(loc, null);
}
public void teleportTo(final Entity entity, final String chargeFor)
{
final long delay = Essentials.getSettings().getTeleportDelay();
if (delay <= 0 || isOp() || isAuthorized("essentials.teleport.timer.bypass"))
{
try
{
if (chargeFor != null) charge(chargeFor);
teleportCooldown(false);
teleportToNow(entity);
}
catch (Throwable ex)
{
sendMessage("§cError: " + ex.getMessage());
}
return;
}
cancelTeleport();
sendMessage("§7Teleportation will commence in " + Essentials.FormatTime(delay) + ". Don't move.");
teleTimer = getServer().getScheduler().scheduleSyncRepeatingTask(Essentials.getStatic(), new TeleportTimer(this, delay)
{
public void DoTeleport()
{
try
{
if (chargeFor != null) charge(chargeFor);
teleportToNow(entity);
}
catch (Throwable ex)
{
sendMessage("§cError: " + ex.getMessage());
}
}
public void DoCancel()
{
cancelTeleport();
}
}, 10, 10);
}
@Override
public void teleportTo(final Entity entity)
{
teleportTo(entity, null);
}
public Location getHome() throws Exception
{
if (data.containsKey("home"))
{
List<Object> vals = (List<Object>)data.get("home");
World world = getServer() == null ? null : getServer().getWorlds().get(0);
if (vals.size() > 5 && getServer() != null)
getServer().getWorld((String)vals.get(5));
return new Location(
world,
(Double)vals.get(0),
(Double)vals.get(1),
(Double)vals.get(2),
((Double)vals.get(3)).floatValue(),
((Double)vals.get(4)).floatValue());
}
try
{
Map<String, Object> gdata = Essentials.getData(this);
List<Object> vals = (List<Object>)gdata.get("home");
World world = getServer().getWorlds().get(0);
if (vals.size() > 5)
getServer().getWorld((String)vals.get(5));
return new Location(world,
(Double)vals.get(0),
(Double)vals.get(1),
(Double)vals.get(2),
((Double)vals.get(3)).floatValue(),
((Double)vals.get(4)).floatValue());
}
catch (Throwable ex)
{
throw new Exception("You have not set a home.");
}
}
public void teleportToHome(final String chargeFor)
{
final long delay = Essentials.getSettings().getTeleportDelay();
Location loc = null;
try
{
// check this first in case user hasn't set a home yet
loc = getHome();
}
catch (Throwable ex)
{
sendMessage("§cTeleport: " + ex.getMessage());
return;
}
if (delay <= 0 || isOp() || isAuthorized("essentials.teleport.timer.bypass"))
{
try
{
if (chargeFor != null) charge(chargeFor);
teleportCooldown(false);
teleportToNow(loc);
sendMessage("§7Teleporting home...");
}
catch (Throwable ex)
{
sendMessage("§cError: " + ex.getMessage());
}
return;
}
cancelTeleport();
sendMessage("§7Teleportation will commence in " + Essentials.FormatTime(delay) + ". Don't move.");
teleTimer = getServer().getScheduler().scheduleSyncRepeatingTask(Essentials.getStatic(), new TeleportTimer(this, delay)
{
public void DoTeleport()
{
try
{
if (chargeFor != null) charge(chargeFor);
teleportToNow(getHome());
}
catch (Throwable ex)
{
sendMessage("§cError: " + ex.getMessage());
}
}
public void DoCancel()
{
cancelTeleport();
}
}, 10, 10);
}
public void teleportToHome()
{
teleportToHome(null);
}
public void teleportToNow(Location loc) throws Exception
{
cancelTeleport();
lastLocation = getLocation();
getBase().teleportTo(getSafeDestination(loc));
}
public void teleportToNow(Entity entity)
{
cancelTeleport();
lastLocation = getLocation();
getBase().teleportTo(entity);
}
public void teleportBack(final String chargeFor)
{
teleportTo(lastLocation, chargeFor);
}
public void teleportBack()
{
teleportBack(null);
}
public void dispose()
{
this.base = new OfflinePlayer(getName());
}
public void charge(IEssentialsCommand cmd) throws Exception
{
charge(cmd.getName());
}
public boolean getJustPortaled()
{
return justPortaled;
}
public void setJustPortaled(boolean value)
{
justPortaled = value;
}
public void setReplyTo(User user)
{
replyTo = user;
}
public User getReplyTo()
{
return replyTo;
}
public void setHome()
{
setHome(getLocation());
}
public void setHome(Location home)
{
List<Object> vals = new ArrayList<Object>(6);
vals.add(new Double(home.getX()));
vals.add(new Double(home.getY()));
vals.add(new Double(home.getZ()));
vals.add(new Double(home.getYaw()));
vals.add(new Double(home.getPitch()));
vals.add(home.getWorld() == null ? "world" : home.getWorld().getName());
data.put("home", vals);
flush();
setCompassTarget(home);
}
public String getNick()
{
Essentials ess = Essentials.getStatic();
String name = ess.getConfiguration().getBoolean("disable-nick", false) ? getName() : ess.readNickname(this);
if (isOp() && ess.getConfiguration().getString("ops-name-color", "c").matches("^[0-9a-f]$")) {
name = "§" + ess.getConfiguration().getString("ops-name-color", "c") + name + "§f";
}
return name;
}
public void warpTo(String warp, final String chargeFor) throws Exception
{
lastLocation = getLocation();
Location loc = Essentials.getWarps().getWarp(warp);
teleportTo(loc, chargeFor);
sendMessage("§7Warping to " + warp + ".");
}
public void warpTo(String string) throws Exception
{
warpTo(string, null);
}
public void clearNewFlag()
{
isNew = false;
}
public int compareTo(User t) {
return ChatColor.stripColor(this.getDisplayName()).compareToIgnoreCase(ChatColor.stripColor(t.getDisplayName()));
}
public Boolean canSpawnItem(int itemId)
{
if(Essentials.getSettings().itemSpawnBlacklist().contains(itemId))return false;
return true;
}
}
|