/
MyZ.java
1454 lines (1340 loc) · 49.5 KB
/
MyZ.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
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
*
*/
package myz;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import myz.api.PlayerBeginBleedingEvent;
import myz.api.PlayerBeginPoisonEvent;
import myz.api.PlayerBreakLegEvent;
import myz.api.PlayerSpawnInWorldEvent;
import myz.api.PlayerWaterDecayEvent;
import myz.chests.ChestManager;
import myz.chests.ChestScanner;
import myz.commands.AddResearchCommand;
import myz.commands.AddSpawnCommand;
import myz.commands.AllowedCommand;
import myz.commands.BaseCommand;
import myz.commands.BlockCommand;
import myz.commands.ChestGetCommand;
import myz.commands.ChestScanCommand;
import myz.commands.ChestSetCommand;
import myz.commands.ClanCommand;
import myz.commands.CreateMedKitCommand;
import myz.commands.FriendCommand;
import myz.commands.FriendsCommand;
import myz.commands.GetUIDCommand;
import myz.commands.ItemConfigurationCommand;
import myz.commands.JoinClanCommand;
import myz.commands.LootSetCommand;
import myz.commands.RemoveSpawnCommand;
import myz.commands.ResearchCommand;
import myz.commands.SaveKitCommand;
import myz.commands.SaveRankCommand;
import myz.commands.SetLobbyCommand;
import myz.commands.SetRankCommand;
import myz.commands.SpawnCommand;
import myz.commands.SpawnsCommand;
import myz.commands.StatsCommand;
import myz.commands.TranslateCommand;
import myz.listeners.CancelZombieDamage;
import myz.listeners.EntityHurtPlayer;
import myz.listeners.EntitySpawn;
import myz.listeners.player.AutoFriend;
import myz.listeners.player.BlockEvent;
import myz.listeners.player.CancelPlayerEvents;
import myz.listeners.player.Chat;
import myz.listeners.player.ConsumeFood;
import myz.listeners.player.Heal;
import myz.listeners.player.JoinQuit;
import myz.listeners.player.KittehTag;
import myz.listeners.player.LibsUndisguiseListener;
import myz.listeners.player.Movement;
import myz.listeners.player.PlayerDeath;
import myz.listeners.player.PlayerHurtEntity;
import myz.listeners.player.PlayerKillEntity;
import myz.listeners.player.PlayerSummonGiant;
import myz.listeners.player.PlayerTakeDamage;
import myz.listeners.player.ResearchItem;
import myz.listeners.player.UndisguiseListener;
import myz.listeners.player.Visibility;
import myz.nmscode.compat.Compat;
import myz.nmscode.compat.MessageUtils;
import myz.nmscode.compat.MobUtils;
import myz.scheduling.Sync;
import myz.scheduling.aSync;
import myz.support.MedKit;
import myz.support.PlayerData;
import myz.support.SQLManager;
import myz.support.Teleport;
import myz.support.interfacing.Configuration;
import myz.support.interfacing.Localizer;
import myz.support.interfacing.Messenger;
import myz.utilities.DisguiseUtils;
import myz.utilities.LibsDisguiseUtils;
import myz.utilities.NMSUtils;
import myz.utilities.Utils;
import myz.utilities.Validate;
import myz.utilities.VaultUtils;
import myz.utilities.WorldlessLocation;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.mcstats.MetricsLite;
/**
* @author Jordan
*
* The main plugin and the interface for most convenience methods.
*/
public class MyZ extends JavaPlugin {
// TODO configurable death loot (?)
// TODO use construction parts to create clans. Builder is clan owner.
// TODO @ chat not showing name (?)
// TODO Research point rank uppance @see ResearchItem#checkRankIncrease
// TODO clan create permission in joinclan.
// TODO zombie spawn functionality
public static MyZ instance;
public static boolean vault;
private List<UUID> online_players = new ArrayList<UUID>();
private FileConfiguration blocks, spawn, chests, research;
private Map<String, FileConfiguration> localizable = new HashMap<String, FileConfiguration>();
private SQLManager sql;
private static final Random random = new Random();
public static boolean alertOps;
private Map<UUID, FileConfiguration> playerdata = new HashMap<UUID, FileConfiguration>();
private List<UUID> flags = new ArrayList<UUID>();
public static Compat version;
/**
* Load the blocks YAML file.
*/
private void loadBlocks() {
File blocks_file = new File(getDataFolder() + File.separator + "blocks.yml");
/*
* Make sure the file exists.
*/
if (!blocks_file.exists())
try {
blocks_file.createNewFile();
} catch (IOException e) {
getLogger().warning("Unable to save blocks.yml: " + e.getMessage());
}
blocks = YamlConfiguration.loadConfiguration(blocks_file);
}
/**
* Load the chests YAML file.
*/
private void loadChests() {
File chests_file = new File(getDataFolder() + File.separator + "chests.yml");
/*
* Make sure the file exists.
*/
if (!chests_file.exists())
try {
chests_file.createNewFile();
} catch (IOException e) {
getLogger().warning("Unable to save chests.yml: " + e.getMessage());
}
chests = YamlConfiguration.loadConfiguration(chests_file);
}
/**
* Load the localizable YAML file.
*/
private void loadLocalizable() {
getLocales();
for (Localizer locale : Localizer.values()) {
File localeable = new File(getDataFolder() + File.separator + "locales" + File.separator + locale.getCode() + ".yml");
if (!localeable.exists())
try {
localeable.createNewFile();
} catch (IOException e) {
getLogger().warning("Unable to create locale " + locale.getCode());
e.printStackTrace();
}
localizable.put(locale.getCode(), YamlConfiguration.loadConfiguration(localeable));
}
}
/**
* Load the playerdata YAML file.
*/
private void loadPlayerData() {
File playerdata_file = new File(getDataFolder() + File.separator + "playerdata.yml");
/*
* Make sure the file exists.
*/
if (!playerdata_file.exists())
return;
FileConfiguration data = YamlConfiguration.loadConfiguration(playerdata_file);
PlayerData.updateData(data);
playerdata_file.delete();
}
/**
* Load the research YAML file.
*/
private void loadResearch() {
File research_file = new File(getDataFolder() + File.separator + "research.yml");
/*
* Make sure the file exists.
*/
if (!research_file.exists())
try {
research_file.createNewFile();
} catch (IOException e) {
getLogger().warning("Unable to save research.yml: " + e.getMessage());
}
research = YamlConfiguration.loadConfiguration(research_file);
}
/**
* Load the spawn YAML file.
*/
private void loadSpawn() {
File spawn_file = new File(getDataFolder() + File.separator + "spawn.yml");
/*
* Make sure the file exists.
*/
if (!spawn_file.exists())
try {
spawn_file.createNewFile();
} catch (IOException e) {
getLogger().warning("Unable to save spawn.yml: " + e.getMessage());
}
spawn = YamlConfiguration.loadConfiguration(spawn_file);
}
/**
* Clean up all of the static variables that have the potential to hold a
* lot of space in memory.
*/
private void nullifyStatics() {
BlockCommand.blockChangers = null;
Sync.safeLogoutPlayers = null;
MedKit.clearKits();
if (getServer().getPluginManager().getPlugin("DisguiseCraft") != null)
myz.utilities.DisguiseUtils.disable();
Utils.packets = null;
}
/**
* Remove all buffs for the given player and make everything right again.
*
* @param player
* The player.
*/
private void wipeBuffs(Player player, boolean clearInventory) {
stopBleeding(player, false);
stopPoison(player, false);
fixLeg(player, false);
player.setHealth(player.getMaxHealth());
player.setFireTicks(0);
if (clearInventory) {
player.getInventory().clear();
player.getInventory().setArmorContents(null);
}
for (PotionEffect effect : player.getActivePotionEffects())
player.removePotionEffect(effect.getType());
player.setSaturation(20);
player.setFoodLevel(20);
}
/**
* Add a friend to the friender's friend list.
*
* @param friender
* The player that is friending the other.
* @param friended
* The friended player.
*/
public void addFriend(Player friender, UUID friended) {
PlayerData data = PlayerData.getDataFor(friender);
if (data != null && !data.getFriends().contains(friended)) {
data.addFriend(friended);
friender.sendMessage(Messenger.getConfigMessage(Localizer.getLocale(friender), "friend.added", getName(friended)));
}
if (sql.isConnected() && !sql.getStringList(friender.getUniqueId(), "friends").contains(friended.toString())) {
String current = sql.getString(friender.getUniqueId(), "friends");
sql.set(friender.getUniqueId(), "friends", current + (current.isEmpty() ? "" : ",") + friended.toString(), true);
friender.sendMessage(Messenger.getConfigMessage(Localizer.getLocale(friender), "friend.added", getName(friended)));
}
}
/**
* Add a player to the MyZ game. Also checks for kickban if the useprelogin
* configuration option is disabled.
*
* @param player
* The player.
*/
public void addPlayer(Player player, boolean clearInventory) {
PlayerData playerdata = PlayerData.getDataFor(player);
if (!(Boolean) Configuration.getConfig(Configuration.PRELOGIN)) {
/*
* Check if the player is still banned against the playerdata and sql.
*/
long now = System.currentTimeMillis();
long timeOfKickExpiry;
if (playerdata != null
&& (timeOfKickExpiry = playerdata.getTimeOfKickban() + (Integer) Configuration.getConfig(Configuration.KICKBAN_TIME)
* 1000) >= now
|| MyZ.instance.getSQLManager().isConnected()
&& (timeOfKickExpiry = MyZ.instance.getSQLManager().getLong(player.getUniqueId(), "timeOfKickban")
+ (Integer) Configuration.getConfig(Configuration.KICKBAN_TIME) * 1000) >= now) {
player.kickPlayer(Messenger.getConfigMessage(Localizer.DEFAULT, "kick.recur", (timeOfKickExpiry - now) / 1000 + ""));
return;
}
}
online_players.add(player.getUniqueId());
/*
* Add the player to the dataset if they're not in it yet. If they weren't in it, put them at the spawn.
*/
if (playerdata == null && (Boolean) Configuration.getConfig(Configuration.DATASTORAGE)) {
playerdata = PlayerData.createDataFor(player, player.getUniqueId(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, false, false, false, 0L,
new ArrayList<String>(), 0, 20, "", 0, 0, 0, 0, 0, 0, 0, 0, false, false);
putPlayerAtSpawn(player, false, clearInventory);
}
if (sql.isConnected() && !sql.isIn(player.getUniqueId())) {
sql.add(player);
putPlayerAtSpawn(player, false, clearInventory);
}
setThirst(player);
if (playerdata != null && playerdata.getTimeOfKickban() != 0)
playerdata.setTimeOfKickban(0L);
if (sql.isConnected() && sql.getLong(player.getUniqueId(), "timeOfKickban") != 0)
sql.set(player.getUniqueId(), "timeOfKickban", 0L, true);
/*
* Cache all values asynchronously to reduce runtime lag.
*/
if (sql.isConnected())
sql.createLinks(player.getUniqueId());
/*
* Teleport the player back to the world spawn if they were killed by an NPC logout.
*/
if (playerdata != null && playerdata.wasKilledNPC() || sql.isConnected() && sql.getBoolean(player.getUniqueId(), "wasNPCKilled")) {
Messenger.sendConfigMessage(player, "player_was_killed_npc");
putPlayerAtSpawn(player, true, clearInventory);
}
if (MyZ.instance.getServer().getPluginManager().getPlugin("DisguiseCraft") != null
&& MyZ.instance.getServer().getPluginManager().getPlugin("DisguiseCraft").isEnabled())
if (playerdata != null && playerdata.isZombie() || MyZ.instance.getSQLManager().isConnected()
&& MyZ.instance.getSQLManager().getBoolean(player.getUniqueId(), "isZombie"))
DisguiseUtils.becomeZombie(player);
if (MyZ.instance.getServer().getPluginManager().getPlugin("LibsDisguises") != null
&& MyZ.instance.getServer().getPluginManager().getPlugin("LibsDisguises").isEnabled())
if (playerdata != null && playerdata.isZombie() || MyZ.instance.getSQLManager().isConnected()
&& MyZ.instance.getSQLManager().getBoolean(player.getUniqueId(), "isZombie"))
LibsDisguiseUtils.becomeZombie(player);
}
/**
* Begin leg-break for this player. Will fail if the player already has a
* broken leg or doesn't have a PlayerData associated AND SQL is not
* connected.
*
* @param player
* The player.
*/
public void breakLeg(Player player) {
if (!isLegBroken(player)) {
PlayerBreakLegEvent event = new PlayerBreakLegEvent(player);
getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
PlayerData data = PlayerData.getDataFor(player);
if (data != null)
data.setLegBroken(true);
if (sql.isConnected())
sql.set(player.getUniqueId(), "legBroken", true, true);
player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 2));
Messenger.sendConfigMessage(player, "damage.leg_break");
}
}
}
/**
* End leg-break for this player. Will fail if the player already has a
* broken leg.
*
* @param player
* The player.
*/
public void fixLeg(Player player, boolean alert) {
if (isLegBroken(player)) {
PlayerData data = PlayerData.getDataFor(player);
if (data != null)
data.setLegBroken(false);
if (sql.isConnected())
sql.set(player.getUniqueId(), "legBroken", false, true);
player.removePotionEffect(PotionEffectType.SLOW);
if (alert)
Messenger.sendConfigMessage(player, "damage.leg_fix");
}
}
/**
* Get the blocks YAML.
*
* @return The FileConfiguration for the blocks.yml or null if not loaded.
*/
public FileConfiguration getBlocksConfig() {
return blocks;
}
/**
* Get the chest YAML.
*
* @return The FileConfiguration for the chests.yml or null if not loaded.
*/
public FileConfiguration getChestsConfig() {
return chests;
}
/**
* Get the list of flagged players, that is, players that have died and are
* about to be kicked. Ensures dead players don't spawn NPC's.
*
* @return The list of players.
*/
public List<UUID> getFlagged() {
return flags;
}
/**
* Get the folder that holds the locales.
*
* @return The folder (MyZ-3/locales/).
*/
public File getLocales() {
File folder = new File(getDataFolder() + File.separator + "locales");
if (!folder.exists())
folder.mkdir();
return folder;
}
/**
* Get the localizable YAML.
*
* @return The FileConfiguration for the localizable.yml or null if not
* loaded.
* @param locale
* The locale to get.
*/
public FileConfiguration getLocalizableConfig(Localizer locale) {
return localizable.get(locale.getCode());
}
public String getName(UUID uid) {
Map<UUID, String> map = lookupPlayers();
for (UUID key : map.keySet())
if (key.equals(uid))
return map.get(key);
return "Guest";
}
public Player getPlayer(UUID uid) {
for (Player p : Bukkit.getOnlinePlayers())
if (p.getUniqueId().equals(uid))
return p;
return null;
}
/**
* Get the playerdata YAML.
*
* @param player
* The player uuid of the data to get for.
* @return The FileConfiguration for the specified player's PlayerData or
* null if not loaded or can't load.
*/
public FileConfiguration getPlayerDataConfig(UUID player) {
if (!playerdata.containsKey(player)) {
File datafolder = new File(getDataFolder() + File.separator + "data");
if (!datafolder.exists())
datafolder.mkdir();
File datafile = new File(getDataFolder() + File.separator + "data" + File.separator + SQLManager.UUIDtoString(player) + ".yml");
if (!datafile.exists())
try {
datafile.createNewFile();
} catch (Exception e) {
Messenger.sendConsoleMessage("&4Unable to save a new PlayerData file for " + SQLManager.UUIDtoString(player) + ": "
+ e.getMessage());
return null;
}
FileConfiguration config = YamlConfiguration.loadConfiguration(datafile);
playerdata.put(player, config);
return config;
}
return playerdata.get(player);
}
/**
* Get the rank number of a player. Defaults to 0.
*
* @param player
* The player.
* @return The rank associated with the player.
*/
public int getRankFor(Player player) {
PlayerData data = PlayerData.getDataFor(player);
if (data != null)
return data.getRank();
if (sql.isConnected())
return sql.getInt(player.getUniqueId(), "rank");
return 0;
}
/**
* Get the research YAML.
*
* @return The FileConfiguration for the research.yml or null if not loaded.
*/
public FileConfiguration getResearchConfig() {
return research;
}
/**
* Get the spawn YAML.
*
* @return The FileConfiguration for the spawn.yml or null if not loaded.
*/
public FileConfiguration getSpawnConfig() {
return spawn;
}
/**
* Gets the SQLManager object.
*
* @return the SQLManager.
*/
public SQLManager getSQLManager() {
return sql;
}
public UUID getUID(String name) {
Map<UUID, String> map = lookupPlayers();
if (map.containsValue(name))
for (UUID key : map.keySet())
if (map.get(key).equals(name))
return key;
return UUID.randomUUID();
}
/**
* @see isBandit(UUID player)
*/
public boolean isBandit(Player player) {
return isBandit(player.getUniqueId());
}
/**
* Whether or not this player is a bandit.
*
* @param player
* The player.
* @return True if the player is a bandit.
*/
public boolean isBandit(UUID player) {
PlayerData data = PlayerData.getDataFor(player);
if (data != null)
return data.isBandit();
if (sql.isConnected())
return sql.getInt(player, "player_kills_life") >= (Integer) Configuration.getConfig(Configuration.BANDIT);
// Theoretically impossible to get to this case.
return false;
}
/**
* Whether or not the given player is currently bleeding.
*
* @param player
* The player.
* @return True if the player is bleeding.
*/
public boolean isBleeding(Player player) {
PlayerData data = PlayerData.getDataFor(player);
if (data != null)
return data.isBleeding();
if (sql.isConnected())
return sql.getBoolean(player.getUniqueId(), "isBleeding");
return false;
}
/**
* @see isFriend(UUID player, UUID name)
*/
public boolean isFriend(Player player, UUID name) {
return isFriend(player.getUniqueId(), name);
}
/**
* Whether or not the uuid provided is a friend of the given player.
*
* @param player
* The player uuid.
* @param name
* The friend uuid to check.
* @return True if @param name is a friend of @param player.
*/
public boolean isFriend(UUID player, UUID name) {
PlayerData data = PlayerData.getDataFor(player);
if (data != null)
return data.isFriend(name);
if (sql.isConnected())
return sql.getStringList(player, "friends").contains(name.toString());
// Theoretically impossible to get to this case.
return false;
}
/**
* @see isHealer(UUID player)
*/
public boolean isHealer(Player player) {
return isHealer(player.getUniqueId());
}
/**
* Whether or not this player is a healer.
*
* @param player
* The player.
* @return True if the player is a healer.
*/
public boolean isHealer(UUID player) {
PlayerData data = PlayerData.getDataFor(player);
if (data != null)
return data.isHealer();
if (sql.isConnected())
return sql.getInt(player, "heals_life") >= (Integer) Configuration.getConfig(Configuration.HEALER);
// Theoretically impossible to get to this case.
return false;
}
/**
* Whether or not the given player currently has a broken leg.
*
* @param player
* The player.
* @return True if the player has a broken leg.
*/
public boolean isLegBroken(Player player) {
PlayerData data = PlayerData.getDataFor(player);
if (data != null)
return data.isLegBroken();
if (sql.isConnected())
return sql.getBoolean(player.getUniqueId(), "legBroken");
return false;
}
/**
* Whether or not the player is currently playing MyZ.
*
* @param player
* The player.
* @return True if the player is playing MyZ.
*/
public boolean isPlayer(Player player) {
return online_players.contains(player.getUniqueId());
}
/**
* Whether or not the given player is currently poisoned.
*
* @param player
* The player.
* @return True if the player is poisoned.
*/
public boolean isPoisoned(Player player) {
PlayerData data = PlayerData.getDataFor(player);
if (data != null)
return data.isPoisoned();
if (sql.isConnected())
return sql.getBoolean(player.getUniqueId(), "isPoisoned");
return false;
}
public Map<UUID, String> lookupPlayers() {
Map<UUID, String> map = new HashMap<UUID, String>();
try {
File folder = new File(getDataFolder() + File.separator + "mappings.yml");
if (!folder.exists())
folder.createNewFile();
FileConfiguration yaml = YamlConfiguration.loadConfiguration(folder);
for (String key : yaml.getKeys(false))
map.put(UUID.fromString(key), yaml.getString(key));
} catch (Exception exc) {
Messenger.sendConsoleMessage("&4Unable to retrieve UUID mappings: " + exc.getMessage());
}
return map;
}
public void map(Player player) {
try {
File folder = new File(getDataFolder() + File.separator + "mappings.yml");
if (!folder.exists())
folder.createNewFile();
FileConfiguration yaml = YamlConfiguration.loadConfiguration(folder);
yaml.set(player.getUniqueId().toString(), player.getName());
yaml.save(folder);
// Store their name.
if (sql.isConnected())
sql.set(player.getUniqueId(), "name", player.getName(), true);
} catch (Exception exc) {
Messenger.sendConsoleMessage("&4Unable to add, save or retrieve UUID mappings: " + exc.getMessage());
}
}
@Override
public void onDisable() {
getServer().getScheduler().cancelTasks(this);
ChestManager.respawnAll(true);
boolean disguise = getServer().getPluginManager().getPlugin("LibsDisguises") != null
&& getServer().getPluginManager().getPlugin("LibsDisguises").isEnabled();
// Remove all entities in all worlds as reloads will cause classloader
// issues to
// do with overriding the pathfinding and entity.
MobUtils.removeCustomPlayers();
for (Player player : Bukkit.getOnlinePlayers())
removePlayer(player, false, true);
if (disguise)
for (Player player : getServer().getOnlinePlayers())
myz.utilities.LibsDisguiseUtils.undisguise(player);
MobUtils.clearCustomPlayers();
for (String name : (List<String>) Configuration.getConfig(Configuration.WORLDS)) {
World world = Bukkit.getWorld(name);
if (world == null) {
Messenger.sendConsoleMessage("&4Specified world (" + name + ") does not exist! Please update your config.yml");
continue;
}
for (Entity entity : world.getEntitiesByClass(LivingEntity.class))
if (entity.getType() == EntityType.ZOMBIE || entity.getType() == EntityType.GIANT || entity.getType() == EntityType.HORSE
|| entity.getType() == EntityType.PIG_ZOMBIE || entity.getType() == EntityType.SKELETON) {
if (disguise)
myz.utilities.LibsDisguiseUtils.undisguise((LivingEntity) entity);
entity.remove();
}
}
/*
* These tend to throw ClassDef errors randomly.
*/
try {
if (Utils.packets != null)
Utils.packets.clear();
} catch (Exception exc) {
}
try {
MobUtils.unregister();
} catch (Exception exc) {
}
try {
MessageUtils.removeAllHolograms();
} catch (Exception exc) {
}
nullifyStatics();
}
@Override
public void onEnable() {
version = Compat.fromString(NMSUtils.version);
if (version == null) {
getLogger()
.warning("This version of MyZ is not compatible with your version of Craftbukkit (" + getServer().getVersion() + ")");
getLogger().warning("Disabling MyZ.");
setEnabled(false);
return;
} else
getLogger().info("Using hooks for " + version);
instance = this;
if (getServer().getPluginManager().getPlugin("Vault") != null && getServer().getPluginManager().isPluginEnabled("Vault"))
vault = VaultUtils.setupPermissions();
getDataFolder().mkdir();
File defaultConfig = new File(getDataFolder() + File.separator + "config.yml");
if (!defaultConfig.exists())
try {
defaultConfig.createNewFile();
} catch (Exception exc) {
getLogger().warning("Unable to save default config: " + exc.getMessage());
}
loadPlayerData();
loadBlocks();
loadLocalizable();
loadSpawn();
loadChests();
loadResearch();
ChestManager.respawnAll(true);
/*
* Register the new enchantment so that MedKits can have their glow.
*/
MedKit.registerNewEnchantment();
Configuration.reload(true);
sql = new SQLManager((String) Configuration.getConfig(Configuration.HOST), (Integer) Configuration.getConfig(Configuration.PORT),
(String) Configuration.getConfig(Configuration.DATABASE), (String) Configuration.getConfig(Configuration.USER),
(String) Configuration.getConfig(Configuration.PASSWORD));
/*
* Register threads.
*/
getServer().getScheduler().runTaskTimerAsynchronously(this, new aSync(), 100L, 20L);
getServer().getScheduler().runTaskTimer(this, new Sync(), 100L, 20L);
/*
* Register all commands.
*/
getCommand("friend").setExecutor(new FriendCommand());
getCommand("friends").setExecutor(new FriendsCommand());
getCommand("start").setExecutor(new SpawnCommand());
if (getServer().getPluginManager().getPlugin("WorldEdit") != null
&& getServer().getPluginManager().getPlugin("WorldEdit").isEnabled())
getCommand("setlobby").setExecutor(new SetLobbyCommand());
getCommand("addspawn").setExecutor(new AddSpawnCommand());
getCommand("removespawn").setExecutor(new RemoveSpawnCommand());
getCommand("spawnpoints").setExecutor(new SpawnsCommand());
getCommand("savekit").setExecutor(new SaveKitCommand());
getCommand("saverank").setExecutor(new SaveRankCommand());
getCommand("savemedkit").setExecutor(new CreateMedKitCommand());
getCommand("setrank").setExecutor(new SetRankCommand());
getCommand("clan").setExecutor(new ClanCommand());
getCommand("joinclan").setExecutor(new JoinClanCommand());
getCommand("getid").setExecutor(new GetUIDCommand());
getCommand("blockallow").setExecutor(new BlockCommand());
getCommand("blocks").setExecutor(new AllowedCommand());
getCommand("research").setExecutor(new ResearchCommand());
getCommand("setresearch").setExecutor(new AddResearchCommand());
getCommand("stats").setExecutor(new StatsCommand());
getCommand("myz").setExecutor(new BaseCommand());
getCommand("configure").setExecutor(new ItemConfigurationCommand());
getCommand("chestscan").setExecutor(new ChestScanCommand());
getCommand("chestset").setExecutor(new ChestSetCommand());
getCommand("chestget").setExecutor(new ChestGetCommand());
getCommand("lootset").setExecutor(new LootSetCommand());
getCommand("mtranslate").setExecutor(new TranslateCommand());
/*
* Register all listeners.
*/
PluginManager p = getServer().getPluginManager();
p.registerEvents(new JoinQuit(), this);
p.registerEvents(new ChestScanner(), this);
p.registerEvents(new BlockEvent(), this);
p.registerEvents(new AutoFriend(), this);
p.registerEvents(new Heal(), this);
p.registerEvents(new CancelZombieDamage(), this);
p.registerEvents(new ConsumeFood(), this);
p.registerEvents(new PlayerHurtEntity(), this);
p.registerEvents(new Visibility(), this);
p.registerEvents(new PlayerKillEntity(), this);
p.registerEvents(new EntityHurtPlayer(), this);
p.registerEvents(new PlayerDeath(), this);
p.registerEvents(new EntitySpawn(), this);
p.registerEvents(new PlayerSummonGiant(), this);
p.registerEvents(new Chat(), this);
p.registerEvents(new CancelPlayerEvents(), this);
p.registerEvents(new Movement(), this);
p.registerEvents(new PlayerTakeDamage(), this);
p.registerEvents(new ResearchItem(), this);
if (getServer().getPluginManager().getPlugin("TagAPI") != null && getServer().getPluginManager().getPlugin("TagAPI").isEnabled())
p.registerEvents(new KittehTag(), this);
if (getServer().getPluginManager().getPlugin("DisguiseCraft") != null
&& getServer().getPluginManager().getPlugin("DisguiseCraft").isEnabled())
p.registerEvents(new UndisguiseListener(), this);
if (getServer().getPluginManager().getPlugin("LibsDisguises") != null
&& getServer().getPluginManager().getPlugin("LibsDisguises").isEnabled()) {
myz.utilities.LibsDisguiseUtils.setup();
p.registerEvents(new LibsUndisguiseListener(), this);
}
/*
* Connect to SQL or use PlayerData.
*/
getServer().getScheduler().runTaskLaterAsynchronously(instance, new Runnable() {
@Override
public void run() {
sql.connect();
if (!sql.isConnected()/* && (Boolean) Configuration.getConfig(Configuration.DATASTORAGE)*/) {
Messenger.sendConsoleMessage(ChatColor.GREEN + "Using PlayerData for this session.");
alertOps = true;
Messenger.sendConsoleMessage(ChatColor.YELLOW + "Visit http://my-z.org/request.php to get a free MyZ MySQL database.");
Configuration.saveConfig(Configuration.DATASTORAGE, true, true);
// Datastorage is a rather extranneous field as it will use
// it no matter the condition set (if mySQL cannot be
// reached).
} else/* if (sql.isConnected() && (Boolean) Configuration.getConfig(Configuration.DATASTORAGE))*/{
Messenger.sendConsoleMessage(ChatColor.GREEN + "Using MySQL for this session.");
// Configuration.saveConfig(Configuration.DATASTORAGE,
// false, false);
}
/*
* Add all players that weren't already in the playerdata to it (in case of a reload).
*/
for (String world : (List<String>) Configuration.getConfig(Configuration.WORLDS)) {
if (Bukkit.getWorld(world) == null) {
Messenger.sendConsoleMessage("&4Specified world (" + world + ") does not exist! Please update your config.yml");
continue;
}
for (Player player : Bukkit.getWorld(world).getPlayers()) {
addPlayer(player, true);
PlayerData data = null;
if ((data = PlayerData.getDataFor(player)) == null || sql.isConnected() && !sql.isIn(player.getUniqueId())) {
if (data == null && (Boolean) Configuration.getConfig(Configuration.DATASTORAGE)) {
PlayerData.createDataFor(player, player.getUniqueId(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, false, false, false,
0L, new ArrayList<String>(), 0, (Integer) Configuration.getConfig(Configuration.THIRST_MAX), "", 0,
0, 0, 0, 0, 0, 0, 0, false, false);
putPlayerAtSpawn(player, false, true);
}
if (sql.isConnected() && !sql.isIn(player.getUniqueId())) {
sql.add(player);
putPlayerAtSpawn(player, false, true);
}
}
}
}
}
}, 0L);
MobUtils.register();
/*
* Run Metrics.
*/
try {
MetricsLite metrics = new MetricsLite(this);
metrics.start();
} catch (IOException exc) {
Messenger.sendConsoleMessage("&4Metrics failed to start.");
}
/*
* Autoupdate.
*/
if ((Boolean) Configuration.getConfig(Configuration.AUTOUPDATE))
new Updater(this, 55557, getFile(), false);
}
/**
* Return the player to the spawn and clear all buffs. If it was a death,
* increment the death count and kickban if necessary.
*
* @param player
* The player.
* @param wasDeath
* Whether or not the return to spawn was a result of a death.
*/
public void putPlayerAtSpawn(Player player, boolean wasDeath, boolean clearInventory) {
if (!player.isOnline())
return;
Teleport.teleport(player, player.getWorld().getSpawnLocation(), false);
if (getServer().getPluginManager().isPluginEnabled("essentials"))
Bukkit.dispatchCommand(player, "spawn");
wipeBuffs(player, clearInventory);
if (wasDeath) {
PlayerData data = PlayerData.getDataFor(player);
setThirst(player, (Integer) Configuration.getConfig(Configuration.THIRST_MAX));
boolean wasNPCKilled = false;
if (data != null) {
wasNPCKilled = data.wasKilledNPC();
data.setWasKilledNPC(false);
data.setDeaths(data.getDeaths() + 1);
data.setMinutesAliveLife(0);
data.setPlayerKillsLife(0);
data.setZombieKillsLife(0);
data.setPigmanKillsLife(0);
data.setGiantKillsLife(0);
}
if (sql.isConnected()) {