From 4c959cc4213c3d50ec764b0b95386b9ed59fc3d8 Mon Sep 17 00:00:00 2001 From: ThiagoROX <51332006+SrBedrock@users.noreply.github.com> Date: Thu, 15 Aug 2024 00:04:54 -0300 Subject: [PATCH] Add PrePlayerKickedClanEvent (#14) --- .../commands/clan/ClanCommands.java | 53 ++++++++++++++----- .../commands/clan/LeaderCommands.java | 45 ++++++++++++---- .../commands/staff/StaffCommands.java | 41 +++++++++++--- .../events/PrePlayerKickedClanEvent.java | 47 ++++++++++++++++ src/main/resources/messages.properties | 1 + 5 files changed, 159 insertions(+), 28 deletions(-) create mode 100644 src/main/java/net/sacredlabyrinth/phaed/simpleclans/events/PrePlayerKickedClanEvent.java diff --git a/src/main/java/net/sacredlabyrinth/phaed/simpleclans/commands/clan/ClanCommands.java b/src/main/java/net/sacredlabyrinth/phaed/simpleclans/commands/clan/ClanCommands.java index 40f37ba7f..03a14778b 100644 --- a/src/main/java/net/sacredlabyrinth/phaed/simpleclans/commands/clan/ClanCommands.java +++ b/src/main/java/net/sacredlabyrinth/phaed/simpleclans/commands/clan/ClanCommands.java @@ -1,14 +1,33 @@ package net.sacredlabyrinth.phaed.simpleclans.commands.clan; import co.aikar.commands.BaseCommand; -import co.aikar.commands.annotation.*; -import net.sacredlabyrinth.phaed.simpleclans.*; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Conditions; +import co.aikar.commands.annotation.Dependency; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.HelpSearchTags; +import co.aikar.commands.annotation.Name; +import co.aikar.commands.annotation.Single; +import co.aikar.commands.annotation.Subcommand; +import net.sacredlabyrinth.phaed.simpleclans.ChatBlock; +import net.sacredlabyrinth.phaed.simpleclans.Clan; +import net.sacredlabyrinth.phaed.simpleclans.ClanPlayer; +import net.sacredlabyrinth.phaed.simpleclans.Helper; +import net.sacredlabyrinth.phaed.simpleclans.SimpleClans; import net.sacredlabyrinth.phaed.simpleclans.commands.ClanInput; import net.sacredlabyrinth.phaed.simpleclans.commands.ClanPlayerInput; import net.sacredlabyrinth.phaed.simpleclans.conversation.ResignPrompt; import net.sacredlabyrinth.phaed.simpleclans.conversation.SCConversation; +import net.sacredlabyrinth.phaed.simpleclans.events.PrePlayerKickedClanEvent; import net.sacredlabyrinth.phaed.simpleclans.events.TagChangeEvent; -import net.sacredlabyrinth.phaed.simpleclans.managers.*; +import net.sacredlabyrinth.phaed.simpleclans.managers.ClanManager; +import net.sacredlabyrinth.phaed.simpleclans.managers.PermissionsManager; +import net.sacredlabyrinth.phaed.simpleclans.managers.ProtectionManager; +import net.sacredlabyrinth.phaed.simpleclans.managers.RequestManager; +import net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager; +import net.sacredlabyrinth.phaed.simpleclans.managers.StorageManager; import net.sacredlabyrinth.phaed.simpleclans.utils.ChatUtils; import net.sacredlabyrinth.phaed.simpleclans.utils.CurrencyFormat; import org.bukkit.entity.Player; @@ -19,7 +38,12 @@ import java.util.Optional; import static net.sacredlabyrinth.phaed.simpleclans.SimpleClans.lang; -import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.*; +import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.CLAN_MAX_ALLIANCES; +import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.CLAN_MAX_DESCRIPTION_LENGTH; +import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.CLAN_MAX_MEMBERS; +import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.CLAN_MIN_DESCRIPTION_LENGTH; +import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.ECONOMY_MAX_MEMBER_FEE; +import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.WAR_START_REQUEST_ENABLED; import static org.bukkit.ChatColor.AQUA; import static org.bukkit.ChatColor.RED; @@ -348,15 +372,16 @@ public void kick(@Conditions("clan_member") Player sender, ChatBlock.sendMessage(sender, RED + lang("you.cannot.kick.yourself", sender)); return; } - Clan clan = cm.getClanByPlayerUniqueId(sender.getUniqueId()); if (Objects.requireNonNull(clan).isLeader(clanPlayer.getUniqueId())) { ChatBlock.sendMessage(sender, RED + lang("you.cannot.kick.another.leader", sender)); return; } - - clan.addBb(sender.getName(), lang("has.been.kicked.by", clanPlayer.getName(), - sender.getName(), sender)); + if (new PrePlayerKickedClanEvent(clan, clanPlayer).callEvent()) { + ChatBlock.sendMessage(sender, RED + lang("error.event.cancelled", sender)); + return; + } + clan.addBb(sender.getName(), lang("has.been.kicked.by", clanPlayer.getName(), sender.getName(), sender)); clan.removePlayerFromClan(clanPlayer.getUniqueId()); } @@ -365,18 +390,18 @@ public void kick(@Conditions("clan_member") Player sender, @Description("{@@command.description.resign}") @HelpSearchTags("leave") public void resignConfirm(Player player, ClanPlayer cp, Clan clan) { - if (clan.isPermanent() || !clan.isLeader(player) || clan.getLeaders().size() > 1) { + if (new PrePlayerKickedClanEvent(clan, cp).callEvent()) { + ChatBlock.sendMessage(player, RED + lang("error.event.cancelled", player)); + } else if (clan.isPermanent() || !clan.isLeader(player) || clan.getLeaders().size() > 1) { clan.addBb(player.getName(), lang("0.has.resigned", player.getName())); cp.addResignTime(clan.getTag()); clan.removePlayerFromClan(player.getUniqueId()); - ChatBlock.sendMessage(cp, AQUA + lang("resign.success", player)); } else if (clan.isLeader(player) && clan.getLeaders().size() == 1) { clan.disband(player, true, false); ChatBlock.sendMessage(cp, RED + lang("clan.has.been.disbanded", player, clan.getName())); } else { - ChatBlock.sendMessage(cp, RED + - lang("last.leader.cannot.resign.you.must.appoint.another.leader.or.disband.the.clan", player)); + ChatBlock.sendMessage(cp, RED + lang("last.leader.cannot.resign.you.must.appoint.another.leader.or.disband.the.clan", player)); } } @@ -385,6 +410,10 @@ public void resignConfirm(Player player, ClanPlayer cp, Clan clan) { @Description("{@@command.description.resign}") @HelpSearchTags("leave") public void resign(@Conditions("clan_member") Player player) { + if (new PrePlayerKickedClanEvent(cm.getClanByPlayerUniqueId(player.getUniqueId()), cm.getClanPlayer(player)).callEvent()) { + ChatBlock.sendMessage(player, RED + lang("error.event.cancelled", player)); + return; + } new SCConversation(plugin, player, new ResignPrompt()).begin(); } } diff --git a/src/main/java/net/sacredlabyrinth/phaed/simpleclans/commands/clan/LeaderCommands.java b/src/main/java/net/sacredlabyrinth/phaed/simpleclans/commands/clan/LeaderCommands.java index ac9b1fb16..116f47e3d 100644 --- a/src/main/java/net/sacredlabyrinth/phaed/simpleclans/commands/clan/LeaderCommands.java +++ b/src/main/java/net/sacredlabyrinth/phaed/simpleclans/commands/clan/LeaderCommands.java @@ -1,16 +1,33 @@ package net.sacredlabyrinth.phaed.simpleclans.commands.clan; import co.aikar.commands.BaseCommand; -import co.aikar.commands.annotation.*; -import net.sacredlabyrinth.phaed.simpleclans.*; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Conditions; +import co.aikar.commands.annotation.Dependency; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.Name; +import co.aikar.commands.annotation.Subcommand; +import net.sacredlabyrinth.phaed.simpleclans.ChatBlock; +import net.sacredlabyrinth.phaed.simpleclans.Clan; +import net.sacredlabyrinth.phaed.simpleclans.ClanPlayer; +import net.sacredlabyrinth.phaed.simpleclans.ClanRequest; +import net.sacredlabyrinth.phaed.simpleclans.SimpleClans; import net.sacredlabyrinth.phaed.simpleclans.commands.ClanPlayerInput; import net.sacredlabyrinth.phaed.simpleclans.conversation.DisbandPrompt; import net.sacredlabyrinth.phaed.simpleclans.conversation.SCConversation; +import net.sacredlabyrinth.phaed.simpleclans.events.PrePlayerKickedClanEvent; import net.sacredlabyrinth.phaed.simpleclans.hooks.discord.DiscordHook; import net.sacredlabyrinth.phaed.simpleclans.hooks.discord.exceptions.DiscordHookException; -import net.sacredlabyrinth.phaed.simpleclans.managers.*; -import net.sacredlabyrinth.phaed.simpleclans.utils.CurrencyFormat; +import net.sacredlabyrinth.phaed.simpleclans.managers.ChatManager; +import net.sacredlabyrinth.phaed.simpleclans.managers.ClanManager; +import net.sacredlabyrinth.phaed.simpleclans.managers.PermissionsManager; +import net.sacredlabyrinth.phaed.simpleclans.managers.RequestManager; +import net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager; +import net.sacredlabyrinth.phaed.simpleclans.managers.StorageManager; import net.sacredlabyrinth.phaed.simpleclans.utils.ChatUtils; +import net.sacredlabyrinth.phaed.simpleclans.utils.CurrencyFormat; import org.bukkit.ChatColor; import org.bukkit.entity.Player; @@ -18,7 +35,14 @@ import static net.sacredlabyrinth.phaed.simpleclans.SimpleClans.lang; import static net.sacredlabyrinth.phaed.simpleclans.events.EconomyTransactionEvent.Cause.DISCORD_CREATION; -import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.*; +import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.CLAN_CONFIRMATION_FOR_DEMOTE; +import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.CLAN_CONFIRMATION_FOR_PROMOTE; +import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.CLAN_MAX_LENGTH; +import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.CLAN_MIN_LENGTH; +import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.CLAN_MIN_TO_VERIFY; +import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.ECONOMY_DISCORD_CREATION_PRICE; +import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.ECONOMY_PURCHASE_CLAN_VERIFY; +import static net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager.ConfigField.ECONOMY_PURCHASE_DISCORD_CREATE; import static org.bukkit.ChatColor.AQUA; import static org.bukkit.ChatColor.RED; @@ -106,11 +130,14 @@ public void promote(Player player, @CommandPermission("simpleclans.leader.disband") @Description("{@@command.description.disband}") public void disband(Player player, ClanPlayer cp, Clan clan) { + if (new PrePlayerKickedClanEvent(clan, cp).callEvent()) { + ChatBlock.sendMessage(player, RED + lang("error.event.cancelled", player)); + return; + } + if (clan.getLeaders().size() != 1) { - requestManager.requestAllLeaders(cp, ClanRequest.DISBAND, clan.getTag(), "asking.to.disband", - player.getName()); - ChatBlock.sendMessage(player, AQUA + - lang("clan.disband.vote.has.been.requested.from.all.leaders", player)); + requestManager.requestAllLeaders(cp, ClanRequest.DISBAND, clan.getTag(), "asking.to.disband", player.getName()); + ChatBlock.sendMessage(player, AQUA + lang("clan.disband.vote.has.been.requested.from.all.leaders", player)); return; } diff --git a/src/main/java/net/sacredlabyrinth/phaed/simpleclans/commands/staff/StaffCommands.java b/src/main/java/net/sacredlabyrinth/phaed/simpleclans/commands/staff/StaffCommands.java index f23a9062b..9b58e4b5a 100644 --- a/src/main/java/net/sacredlabyrinth/phaed/simpleclans/commands/staff/StaffCommands.java +++ b/src/main/java/net/sacredlabyrinth/phaed/simpleclans/commands/staff/StaffCommands.java @@ -1,10 +1,25 @@ package net.sacredlabyrinth.phaed.simpleclans.commands.staff; import co.aikar.commands.BaseCommand; -import co.aikar.commands.annotation.*; -import net.sacredlabyrinth.phaed.simpleclans.*; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Conditions; +import co.aikar.commands.annotation.Dependency; +import co.aikar.commands.annotation.Description; +import co.aikar.commands.annotation.HelpSearchTags; +import co.aikar.commands.annotation.Name; +import co.aikar.commands.annotation.Single; +import co.aikar.commands.annotation.Subcommand; +import co.aikar.commands.annotation.Values; +import net.sacredlabyrinth.phaed.simpleclans.ChatBlock; +import net.sacredlabyrinth.phaed.simpleclans.Clan; +import net.sacredlabyrinth.phaed.simpleclans.ClanPlayer; +import net.sacredlabyrinth.phaed.simpleclans.Helper; +import net.sacredlabyrinth.phaed.simpleclans.SimpleClans; import net.sacredlabyrinth.phaed.simpleclans.commands.ClanInput; import net.sacredlabyrinth.phaed.simpleclans.commands.ClanPlayerInput; +import net.sacredlabyrinth.phaed.simpleclans.events.PrePlayerKickedClanEvent; import net.sacredlabyrinth.phaed.simpleclans.events.PlayerHomeSetEvent; import net.sacredlabyrinth.phaed.simpleclans.events.PlayerResetKdrEvent; import net.sacredlabyrinth.phaed.simpleclans.events.TagChangeEvent; @@ -15,7 +30,6 @@ import net.sacredlabyrinth.phaed.simpleclans.managers.StorageManager; import net.sacredlabyrinth.phaed.simpleclans.ui.InventoryController; import net.sacredlabyrinth.phaed.simpleclans.utils.ChatUtils; -import net.sacredlabyrinth.phaed.simpleclans.utils.TagValidator; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -58,11 +72,16 @@ public void place(CommandSender sender, @Name("player") ClanPlayerInput cpInput, if (oldCp != null) { Clan oldClan = Objects.requireNonNull(oldCp.getClan()); + if (new PrePlayerKickedClanEvent(oldClan, oldCp).callEvent()) { + ChatBlock.sendMessage(sender, RED + lang("error.event.cancelled", sender)); + return; + } if (oldClan.equals(newClan)) { ChatBlock.sendMessage(sender, lang("player.already.in.this.clan", sender)); return; } + if (!oldClan.isPermanent() && oldClan.isLeader(uuid) && oldClan.getLeaders().size() <= 1) { ChatBlock.sendMessage(sender, RED + lang("you.cannot.move.the.last.leader", sender)); return; @@ -247,8 +266,13 @@ public void purge(CommandSender sender, @Name("player") ClanPlayerInput player) Clan clan = player.getClanPlayer().getClan(); if (clan != null && clan.getMembers().size() == 1) { + if (new PrePlayerKickedClanEvent(clan, player.getClanPlayer()).callEvent()) { + ChatBlock.sendMessage(sender, RED + lang("error.event.cancelled", sender)); + return; + } clan.disband(sender, false, false); } + cm.deleteClanPlayer(player.getClanPlayer()); ChatBlock.sendMessage(sender, AQUA + lang("player.purged", sender)); } @@ -260,13 +284,17 @@ public void purge(CommandSender sender, @Name("player") ClanPlayerInput player) public void kick(CommandSender sender, @Conditions("clan_member") @Name("player") ClanPlayerInput cp) { ClanPlayer clanPlayer = cp.getClanPlayer(); Clan clan = Objects.requireNonNull(clanPlayer.getClan()); + if (new PrePlayerKickedClanEvent(clan, clanPlayer).callEvent()) { + ChatBlock.sendMessage(sender, RED + lang("error.event.cancelled", sender)); + return; + } + if (clanPlayer.isLeader() && clan.getLeaders().size() == 1) { ChatBlock.sendMessageKey(sender, "cannot.kick.last.leader"); return; } - clan.addBb(sender.getName(), lang("has.been.kicked.by", clanPlayer.getName(), - sender.getName(), sender)); + clan.addBb(sender.getName(), lang("has.been.kicked.by", clanPlayer.getName(), sender.getName(), sender)); clan.removePlayerFromClan(clanPlayer.getUniqueId()); } @@ -286,8 +314,7 @@ public void promote(CommandSender sender, @Conditions("online|clan_member") @Nam ClanPlayer clanPlayer = promote.getClanPlayer(); Player promotePl = Objects.requireNonNull(clanPlayer.toPlayer()); if (!permissions.has(promotePl, "simpleclans.leader.promotable")) { - ChatBlock.sendMessage(sender, RED + lang("the.player.does.not.have.the.permissions.to.lead.a.clan", - sender)); + ChatBlock.sendMessage(sender, RED + lang("the.player.does.not.have.the.permissions.to.lead.a.clan", sender)); return; } Clan clan = Objects.requireNonNull(clanPlayer.getClan()); diff --git a/src/main/java/net/sacredlabyrinth/phaed/simpleclans/events/PrePlayerKickedClanEvent.java b/src/main/java/net/sacredlabyrinth/phaed/simpleclans/events/PrePlayerKickedClanEvent.java new file mode 100644 index 000000000..383248ba9 --- /dev/null +++ b/src/main/java/net/sacredlabyrinth/phaed/simpleclans/events/PrePlayerKickedClanEvent.java @@ -0,0 +1,47 @@ +package net.sacredlabyrinth.phaed.simpleclans.events; + +import net.sacredlabyrinth.phaed.simpleclans.Clan; +import net.sacredlabyrinth.phaed.simpleclans.ClanPlayer; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +/** + * @author ThiagoROX + */ +public class PrePlayerKickedClanEvent extends Event { + + private static final HandlerList handlers = new HandlerList(); + private final Clan clan; + private final ClanPlayer target; + private boolean cancelled; + + public PrePlayerKickedClanEvent(Clan clan, ClanPlayer target) { + this.clan = clan; + this.target = target; + } + + public Clan getClan() { + return this.clan; + } + + public ClanPlayer getClanPlayer() { + return this.target; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + public boolean isCancelled() { + return cancelled; + } + + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } +} diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index e56cbc37c..dd1a8dd53 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -883,3 +883,4 @@ invitedplayer.invite.off=This player has disabled clan invites user.hasnt.played.before=&cThis user has not played before clan.list.balance.header=\n{0} &7top balance &8-----------------------\n \n&8Total clans: &7{1}\n \n&8#&7. &8Clan &7- &8Balance clan.list.balance.line=&f{0}. {1}{2}{3}&b{4} &7- &e{5}$ +error.event.cancelled=&cThe event was cancelled \ No newline at end of file