-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathGuildHandler.java
More file actions
815 lines (729 loc) · 28.8 KB
/
GuildHandler.java
File metadata and controls
815 lines (729 loc) · 28.8 KB
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
/*
* MIT License
*
* Copyright (c) 2019 Glare
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.glaremasters.guilds.guild;
import ch.jalu.configme.SettingsManager;
import co.aikar.commands.ACFBukkitUtil;
import co.aikar.commands.ACFUtil;
import co.aikar.commands.CommandManager;
import lombok.Getter;
import me.glaremasters.guilds.Guilds;
import me.glaremasters.guilds.configuration.sections.GuildSettings;
import me.glaremasters.guilds.configuration.sections.GuildVaultSettings;
import me.glaremasters.guilds.configuration.sections.TicketSettings;
import me.glaremasters.guilds.database.DatabaseProvider;
import me.glaremasters.guilds.exceptions.ExpectationNotMet;
import me.glaremasters.guilds.messages.Messages;
import me.glaremasters.guilds.utils.ItemBuilder;
import me.glaremasters.guilds.utils.Serialization;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
public class GuildHandler {
@Getter private List<Guild> guilds;
private final List<GuildRole> roles;
private final List<GuildTier> tiers;
@Getter private final List<Player> spies;
@Getter private final List<Player> guildChat;
@Getter private Map<Guild, List<Inventory>> cachedVaults;
@Getter private List<Player> openedVault;
private final DatabaseProvider databaseProvider;
private final CommandManager commandManager;
private final Permission permission;
private final SettingsManager settingsManager;
//as well as guild permissions from tiers using permission field and tiers list.
public GuildHandler(DatabaseProvider databaseProvider, CommandManager commandManager, Permission permission, FileConfiguration config, SettingsManager settingsManager) {
this.databaseProvider = databaseProvider;
this.commandManager = commandManager;
this.permission = permission;
this.settingsManager = settingsManager;
roles = new ArrayList<>();
tiers = new ArrayList<>();
spies = new ArrayList<>();
guildChat = new ArrayList<>();
cachedVaults = new HashMap<>();
openedVault = new ArrayList<>();
//GuildRoles objects
ConfigurationSection roleSection = config.getConfigurationSection("roles");
for (String s : roleSection.getKeys(false)) {
String path = s + ".permissions.";
roles.add(GuildRole.builder().name(roleSection.getString(s + ".name"))
.node(roleSection.getString(s + ".permission-node"))
.level(Integer.parseInt(s))
.chat(roleSection.getBoolean(path + "chat"))
.allyChat(roleSection.getBoolean(path + "ally-chat"))
.invite(roleSection.getBoolean(path + "invite"))
.kick(roleSection.getBoolean(path + "kick"))
.promote(roleSection.getBoolean(path + "promote"))
.demote(roleSection.getBoolean(path + "demote"))
.addAlly(roleSection.getBoolean(path + "add-ally"))
.removeAlly(roleSection.getBoolean(path + "remove-ally"))
.changePrefix(roleSection.getBoolean(path + "change-prefix"))
.changeName(roleSection.getBoolean(path + "rename"))
.changeHome(roleSection.getBoolean(path + "change-home"))
.removeGuild(roleSection.getBoolean(path + "remove-guild"))
.changeStatus(roleSection.getBoolean(path + "toggle-guild"))
.openVault(roleSection.getBoolean(path + "open-vault"))
.transferGuild(roleSection.getBoolean(path + "transfer-guild"))
.activateBuff(roleSection.getBoolean(path + "activate-buff"))
.upgradeGuild(roleSection.getBoolean(path + "upgrade-guild"))
.depositMoney(roleSection.getBoolean(path + "deposit-money"))
.withdrawMoney(roleSection.getBoolean(path + "withdraw-money"))
.claimLand(roleSection.getBoolean(path + "claim-land"))
.unclaimLand(roleSection.getBoolean(path + "unclaim-land"))
.destroy(roleSection.getBoolean(path + "destroy"))
.place(roleSection.getBoolean(path + "place"))
.interact(roleSection.getBoolean(path + "interact"))
.createCode(roleSection.getBoolean(path + "create-code"))
.deleteCode(roleSection.getBoolean(path + "delete-code"))
.seeCodeRedeemers(roleSection.getBoolean(path + "see-code-redeemers"))
.modifyMotd(roleSection.getBoolean(path + "modify-motd"))
.build());
}
//GuildTier objects
ConfigurationSection tierSection = config.getConfigurationSection("tiers.list");
for (String key : tierSection.getKeys(false)) {
tiers.add(GuildTier.builder()
.level(tierSection.getInt(key + ".level"))
.name(tierSection.getString(key + ".name"))
.cost(tierSection.getDouble(key + ".cost"))
.maxMembers(tierSection.getInt(key + ".max-members"))
.vaultAmount(tierSection.getInt(key + ".vault-amount"))
.mobXpMultiplier(tierSection.getDouble(key + ".mob-xp-multiplier"))
.damageMultiplier(tierSection.getDouble(key + ".damage-multiplier"))
.maxBankBalance(tierSection.getDouble(key + ".max-bank-balance"))
.membersToRankup(tierSection.getInt(key + ".members-to-rankup"))
.useBuffs(tierSection.getBoolean(key + ".use-buffs"))
.permissions(tierSection.getStringList(key + ".permissions"))
.build());
}
Guilds.newChain().async(() -> {
try {
guilds = databaseProvider.loadGuilds();
} catch (IOException e) {
e.printStackTrace();
}
}).sync(() -> guilds.forEach(this::createVaultCache)).sync(() -> guilds.forEach(g -> {
g.setTier(getGuildTier(g.getTier().getLevel()));
g.getMembers().forEach(m -> m.setRole(getGuildRole(m.getRole().getLevel())));
})).execute();
}
/**
* Saves the data of guilds
*/
public void saveData() throws IOException {
guilds.forEach(this::saveVaultCache);
databaseProvider.saveGuilds(guilds);
}
/**
* This method is used to add a Guild to the list
*
* @param guild the guild being added
*/
public void addGuild(@NotNull Guild guild) {
guilds.add(guild);
createVaultCache(guild);
}
/**
* This method is used to remove a Guild from the list
*
* @param guild the guild being removed
*/
public void removeGuild(@NotNull Guild guild) {
cachedVaults.remove(guild);
guilds.remove(guild);
}
/**
* Retrieve a guild by it's name
*
* @return the guild object with given name
*/
public Guild getGuild(@NotNull String name) {
return guilds.stream().filter(guild -> guild.getName().equals(name)).findFirst().orElse(null);
}
/**
* Retrieve a guild by a player
*
* @return the guild object by player
*/
public Guild getGuild(@NotNull OfflinePlayer p) {
return guilds.stream().filter(guild -> guild.getMember(p.getUniqueId()) != null).findFirst().orElse(null);
}
/**
* Gets a guild by it's uuid
* @param uuid the input
* @return the output
*/
public Guild getGuild(@NotNull UUID uuid) {
return guilds.stream().filter(guild -> guild.getId().equals(uuid)).findFirst().orElse(null);
}
/**
* Check the guild based on the invite code
*
* @param code the invite code being used
* @return the guild who the code belong to
*/
public Guild getGuildByCode(@NotNull String code) {
return guilds.stream().filter(guild -> guild.hasInviteCode(code)).findFirst().orElse(null);
}
/**
* Get a guild's name by it's ID
* @param uuid the input ID
* @return the output guild
*/
public String getNameById(@NotNull UUID uuid) {
return getGuild(uuid).getName();
}
/**
* Retrieve a guild tier by name
*
* @param name the name of the tier
* @return the tier object if found
*/
public GuildTier getGuildTier(String name) {
return tiers.stream().filter(tier -> tier.getName().equals(name)).findFirst().orElse(null);
}
/**
* Retrieve a guild tier by level
*
* @param level the level of the tier
* @return the tier object if found
*/
public GuildTier getGuildTier(int level) {
return tiers.stream().filter(tier -> tier.getLevel() == level).findFirst().orElse(null);
}
/**
* Retrieve a guild role by level
*
* @param level the level of the role
* @return the role object if found
*/
public GuildRole getGuildRole(int level) {
return roles.stream().filter(guildRole -> guildRole.getLevel() == level).findFirst().orElse(null);
}
/**
* Adds an ally to both guilds
*
* @param guild the guild to ally
* @param targetGuild the other guild to ally
*/
public void addAlly(Guild guild, Guild targetGuild) {
guild.addAlly(targetGuild);
targetGuild.addAlly(guild);
removePendingAlly(guild, targetGuild);
}
/**
* Simple method to check if two guilds are allies
* @param guild the first guild
* @param target the second guild
* @return allies or not
*/
public boolean isAlly(Guild guild, Guild target) {
return guild.getAllies().contains(target.getId());
}
/**
* Check if players are allies
* @param player the player
* @param target the target
* @return allies or not
*/
public boolean isAlly(Player player, Player target) {
Guild pGuild = getGuild(player);
Guild tGuild = getGuild(target);
if (pGuild == null || tGuild == null)
return false;
return pGuild.getAllies().contains(tGuild.getId());
}
/**
* Simple method to check if players are in the same guild
* @param player the player being checked
* @param target the target being checked
* @return if same guild or not
*/
public boolean isSameGuild(Player player, Player target) {
Guild pGuild = getGuild(player);
Guild tGuild = getGuild(target);
if (pGuild == null || tGuild == null)
return false;
return pGuild.getId().toString().equals(tGuild.getId().toString());
}
/**
* Removes an ally.
*
* @param guild the guild to remove as ally
* @param targetGuild the guild to remove as ally
*/
public void removeAlly(Guild guild, Guild targetGuild) {
guild.removeAlly(targetGuild);
targetGuild.removeAlly(guild);
}
/**
* Adds a pending ally
*
* @param guild the first pending guild
* @param targetGuild the second pending guild
*/
public void addPendingAlly(Guild guild, Guild targetGuild) {
guild.addPendingAlly(targetGuild);
targetGuild.addPendingAlly(guild);
}
/**
* Removes a pending ally
*
* @param guild the first pending guild
* @param targetGuild the second pending guild
*/
public void removePendingAlly(Guild guild, Guild targetGuild) {
guild.removePendingAlly(targetGuild);
targetGuild.removePendingAlly(guild);
}
/**
* Returns the amount of guilds existing
*
* @return an integer of size.
*/
public int getGuildsSize() {
return guilds.size();
}
/**
* Returns the max tier level
*
* @return the max tier level
*/
public int getMaxTierLevel() {
return tiers.size();
}
/**
* Simple method to check if guild is max tier
* @param guild the guild to check
* @return if they are max or not
*/
public boolean isMaxTier(Guild guild) {
return guild.getTier().getLevel() >= getMaxTierLevel();
}
/**
* Returns the lowest guild role
*
* @return guild role
*/
public GuildRole getLowestGuildRole() {
return roles.get(roles.size() - 1);
}
/**
* Upgrades the tier of a guild
*
* @param guild the guild upgrading
*/
public void upgradeTier(Guild guild) {
guild.setTier(getGuildTier(guild.getTier().getLevel() + 1));
}
/**
* Returns a string list of all the guilds that a member is invited to
*
* @param uuid the uuid of the member
* @return a string list of guilds's names.
*/
public List<String> getInvitedGuilds(UUID uuid) {
return guilds.stream().filter(guild -> guild.getInvitedMembers().contains(uuid)).map(Guild::getName).collect(Collectors.toList());
}
/**
* Returns a string list of the name of all guilds on the server
*
* @return a string list of guild names
*/
public List<String> getGuildNames() {
return guilds.stream().map(Guild::getName).collect(Collectors.toList());
}
/**
* Create the cache of a vault for the guild
*
* @param guild the guild being cached
*/
private void createVaultCache(Guild guild) {
List<Inventory> vaults = new ArrayList<>();
if (guild.getVaults() == null) return;
guild.getVaults().forEach(v -> {
try {
vaults.add(Serialization.deserializeInventory(v, settingsManager));
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
});
cachedVaults.put(guild, vaults);
}
/**
* Save the vaults of a guild
*
* @param guild the guild being saved
*/
private void saveVaultCache(Guild guild) {
List<String> vaults = new ArrayList<>();
if (guild.getVaults() == null) return;
cachedVaults.get(guild).forEach(v -> vaults.add(Serialization.serializeInventory(v)));
guild.setVaults(vaults);
}
/**
* Open a guild vault
*
* @param guild the owner of the vault
* @param vault which vault to open
* @return the inventory to open
*/
public Inventory getGuildVault(Guild guild, int vault) {
return cachedVaults.get(guild).get(vault - 1);
}
/**
* Check if player is a spy
*
* @param player the player being checked
* @return if they are a spy
*/
private boolean isSpy(Player player) {
return spies.contains(player);
}
/**
* Add a player to the list of spies
*
* @param player player being added
*/
private void addSpy(CommandManager manager, Player player) {
spies.add(player);
manager.getCommandIssuer(player).sendInfo(Messages.ADMIN__SPY_ON);
}
/**
* Remove a player from the list of spies
*
* @param player player being removed
*/
public void removeSpy(CommandManager manager, Player player) {
spies.remove(player);
manager.getCommandIssuer(player).sendInfo(Messages.ADMIN__SPY_OFF);
}
/**
* This method handles combining all the spy methods together to make a simple, clean method.
*
* @param player the player being modified
*/
public void toggleSpy(CommandManager manager, Player player) {
if (isSpy(player)) {
removeSpy(manager, player);
} else {
addSpy(manager, player);
}
}
/**
* Check if a player is in guild chat mode or not
*
* @param player the player being checked
* @return if they are in the mode or not
*/
public boolean checkGuildChat(Player player) {
return guildChat.contains(player);
}
/**
* Add a player to guild chat mode
*
* @param player the player being checked
*/
private void addGuildChat(CommandManager manager, Player player) {
guildChat.add(player);
manager.getCommandIssuer(player).sendInfo(Messages.CHAT__ENABLED);
}
/**
* Remove a player from guild chat mode
*
* @param player the player being checked
*/
public void removeGuildChat(CommandManager manager, Player player) {
guildChat.remove(player);
manager.getCommandIssuer(player).sendInfo(Messages.CHAT__DISABLED);
}
/**
* Handler for taking players in and out of guild chat
*
* @param player the player being toggled
*/
public void toggleGuildChat(CommandManager manager, Player player) {
if (checkGuildChat(player)) {
removeGuildChat(manager, player);
} else {
addGuildChat(manager, player);
}
}
/**
* This method is ran when a player logs out to ensure they aren't in the list.
* @param player player being removed
*/
public void chatLogout(Player player) {
spies.remove(player);
guildChat.remove(player);
}
/**
* Clear both lists
*/
public void chatLogout() {
spies.clear();
guildChat.clear();
}
/**
* Simple method to check a player has any invites
* @param manager the command manager
* @param player the player being checked
*/
public void checkInvites(CommandManager manager, Player player) {
List<String> list = getInvitedGuilds(player.getUniqueId());
if (list.isEmpty()) {
manager.getCommandIssuer(player).sendInfo(Messages.ERROR__NO_GUILD);
return;
}
manager.getCommandIssuer(player).sendInfo(Messages.PENDING__INVITES, "{number}", String.valueOf(list.size()), "{guilds}", String.join(",", list));
}
/**
* Basically check if they can upgrade with the member check
* @param guild the guild being checked
* @return if they pass the check or not
*/
public boolean memberCheck(Guild guild) {
GuildTier tier = guild.getTier();
return tier.getMembersToRankup() != 0 && guild.getMembers().size() < tier.getMembersToRankup();
}
/**
* Check in a input name for the guild is proper
* @param name the name input
* @param settingsManager setting manager
* @return valid or not
*/
public boolean nameCheck(String name, SettingsManager settingsManager) {
String regex = settingsManager.getProperty(GuildSettings.NAME_REQUIREMENTS);
return name.matches(regex);
}
/**
* Simple method to check in a prefix is valid or not
* @param name the prefix
* @param settingsManager setting manager
* @return valid or not
*/
public boolean prefixCheck(String name, SettingsManager settingsManager) {
String regex = settingsManager.getProperty(GuildSettings.PREFIX_REQUIREMENTS);
return name.matches(regex);
}
/**
* Check if a word is in the blacklist or not
* @param name name to check
* @param settingsManager settings manager
* @return blacklisted or not
*/
public boolean blacklistCheck(String name, SettingsManager settingsManager) {
return settingsManager.getProperty(GuildSettings.BLACKLIST_WORDS).stream().anyMatch(s -> s.toLowerCase().contains(name));
}
/**
* Check if a guild has a specific vault unlocked
* @param vault the vault being opened
* @param guild the guild opening the vault
* @return if they can open it or not
*/
public boolean hasVaultUnlocked(int vault, Guild guild) {
return vault <= getGuildTier(guild.getTier().getLevel()).getVaultAmount();
}
/**
* Method to create new vault
* @param settingsManager settings manager
* @return new vault
*/
public Inventory createNewVault(SettingsManager settingsManager) {
return Bukkit.createInventory(null, (9*settingsManager.getProperty(GuildVaultSettings.VAULT_ROWS)), ACFBukkitUtil.color(settingsManager.getProperty(GuildVaultSettings.VAULT_NAME)));
}
/**
* Get a list of the online members that can invite people
* @param guild the guild to check
* @return list of online members
*/
public List<Player> getOnlineInviters(Guild guild) {
List<GuildMember> members = guild.getOnlineMembers().stream().filter(m -> m.getRole().isInvite()).collect(Collectors.toList());
return members.stream().map(m -> Bukkit.getPlayer(m.getUuid())).collect(Collectors.toList());
}
/**
* Simple method to inform all online inviters that someone wants to join
* @param guild guild to be requested
* @param commandManager command manager
* @param player player requesting
*/
public void pingOnlineInviters(Guild guild, CommandManager commandManager, Player player) {
getOnlineInviters(guild).forEach(m -> commandManager.getCommandIssuer(m).sendInfo(Messages.REQUEST__INCOMING_REQUEST, "{player}", player.getName()));
}
/**
* Create a guild upgrade ticket
* @param settingsManager the settings manager
* @param amount the amount of tickets to give
* @return the guild upgrade ticket
*/
public ItemStack getUpgradeTicket(SettingsManager settingsManager, int amount) {
ItemBuilder builder = new ItemBuilder(Material.valueOf(settingsManager.getProperty(TicketSettings.TICKET_MATERIAL)));
builder.setAmount(amount);
builder.setName(ACFBukkitUtil.color(settingsManager.getProperty(TicketSettings.TICKET_NAME)));
builder.setLore(settingsManager.getProperty(TicketSettings.TICKET_LORE).stream().map(ACFBukkitUtil::color).collect(Collectors.toList()));
return builder.build();
}
/**
* Check the guild ticket itemstack
* @param settingsManager settings manager
* @return the itemstack
*/
public ItemStack matchTicket(SettingsManager settingsManager) {
ItemBuilder builder = new ItemBuilder(Material.valueOf(settingsManager.getProperty(TicketSettings.TICKET_MATERIAL)));
builder.setAmount(1);
builder.setName(ACFBukkitUtil.color(settingsManager.getProperty(TicketSettings.TICKET_NAME)));
builder.setLore(settingsManager.getProperty(TicketSettings.TICKET_LORE).stream().map(ACFBukkitUtil::color).collect(Collectors.toList()));
return builder.build();
}
/**
* Simple method to check if a guild is full or not
* @return full or not
*/
public boolean checkIfFull(Guild guild) {
return guild.getSize() >= getGuildTier(guild.getTier().getLevel()).getMaxMembers();
}
/**
* Remove perms from a single player
* @param permission the permission to remove
* @param player the player to remove from
*/
public void removePerms(Permission permission, OfflinePlayer player) {
Guild guild = getGuild(player);
if (guild == null)
return;
GuildTier tier = getGuildTier(guild.getTier().getLevel());
if (tier.getPermissions().isEmpty())
return;
Guilds.newChain().async(() -> tier.getPermissions().forEach(perm -> permission.playerRemove(null, player, perm))).execute();
}
/**
* Add perms to a single player
* @param permission the permission to add
* @param player the player to add to
*/
public void addPerms(Permission permission, OfflinePlayer player) {
Guild guild = getGuild(player);
if (guild == null)
return;
GuildTier tier = getGuildTier(guild.getTier().getLevel());
if (tier.getPermissions().isEmpty())
return;
Guilds.newChain().async(() -> tier.getPermissions().forEach(perm -> permission.playerAdd(null, player, perm))).execute();
}
/**
* Add all the perms to a player for the guild
* @param permission permission to add
* @param guild the guild being modified
*/
public void addPermsToAll(Permission permission, Guild guild) {
GuildTier tier = getGuildTier(guild.getTier().getLevel());
if (tier.getPermissions().isEmpty())
return;
Guilds.newChain().async(() -> guild.getAllAsPlayers().forEach(player -> getGuildTier(guild.getTier().getLevel()).getPermissions().forEach(perm -> permission.playerAdd(null, player, perm)))).execute();
}
/**
* Remove all perms from a player for the guild
* @param permission permission to remove
* @param guild the guild being modified
*/
public void removePermsFromAll(Permission permission, Guild guild) {
GuildTier tier = getGuildTier(guild.getTier().getLevel());
if (tier.getPermissions().isEmpty())
return;
Guilds.newChain().async(() -> guild.getAllAsPlayers().forEach(player -> getGuildTier(guild.getTier().getLevel()).getPermissions().forEach(perm -> permission.playerRemove(null, player, perm)))).execute();
}
/**
* Handle inviting player when redeeming a code
* @param manager command manager
* @param player the player redeeming the code
* @param guild the guild they are trying to join
* @param code the code being redeemed
*/
public void handleInvite(CommandManager manager, Player player, Guild guild, GuildCode code) {
if (code.getUses() <= 0)
ACFUtil.sneaky(new ExpectationNotMet(Messages.CODES__OUT));
code.addRedeemer(player);
guild.addMemberByCode(new GuildMember(player.getUniqueId(), getLowestGuildRole()));
manager.getCommandIssuer(player).sendInfo(Messages.CODES__JOINED, "{guild}", guild.getName());
guild.sendMessage(manager, Messages.CODES__GUILD_MESSAGE, "{player}", player.getName(), "{creator}", Bukkit.getOfflinePlayer(code.getCreator()).getName());
}
/**
* Handle sending code list message to prevent DRY
* @param commandManager command manager
* @param player player to send list to
* @param codes list of codes
*/
public void handleCodeList(CommandManager commandManager, Player player, List<GuildCode> codes) {
codes.forEach(c -> commandManager.getCommandIssuer(player).sendInfo(Messages.CODES__LIST_ITEM,
"{code}", c.getId(),
"{amount}", String.valueOf(c.getUses()),
"{creator}", Bukkit.getOfflinePlayer(c.getCreator()).getName()));
}
/**
* Remove a guild from all other guilds allies or pending allies when deleted
* @param guild the guild to check
*/
public void removeAlliesOnDelete(Guild guild) {
getGuilds().forEach(g -> g.getPendingAllies().removeIf(x -> x.equals(guild.getId())));
getGuilds().forEach(g -> g.getAllies().removeIf(x -> x.equals(guild.getId())));
}
/**
* Notify all allies of a guild that's being deleted.
* @param guild the guild being deleted
* @param commandManager the command manager
*/
public void notifyAllies(Guild guild, CommandManager commandManager) {
guild.getAllies().forEach(g -> getGuild(g).sendMessage(commandManager, Messages.DELETE__NOTIFY_ALLIES, "{guild}", guild.getName()));
}
/**
* Get a list of all public guilds on the server
* @return list of public guilds
*/
public List<String> getPublicGuilds() {
return guilds.stream().filter(g -> !g.isPrivate()).map(Guild::getName).collect(Collectors.toList());
}
/**
* Check if a guild name already exists
* @param name name to check
* @return exists or not
*/
public boolean checkGuildNames(String name) {
return guilds.stream().anyMatch(g -> g.getName().equalsIgnoreCase(name));
}
}