From 162c605dacb52ddd2fae6797ffd7cbd9cb4bb84e Mon Sep 17 00:00:00 2001 From: PikaMug <2267126+PikaMug@users.noreply.github.com> Date: Wed, 22 Sep 2021 18:05:41 -0400 Subject: [PATCH] Trigger change event for party create/disband (#4620) --- .../commands/party/PartyDisbandCommand.java | 10 ++++++---- .../events/party/McMMOPartyChangeEvent.java | 10 ++++++++++ .../com/gmail/nossr50/party/PartyManager.java | 16 +++++++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/commands/party/PartyDisbandCommand.java b/src/main/java/com/gmail/nossr50/commands/party/PartyDisbandCommand.java index f4f8f3d2a..e21776370 100644 --- a/src/main/java/com/gmail/nossr50/commands/party/PartyDisbandCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/party/PartyDisbandCommand.java @@ -1,6 +1,7 @@ package com.gmail.nossr50.commands.party; import com.gmail.nossr50.datatypes.party.Party; +import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.events.party.McMMOPartyChangeEvent.EventReason; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.party.PartyManager; @@ -15,13 +16,14 @@ public class PartyDisbandCommand implements CommandExecutor { @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { if (args.length == 1) { - if (UserManager.getPlayer((Player) sender) == null) { + final McMMOPlayer mcMMOPlayer = UserManager.getPlayer((Player) sender); + if (mcMMOPlayer == null) { sender.sendMessage(LocaleLoader.getString("Profile.PendingLoad")); return true; } - Party playerParty = UserManager.getPlayer((Player) sender).getParty(); - String partyName = playerParty.getName(); + final Party playerParty = mcMMOPlayer.getParty(); + final String partyName = playerParty.getName(); for (Player member : playerParty.getOnlineMembers()) { if (!PartyManager.handlePartyChangeEvent(member, partyName, null, EventReason.KICKED_FROM_PARTY)) { @@ -31,7 +33,7 @@ public class PartyDisbandCommand implements CommandExecutor { member.sendMessage(LocaleLoader.getString("Party.Disband")); } - PartyManager.disbandParty(playerParty); + PartyManager.disbandParty(mcMMOPlayer, playerParty); return true; } sender.sendMessage(LocaleLoader.getString("Commands.Usage.1", "party", "disband")); diff --git a/src/main/java/com/gmail/nossr50/events/party/McMMOPartyChangeEvent.java b/src/main/java/com/gmail/nossr50/events/party/McMMOPartyChangeEvent.java index 365236d91..1f9b30696 100644 --- a/src/main/java/com/gmail/nossr50/events/party/McMMOPartyChangeEvent.java +++ b/src/main/java/com/gmail/nossr50/events/party/McMMOPartyChangeEvent.java @@ -53,6 +53,16 @@ public class McMMOPartyChangeEvent extends PlayerEvent implements Cancellable { * A list of reasons why the event may have been fired */ public enum EventReason { + /** + * Created a party. + */ + CREATED_PARTY, + + /** + * Disbanded a party. + */ + DISBANDED_PARTY, + /** * Joined a party for the first time. */ diff --git a/src/main/java/com/gmail/nossr50/party/PartyManager.java b/src/main/java/com/gmail/nossr50/party/PartyManager.java index 4cd483329..16965629f 100644 --- a/src/main/java/com/gmail/nossr50/party/PartyManager.java +++ b/src/main/java/com/gmail/nossr50/party/PartyManager.java @@ -347,10 +347,20 @@ public final class PartyManager { * Disband a party. Kicks out all members and removes the party. * * @param party The party to remove + * @deprecated Use {@link #disbandParty(McMMOPlayer, Party)} */ public static void disbandParty(Party party) { + disbandParty(null, party); + } + + /** + * Disband a party. Kicks out all members and removes the party. + * + * @param party The party to remove + */ + public static void disbandParty(McMMOPlayer mcMMOPlayer, Party party) { //TODO: Potential issues with unloaded profile? - for (Player member : party.getOnlineMembers()) { + for (final Player member : party.getOnlineMembers()) { //Profile not loaded if(UserManager.getPlayer(member) == null) { @@ -366,6 +376,9 @@ public final class PartyManager { } parties.remove(party); + if (mcMMOPlayer != null) { + handlePartyChangeEvent(mcMMOPlayer.getPlayer(), party.getName(), null, EventReason.DISBANDED_PARTY); + } } /** @@ -388,6 +401,7 @@ public final class PartyManager { player.sendMessage(LocaleLoader.getString("Commands.Party.Create", party.getName())); addToParty(mcMMOPlayer, party); + handlePartyChangeEvent(player, null, partyName, EventReason.CREATED_PARTY); } /**