-
Notifications
You must be signed in to change notification settings - Fork 0
/
Skywars.java
805 lines (682 loc) · 26.4 KB
/
Skywars.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
package me.brunorm.skywars;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.CommandExecutor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;
import com.cryptomorin.xseries.XMaterial;
import me.brunorm.skywars.commands.ForceStartCommand;
import me.brunorm.skywars.commands.LeaveCommand;
import me.brunorm.skywars.commands.MainCommand;
import me.brunorm.skywars.commands.StartCommand;
import me.brunorm.skywars.commands.WhereCommand;
import me.brunorm.skywars.events.DisableWeather;
import me.brunorm.skywars.events.Events;
import me.brunorm.skywars.events.InteractEvent;
import me.brunorm.skywars.events.MessageSound;
import me.brunorm.skywars.events.ProjectileTrails;
import me.brunorm.skywars.events.SetupEvents;
import me.brunorm.skywars.handlers.SkywarsActionbar;
import me.brunorm.skywars.handlers.SkywarsScoreboard;
import me.brunorm.skywars.handlers.SkywarsTablist;
import me.brunorm.skywars.holograms.DecentHologramsController;
import me.brunorm.skywars.holograms.HologramController;
import me.brunorm.skywars.holograms.HolographicDisplaysNewController;
import me.brunorm.skywars.holograms.HolographicDisplaysOldController;
import me.brunorm.skywars.managers.ChestManager;
import me.brunorm.skywars.managers.MapManager;
import me.brunorm.skywars.managers.SignManager;
import me.brunorm.skywars.menus.ConfigMenu;
import me.brunorm.skywars.menus.GameOptionsMenu;
import me.brunorm.skywars.menus.GamesMenu;
import me.brunorm.skywars.menus.KitsMenu;
import me.brunorm.skywars.menus.MapMenu;
import me.brunorm.skywars.menus.PlayerInventoryManager;
import me.brunorm.skywars.nms.ReflectionNMS;
import me.brunorm.skywars.schematics.SchematicHandler;
import me.brunorm.skywars.structures.Arena;
import me.brunorm.skywars.structures.Kit;
import me.brunorm.skywars.structures.SkywarsUser;
import net.milkbowl.vault.economy.Economy;
@SuppressWarnings("deprecation")
public class Skywars extends JavaPlugin {
// get plugin data
PluginDescriptionFile pdf = this.getDescription();
public String name = this.pdf.getName();
public String version = this.pdf.getVersion();
public List<String> authors = this.pdf.getAuthors();
private final String prefix = Messager.color("&6[&e%s&6]&e", this.name);
private final String debugPrefix = Messager.color("&7[&c%s&7]&e", this.name);
public static String kitsPath;
public static String worldsPath;
public static String mapsPath;
public static String schematicsPath;
public static String playersPath;
public static String chestsPath;
public static SkywarsConfiguration configuration;
public static boolean holograms = false;
public static boolean placeholders = false;
HologramController hologramController;
public static boolean economyEnabled;
Economy economy;
RegisteredServiceProvider<Economy> economyProvider;
SignManager signManager;
public SignManager getSignManager() {
return this.signManager;
}
public Economy getEconomy() {
return this.economy;
}
public static YamlConfiguration config;
public static YamlConfiguration langConfig;
public static YamlConfiguration lobbyConfig;
public HashMap<Player, Location> playerLocations = new HashMap<Player, Location>();
private String packageName;
private String serverPackageVersion;
private ReflectionNMS nmsHandler;
public static Skywars plugin;
public static Skywars get() {
return plugin;
}
public Skywars() {
super();
}
protected Skywars(JavaPluginLoader loader, PluginDescriptionFile description, File dataFolder, File file) {
super(loader, description, dataFolder, file);
}
public File file() {
return this.getFile();
}
private Location lobby;
public void setLobby(Location lobby) {
lobbyConfig.set("lobby.x", lobby.getX());
lobbyConfig.set("lobby.y", lobby.getY());
lobbyConfig.set("lobby.z", lobby.getZ());
lobbyConfig.set("lobby.world", lobby.getWorld().getName());
ConfigurationUtils.saveConfiguration(lobbyConfig, "lobby.yml");
this.lobby = lobby;
}
public Location getLobby() {
return this.lobby;
}
public void setLobbyFromConfig() {
if (lobbyConfig == null || lobbyConfig.get("lobby") == null) {
this.lobby = null;
return;
}
final String worldName = lobbyConfig.getString("lobby.world");
if (worldName != null) {
final World world = Bukkit.getWorld(worldName);
if (world != null) {
final double x = lobbyConfig.getDouble("lobby.x");
final double y = lobbyConfig.getDouble("lobby.y");
final double z = lobbyConfig.getDouble("lobby.z");
this.lobby = new Location(world, x, y, z);
} else
this.sendMessage("&cLobby world (&b%s&c) does not exist!", worldName);
}
}
boolean updated = false;
public void Reload() {
this.loadConfig();
this.mapManager.loadMaps();
this.chestManager.loadChests();
this.signManager.loadSigns();
this.loadKits();
}
@Override
public void onEnable() {
// set stuff
plugin = this;
worldsPath = this.getDataFolder() + "/worlds";
mapsPath = this.getDataFolder() + "/maps";
kitsPath = this.getDataFolder() + "/kits";
schematicsPath = this.getDataFolder() + "/schematics";
playersPath = this.getDataFolder() + "/players";
chestsPath = this.getDataFolder() + "/chests";
this.packageName = this.getServer().getClass().getPackage().getName();
this.serverPackageVersion = this.packageName.substring(this.packageName.lastIndexOf('.') + 1);
this.sendDebugMessage("&bServer version: &e%s (&a%s&e)", this.packageName, this.serverPackageVersion);
final File worldsToDeleteFile = new File(this.getDataFolder(), "delete_worlds.yml");
if (worldsToDeleteFile.exists()) {
final YamlConfiguration deleteWorldsConfig = YamlConfiguration.loadConfiguration(worldsToDeleteFile);
final List<String> list = deleteWorldsConfig.getStringList("worlds");
for (final String worldName : new ArrayList<String>(list)) {
final World world = Bukkit.getWorld(worldName);
if (world != null) {
for (final Player p : world.getPlayers())
SkywarsUtils.teleportPlayerBackToTheLobbyOrToTheirLastLocationIfTheLobbyIsNotSet(p, true);
Bukkit.unloadWorld(worldName, false);
}
final File worldFolder = new File(Bukkit.getWorldContainer(), worldName);
if (worldFolder.exists() && worldFolder.isDirectory())
try {
FileUtils.deleteDirectory(worldFolder);
} catch (final IOException e) {
e.printStackTrace();
Skywars.get().sendMessage("&cCould not delete world folder: &b" + worldFolder.getPath());
}
list.remove(worldName);
}
deleteWorldsConfig.set("worlds", list);
try {
deleteWorldsConfig.save(worldsToDeleteFile);
} catch (final IOException e) {
e.printStackTrace();
this.sendMessage("Could not save the world deletion list to file: " + worldsToDeleteFile.getPath());
}
}
// load stuff
if (!this.loadConfig()) {
this.sendMessage("Could not load configuration files! Disabling plugin.");
this.setEnabled(false);
return;
}
if (!config.getBoolean("disableUpdates") && SkywarsUpdater.update(config.getBoolean("autoUpdate"))) {
this.updated = true;
this.setEnabled(false);
return;
}
this.loadCommands();
this.loadEvents();
this.mapManager.loadMaps();
this.mapManager.loadWorlds();
this.chestManager.loadChests();
this.signManager.loadSigns();
this.loadKits();
this.nmsHandler = new ReflectionNMS();
SchematicHandler.initializeReflection();
this.sendMessage("&b&l--- PLUGIN HOOKS ---");
if (Bukkit.getPluginManager().isPluginEnabled("DecentHolograms")) {
this.hologramController = new DecentHologramsController();
} else if (Bukkit.getPluginManager().isPluginEnabled("HolographicDisplays")) {
if (SkywarsUtils.checkClass("com.gmail.filoghost.holographicdisplays.api.HologramsAPI"))
this.hologramController = new HolographicDisplaysOldController();
else if (SkywarsUtils.checkClass("me.filoghost.holographicdisplays.api.HolographicDisplaysAPI"))
this.hologramController = new HolographicDisplaysNewController();
}
if (this.hologramController == null) {
this.hologramController = new HologramController() {
@Override
public void removeHologram(Object id) {
}
@Override
public String createHologram(Object id, Location location, String text) {
return null;
}
@Override
public boolean changeHologram(Object id, String text, int line) {
return false;
}
};
this.sendMessage("&eHolograms: &cno supported holograms plugin found!");
} else {
holograms = true;
this.sendMessage("&eHolograms: &a" + this.hologramController.getClass().getSimpleName());
}
economyEnabled = Skywars.get().getConfig().getBoolean("economy.enabled");
if (economyEnabled)
try {
if (this.setupEconomy()) {
this.sendMessage("&eEconomy (Vault): &a" + this.economyProvider.getPlugin().getName());
}
} catch (final Exception e) {
this.sendMessage("&eEconomy (Vault): &ccould not hook.");
e.printStackTrace();
}
else
this.sendMessage("&eEconomy (Vault): &6disabled in config.");
// placeholder api
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
placeholders = true;
}
this.sendMessage("&ePlaceholderAPI: " + (placeholders ? "&ahooked." : "&cnot found."));
// done
this.sendMessage("&ahas been enabled: &bv%s", this.version);
if (placeholders)
new SkywarsPlaceholderExpansion().register();
if (!Skywars.get().getConfig().getBoolean("taskUpdate.disabled"))
Bukkit.getScheduler().runTaskTimer(Skywars.get(), new Runnable() {
@Override
public void run() {
for (final Player player : Bukkit.getOnlinePlayers()) {
SkywarsScoreboard.update(player);
SkywarsActionbar.update(player);
SkywarsTablist.update(player);
}
}
}, 0L, Skywars.get().getConfig().getLong("taskUpdate.interval") * 20);
}
@Override
public void onDisable() {
if (this.updated) {
this.sendMessage("&6The plugin has been updated and disabled.");
return;
}
for (final Player player : Bukkit.getOnlinePlayers()) {
final Arena arena = this.getPlayerArena(player);
if (arena != null) {
arena.exitPlayer(player);
}
}
final ItemStack item = SetupEvents.item;
ConfigMenu.currentArenas.forEach((player, arena) -> {
if (item == null)
return;
player.getInventory().removeItem(item);
SkywarsUtils.teleportPlayerBackToTheLobbyOrToTheirLastLocationIfTheLobbyIsNotSet(player);
});
this.sendDebugMessage("Stopping arenas...");
// add world names to a file
// to try to delete those worlds
// the next time the plugin starts up
final List<String> worldNames = new ArrayList<String>(this.arenas.size());
for (final Arena arena : this.arenas) {
arena.clear(false);
worldNames.add(arena.getWorldName());
}
final File file = new File(this.getDataFolder(), "delete_worlds.yml");
final YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
worldNames.addAll(config.getStringList("worlds"));
config.set("worlds", worldNames);
try {
if (!file.exists())
file.createNewFile();
config.save(file);
} catch (final IOException e) {
e.printStackTrace();
this.sendMessage("Could not write world list to file.");
}
this.arenas.clear();
}
public ReflectionNMS NMS() {
return this.nmsHandler;
}
public String getServerPackageVersion() {
return this.serverPackageVersion;
}
private boolean setupEconomy() {
if (!economyEnabled)
return false;
if (this.getServer().getPluginManager().getPlugin("Vault") == null) {
this.sendMessage("&eEconomy (Vault): &6plugin not found.");
return false;
}
this.economyProvider = this.getServer().getServicesManager().getRegistration(Economy.class);
if (this.economyProvider == null) {
this.sendMessage("&eEconomy (Vault): &6plugin found &cbut no registered service provider!");
return false;
}
this.economy = this.economyProvider.getProvider();
return this.economy != null;
}
public void loadEvents() {
final FileConfiguration config = this.getConfig();
final PluginManager pluginManager = this.getServer().getPluginManager();
if (config.getBoolean("signsEnabled")) {
this.signManager = new SignManager();
pluginManager.registerEvents(this.signManager, this);
}
if (config.getBoolean("messageSounds.enabled")) {
pluginManager.registerEvents(new MessageSound(), this);
}
if (config.getBoolean("disableWeather")) {
pluginManager.registerEvents(new DisableWeather(), this);
}
if (config.getBoolean("debug.projectileTests")) {
pluginManager.registerEvents(new ProjectileTrails(), this);
}
final Listener[] listeners = { new InteractEvent(), new Events(), new GamesMenu(), new MapMenu(),
new KitsMenu(), new SetupEvents(), new ConfigMenu(), new GameOptionsMenu(),
new PlayerInventoryManager(), };
for (final Listener listener : listeners) {
pluginManager.registerEvents(listener, this);
}
}
public void loadCommands() {
final HashMap<String, CommandExecutor> cmds = new HashMap<>();
cmds.put("skywars", new MainCommand());
cmds.put("where", new WhereCommand());
cmds.put("start", new StartCommand());
cmds.put("forcestart", new ForceStartCommand());
cmds.put("leave", new LeaveCommand());
for (final String cmd : cmds.keySet()) {
if (!this.getConfig().getStringList("disabledCommands").contains(cmd)) {
this.sendDebugMessage("&eLoading command &a%s&e...", cmd);
this.getCommand(cmd).setExecutor(cmds.get(cmd));
} else
this.sendDebugMessage("&7Skipping command &c%s&e...", cmd);
}
}
ChestManager chestManager = new ChestManager();
public ChestManager getChestManager() {
return this.chestManager;
}
MapManager mapManager = new MapManager();
public MapManager getMapManager() {
return this.mapManager;
}
// kits
private final List<Kit> kits = new ArrayList<Kit>();
public List<Kit> getKits() {
return this.kits;
}
public void loadKits() {
this.kits.clear();
final File folder = new File(kitsPath);
if (!folder.exists()) {
folder.mkdirs();
}
if (folder.listFiles().length <= 0) {
this.sendDebugMessage("&eSetting up default kit.");
ConfigurationUtils.copyDefaultContentsToFile("kits/default.yml", new File(kitsPath, "default.yml"));
}
for (final File file : folder.listFiles()) {
final String name = file.getName().replaceFirst("[.][^.]+$", "");
final YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
ConfigurationUtils.createMissingKeys(config, ConfigurationUtils.getDefaultConfig("kits/default.yml"),
file.getPath());
// create kit and set values from config
final Kit kit = new Kit(name);
kit.setConfig(config);
kit.setFile(file);
kit.setDisplayName(config.getString("name"));
// load items
final List<ItemStack> items = new ArrayList<ItemStack>();
for (final Object a : config.getList("items")) {
final ItemStack item = ConfigurationUtils.parseItemFromConfig(a);
if (item == null)
continue;
items.add(item);
}
kit.setItems(items.toArray(new ItemStack[0]));
String iconItem = config.getString("icon");
if (iconItem == null)
iconItem = "BEDROCK";
kit.setIcon(XMaterial.valueOf(iconItem).parseItem());
kit.setPrice(config.getDouble("price", 0));
// add kit to the arena list
this.kits.add(kit);
this.sendDebugMessage("&eLoaded kit: &a%s", kit.getName());
}
this.sendDebugMessage("Finished loading kits.");
}
public static void createBigCase(Location location, XMaterial material) {
final int[][] blocks = {
// base
{ -1, -1, -1 }, { 0, -1, -1 }, { 1, -1, -1 }, { -1, -1, 0 }, { 0, -1, 0 }, { 1, -1, 0 }, { -1, -1, 1 },
{ 0, -1, 1 }, { 1, -1, 1 },
// top
{ -1, 3, -1 }, { 0, 3, -1 }, { 1, 3, -1 }, { -1, 3, 0 }, { 0, 3, 0 }, { 1, 3, 0 }, { -1, 3, 1 },
{ 0, 3, 1 }, { 1, 3, 1 },
// left wall
{ 2, 0, -1 }, { 2, 0, 0 }, { 2, 0, 1 }, { 2, 1, -1 }, { 2, 1, 0 }, { 2, 1, 1 }, { 2, 2, -1 },
{ 2, 2, 0 }, { 2, 2, 1 },
// front wall
{ -1, 0, 2 }, { 0, 0, 2 }, { 1, 0, 2 }, { -1, 1, 2 }, { 0, 1, 2 }, { 1, 1, 2 }, { -1, 2, 2 },
{ 0, 2, 2 }, { 1, 2, 2 },
// right wall
{ -2, 0, -1 }, { -2, 0, 0 }, { -2, 0, 1 }, { -2, 1, -1 }, { -2, 1, 0 }, { -2, 1, 1 }, { -2, 2, -1 },
{ -2, 2, 0 }, { -2, 2, 1 },
// back wall
{ -1, 0, -2 }, { 0, 0, -2 }, { 1, 0, -2 }, { -1, 1, -2 }, { 0, 1, -2 }, { 1, 1, -2 }, { -1, 2, -2 },
{ 0, 2, -2 }, { 1, 2, -2 }, };
final int[][] airBlocks = { { -1, 0, -1 }, { 0, 0, -1 }, { 1, 0, -1 }, { -1, 0, 0 }, { 0, 0, 0 }, { 1, 0, 0 },
{ -1, 0, 1 }, { 0, 0, 1 }, { 1, 0, 1 },
{ -1, 1, -1 }, { 0, 1, -1 }, { 1, 1, -1 }, { -1, 1, 0 }, { 0, 1, 0 }, { 1, 1, 0 }, { -1, 1, 1 },
{ 0, 1, 1 }, { 1, 1, 1 },
{ -1, 2, -1 }, { 0, 2, -1 }, { 1, 2, -1 }, { -1, 2, 0 }, { 0, 2, 0 }, { 1, 2, 0 }, { -1, 2, 1 },
{ 0, 2, 1 }, { 1, 2, 1 }, };
for (final int[] relative : airBlocks) {
final Block block = location.getBlock().getRelative(relative[0], relative[1], relative[2]);
block.setType(XMaterial.AIR.parseMaterial());
}
for (final int[] relative : blocks) {
final Block block = location.getBlock().getRelative(relative[0], relative[1], relative[2]);
block.setType(material.parseMaterial());
if (!XMaterial.isNewVersion()) {
try {
block.getClass().getMethod("setData", byte.class).invoke(block, material.getData());
} catch (final Exception e) {
e.printStackTrace();
}
}
}
}
public static void createCase(Location location, XMaterial material) {
if (config.getBoolean("debug.bigCases")) {
createBigCase(location, material);
return;
}
final int[][] blocks = {
// first layer
{ -1, 0, 0 }, { 1, 0, 0 }, { 0, 0, -1 }, { 0, 0, 1 },
// second layer
{ -1, 1, 0 }, { 1, 1, 0 }, { 0, 1, -1 }, { 0, 1, 1 },
// third layer
{ -1, 2, 0 }, { 1, 2, 0 }, { 0, 2, -1 }, { 0, 2, 1 },
// base and top
{ 0, -1, 0 }, { 0, 3, 0 },
// base joints
{ -1, -1, 0 }, { 1, -1, 0 }, { 0, -1, -1 }, { 0, -1, 1 },
// top joints
{ -1, 3, 0 }, { 1, 3, 0 }, { 0, 3, -1 }, { 0, 3, 1 }, };
final int[][] airBlocks = { { 0, 0, 0 }, { 0, 1, 0 }, { 0, 2, 0 } };
for (final int[] relative : airBlocks) {
final Block block = location.getBlock().getRelative(relative[0], relative[1], relative[2]);
block.setType(XMaterial.AIR.parseMaterial());
}
for (final int[] relative : blocks) {
final Block block = location.getBlock().getRelative(relative[0], relative[1], relative[2]);
block.setType(material.parseMaterial());
if (!XMaterial.isNewVersion()) {
try {
block.getClass().getMethod("setData", byte.class).invoke(block, material.getData());
} catch (final Exception e) {
e.printStackTrace();
}
}
}
}
// arenas
private final ArrayList<Arena> arenas = new ArrayList<>();
public List<Arena> getArenas() {
return this.arenas;
}
public Arena getRandomJoinableArena() {
for (final Arena arena : this.arenas) {
if (!arena.isJoinable())
continue;
return arena;
}
return null;
}
public SkywarsUser getSkywarsUser(Player player) {
for (int i = 0; i < this.arenas.size(); i++) {
final List<SkywarsUser> players = this.arenas.get(i).getUsers();
for (int j = 0; j < players.size(); j++) {
if (players.get(j).getPlayer().equals(player)) {
return players.get(j);
}
}
}
return null;
}
// TODO make this a cached hashmap
public Arena getPlayerArena(Player player) {
for (int i = 0; i < this.arenas.size(); i++) {
final List<SkywarsUser> players = this.arenas.get(i).getUsers();
for (int j = 0; j < players.size(); j++) {
if (players.get(j).getPlayer().equals(player)) {
return this.arenas.get(i);
}
}
}
return null;
}
public File getSchematicFile(String schematicName) {
final File schematic = new File(schematicsPath, schematicName);
return schematic;
}
// player config
public File getPlayerConfigFile(OfflinePlayer player) {
return new File(playersPath, player.getUniqueId() + ".yml");
}
public YamlConfiguration getPlayerConfig(OfflinePlayer player) {
final File folder = new File(playersPath);
if (!folder.exists())
folder.mkdir();
final File file = this.getPlayerConfigFile(player);
if (!file.exists())
ConfigurationUtils.copyDefaultContentsToFile("players/default.yml", file);
return YamlConfiguration.loadConfiguration(file);
}
public void savePlayerConfig(OfflinePlayer player, YamlConfiguration config) {
try {
final File file = this.getPlayerConfigFile(player);
config.save(file);
ConfigurationUtils.createMissingKeys(config, ConfigurationUtils.getDefaultConfig("players/default.yml"),
file.getPath());
} catch (final IOException e) {
e.printStackTrace();
}
}
// kits
public Kit getKit(String name) {
for (int i = 0; i < this.kits.size(); i++) {
if (this.kits.get(i).getName().equalsIgnoreCase(name)
|| this.kits.get(i).getDisplayName().equalsIgnoreCase(name)) {
return this.kits.get(i);
}
}
return null;
}
public void setPlayerKit(OfflinePlayer player, Kit kit) {
final YamlConfiguration conf = this.getPlayerConfig(player);
conf.set("kit", kit.getName());
this.savePlayerConfig(player, conf);
}
public Kit getPlayerKit(OfflinePlayer player) {
final String kitName = this.getPlayerConfig(player).getString("kit");
if (kitName == null)
return null;
return this.getKit(kitName);
}
// cases
public void setPlayerCaseMaterial(Player player, String name) {
final YamlConfiguration conf = this.getPlayerConfig(player);
conf.set("case", name);
this.savePlayerConfig(player, conf);
}
public XMaterial getPlayerCaseXMaterial(Player player) {
final String glass = this.getPlayerConfig(player).getString("case");
if (glass == null)
return configuration.defaultCaseMaterial;
final XMaterial mat = XMaterial.valueOf(glass);
if (mat == null)
return configuration.defaultCaseMaterial;
return mat;
}
// kills
public int getPlayerTotalKills(OfflinePlayer player) {
return this.getPlayerConfig(player).getInt("stats.solo.kills");
}
public void setPlayerTotalKills(OfflinePlayer player, int kills) {
final YamlConfiguration config = this.getPlayerConfig(player);
config.set("stats.solo.kills", kills);
this.savePlayerConfig(player, config);
}
public void incrementPlayerTotalKills(OfflinePlayer player) {
this.setPlayerTotalKills(player, this.getPlayerTotalKills(player) + 1);
}
// souls
public int getPlayerSouls(OfflinePlayer player) {
return this.getPlayerConfig(player).getInt("souls");
}
public void setPlayerSouls(OfflinePlayer player, int kills) {
final YamlConfiguration config = this.getPlayerConfig(player);
config.set("souls", kills);
this.savePlayerConfig(player, config);
}
public void incrementPlayerSouls(OfflinePlayer player) {
this.setPlayerSouls(player, this.getPlayerSouls(player) + 1);
}
// deaths
public int getPlayerTotalDeaths(OfflinePlayer player) {
return this.getPlayerConfig(player).getInt("stats.solo.deaths");
}
public void setPlayerTotalDeaths(OfflinePlayer player, int deaths) {
final YamlConfiguration config = this.getPlayerConfig(player);
config.set("stats.solo.deaths", deaths);
this.savePlayerConfig(player, config);
}
public void incrementPlayerTotalDeaths(OfflinePlayer player) {
this.setPlayerTotalDeaths(player, this.getPlayerTotalDeaths(player) + 1);
}
// wins
public int getPlayerTotalWins(OfflinePlayer player) {
return this.getPlayerConfig(player).getInt("stats.solo.wins");
}
public void setPlayerTotalWins(OfflinePlayer player, int wins) {
final YamlConfiguration config = this.getPlayerConfig(player);
config.set("stats.solo.wins", wins);
this.savePlayerConfig(player, config);
}
public void incrementPlayerTotalWins(OfflinePlayer player) {
this.setPlayerTotalWins(player, this.getPlayerTotalWins(player) + 1);
}
//
public boolean loadConfig() {
this.sendDebugMessage("Loading configuration...");
config = ConfigurationUtils.loadConfiguration("config.yml", "config.yml");
configuration = new SkywarsConfiguration();
configuration.load(config);
final String lang = Skywars.get().getConfig().getString("locale");
langConfig = ConfigurationUtils.loadConfiguration("lang/" + lang + ".yml", "lang/" + lang + ".yml",
"lang/en.yml");
this.sendDebugMessage("Loaded locale: " + lang + " - " + langConfig.getString("language_name"));
lobbyConfig = ConfigurationUtils.loadConfiguration("lobby.yml", "lobby.yml");
if (config == null || langConfig == null || lobbyConfig == null)
return false;
// load lobby
this.setLobbyFromConfig();
this.sendDebugMessage("Finished loading configuration.");
return true;
}
public void sendDebugMessage(String text, Object... format) {
if (Skywars.config != null && !Skywars.config.getBoolean("debug.enabled"))
return;
this.sendMessageWithPrefix(this.debugPrefix, text, format);
}
public void sendDebugMessageWithPrefix(String prefix, String text, Object... format) {
if (Skywars.config != null && !Skywars.config.getBoolean("debug.enabled"))
return;
this.sendMessageWithPrefix(prefix, text, format);
}
public void sendMessage(String text, Object... format) {
this.sendMessageWithPrefix(Messager.color(this.prefix), Messager.color(text, format));
}
public void sendMessageWithPrefix(String prefix, String text, Object... format) {
Bukkit.getConsoleSender().sendMessage(Messager.color(prefix) + " " + Messager.color(text, format));
}
public HologramController getHologramController() {
return this.hologramController;
}
}